blob: 4bd33245bad625d7bffb7a7002dc776effd3e956 [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
482 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
483 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400484
485 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
486 for (i = 0; i < rx_scrqs; i++) {
487 rx_pool = &adapter->rx_pool[i];
488
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500489 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
490
John Allen896d8692018-01-18 16:26:31 -0600491 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
492 free_long_term_buff(adapter, &rx_pool->long_term_buff);
493 rx_pool->buff_size = be64_to_cpu(size_array[i]);
Thomas Falcon7c940b12019-06-07 16:03:55 -0500494 rc = alloc_long_term_buff(adapter,
495 &rx_pool->long_term_buff,
496 rx_pool->size *
497 rx_pool->buff_size);
John Allen896d8692018-01-18 16:26:31 -0600498 } else {
499 rc = reset_long_term_buff(adapter,
500 &rx_pool->long_term_buff);
501 }
502
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500503 if (rc)
504 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400505
506 for (j = 0; j < rx_pool->size; j++)
507 rx_pool->free_map[j] = j;
508
509 memset(rx_pool->rx_buff, 0,
510 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
511
512 atomic_set(&rx_pool->available, 0);
513 rx_pool->next_alloc = 0;
514 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500515 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400516 }
517
518 return 0;
519}
520
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400521static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600522{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400523 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400524 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600525
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400526 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600527 return;
528
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600529 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400530 rx_pool = &adapter->rx_pool[i];
531
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500532 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
533
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400534 kfree(rx_pool->free_map);
535 free_long_term_buff(adapter, &rx_pool->long_term_buff);
536
537 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400538 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400539
540 for (j = 0; j < rx_pool->size; j++) {
541 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600542 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
543 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400544 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600545 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400546
547 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600548 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400549
550 kfree(adapter->rx_pool);
551 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600552 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400553}
554
555static int init_rx_pools(struct net_device *netdev)
556{
557 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
558 struct device *dev = &adapter->vdev->dev;
559 struct ibmvnic_rx_pool *rx_pool;
560 int rxadd_subcrqs;
561 u64 *size_array;
562 int i, j;
563
564 rxadd_subcrqs =
565 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
566 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
567 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
568
569 adapter->rx_pool = kcalloc(rxadd_subcrqs,
570 sizeof(struct ibmvnic_rx_pool),
571 GFP_KERNEL);
572 if (!adapter->rx_pool) {
573 dev_err(dev, "Failed to allocate rx pools\n");
574 return -1;
575 }
576
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600577 adapter->num_active_rx_pools = rxadd_subcrqs;
578
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400579 for (i = 0; i < rxadd_subcrqs; i++) {
580 rx_pool = &adapter->rx_pool[i];
581
582 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500583 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400584 i, adapter->req_rx_add_entries_per_subcrq,
585 be64_to_cpu(size_array[i]));
586
587 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
588 rx_pool->index = i;
589 rx_pool->buff_size = be64_to_cpu(size_array[i]);
590 rx_pool->active = 1;
591
592 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
593 GFP_KERNEL);
594 if (!rx_pool->free_map) {
595 release_rx_pools(adapter);
596 return -1;
597 }
598
599 rx_pool->rx_buff = kcalloc(rx_pool->size,
600 sizeof(struct ibmvnic_rx_buff),
601 GFP_KERNEL);
602 if (!rx_pool->rx_buff) {
603 dev_err(dev, "Couldn't alloc rx buffers\n");
604 release_rx_pools(adapter);
605 return -1;
606 }
607
608 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
609 rx_pool->size * rx_pool->buff_size)) {
610 release_rx_pools(adapter);
611 return -1;
612 }
613
614 for (j = 0; j < rx_pool->size; ++j)
615 rx_pool->free_map[j] = j;
616
617 atomic_set(&rx_pool->available, 0);
618 rx_pool->next_alloc = 0;
619 rx_pool->next_free = 0;
620 }
621
622 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600623}
624
Thomas Falcone26dc252018-03-16 20:00:25 -0500625static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
626 struct ibmvnic_tx_pool *tx_pool)
627{
628 int rc, i;
629
630 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
631 if (rc)
632 return rc;
633
634 memset(tx_pool->tx_buff, 0,
635 tx_pool->num_buffers *
636 sizeof(struct ibmvnic_tx_buff));
637
638 for (i = 0; i < tx_pool->num_buffers; i++)
639 tx_pool->free_map[i] = i;
640
641 tx_pool->consumer_index = 0;
642 tx_pool->producer_index = 0;
643
644 return 0;
645}
646
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400647static int reset_tx_pools(struct ibmvnic_adapter *adapter)
648{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400649 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500650 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400651
652 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
653 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500654 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500655 if (rc)
656 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500657 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500658 if (rc)
659 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400660 }
661
662 return 0;
663}
664
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200665static void release_vpd_data(struct ibmvnic_adapter *adapter)
666{
667 if (!adapter->vpd)
668 return;
669
670 kfree(adapter->vpd->buff);
671 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600672
673 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200674}
675
Thomas Falconfb794212018-03-16 20:00:26 -0500676static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
677 struct ibmvnic_tx_pool *tx_pool)
678{
679 kfree(tx_pool->tx_buff);
680 kfree(tx_pool->free_map);
681 free_long_term_buff(adapter, &tx_pool->long_term_buff);
682}
683
Nathan Fontenotc657e322017-03-30 02:49:06 -0400684static void release_tx_pools(struct ibmvnic_adapter *adapter)
685{
John Allen896d8692018-01-18 16:26:31 -0600686 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400687
688 if (!adapter->tx_pool)
689 return;
690
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600691 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500692 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
693 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400694 }
695
696 kfree(adapter->tx_pool);
697 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500698 kfree(adapter->tso_pool);
699 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600700 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400701}
702
Thomas Falcon32053062018-03-16 20:00:27 -0500703static int init_one_tx_pool(struct net_device *netdev,
704 struct ibmvnic_tx_pool *tx_pool,
705 int num_entries, int buf_size)
706{
707 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
708 int i;
709
710 tx_pool->tx_buff = kcalloc(num_entries,
711 sizeof(struct ibmvnic_tx_buff),
712 GFP_KERNEL);
713 if (!tx_pool->tx_buff)
714 return -1;
715
716 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
717 num_entries * buf_size))
718 return -1;
719
720 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
721 if (!tx_pool->free_map)
722 return -1;
723
724 for (i = 0; i < num_entries; i++)
725 tx_pool->free_map[i] = i;
726
727 tx_pool->consumer_index = 0;
728 tx_pool->producer_index = 0;
729 tx_pool->num_buffers = num_entries;
730 tx_pool->buf_size = buf_size;
731
732 return 0;
733}
734
Nathan Fontenotc657e322017-03-30 02:49:06 -0400735static int init_tx_pools(struct net_device *netdev)
736{
737 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400738 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500739 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400740
741 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
742 adapter->tx_pool = kcalloc(tx_subcrqs,
743 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
744 if (!adapter->tx_pool)
745 return -1;
746
Thomas Falcon32053062018-03-16 20:00:27 -0500747 adapter->tso_pool = kcalloc(tx_subcrqs,
748 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
749 if (!adapter->tso_pool)
750 return -1;
751
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600752 adapter->num_active_tx_pools = tx_subcrqs;
753
Nathan Fontenotc657e322017-03-30 02:49:06 -0400754 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500755 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
756 adapter->req_tx_entries_per_subcrq,
757 adapter->req_mtu + VLAN_HLEN);
758 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400759 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500760 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400761 }
762
Thomas Falcon7c940b12019-06-07 16:03:55 -0500763 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
764 IBMVNIC_TSO_BUFS,
765 IBMVNIC_TSO_BUF_SZ);
Thomas Falcon32053062018-03-16 20:00:27 -0500766 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400767 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500768 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400769 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400770 }
771
772 return 0;
773}
774
John Allend944c3d62017-05-26 10:30:13 -0400775static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
776{
777 int i;
778
779 if (adapter->napi_enabled)
780 return;
781
782 for (i = 0; i < adapter->req_rx_queues; i++)
783 napi_enable(&adapter->napi[i]);
784
785 adapter->napi_enabled = true;
786}
787
788static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
789{
790 int i;
791
792 if (!adapter->napi_enabled)
793 return;
794
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500795 for (i = 0; i < adapter->req_rx_queues; i++) {
796 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400797 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500798 }
John Allend944c3d62017-05-26 10:30:13 -0400799
800 adapter->napi_enabled = false;
801}
802
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600803static int init_napi(struct ibmvnic_adapter *adapter)
804{
805 int i;
806
807 adapter->napi = kcalloc(adapter->req_rx_queues,
808 sizeof(struct napi_struct), GFP_KERNEL);
809 if (!adapter->napi)
810 return -ENOMEM;
811
812 for (i = 0; i < adapter->req_rx_queues; i++) {
813 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
814 netif_napi_add(adapter->netdev, &adapter->napi[i],
815 ibmvnic_poll, NAPI_POLL_WEIGHT);
816 }
817
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600818 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600819 return 0;
820}
821
822static void release_napi(struct ibmvnic_adapter *adapter)
823{
824 int i;
825
826 if (!adapter->napi)
827 return;
828
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600829 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800830 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
831 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600832 }
833
834 kfree(adapter->napi);
835 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600836 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500837 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600838}
839
John Allena57a5d22017-03-17 17:13:41 -0500840static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600841{
842 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500843 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500844 int retry_count = 0;
Thomas Falconeb110412018-05-24 14:37:53 -0500845 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500846 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600847
John Allenbd0b6722017-03-17 17:13:40 -0500848 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500849 retry = false;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500850 if (retry_count > IBMVNIC_MAX_QUEUES) {
851 netdev_warn(netdev, "Login attempts exceeded\n");
852 return -1;
853 }
854
855 adapter->init_done_rc = 0;
856 reinit_completion(&adapter->init_done);
857 rc = send_login(adapter);
858 if (rc) {
859 netdev_warn(netdev, "Unable to login\n");
860 return rc;
861 }
862
863 if (!wait_for_completion_timeout(&adapter->init_done,
864 timeout)) {
865 netdev_warn(netdev, "Login timed out\n");
866 return -1;
867 }
868
869 if (adapter->init_done_rc == PARTIALSUCCESS) {
870 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600871 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500872
Thomas Falconeb110412018-05-24 14:37:53 -0500873 retry = true;
874 netdev_dbg(netdev,
875 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500876 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500877 reinit_completion(&adapter->init_done);
878 send_cap_queries(adapter);
879 if (!wait_for_completion_timeout(&adapter->init_done,
880 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500881 netdev_warn(netdev,
882 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500883 return -1;
884 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500885
Thomas Falcon4d96f122017-08-01 15:04:36 -0500886 rc = init_sub_crqs(adapter);
887 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500888 netdev_warn(netdev,
889 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500890 return -1;
891 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500892
Thomas Falcon4d96f122017-08-01 15:04:36 -0500893 rc = init_sub_crq_irqs(adapter);
894 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500895 netdev_warn(netdev,
896 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500897 return -1;
898 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500899 } else if (adapter->init_done_rc) {
900 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500901 return -1;
902 }
Thomas Falconeb110412018-05-24 14:37:53 -0500903 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500904
Thomas Falcon62740e92019-05-09 23:13:43 -0500905 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600906
John Allena57a5d22017-03-17 17:13:41 -0500907 return 0;
908}
909
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600910static void release_login_buffer(struct ibmvnic_adapter *adapter)
911{
912 kfree(adapter->login_buf);
913 adapter->login_buf = NULL;
914}
915
916static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
917{
918 kfree(adapter->login_rsp_buf);
919 adapter->login_rsp_buf = NULL;
920}
921
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400922static void release_resources(struct ibmvnic_adapter *adapter)
923{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200924 release_vpd_data(adapter);
925
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400926 release_tx_pools(adapter);
927 release_rx_pools(adapter);
928
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600929 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600930 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400931}
932
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400933static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
934{
935 struct net_device *netdev = adapter->netdev;
936 unsigned long timeout = msecs_to_jiffies(30000);
937 union ibmvnic_crq crq;
938 bool resend;
939 int rc;
940
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500941 netdev_dbg(netdev, "setting link state %d\n", link_state);
942
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400943 memset(&crq, 0, sizeof(crq));
944 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
945 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
946 crq.logical_link_state.link_state = link_state;
947
948 do {
949 resend = false;
950
951 reinit_completion(&adapter->init_done);
952 rc = ibmvnic_send_crq(adapter, &crq);
953 if (rc) {
954 netdev_err(netdev, "Failed to set link state\n");
955 return rc;
956 }
957
958 if (!wait_for_completion_timeout(&adapter->init_done,
959 timeout)) {
960 netdev_err(netdev, "timeout setting link state\n");
961 return -1;
962 }
963
964 if (adapter->init_done_rc == 1) {
965 /* Partuial success, delay and re-send */
966 mdelay(1000);
967 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500968 } else if (adapter->init_done_rc) {
969 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
970 adapter->init_done_rc);
971 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400972 }
973 } while (resend);
974
975 return 0;
976}
977
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400978static int set_real_num_queues(struct net_device *netdev)
979{
980 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
981 int rc;
982
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500983 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
984 adapter->req_tx_queues, adapter->req_rx_queues);
985
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400986 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
987 if (rc) {
988 netdev_err(netdev, "failed to set the number of tx queues\n");
989 return rc;
990 }
991
992 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
993 if (rc)
994 netdev_err(netdev, "failed to set the number of rx queues\n");
995
996 return rc;
997}
998
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200999static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1000{
1001 struct device *dev = &adapter->vdev->dev;
1002 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001003 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001004 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001005
1006 if (adapter->vpd->buff)
1007 len = adapter->vpd->len;
1008
Thomas Falconff25dcb2019-11-25 17:12:56 -06001009 mutex_lock(&adapter->fw_lock);
1010 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001011 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001012
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001013 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1014 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001015 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001016 if (rc) {
1017 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001018 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001019 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001020
1021 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1022 if (rc) {
1023 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001024 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001025 return rc;
1026 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001027 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001028
1029 if (!adapter->vpd->len)
1030 return -ENODATA;
1031
1032 if (!adapter->vpd->buff)
1033 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1034 else if (adapter->vpd->len != len)
1035 adapter->vpd->buff =
1036 krealloc(adapter->vpd->buff,
1037 adapter->vpd->len, GFP_KERNEL);
1038
1039 if (!adapter->vpd->buff) {
1040 dev_err(dev, "Could allocate VPD buffer\n");
1041 return -ENOMEM;
1042 }
1043
1044 adapter->vpd->dma_addr =
1045 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1046 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -02001047 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001048 dev_err(dev, "Could not map VPD buffer\n");
1049 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -06001050 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001051 return -ENOMEM;
1052 }
1053
Thomas Falconff25dcb2019-11-25 17:12:56 -06001054 mutex_lock(&adapter->fw_lock);
1055 adapter->fw_done_rc = 0;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001056 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001057
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001058 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1059 crq.get_vpd.cmd = GET_VPD;
1060 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1061 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001062 rc = ibmvnic_send_crq(adapter, &crq);
1063 if (rc) {
1064 kfree(adapter->vpd->buff);
1065 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001066 mutex_unlock(&adapter->fw_lock);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001067 return rc;
1068 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06001069
1070 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1071 if (rc) {
1072 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1073 kfree(adapter->vpd->buff);
1074 adapter->vpd->buff = NULL;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001075 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06001076 return rc;
1077 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001078
Thomas Falconff25dcb2019-11-25 17:12:56 -06001079 mutex_unlock(&adapter->fw_lock);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001080 return 0;
1081}
1082
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001083static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001084{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001085 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001086 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001087
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001088 rc = set_real_num_queues(netdev);
1089 if (rc)
1090 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001091
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001092 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1093 if (!adapter->vpd)
1094 return -ENOMEM;
1095
John Allen69d08dc2018-01-18 16:27:58 -06001096 /* Vital Product Data (VPD) */
1097 rc = ibmvnic_get_vpd(adapter);
1098 if (rc) {
1099 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1100 return rc;
1101 }
1102
Thomas Falcon032c5e82015-12-21 11:26:06 -06001103 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001104
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001105 rc = init_napi(adapter);
1106 if (rc)
1107 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001108
Thomas Falcon032c5e82015-12-21 11:26:06 -06001109 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001110
1111 rc = init_rx_pools(netdev);
1112 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001113 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001114
Nathan Fontenotc657e322017-03-30 02:49:06 -04001115 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001116 return rc;
1117}
1118
Nathan Fontenoted651a12017-05-03 14:04:38 -04001119static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001120{
1121 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001122 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001123 int i, rc;
1124
Nathan Fontenot90c80142017-05-03 14:04:32 -04001125 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001126 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001127 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001128
Thomas Falcon032c5e82015-12-21 11:26:06 -06001129 /* We're ready to receive frames, enable the sub-crq interrupts and
1130 * set the logical link state to up
1131 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001132 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001133 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001134 if (prev_state == VNIC_CLOSED)
1135 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001136 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001137 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001138
Nathan Fontenoted651a12017-05-03 14:04:38 -04001139 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001140 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001141 if (prev_state == VNIC_CLOSED)
1142 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001143 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001144 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001145
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001146 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001147 if (rc) {
1148 for (i = 0; i < adapter->req_rx_queues; i++)
1149 napi_disable(&adapter->napi[i]);
1150 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001151 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001152 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001153
Nathan Fontenoted651a12017-05-03 14:04:38 -04001154 netif_tx_start_all_queues(netdev);
1155
1156 if (prev_state == VNIC_CLOSED) {
1157 for (i = 0; i < adapter->req_rx_queues; i++)
1158 napi_schedule(&adapter->napi[i]);
1159 }
1160
1161 adapter->state = VNIC_OPEN;
1162 return rc;
1163}
1164
1165static int ibmvnic_open(struct net_device *netdev)
1166{
1167 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001168 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001169
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001170 /* If device failover is pending, just set device state and return.
1171 * Device operation will be handled by reset routine.
1172 */
1173 if (adapter->failover_pending) {
1174 adapter->state = VNIC_OPEN;
1175 return 0;
1176 }
1177
Nathan Fontenoted651a12017-05-03 14:04:38 -04001178 if (adapter->state != VNIC_CLOSED) {
1179 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001180 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001181 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001182
1183 rc = init_resources(adapter);
1184 if (rc) {
1185 netdev_err(netdev, "failed to initialize resources\n");
1186 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001187 return rc;
1188 }
1189 }
1190
1191 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001192
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001193 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001194}
1195
Thomas Falcond0869c02018-02-13 18:23:43 -06001196static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1197{
1198 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001199 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001200 u64 rx_entries;
1201 int rx_scrqs;
1202 int i, j;
1203
1204 if (!adapter->rx_pool)
1205 return;
1206
Thomas Falcon660e3092018-04-20 14:25:32 -05001207 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001208 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1209
1210 /* Free any remaining skbs in the rx buffer pools */
1211 for (i = 0; i < rx_scrqs; i++) {
1212 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001213 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001214 continue;
1215
1216 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1217 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001218 rx_buff = &rx_pool->rx_buff[j];
1219 if (rx_buff && rx_buff->skb) {
1220 dev_kfree_skb_any(rx_buff->skb);
1221 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001222 }
1223 }
1224 }
1225}
1226
Thomas Falcone9e1e972018-03-16 20:00:30 -05001227static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1228 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001229{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001230 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001231 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001232 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001233
Dan Carpenter050e85c2018-03-23 14:36:15 +03001234 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001235 return;
1236
1237 tx_entries = tx_pool->num_buffers;
1238
1239 for (i = 0; i < tx_entries; i++) {
1240 tx_buff = &tx_pool->tx_buff[i];
1241 if (tx_buff && tx_buff->skb) {
1242 dev_kfree_skb_any(tx_buff->skb);
1243 tx_buff->skb = NULL;
1244 }
1245 }
1246}
1247
1248static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1249{
1250 int tx_scrqs;
1251 int i;
1252
1253 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001254 return;
1255
Thomas Falcon660e3092018-04-20 14:25:32 -05001256 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001257
1258 /* Free any remaining skbs in the tx buffer pools */
1259 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001260 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001261 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1262 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001263 }
1264}
1265
John Allen6095e592018-03-30 13:44:21 -05001266static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001267{
John Allen6095e592018-03-30 13:44:21 -05001268 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001269 int i;
1270
Nathan Fontenot46293b92017-05-03 14:05:02 -04001271 if (adapter->tx_scrq) {
1272 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001273 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001274 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001275 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001276 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001277 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001278 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001279 }
1280
Nathan Fontenot46293b92017-05-03 14:05:02 -04001281 if (adapter->rx_scrq) {
1282 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001283 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001284 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001285 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001286 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001287 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001288 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001289 }
1290 }
John Allen6095e592018-03-30 13:44:21 -05001291}
1292
1293static void ibmvnic_cleanup(struct net_device *netdev)
1294{
1295 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1296
1297 /* ensure that transmissions are stopped if called by do_reset */
Juliet Kim7ed5b312019-09-20 16:11:23 -04001298 if (test_bit(0, &adapter->resetting))
John Allen6095e592018-03-30 13:44:21 -05001299 netif_tx_disable(netdev);
1300 else
1301 netif_tx_stop_all_queues(netdev);
1302
1303 ibmvnic_napi_disable(adapter);
1304 ibmvnic_disable_irqs(adapter);
1305
Thomas Falcond0869c02018-02-13 18:23:43 -06001306 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001307 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001308}
1309
1310static int __ibmvnic_close(struct net_device *netdev)
1311{
1312 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1313 int rc = 0;
1314
1315 adapter->state = VNIC_CLOSING;
1316 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1317 if (rc)
1318 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001319 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001320 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001321}
1322
Nathan Fontenoted651a12017-05-03 14:04:38 -04001323static int ibmvnic_close(struct net_device *netdev)
1324{
1325 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1326 int rc;
1327
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001328 /* If device failover is pending, just set device state and return.
1329 * Device operation will be handled by reset routine.
1330 */
1331 if (adapter->failover_pending) {
1332 adapter->state = VNIC_CLOSED;
1333 return 0;
1334 }
1335
Nathan Fontenoted651a12017-05-03 14:04:38 -04001336 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001337 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001338
1339 return rc;
1340}
1341
Thomas Falconad7775d2016-04-01 17:20:34 -05001342/**
1343 * build_hdr_data - creates L2/L3/L4 header data buffer
1344 * @hdr_field - bitfield determining needed headers
1345 * @skb - socket buffer
1346 * @hdr_len - array of header lengths
1347 * @tot_len - total length of data
1348 *
1349 * Reads hdr_field to determine which headers are needed by firmware.
1350 * Builds a buffer containing these headers. Saves individual header
1351 * lengths and total buffer length to be used to build descriptors.
1352 */
1353static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1354 int *hdr_len, u8 *hdr_data)
1355{
1356 int len = 0;
1357 u8 *hdr;
1358
Thomas Falconda75e3b2018-03-12 11:51:02 -05001359 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1360 hdr_len[0] = sizeof(struct vlan_ethhdr);
1361 else
1362 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001363
1364 if (skb->protocol == htons(ETH_P_IP)) {
1365 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1366 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1367 hdr_len[2] = tcp_hdrlen(skb);
1368 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1369 hdr_len[2] = sizeof(struct udphdr);
1370 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1371 hdr_len[1] = sizeof(struct ipv6hdr);
1372 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1373 hdr_len[2] = tcp_hdrlen(skb);
1374 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1375 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001376 } else if (skb->protocol == htons(ETH_P_ARP)) {
1377 hdr_len[1] = arp_hdr_len(skb->dev);
1378 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001379 }
1380
1381 memset(hdr_data, 0, 120);
1382 if ((hdr_field >> 6) & 1) {
1383 hdr = skb_mac_header(skb);
1384 memcpy(hdr_data, hdr, hdr_len[0]);
1385 len += hdr_len[0];
1386 }
1387
1388 if ((hdr_field >> 5) & 1) {
1389 hdr = skb_network_header(skb);
1390 memcpy(hdr_data + len, hdr, hdr_len[1]);
1391 len += hdr_len[1];
1392 }
1393
1394 if ((hdr_field >> 4) & 1) {
1395 hdr = skb_transport_header(skb);
1396 memcpy(hdr_data + len, hdr, hdr_len[2]);
1397 len += hdr_len[2];
1398 }
1399 return len;
1400}
1401
1402/**
1403 * create_hdr_descs - create header and header extension descriptors
1404 * @hdr_field - bitfield determining needed headers
1405 * @data - buffer containing header data
1406 * @len - length of data buffer
1407 * @hdr_len - array of individual header lengths
1408 * @scrq_arr - descriptor array
1409 *
1410 * Creates header and, if needed, header extension descriptors and
1411 * places them in a descriptor array, scrq_arr
1412 */
1413
Thomas Falcon2de09682017-10-16 10:02:11 -05001414static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1415 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001416{
1417 union sub_crq hdr_desc;
1418 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001419 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001420 u8 *data, *cur;
1421 int tmp;
1422
1423 while (tmp_len > 0) {
1424 cur = hdr_data + len - tmp_len;
1425
1426 memset(&hdr_desc, 0, sizeof(hdr_desc));
1427 if (cur != hdr_data) {
1428 data = hdr_desc.hdr_ext.data;
1429 tmp = tmp_len > 29 ? 29 : tmp_len;
1430 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1431 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1432 hdr_desc.hdr_ext.len = tmp;
1433 } else {
1434 data = hdr_desc.hdr.data;
1435 tmp = tmp_len > 24 ? 24 : tmp_len;
1436 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1437 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1438 hdr_desc.hdr.len = tmp;
1439 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1440 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1441 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1442 hdr_desc.hdr.flag = hdr_field << 1;
1443 }
1444 memcpy(data, cur, tmp);
1445 tmp_len -= tmp;
1446 *scrq_arr = hdr_desc;
1447 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001448 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001449 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001450
1451 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001452}
1453
1454/**
1455 * build_hdr_descs_arr - build a header descriptor array
1456 * @skb - socket buffer
1457 * @num_entries - number of descriptors to be sent
1458 * @subcrq - first TX descriptor
1459 * @hdr_field - bit field determining which headers will be sent
1460 *
1461 * This function will build a TX descriptor array with applicable
1462 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1463 */
1464
1465static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1466 int *num_entries, u8 hdr_field)
1467{
1468 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001469 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001470 u8 *hdr_data = txbuff->hdr_data;
1471
1472 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1473 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001474 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001475 txbuff->indir_arr + 1);
1476}
1477
Thomas Falcon1f247a62018-03-12 11:51:04 -05001478static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1479 struct net_device *netdev)
1480{
1481 /* For some backing devices, mishandling of small packets
1482 * can result in a loss of connection or TX stall. Device
1483 * architects recommend that no packet should be smaller
1484 * than the minimum MTU value provided to the driver, so
1485 * pad any packets to that length
1486 */
1487 if (skb->len < netdev->min_mtu)
1488 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001489
1490 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001491}
1492
YueHaibing94b2bb22018-09-18 14:35:47 +08001493static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001494{
1495 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1496 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001497 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001498 struct device *dev = &adapter->vdev->dev;
1499 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001500 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001501 struct ibmvnic_tx_pool *tx_pool;
1502 unsigned int tx_send_failed = 0;
1503 unsigned int tx_map_failed = 0;
1504 unsigned int tx_dropped = 0;
1505 unsigned int tx_packets = 0;
1506 unsigned int tx_bytes = 0;
1507 dma_addr_t data_dma_addr;
1508 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001509 unsigned long lpar_rc;
1510 union sub_crq tx_crq;
1511 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001512 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001513 unsigned char *dst;
1514 u64 *handle_array;
1515 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001516 u8 proto = 0;
YueHaibing94b2bb22018-09-18 14:35:47 +08001517 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001518
Juliet Kim7ed5b312019-09-20 16:11:23 -04001519 if (test_bit(0, &adapter->resetting)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001520 if (!netif_subqueue_stopped(netdev, skb))
1521 netif_stop_subqueue(netdev, queue_num);
1522 dev_kfree_skb_any(skb);
1523
Thomas Falcon032c5e82015-12-21 11:26:06 -06001524 tx_send_failed++;
1525 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001526 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001527 goto out;
1528 }
1529
Thomas Falcon7083a452018-03-12 21:05:26 -05001530 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001531 tx_dropped++;
1532 tx_send_failed++;
1533 ret = NETDEV_TX_OK;
1534 goto out;
1535 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001536 if (skb_is_gso(skb))
1537 tx_pool = &adapter->tso_pool[queue_num];
1538 else
1539 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001540
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001541 tx_scrq = adapter->tx_scrq[queue_num];
1542 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1543 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1544 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1545
Thomas Falcon032c5e82015-12-21 11:26:06 -06001546 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001547
Thomas Falcon86b61a52018-03-16 20:00:29 -05001548 if (index == IBMVNIC_INVALID_MAP) {
1549 dev_kfree_skb_any(skb);
1550 tx_send_failed++;
1551 tx_dropped++;
1552 ret = NETDEV_TX_OK;
1553 goto out;
1554 }
1555
1556 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1557
Thomas Falcon06b3e352018-03-16 20:00:28 -05001558 offset = index * tx_pool->buf_size;
1559 dst = tx_pool->long_term_buff.buff + offset;
1560 memset(dst, 0, tx_pool->buf_size);
1561 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001562
Thomas Falcon15482052017-10-17 12:36:54 -05001563 if (skb_shinfo(skb)->nr_frags) {
1564 int cur, i;
1565
1566 /* Copy the head */
1567 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1568 cur = skb_headlen(skb);
1569
1570 /* Copy the frags */
1571 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1572 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1573
1574 memcpy(dst + cur,
1575 page_address(skb_frag_page(frag)) +
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001576 skb_frag_off(frag), skb_frag_size(frag));
Thomas Falcon15482052017-10-17 12:36:54 -05001577 cur += skb_frag_size(frag);
1578 }
1579 } else {
1580 skb_copy_from_linear_data(skb, dst, skb->len);
1581 }
1582
Thomas Falcon032c5e82015-12-21 11:26:06 -06001583 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001584 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001585
1586 tx_buff = &tx_pool->tx_buff[index];
1587 tx_buff->skb = skb;
1588 tx_buff->data_dma[0] = data_dma_addr;
1589 tx_buff->data_len[0] = skb->len;
1590 tx_buff->index = index;
1591 tx_buff->pool_index = queue_num;
1592 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001593
1594 memset(&tx_crq, 0, sizeof(tx_crq));
1595 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1596 tx_crq.v1.type = IBMVNIC_TX_DESC;
1597 tx_crq.v1.n_crq_elem = 1;
1598 tx_crq.v1.n_sge = 1;
1599 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001600
Thomas Falconfdb06102017-10-17 12:36:55 -05001601 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001602 tx_crq.v1.correlator =
1603 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001604 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001605 tx_crq.v1.correlator = cpu_to_be32(index);
1606 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001607 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1608 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1609
Michał Mirosławe84b4792018-11-07 17:50:52 +01001610 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001611 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1612 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1613 }
1614
1615 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001616 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1617 proto = ip_hdr(skb)->protocol;
1618 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1619 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1620 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001621 }
1622
Thomas Falcona0dca102018-01-18 19:29:48 -06001623 if (proto == IPPROTO_TCP)
1624 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1625 else if (proto == IPPROTO_UDP)
1626 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1627
Thomas Falconad7775d2016-04-01 17:20:34 -05001628 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001629 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001630 hdrs += 2;
1631 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001632 if (skb_is_gso(skb)) {
1633 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1634 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1635 hdrs += 2;
1636 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001637 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001638 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001639 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1640 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001641 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001642 tx_buff->indir_arr[0] = tx_crq;
1643 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1644 sizeof(tx_buff->indir_arr),
1645 DMA_TO_DEVICE);
1646 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001647 dev_kfree_skb_any(skb);
1648 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001649 if (!firmware_has_feature(FW_FEATURE_CMO))
1650 dev_err(dev, "tx: unable to map descriptor array\n");
1651 tx_map_failed++;
1652 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001653 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001654 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001655 }
John Allen498cd8e2016-04-06 11:49:55 -05001656 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001657 (u64)tx_buff->indir_dma,
1658 (u64)num_entries);
Thomas Falcon80f0fe02019-08-14 14:57:05 -05001659 dma_unmap_single(dev, tx_buff->indir_dma,
1660 sizeof(tx_buff->indir_arr), DMA_TO_DEVICE);
Thomas Falconad7775d2016-04-01 17:20:34 -05001661 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001662 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001663 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1664 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001665 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001666 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001667 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1668 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001669 dev_kfree_skb_any(skb);
1670 tx_buff->skb = NULL;
1671
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001672 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1673 /* Disable TX and report carrier off if queue is closed
1674 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001675 * Firmware guarantees that a signal will be sent to the
1676 * driver, triggering a reset or some other action.
1677 */
1678 netif_tx_stop_all_queues(netdev);
1679 netif_carrier_off(netdev);
1680 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001681
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682 tx_send_failed++;
1683 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001684 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001685 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001687
Thomas Falconffc385b2018-02-18 10:08:41 -06001688 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001689 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001690 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001691 netif_stop_subqueue(netdev, queue_num);
1692 }
1693
Thomas Falcon032c5e82015-12-21 11:26:06 -06001694 tx_packets++;
1695 tx_bytes += skb->len;
1696 txq->trans_start = jiffies;
1697 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001698 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001699
Thomas Falcon86b61a52018-03-16 20:00:29 -05001700tx_err_out:
1701 /* roll back consumer index and map array*/
1702 if (tx_pool->consumer_index == 0)
1703 tx_pool->consumer_index =
1704 tx_pool->num_buffers - 1;
1705 else
1706 tx_pool->consumer_index--;
1707 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001708out:
1709 netdev->stats.tx_dropped += tx_dropped;
1710 netdev->stats.tx_bytes += tx_bytes;
1711 netdev->stats.tx_packets += tx_packets;
1712 adapter->tx_send_failed += tx_send_failed;
1713 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001714 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1715 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1716 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001717
1718 return ret;
1719}
1720
1721static void ibmvnic_set_multi(struct net_device *netdev)
1722{
1723 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1724 struct netdev_hw_addr *ha;
1725 union ibmvnic_crq crq;
1726
1727 memset(&crq, 0, sizeof(crq));
1728 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1729 crq.request_capability.cmd = REQUEST_CAPABILITY;
1730
1731 if (netdev->flags & IFF_PROMISC) {
1732 if (!adapter->promisc_supported)
1733 return;
1734 } else {
1735 if (netdev->flags & IFF_ALLMULTI) {
1736 /* Accept all multicast */
1737 memset(&crq, 0, sizeof(crq));
1738 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1739 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1740 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1741 ibmvnic_send_crq(adapter, &crq);
1742 } else if (netdev_mc_empty(netdev)) {
1743 /* Reject all multicast */
1744 memset(&crq, 0, sizeof(crq));
1745 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1746 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1747 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1748 ibmvnic_send_crq(adapter, &crq);
1749 } else {
1750 /* Accept one or more multicast(s) */
1751 netdev_for_each_mc_addr(ha, netdev) {
1752 memset(&crq, 0, sizeof(crq));
1753 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1754 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1755 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1756 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1757 ha->addr);
1758 ibmvnic_send_crq(adapter, &crq);
1759 }
1760 }
1761 }
1762}
1763
Thomas Falcon62740e92019-05-09 23:13:43 -05001764static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001765{
1766 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001767 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001768 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001769
Thomas Falcon62740e92019-05-09 23:13:43 -05001770 if (!is_valid_ether_addr(dev_addr)) {
1771 rc = -EADDRNOTAVAIL;
1772 goto err;
1773 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774
1775 memset(&crq, 0, sizeof(crq));
1776 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1777 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001778 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001779
Thomas Falconff25dcb2019-11-25 17:12:56 -06001780 mutex_lock(&adapter->fw_lock);
1781 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06001782 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06001783
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001784 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001785 if (rc) {
1786 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001787 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001788 goto err;
1789 }
1790
Thomas Falcon476d96c2019-11-25 17:12:55 -06001791 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001792 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon476d96c2019-11-25 17:12:55 -06001793 if (rc || adapter->fw_done_rc) {
Thomas Falcon62740e92019-05-09 23:13:43 -05001794 rc = -EIO;
Thomas Falconff25dcb2019-11-25 17:12:56 -06001795 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001796 goto err;
1797 }
Thomas Falconff25dcb2019-11-25 17:12:56 -06001798 mutex_unlock(&adapter->fw_lock);
Thomas Falcon62740e92019-05-09 23:13:43 -05001799 return 0;
1800err:
1801 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1802 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001803}
1804
John Allenc26eba02017-10-26 16:23:25 -05001805static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1806{
1807 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1808 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001809 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001810
Thomas Falcon62740e92019-05-09 23:13:43 -05001811 rc = 0;
1812 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1813 if (adapter->state != VNIC_PROBED)
1814 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001815
Thomas Falconf8136142018-01-29 13:45:05 -06001816 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001817}
1818
Nathan Fontenoted651a12017-05-03 14:04:38 -04001819/**
Juliet Kimb27507b2019-09-20 16:11:22 -04001820 * do_change_param_reset returns zero if we are able to keep processing reset
1821 * events, or non-zero if we hit a fatal error and must halt.
1822 */
1823static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1824 struct ibmvnic_rwi *rwi,
1825 u32 reset_state)
1826{
1827 struct net_device *netdev = adapter->netdev;
1828 int i, rc;
1829
1830 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1831 rwi->reset_reason);
1832
1833 netif_carrier_off(netdev);
1834 adapter->reset_reason = rwi->reset_reason;
1835
1836 ibmvnic_cleanup(netdev);
1837
1838 if (reset_state == VNIC_OPEN) {
1839 rc = __ibmvnic_close(netdev);
1840 if (rc)
1841 return rc;
1842 }
1843
1844 release_resources(adapter);
1845 release_sub_crqs(adapter, 1);
1846 release_crq_queue(adapter);
1847
1848 adapter->state = VNIC_PROBED;
1849
1850 rc = init_crq_queue(adapter);
1851
1852 if (rc) {
1853 netdev_err(adapter->netdev,
1854 "Couldn't initialize crq. rc=%d\n", rc);
1855 return rc;
1856 }
1857
1858 rc = ibmvnic_reset_init(adapter);
1859 if (rc)
1860 return IBMVNIC_INIT_FAILED;
1861
1862 /* If the adapter was in PROBE state prior to the reset,
1863 * exit here.
1864 */
1865 if (reset_state == VNIC_PROBED)
1866 return 0;
1867
1868 rc = ibmvnic_login(netdev);
1869 if (rc) {
1870 adapter->state = reset_state;
1871 return rc;
1872 }
1873
1874 rc = init_resources(adapter);
1875 if (rc)
1876 return rc;
1877
1878 ibmvnic_disable_irqs(adapter);
1879
1880 adapter->state = VNIC_CLOSED;
1881
1882 if (reset_state == VNIC_CLOSED)
1883 return 0;
1884
1885 rc = __ibmvnic_open(netdev);
1886 if (rc)
1887 return IBMVNIC_OPEN_FAILED;
1888
1889 /* refresh device's multicast list */
1890 ibmvnic_set_multi(netdev);
1891
1892 /* kick napi */
1893 for (i = 0; i < adapter->req_rx_queues; i++)
1894 napi_schedule(&adapter->napi[i]);
1895
1896 return 0;
1897}
1898
1899/**
Nathan Fontenoted651a12017-05-03 14:04:38 -04001900 * do_reset returns zero if we are able to keep processing reset events, or
1901 * non-zero if we hit a fatal error and must halt.
1902 */
1903static int do_reset(struct ibmvnic_adapter *adapter,
1904 struct ibmvnic_rwi *rwi, u32 reset_state)
1905{
John Allen896d8692018-01-18 16:26:31 -06001906 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001907 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001908 struct net_device *netdev = adapter->netdev;
1909 int i, rc;
1910
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001911 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1912 rwi->reset_reason);
1913
Juliet Kimb27507b2019-09-20 16:11:22 -04001914 rtnl_lock();
1915
Nathan Fontenoted651a12017-05-03 14:04:38 -04001916 netif_carrier_off(netdev);
1917 adapter->reset_reason = rwi->reset_reason;
1918
John Allen896d8692018-01-18 16:26:31 -06001919 old_num_rx_queues = adapter->req_rx_queues;
1920 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001921 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1922 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001923
Nathan Fontenot30f79622018-04-06 18:37:06 -05001924 ibmvnic_cleanup(netdev);
1925
Thomas Falcon1f946082019-06-07 16:03:53 -05001926 if (reset_state == VNIC_OPEN &&
1927 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001928 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Juliet Kimb27507b2019-09-20 16:11:22 -04001929 adapter->state = VNIC_CLOSING;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001930
Juliet Kimb27507b2019-09-20 16:11:22 -04001931 /* Release the RTNL lock before link state change and
1932 * re-acquire after the link state change to allow
1933 * linkwatch_event to grab the RTNL lock and run during
1934 * a reset.
1935 */
1936 rtnl_unlock();
1937 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1938 rtnl_lock();
1939 if (rc)
1940 goto out;
1941
1942 if (adapter->state != VNIC_CLOSING) {
1943 rc = -1;
1944 goto out;
1945 }
1946
1947 adapter->state = VNIC_CLOSED;
John Allenc26eba02017-10-26 16:23:25 -05001948 }
1949
John Allen8cb31cf2017-05-26 10:30:37 -04001950 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1951 /* remove the closed state so when we call open it appears
1952 * we are coming from the probed state.
1953 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001954 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001955
Juliet Kimb27507b2019-09-20 16:11:22 -04001956 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
Nathan Fontenot30f79622018-04-06 18:37:06 -05001957 rc = ibmvnic_reenable_crq_queue(adapter);
1958 release_sub_crqs(adapter, 1);
1959 } else {
1960 rc = ibmvnic_reset_crq(adapter);
1961 if (!rc)
1962 rc = vio_enable_interrupts(adapter->vdev);
1963 }
1964
1965 if (rc) {
1966 netdev_err(adapter->netdev,
1967 "Couldn't initialize crq. rc=%d\n", rc);
Juliet Kimb27507b2019-09-20 16:11:22 -04001968 goto out;
Nathan Fontenot30f79622018-04-06 18:37:06 -05001969 }
1970
Thomas Falcon8a348452018-05-23 13:38:00 -05001971 rc = ibmvnic_reset_init(adapter);
Juliet Kimb27507b2019-09-20 16:11:22 -04001972 if (rc) {
1973 rc = IBMVNIC_INIT_FAILED;
1974 goto out;
1975 }
John Allen8cb31cf2017-05-26 10:30:37 -04001976
1977 /* If the adapter was in PROBE state prior to the reset,
1978 * exit here.
1979 */
Juliet Kimb27507b2019-09-20 16:11:22 -04001980 if (reset_state == VNIC_PROBED) {
1981 rc = 0;
1982 goto out;
1983 }
John Allen8cb31cf2017-05-26 10:30:37 -04001984
1985 rc = ibmvnic_login(netdev);
1986 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05001987 adapter->state = reset_state;
Juliet Kimb27507b2019-09-20 16:11:22 -04001988 goto out;
John Allen8cb31cf2017-05-26 10:30:37 -04001989 }
1990
Juliet Kimb27507b2019-09-20 16:11:22 -04001991 if (adapter->req_rx_queues != old_num_rx_queues ||
1992 adapter->req_tx_queues != old_num_tx_queues ||
1993 adapter->req_rx_add_entries_per_subcrq !=
1994 old_num_rx_slots ||
1995 adapter->req_tx_entries_per_subcrq !=
1996 old_num_tx_slots) {
John Allen896d8692018-01-18 16:26:31 -06001997 release_rx_pools(adapter);
1998 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06001999 release_napi(adapter);
2000 release_vpd_data(adapter);
2001
2002 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05002003 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002004 goto out;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06002005
John Allenc26eba02017-10-26 16:23:25 -05002006 } else {
2007 rc = reset_tx_pools(adapter);
2008 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002009 goto out;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04002010
John Allenc26eba02017-10-26 16:23:25 -05002011 rc = reset_rx_pools(adapter);
2012 if (rc)
Juliet Kimb27507b2019-09-20 16:11:22 -04002013 goto out;
John Allenc26eba02017-10-26 16:23:25 -05002014 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05002015 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002016 }
John Allene676d812018-03-14 10:41:29 -05002017 adapter->state = VNIC_CLOSED;
2018
Juliet Kimb27507b2019-09-20 16:11:22 -04002019 if (reset_state == VNIC_CLOSED) {
2020 rc = 0;
2021 goto out;
2022 }
John Allene676d812018-03-14 10:41:29 -05002023
Nathan Fontenoted651a12017-05-03 14:04:38 -04002024 rc = __ibmvnic_open(netdev);
2025 if (rc) {
Juliet Kimb27507b2019-09-20 16:11:22 -04002026 rc = IBMVNIC_OPEN_FAILED;
2027 goto out;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002028 }
2029
Thomas Falconbe32a242019-06-07 16:03:54 -05002030 /* refresh device's multicast list */
2031 ibmvnic_set_multi(netdev);
2032
Nathan Fontenoted651a12017-05-03 14:04:38 -04002033 /* kick napi */
2034 for (i = 0; i < adapter->req_rx_queues; i++)
2035 napi_schedule(&adapter->napi[i]);
2036
Juliet Kimb27507b2019-09-20 16:11:22 -04002037 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
Thomas Falcon986103e2018-11-30 10:59:08 -06002038 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04002039
Juliet Kimb27507b2019-09-20 16:11:22 -04002040 rc = 0;
2041
2042out:
2043 rtnl_unlock();
2044
2045 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002046}
2047
Thomas Falcon2770a792018-05-23 13:38:02 -05002048static int do_hard_reset(struct ibmvnic_adapter *adapter,
2049 struct ibmvnic_rwi *rwi, u32 reset_state)
2050{
2051 struct net_device *netdev = adapter->netdev;
2052 int rc;
2053
2054 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2055 rwi->reset_reason);
2056
2057 netif_carrier_off(netdev);
2058 adapter->reset_reason = rwi->reset_reason;
2059
2060 ibmvnic_cleanup(netdev);
2061 release_resources(adapter);
2062 release_sub_crqs(adapter, 0);
2063 release_crq_queue(adapter);
2064
2065 /* remove the closed state so when we call open it appears
2066 * we are coming from the probed state.
2067 */
2068 adapter->state = VNIC_PROBED;
2069
Thomas Falconbbd669a2019-04-04 18:58:26 -05002070 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05002071 rc = init_crq_queue(adapter);
2072 if (rc) {
2073 netdev_err(adapter->netdev,
2074 "Couldn't initialize crq. rc=%d\n", rc);
2075 return rc;
2076 }
2077
2078 rc = ibmvnic_init(adapter);
2079 if (rc)
2080 return rc;
2081
2082 /* If the adapter was in PROBE state prior to the reset,
2083 * exit here.
2084 */
2085 if (reset_state == VNIC_PROBED)
2086 return 0;
2087
2088 rc = ibmvnic_login(netdev);
2089 if (rc) {
2090 adapter->state = VNIC_PROBED;
2091 return 0;
2092 }
Juliet Kima5681e22018-11-19 15:59:22 -06002093
2094 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05002095 if (rc)
2096 return rc;
2097
2098 ibmvnic_disable_irqs(adapter);
2099 adapter->state = VNIC_CLOSED;
2100
2101 if (reset_state == VNIC_CLOSED)
2102 return 0;
2103
2104 rc = __ibmvnic_open(netdev);
Juliet Kimb27507b2019-09-20 16:11:22 -04002105 if (rc)
2106 return IBMVNIC_OPEN_FAILED;
Thomas Falcon2770a792018-05-23 13:38:02 -05002107
Thomas Falcon2770a792018-05-23 13:38:02 -05002108 return 0;
2109}
2110
Nathan Fontenoted651a12017-05-03 14:04:38 -04002111static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2112{
2113 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002114 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002115
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002116 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002117
2118 if (!list_empty(&adapter->rwi_list)) {
2119 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2120 list);
2121 list_del(&rwi->list);
2122 } else {
2123 rwi = NULL;
2124 }
2125
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002126 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002127 return rwi;
2128}
2129
2130static void free_all_rwi(struct ibmvnic_adapter *adapter)
2131{
2132 struct ibmvnic_rwi *rwi;
2133
2134 rwi = get_next_rwi(adapter);
2135 while (rwi) {
2136 kfree(rwi);
2137 rwi = get_next_rwi(adapter);
2138 }
2139}
2140
2141static void __ibmvnic_reset(struct work_struct *work)
2142{
2143 struct ibmvnic_rwi *rwi;
2144 struct ibmvnic_adapter *adapter;
Juliet Kim7d7195a2020-03-10 09:23:58 -05002145 bool saved_state = false;
2146 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002147 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05002148 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002149
2150 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002151
Juliet Kim7ed5b312019-09-20 16:11:23 -04002152 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2153 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2154 IBMVNIC_RESET_DELAY);
2155 return;
2156 }
2157
Nathan Fontenoted651a12017-05-03 14:04:38 -04002158 rwi = get_next_rwi(adapter);
2159 while (rwi) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002160 spin_lock_irqsave(&adapter->state_lock, flags);
2161
Thomas Falcon36f10312019-08-27 11:10:04 -05002162 if (adapter->state == VNIC_REMOVING ||
Michal Suchanekc8dc5592019-09-09 22:44:51 +02002163 adapter->state == VNIC_REMOVED) {
Juliet Kim7d7195a2020-03-10 09:23:58 -05002164 spin_unlock_irqrestore(&adapter->state_lock, flags);
Juliet Kim1c2977c2019-09-05 17:30:01 -04002165 kfree(rwi);
2166 rc = EBUSY;
2167 break;
2168 }
Thomas Falcon36f10312019-08-27 11:10:04 -05002169
Juliet Kim7d7195a2020-03-10 09:23:58 -05002170 if (!saved_state) {
2171 reset_state = adapter->state;
2172 adapter->state = VNIC_RESETTING;
2173 saved_state = true;
2174 }
2175 spin_unlock_irqrestore(&adapter->state_lock, flags);
2176
Juliet Kimb27507b2019-09-20 16:11:22 -04002177 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2178 /* CHANGE_PARAM requestor holds rtnl_lock */
2179 rc = do_change_param_reset(adapter, rwi, reset_state);
2180 } else if (adapter->force_reset_recovery) {
2181 /* Transport event occurred during previous reset */
2182 if (adapter->wait_for_reset) {
2183 /* Previous was CHANGE_PARAM; caller locked */
2184 adapter->force_reset_recovery = false;
2185 rc = do_hard_reset(adapter, rwi, reset_state);
2186 } else {
2187 rtnl_lock();
2188 adapter->force_reset_recovery = false;
2189 rc = do_hard_reset(adapter, rwi, reset_state);
2190 rtnl_unlock();
2191 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002192 } else {
2193 rc = do_reset(adapter, rwi, reset_state);
2194 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002195 kfree(rwi);
Juliet Kimb27507b2019-09-20 16:11:22 -04002196 if (rc == IBMVNIC_OPEN_FAILED) {
2197 if (list_empty(&adapter->rwi_list))
2198 adapter->state = VNIC_CLOSED;
2199 else
2200 adapter->state = reset_state;
2201 rc = 0;
2202 } else if (rc && rc != IBMVNIC_INIT_FAILED &&
Thomas Falcon2770a792018-05-23 13:38:02 -05002203 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002204 break;
2205
2206 rwi = get_next_rwi(adapter);
Juliet Kim7ed5b312019-09-20 16:11:23 -04002207
2208 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2209 rwi->reset_reason == VNIC_RESET_MOBILITY))
2210 adapter->force_reset_recovery = true;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002211 }
2212
John Allenc26eba02017-10-26 16:23:25 -05002213 if (adapter->wait_for_reset) {
John Allenc26eba02017-10-26 16:23:25 -05002214 adapter->reset_done_rc = rc;
2215 complete(&adapter->reset_done);
2216 }
2217
Nathan Fontenoted651a12017-05-03 14:04:38 -04002218 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002219 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002220 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002221 }
Juliet Kim1c2977c2019-09-05 17:30:01 -04002222
Juliet Kim7ed5b312019-09-20 16:11:23 -04002223 clear_bit_unlock(0, &adapter->resetting);
2224}
2225
2226static void __ibmvnic_delayed_reset(struct work_struct *work)
2227{
2228 struct ibmvnic_adapter *adapter;
2229
2230 adapter = container_of(work, struct ibmvnic_adapter,
2231 ibmvnic_delayed_reset.work);
2232 __ibmvnic_reset(&adapter->ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002233}
2234
Thomas Falconaf894d22018-04-06 18:37:04 -05002235static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2236 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002237{
Thomas Falcon2770a792018-05-23 13:38:02 -05002238 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002239 struct ibmvnic_rwi *rwi, *tmp;
2240 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002241 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002242 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002243
2244 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002245 adapter->state == VNIC_REMOVED ||
2246 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002247 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002248 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002249 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002250 }
2251
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002252 if (adapter->state == VNIC_PROBING) {
2253 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002254 ret = adapter->init_done_rc = EAGAIN;
2255 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002256 }
2257
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002258 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002259
2260 list_for_each(entry, &adapter->rwi_list) {
2261 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2262 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002263 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002264 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002265 ret = EBUSY;
2266 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002267 }
2268 }
2269
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002270 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002271 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002272 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002273 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002274 ret = ENOMEM;
2275 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002276 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002277 /* if we just received a transport event,
2278 * flush reset queue and process this reset
2279 */
2280 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2281 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2282 list_del(entry);
2283 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002284 rwi->reset_reason = reason;
2285 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002286 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002287 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002288 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002289
2290 return 0;
2291err:
Thomas Falconaf894d22018-04-06 18:37:04 -05002292 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002293}
2294
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002295static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002296{
2297 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002298
Nathan Fontenoted651a12017-05-03 14:04:38 -04002299 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002300}
2301
2302static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2303 struct ibmvnic_rx_buff *rx_buff)
2304{
2305 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2306
2307 rx_buff->skb = NULL;
2308
2309 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2310 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2311
2312 atomic_dec(&pool->available);
2313}
2314
2315static int ibmvnic_poll(struct napi_struct *napi, int budget)
2316{
2317 struct net_device *netdev = napi->dev;
2318 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2319 int scrq_num = (int)(napi - adapter->napi);
2320 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002321
Thomas Falcon032c5e82015-12-21 11:26:06 -06002322restart_poll:
2323 while (frames_processed < budget) {
2324 struct sk_buff *skb;
2325 struct ibmvnic_rx_buff *rx_buff;
2326 union sub_crq *next;
2327 u32 length;
2328 u16 offset;
2329 u8 flags = 0;
2330
Juliet Kim7ed5b312019-09-20 16:11:23 -04002331 if (unlikely(test_bit(0, &adapter->resetting) &&
John Allen34686562018-02-06 16:21:49 -06002332 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002333 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2334 napi_complete_done(napi, frames_processed);
2335 return frames_processed;
2336 }
2337
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2339 break;
2340 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2341 rx_buff =
2342 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2343 rx_comp.correlator);
2344 /* do error checking */
2345 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002346 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2347 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002348 /* free the entry */
2349 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002350 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002351 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002352 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002353 } else if (!rx_buff->skb) {
2354 /* free the entry */
2355 next->rx_comp.first = 0;
2356 remove_buff_from_pool(adapter, rx_buff);
2357 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002358 }
2359
2360 length = be32_to_cpu(next->rx_comp.len);
2361 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2362 flags = next->rx_comp.flags;
2363 skb = rx_buff->skb;
2364 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2365 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002366
2367 /* VLAN Header has been stripped by the system firmware and
2368 * needs to be inserted by the driver
2369 */
2370 if (adapter->rx_vlan_header_insertion &&
2371 (flags & IBMVNIC_VLAN_STRIPPED))
2372 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2373 ntohs(next->rx_comp.vlan_tci));
2374
Thomas Falcon032c5e82015-12-21 11:26:06 -06002375 /* free the entry */
2376 next->rx_comp.first = 0;
2377 remove_buff_from_pool(adapter, rx_buff);
2378
2379 skb_put(skb, length);
2380 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002381 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002382
2383 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2384 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2385 skb->ip_summed = CHECKSUM_UNNECESSARY;
2386 }
2387
2388 length = skb->len;
2389 napi_gro_receive(napi, skb); /* send it up */
2390 netdev->stats.rx_packets++;
2391 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002392 adapter->rx_stats_buffers[scrq_num].packets++;
2393 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002394 frames_processed++;
2395 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002396
2397 if (adapter->state != VNIC_CLOSING)
2398 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002399
2400 if (frames_processed < budget) {
2401 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002402 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002403 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2404 napi_reschedule(napi)) {
2405 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2406 goto restart_poll;
2407 }
2408 }
2409 return frames_processed;
2410}
2411
John Allenc26eba02017-10-26 16:23:25 -05002412static int wait_for_reset(struct ibmvnic_adapter *adapter)
2413{
Thomas Falconaf894d22018-04-06 18:37:04 -05002414 int rc, ret;
2415
John Allenc26eba02017-10-26 16:23:25 -05002416 adapter->fallback.mtu = adapter->req_mtu;
2417 adapter->fallback.rx_queues = adapter->req_rx_queues;
2418 adapter->fallback.tx_queues = adapter->req_tx_queues;
2419 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2420 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2421
Thomas Falcon070eca92019-11-25 17:12:53 -06002422 reinit_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002423 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002424 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002425
2426 if (rc) {
2427 ret = rc;
2428 goto out;
2429 }
2430 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2431 if (rc) {
2432 ret = -ENODEV;
2433 goto out;
2434 }
John Allenc26eba02017-10-26 16:23:25 -05002435
Thomas Falconaf894d22018-04-06 18:37:04 -05002436 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002437 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002438 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002439 adapter->desired.mtu = adapter->fallback.mtu;
2440 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2441 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2442 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2443 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2444
Thomas Falcon070eca92019-11-25 17:12:53 -06002445 reinit_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002446 adapter->wait_for_reset = true;
2447 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
Thomas Falcon476d96c2019-11-25 17:12:55 -06002448 if (rc) {
2449 ret = rc;
2450 goto out;
2451 }
2452 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2453 60000);
2454 if (rc) {
2455 ret = -ENODEV;
2456 goto out;
2457 }
John Allenc26eba02017-10-26 16:23:25 -05002458 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06002459out:
John Allenc26eba02017-10-26 16:23:25 -05002460 adapter->wait_for_reset = false;
2461
Thomas Falconaf894d22018-04-06 18:37:04 -05002462 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002463}
2464
John Allen3a807b72017-06-06 16:55:52 -05002465static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2466{
John Allenc26eba02017-10-26 16:23:25 -05002467 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2468
2469 adapter->desired.mtu = new_mtu + ETH_HLEN;
2470
2471 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002472}
2473
Thomas Falconf10b09e2018-03-12 11:51:05 -05002474static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2475 struct net_device *dev,
2476 netdev_features_t features)
2477{
2478 /* Some backing hardware adapters can not
2479 * handle packets with a MSS less than 224
2480 * or with only one segment.
2481 */
2482 if (skb_is_gso(skb)) {
2483 if (skb_shinfo(skb)->gso_size < 224 ||
2484 skb_shinfo(skb)->gso_segs == 1)
2485 features &= ~NETIF_F_GSO_MASK;
2486 }
2487
2488 return features;
2489}
2490
Thomas Falcon032c5e82015-12-21 11:26:06 -06002491static const struct net_device_ops ibmvnic_netdev_ops = {
2492 .ndo_open = ibmvnic_open,
2493 .ndo_stop = ibmvnic_close,
2494 .ndo_start_xmit = ibmvnic_xmit,
2495 .ndo_set_rx_mode = ibmvnic_set_multi,
2496 .ndo_set_mac_address = ibmvnic_set_mac,
2497 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002498 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002499 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002500 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501};
2502
2503/* ethtool functions */
2504
Philippe Reynes8a433792017-01-07 22:37:29 +01002505static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2506 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002507{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002508 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2509 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002510
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002511 rc = send_query_phys_parms(adapter);
2512 if (rc) {
2513 adapter->speed = SPEED_UNKNOWN;
2514 adapter->duplex = DUPLEX_UNKNOWN;
2515 }
2516 cmd->base.speed = adapter->speed;
2517 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002518 cmd->base.port = PORT_FIBRE;
2519 cmd->base.phy_address = 0;
2520 cmd->base.autoneg = AUTONEG_ENABLE;
2521
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522 return 0;
2523}
2524
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002525static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002526 struct ethtool_drvinfo *info)
2527{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002528 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2529
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2531 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002532 strlcpy(info->fw_version, adapter->fw_version,
2533 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002534}
2535
2536static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2537{
2538 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2539
2540 return adapter->msg_enable;
2541}
2542
2543static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2544{
2545 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2546
2547 adapter->msg_enable = data;
2548}
2549
2550static u32 ibmvnic_get_link(struct net_device *netdev)
2551{
2552 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2553
2554 /* Don't need to send a query because we request a logical link up at
2555 * init and then we wait for link state indications
2556 */
2557 return adapter->logical_link_state;
2558}
2559
2560static void ibmvnic_get_ringparam(struct net_device *netdev,
2561 struct ethtool_ringparam *ring)
2562{
John Allenbc131b32017-08-02 16:46:30 -05002563 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2564
Thomas Falcon723ad912018-09-28 18:38:26 -05002565 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2566 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2567 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2568 } else {
2569 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2570 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2571 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002572 ring->rx_mini_max_pending = 0;
2573 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002574 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2575 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002576 ring->rx_mini_pending = 0;
2577 ring->rx_jumbo_pending = 0;
2578}
2579
John Allenc26eba02017-10-26 16:23:25 -05002580static int ibmvnic_set_ringparam(struct net_device *netdev,
2581 struct ethtool_ringparam *ring)
2582{
2583 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002584 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002585
Thomas Falcon723ad912018-09-28 18:38:26 -05002586 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002587 adapter->desired.rx_entries = ring->rx_pending;
2588 adapter->desired.tx_entries = ring->tx_pending;
2589
Thomas Falcon723ad912018-09-28 18:38:26 -05002590 ret = wait_for_reset(adapter);
2591
2592 if (!ret &&
2593 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2594 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2595 netdev_info(netdev,
2596 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2597 ring->rx_pending, ring->tx_pending,
2598 adapter->req_rx_add_entries_per_subcrq,
2599 adapter->req_tx_entries_per_subcrq);
2600 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002601}
2602
John Allenc2dbeb62017-08-02 16:47:17 -05002603static void ibmvnic_get_channels(struct net_device *netdev,
2604 struct ethtool_channels *channels)
2605{
2606 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2607
Thomas Falcon723ad912018-09-28 18:38:26 -05002608 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2609 channels->max_rx = adapter->max_rx_queues;
2610 channels->max_tx = adapter->max_tx_queues;
2611 } else {
2612 channels->max_rx = IBMVNIC_MAX_QUEUES;
2613 channels->max_tx = IBMVNIC_MAX_QUEUES;
2614 }
2615
John Allenc2dbeb62017-08-02 16:47:17 -05002616 channels->max_other = 0;
2617 channels->max_combined = 0;
2618 channels->rx_count = adapter->req_rx_queues;
2619 channels->tx_count = adapter->req_tx_queues;
2620 channels->other_count = 0;
2621 channels->combined_count = 0;
2622}
2623
John Allenc26eba02017-10-26 16:23:25 -05002624static int ibmvnic_set_channels(struct net_device *netdev,
2625 struct ethtool_channels *channels)
2626{
2627 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002628 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002629
Thomas Falcon723ad912018-09-28 18:38:26 -05002630 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002631 adapter->desired.rx_queues = channels->rx_count;
2632 adapter->desired.tx_queues = channels->tx_count;
2633
Thomas Falcon723ad912018-09-28 18:38:26 -05002634 ret = wait_for_reset(adapter);
2635
2636 if (!ret &&
2637 (adapter->req_rx_queues != channels->rx_count ||
2638 adapter->req_tx_queues != channels->tx_count))
2639 netdev_info(netdev,
2640 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2641 channels->rx_count, channels->tx_count,
2642 adapter->req_rx_queues, adapter->req_tx_queues);
2643 return ret;
2644
John Allenc26eba02017-10-26 16:23:25 -05002645}
2646
Thomas Falcon032c5e82015-12-21 11:26:06 -06002647static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2648{
John Allen3d52b592017-08-02 16:44:14 -05002649 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002650 int i;
2651
Thomas Falcon723ad912018-09-28 18:38:26 -05002652 switch (stringset) {
2653 case ETH_SS_STATS:
2654 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2655 i++, data += ETH_GSTRING_LEN)
2656 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2657
2658 for (i = 0; i < adapter->req_tx_queues; i++) {
2659 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2660 data += ETH_GSTRING_LEN;
2661
2662 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2663 data += ETH_GSTRING_LEN;
2664
2665 snprintf(data, ETH_GSTRING_LEN,
2666 "tx%d_dropped_packets", i);
2667 data += ETH_GSTRING_LEN;
2668 }
2669
2670 for (i = 0; i < adapter->req_rx_queues; i++) {
2671 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2672 data += ETH_GSTRING_LEN;
2673
2674 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2675 data += ETH_GSTRING_LEN;
2676
2677 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2678 data += ETH_GSTRING_LEN;
2679 }
2680 break;
2681
2682 case ETH_SS_PRIV_FLAGS:
2683 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2684 strcpy(data + i * ETH_GSTRING_LEN,
2685 ibmvnic_priv_flags[i]);
2686 break;
2687 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002688 return;
John Allen3d52b592017-08-02 16:44:14 -05002689 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690}
2691
2692static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2693{
John Allen3d52b592017-08-02 16:44:14 -05002694 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2695
Thomas Falcon032c5e82015-12-21 11:26:06 -06002696 switch (sset) {
2697 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002698 return ARRAY_SIZE(ibmvnic_stats) +
2699 adapter->req_tx_queues * NUM_TX_STATS +
2700 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002701 case ETH_SS_PRIV_FLAGS:
2702 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 default:
2704 return -EOPNOTSUPP;
2705 }
2706}
2707
2708static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2709 struct ethtool_stats *stats, u64 *data)
2710{
2711 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2712 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002713 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002714 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715
2716 memset(&crq, 0, sizeof(crq));
2717 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2718 crq.request_statistics.cmd = REQUEST_STATISTICS;
2719 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2720 crq.request_statistics.len =
2721 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722
2723 /* Wait for data to be written */
Thomas Falcon070eca92019-11-25 17:12:53 -06002724 reinit_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002725 rc = ibmvnic_send_crq(adapter, &crq);
2726 if (rc)
2727 return;
Thomas Falcon476d96c2019-11-25 17:12:55 -06002728 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2729 if (rc)
2730 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731
2732 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002733 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2734 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002735
2736 for (j = 0; j < adapter->req_tx_queues; j++) {
2737 data[i] = adapter->tx_stats_buffers[j].packets;
2738 i++;
2739 data[i] = adapter->tx_stats_buffers[j].bytes;
2740 i++;
2741 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2742 i++;
2743 }
2744
2745 for (j = 0; j < adapter->req_rx_queues; j++) {
2746 data[i] = adapter->rx_stats_buffers[j].packets;
2747 i++;
2748 data[i] = adapter->rx_stats_buffers[j].bytes;
2749 i++;
2750 data[i] = adapter->rx_stats_buffers[j].interrupts;
2751 i++;
2752 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002753}
2754
Thomas Falcon723ad912018-09-28 18:38:26 -05002755static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2756{
2757 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2758
2759 return adapter->priv_flags;
2760}
2761
2762static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2763{
2764 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2765 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2766
2767 if (which_maxes)
2768 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2769 else
2770 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2771
2772 return 0;
2773}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002774static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002775 .get_drvinfo = ibmvnic_get_drvinfo,
2776 .get_msglevel = ibmvnic_get_msglevel,
2777 .set_msglevel = ibmvnic_set_msglevel,
2778 .get_link = ibmvnic_get_link,
2779 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002780 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002781 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002782 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002783 .get_strings = ibmvnic_get_strings,
2784 .get_sset_count = ibmvnic_get_sset_count,
2785 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002786 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002787 .get_priv_flags = ibmvnic_get_priv_flags,
2788 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002789};
2790
2791/* Routines for managing CRQs/sCRQs */
2792
Nathan Fontenot57a49432017-05-26 10:31:12 -04002793static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2794 struct ibmvnic_sub_crq_queue *scrq)
2795{
2796 int rc;
2797
2798 if (scrq->irq) {
2799 free_irq(scrq->irq, scrq);
2800 irq_dispose_mapping(scrq->irq);
2801 scrq->irq = 0;
2802 }
2803
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002804 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002805 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002806 scrq->cur = 0;
2807
2808 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2809 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2810 return rc;
2811}
2812
2813static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2814{
2815 int i, rc;
2816
2817 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002818 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002819 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2820 if (rc)
2821 return rc;
2822 }
2823
2824 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002825 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002826 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2827 if (rc)
2828 return rc;
2829 }
2830
Nathan Fontenot57a49432017-05-26 10:31:12 -04002831 return rc;
2832}
2833
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002835 struct ibmvnic_sub_crq_queue *scrq,
2836 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002837{
2838 struct device *dev = &adapter->vdev->dev;
2839 long rc;
2840
2841 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2842
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002843 if (do_h_free) {
2844 /* Close the sub-crqs */
2845 do {
2846 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2847 adapter->vdev->unit_address,
2848 scrq->crq_num);
2849 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002850
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002851 if (rc) {
2852 netdev_err(adapter->netdev,
2853 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2854 scrq->crq_num, rc);
2855 }
Thomas Falconffa73852017-04-19 13:44:29 -04002856 }
2857
Thomas Falcon032c5e82015-12-21 11:26:06 -06002858 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2859 DMA_BIDIRECTIONAL);
2860 free_pages((unsigned long)scrq->msgs, 2);
2861 kfree(scrq);
2862}
2863
2864static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2865 *adapter)
2866{
2867 struct device *dev = &adapter->vdev->dev;
2868 struct ibmvnic_sub_crq_queue *scrq;
2869 int rc;
2870
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002871 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002872 if (!scrq)
2873 return NULL;
2874
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002875 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002876 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002877 if (!scrq->msgs) {
2878 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2879 goto zero_page_failed;
2880 }
2881
2882 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2883 DMA_BIDIRECTIONAL);
2884 if (dma_mapping_error(dev, scrq->msg_token)) {
2885 dev_warn(dev, "Couldn't map crq queue messages page\n");
2886 goto map_failed;
2887 }
2888
2889 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2890 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2891
2892 if (rc == H_RESOURCE)
2893 rc = ibmvnic_reset_crq(adapter);
2894
2895 if (rc == H_CLOSED) {
2896 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2897 } else if (rc) {
2898 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2899 goto reg_failed;
2900 }
2901
Thomas Falcon032c5e82015-12-21 11:26:06 -06002902 scrq->adapter = adapter;
2903 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002904 spin_lock_init(&scrq->lock);
2905
2906 netdev_dbg(adapter->netdev,
2907 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2908 scrq->crq_num, scrq->hw_irq, scrq->irq);
2909
2910 return scrq;
2911
Thomas Falcon032c5e82015-12-21 11:26:06 -06002912reg_failed:
2913 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2914 DMA_BIDIRECTIONAL);
2915map_failed:
2916 free_pages((unsigned long)scrq->msgs, 2);
2917zero_page_failed:
2918 kfree(scrq);
2919
2920 return NULL;
2921}
2922
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002923static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002924{
2925 int i;
2926
2927 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002928 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002929 if (!adapter->tx_scrq[i])
2930 continue;
2931
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002932 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2933 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002934 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002935 free_irq(adapter->tx_scrq[i]->irq,
2936 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002937 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002938 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002939 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002940
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002941 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2942 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002943 }
2944
Nathan Fontenot9501df32017-03-15 23:38:07 -04002945 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002946 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002947 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002948 }
2949
2950 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002951 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002952 if (!adapter->rx_scrq[i])
2953 continue;
2954
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002955 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2956 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002957 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002958 free_irq(adapter->rx_scrq[i]->irq,
2959 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002960 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002961 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002962 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002963
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002964 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2965 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002966 }
2967
Nathan Fontenot9501df32017-03-15 23:38:07 -04002968 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002969 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002970 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002972}
2973
2974static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2975 struct ibmvnic_sub_crq_queue *scrq)
2976{
2977 struct device *dev = &adapter->vdev->dev;
2978 unsigned long rc;
2979
2980 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2981 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2982 if (rc)
2983 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2984 scrq->hw_irq, rc);
2985 return rc;
2986}
2987
2988static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2989 struct ibmvnic_sub_crq_queue *scrq)
2990{
2991 struct device *dev = &adapter->vdev->dev;
2992 unsigned long rc;
2993
2994 if (scrq->hw_irq > 0x100000000ULL) {
2995 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2996 return 1;
2997 }
2998
Juliet Kim7ed5b312019-09-20 16:11:23 -04002999 if (test_bit(0, &adapter->resetting) &&
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003000 adapter->reset_reason == VNIC_RESET_MOBILITY) {
Juliet Kim284f87d2019-11-20 10:50:03 -05003001 u64 val = (0xff000000) | scrq->hw_irq;
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003002
Juliet Kim284f87d2019-11-20 10:50:03 -05003003 rc = plpar_hcall_norets(H_EOI, val);
Juliet Kim2df5c602019-11-20 10:50:04 -05003004 /* H_EOI would fail with rc = H_FUNCTION when running
3005 * in XIVE mode which is expected, but not an error.
3006 */
3007 if (rc && (rc != H_FUNCTION))
Juliet Kim284f87d2019-11-20 10:50:03 -05003008 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3009 val, rc);
Nathan Fontenot73f9d362018-05-22 11:21:10 -05003010 }
Thomas Falconf23e0642018-04-15 18:53:36 -05003011
Thomas Falcon032c5e82015-12-21 11:26:06 -06003012 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3013 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3014 if (rc)
3015 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3016 scrq->hw_irq, rc);
3017 return rc;
3018}
3019
3020static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3021 struct ibmvnic_sub_crq_queue *scrq)
3022{
3023 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05003024 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 struct ibmvnic_tx_buff *txbuff;
3026 union sub_crq *next;
3027 int index;
3028 int i, j;
3029
3030restart_loop:
3031 while (pending_scrq(adapter, scrq)) {
3032 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06003033 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003034
3035 next = ibmvnic_next_scrq(adapter, scrq);
3036 for (i = 0; i < next->tx_comp.num_comps; i++) {
3037 if (next->tx_comp.rcs[i]) {
3038 dev_err(dev, "tx error %x\n",
3039 next->tx_comp.rcs[i]);
3040 continue;
3041 }
3042 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05003043 if (index & IBMVNIC_TSO_POOL_MASK) {
3044 tx_pool = &adapter->tso_pool[pool];
3045 index &= ~IBMVNIC_TSO_POOL_MASK;
3046 } else {
3047 tx_pool = &adapter->tx_pool[pool];
3048 }
3049
3050 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06003051
3052 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
3053 if (!txbuff->data_dma[j])
3054 continue;
3055
3056 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003057 }
3058
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003059 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003060 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003061 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06003062 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003063
Thomas Falconffc385b2018-02-18 10:08:41 -06003064 num_entries += txbuff->num_entries;
3065
Thomas Falcon06b3e352018-03-16 20:00:28 -05003066 tx_pool->free_map[tx_pool->producer_index] = index;
3067 tx_pool->producer_index =
3068 (tx_pool->producer_index + 1) %
3069 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070 }
3071 /* remove tx_comp scrq*/
3072 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003073
Thomas Falconffc385b2018-02-18 10:08:41 -06003074 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003075 (adapter->req_tx_entries_per_subcrq / 2) &&
3076 __netif_subqueue_stopped(adapter->netdev,
3077 scrq->pool_index)) {
3078 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06003079 netdev_dbg(adapter->netdev, "Started queue %d\n",
3080 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04003081 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082 }
3083
3084 enable_scrq_irq(adapter, scrq);
3085
3086 if (pending_scrq(adapter, scrq)) {
3087 disable_scrq_irq(adapter, scrq);
3088 goto restart_loop;
3089 }
3090
3091 return 0;
3092}
3093
3094static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3095{
3096 struct ibmvnic_sub_crq_queue *scrq = instance;
3097 struct ibmvnic_adapter *adapter = scrq->adapter;
3098
3099 disable_scrq_irq(adapter, scrq);
3100 ibmvnic_complete_tx(adapter, scrq);
3101
3102 return IRQ_HANDLED;
3103}
3104
3105static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3106{
3107 struct ibmvnic_sub_crq_queue *scrq = instance;
3108 struct ibmvnic_adapter *adapter = scrq->adapter;
3109
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06003110 /* When booting a kdump kernel we can hit pending interrupts
3111 * prior to completing driver initialization.
3112 */
3113 if (unlikely(adapter->state != VNIC_OPEN))
3114 return IRQ_NONE;
3115
John Allen3d52b592017-08-02 16:44:14 -05003116 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3117
Thomas Falcon032c5e82015-12-21 11:26:06 -06003118 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3119 disable_scrq_irq(adapter, scrq);
3120 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3121 }
3122
3123 return IRQ_HANDLED;
3124}
3125
Thomas Falconea22d512016-07-06 15:35:17 -05003126static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3127{
3128 struct device *dev = &adapter->vdev->dev;
3129 struct ibmvnic_sub_crq_queue *scrq;
3130 int i = 0, j = 0;
3131 int rc = 0;
3132
3133 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003134 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3135 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003136 scrq = adapter->tx_scrq[i];
3137 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3138
Michael Ellerman99c17902016-09-10 19:59:05 +10003139 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003140 rc = -EINVAL;
3141 dev_err(dev, "Error mapping irq\n");
3142 goto req_tx_irq_failed;
3143 }
3144
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003145 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3146 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003147 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003148 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003149
3150 if (rc) {
3151 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3152 scrq->irq, rc);
3153 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06003154 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05003155 }
3156 }
3157
3158 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05003159 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3160 i);
Thomas Falconea22d512016-07-06 15:35:17 -05003161 scrq = adapter->rx_scrq[i];
3162 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10003163 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05003164 rc = -EINVAL;
3165 dev_err(dev, "Error mapping irq\n");
3166 goto req_rx_irq_failed;
3167 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003168 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3169 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05003170 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03003171 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05003172 if (rc) {
3173 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3174 scrq->irq, rc);
3175 irq_dispose_mapping(scrq->irq);
3176 goto req_rx_irq_failed;
3177 }
3178 }
3179 return rc;
3180
3181req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003182 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003183 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3184 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003185 }
Thomas Falconea22d512016-07-06 15:35:17 -05003186 i = adapter->req_tx_queues;
3187req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003188 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05003189 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
3190 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05003191 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003192 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05003193 return rc;
3194}
3195
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003196static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003197{
3198 struct device *dev = &adapter->vdev->dev;
3199 struct ibmvnic_sub_crq_queue **allqueues;
3200 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201 int total_queues;
3202 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05003203 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003204
Thomas Falcon032c5e82015-12-21 11:26:06 -06003205 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3206
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003207 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003208 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003209 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210
3211 for (i = 0; i < total_queues; i++) {
3212 allqueues[i] = init_sub_crq_queue(adapter);
3213 if (!allqueues[i]) {
3214 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3215 break;
3216 }
3217 registered_queues++;
3218 }
3219
3220 /* Make sure we were able to register the minimum number of queues */
3221 if (registered_queues <
3222 adapter->min_tx_queues + adapter->min_rx_queues) {
3223 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3224 goto tx_failed;
3225 }
3226
3227 /* Distribute the failed allocated queues*/
3228 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3229 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3230 switch (i % 3) {
3231 case 0:
3232 if (adapter->req_rx_queues > adapter->min_rx_queues)
3233 adapter->req_rx_queues--;
3234 else
3235 more++;
3236 break;
3237 case 1:
3238 if (adapter->req_tx_queues > adapter->min_tx_queues)
3239 adapter->req_tx_queues--;
3240 else
3241 more++;
3242 break;
3243 }
3244 }
3245
3246 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003247 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003248 if (!adapter->tx_scrq)
3249 goto tx_failed;
3250
3251 for (i = 0; i < adapter->req_tx_queues; i++) {
3252 adapter->tx_scrq[i] = allqueues[i];
3253 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003254 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003255 }
3256
3257 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003258 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003259 if (!adapter->rx_scrq)
3260 goto rx_failed;
3261
3262 for (i = 0; i < adapter->req_rx_queues; i++) {
3263 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3264 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003265 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003266 }
3267
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003268 kfree(allqueues);
3269 return 0;
3270
3271rx_failed:
3272 kfree(adapter->tx_scrq);
3273 adapter->tx_scrq = NULL;
3274tx_failed:
3275 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003276 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003277 kfree(allqueues);
3278 return -1;
3279}
3280
3281static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3282{
3283 struct device *dev = &adapter->vdev->dev;
3284 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003285 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003286
3287 if (!retry) {
3288 /* Sub-CRQ entries are 32 byte long */
3289 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3290
3291 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3292 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3293 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3294 return;
3295 }
3296
John Allenc26eba02017-10-26 16:23:25 -05003297 if (adapter->desired.mtu)
3298 adapter->req_mtu = adapter->desired.mtu;
3299 else
3300 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003301
John Allenc26eba02017-10-26 16:23:25 -05003302 if (!adapter->desired.tx_entries)
3303 adapter->desired.tx_entries =
3304 adapter->max_tx_entries_per_subcrq;
3305 if (!adapter->desired.rx_entries)
3306 adapter->desired.rx_entries =
3307 adapter->max_rx_add_entries_per_subcrq;
3308
3309 max_entries = IBMVNIC_MAX_LTB_SIZE /
3310 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3311
3312 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3313 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3314 adapter->desired.tx_entries = max_entries;
3315 }
3316
3317 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3318 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3319 adapter->desired.rx_entries = max_entries;
3320 }
3321
3322 if (adapter->desired.tx_entries)
3323 adapter->req_tx_entries_per_subcrq =
3324 adapter->desired.tx_entries;
3325 else
3326 adapter->req_tx_entries_per_subcrq =
3327 adapter->max_tx_entries_per_subcrq;
3328
3329 if (adapter->desired.rx_entries)
3330 adapter->req_rx_add_entries_per_subcrq =
3331 adapter->desired.rx_entries;
3332 else
3333 adapter->req_rx_add_entries_per_subcrq =
3334 adapter->max_rx_add_entries_per_subcrq;
3335
3336 if (adapter->desired.tx_queues)
3337 adapter->req_tx_queues =
3338 adapter->desired.tx_queues;
3339 else
3340 adapter->req_tx_queues =
3341 adapter->opt_tx_comp_sub_queues;
3342
3343 if (adapter->desired.rx_queues)
3344 adapter->req_rx_queues =
3345 adapter->desired.rx_queues;
3346 else
3347 adapter->req_rx_queues =
3348 adapter->opt_rx_comp_queues;
3349
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003350 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003351 }
3352
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 memset(&crq, 0, sizeof(crq));
3354 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3355 crq.request_capability.cmd = REQUEST_CAPABILITY;
3356
3357 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003358 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003359 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003360 ibmvnic_send_crq(adapter, &crq);
3361
3362 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003363 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003364 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003365 ibmvnic_send_crq(adapter, &crq);
3366
3367 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003368 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003369 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003370 ibmvnic_send_crq(adapter, &crq);
3371
3372 crq.request_capability.capability =
3373 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3374 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003375 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003376 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377 ibmvnic_send_crq(adapter, &crq);
3378
3379 crq.request_capability.capability =
3380 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3381 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003382 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003383 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003384 ibmvnic_send_crq(adapter, &crq);
3385
3386 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003387 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003388 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003389 ibmvnic_send_crq(adapter, &crq);
3390
3391 if (adapter->netdev->flags & IFF_PROMISC) {
3392 if (adapter->promisc_supported) {
3393 crq.request_capability.capability =
3394 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003395 crq.request_capability.number = cpu_to_be64(1);
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 } else {
3400 crq.request_capability.capability =
3401 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003402 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003403 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003404 ibmvnic_send_crq(adapter, &crq);
3405 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003406}
3407
3408static int pending_scrq(struct ibmvnic_adapter *adapter,
3409 struct ibmvnic_sub_crq_queue *scrq)
3410{
3411 union sub_crq *entry = &scrq->msgs[scrq->cur];
3412
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003413 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003414 return 1;
3415 else
3416 return 0;
3417}
3418
3419static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3420 struct ibmvnic_sub_crq_queue *scrq)
3421{
3422 union sub_crq *entry;
3423 unsigned long flags;
3424
3425 spin_lock_irqsave(&scrq->lock, flags);
3426 entry = &scrq->msgs[scrq->cur];
3427 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3428 if (++scrq->cur == scrq->size)
3429 scrq->cur = 0;
3430 } else {
3431 entry = NULL;
3432 }
3433 spin_unlock_irqrestore(&scrq->lock, flags);
3434
3435 return entry;
3436}
3437
3438static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3439{
3440 struct ibmvnic_crq_queue *queue = &adapter->crq;
3441 union ibmvnic_crq *crq;
3442
3443 crq = &queue->msgs[queue->cur];
3444 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3445 if (++queue->cur == queue->size)
3446 queue->cur = 0;
3447 } else {
3448 crq = NULL;
3449 }
3450
3451 return crq;
3452}
3453
Thomas Falcon2d14d372018-07-13 12:03:32 -05003454static void print_subcrq_error(struct device *dev, int rc, const char *func)
3455{
3456 switch (rc) {
3457 case H_PARAMETER:
3458 dev_warn_ratelimited(dev,
3459 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3460 func, rc);
3461 break;
3462 case H_CLOSED:
3463 dev_warn_ratelimited(dev,
3464 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3465 func, rc);
3466 break;
3467 default:
3468 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3469 break;
3470 }
3471}
3472
Thomas Falcon032c5e82015-12-21 11:26:06 -06003473static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3474 union sub_crq *sub_crq)
3475{
3476 unsigned int ua = adapter->vdev->unit_address;
3477 struct device *dev = &adapter->vdev->dev;
3478 u64 *u64_crq = (u64 *)sub_crq;
3479 int rc;
3480
3481 netdev_dbg(adapter->netdev,
3482 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3483 (unsigned long int)cpu_to_be64(remote_handle),
3484 (unsigned long int)cpu_to_be64(u64_crq[0]),
3485 (unsigned long int)cpu_to_be64(u64_crq[1]),
3486 (unsigned long int)cpu_to_be64(u64_crq[2]),
3487 (unsigned long int)cpu_to_be64(u64_crq[3]));
3488
3489 /* Make sure the hypervisor sees the complete request */
3490 mb();
3491
3492 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3493 cpu_to_be64(remote_handle),
3494 cpu_to_be64(u64_crq[0]),
3495 cpu_to_be64(u64_crq[1]),
3496 cpu_to_be64(u64_crq[2]),
3497 cpu_to_be64(u64_crq[3]));
3498
Thomas Falcon2d14d372018-07-13 12:03:32 -05003499 if (rc)
3500 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003501
3502 return rc;
3503}
3504
Thomas Falconad7775d2016-04-01 17:20:34 -05003505static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3506 u64 remote_handle, u64 ioba, u64 num_entries)
3507{
3508 unsigned int ua = adapter->vdev->unit_address;
3509 struct device *dev = &adapter->vdev->dev;
3510 int rc;
3511
3512 /* Make sure the hypervisor sees the complete request */
3513 mb();
3514 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3515 cpu_to_be64(remote_handle),
3516 ioba, num_entries);
3517
Thomas Falcon2d14d372018-07-13 12:03:32 -05003518 if (rc)
3519 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003520
3521 return rc;
3522}
3523
Thomas Falcon032c5e82015-12-21 11:26:06 -06003524static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3525 union ibmvnic_crq *crq)
3526{
3527 unsigned int ua = adapter->vdev->unit_address;
3528 struct device *dev = &adapter->vdev->dev;
3529 u64 *u64_crq = (u64 *)crq;
3530 int rc;
3531
3532 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3533 (unsigned long int)cpu_to_be64(u64_crq[0]),
3534 (unsigned long int)cpu_to_be64(u64_crq[1]));
3535
Thomas Falcon51536982018-05-23 13:37:56 -05003536 if (!adapter->crq.active &&
3537 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3538 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3539 return -EINVAL;
3540 }
3541
Thomas Falcon032c5e82015-12-21 11:26:06 -06003542 /* Make sure the hypervisor sees the complete request */
3543 mb();
3544
3545 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3546 cpu_to_be64(u64_crq[0]),
3547 cpu_to_be64(u64_crq[1]));
3548
3549 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003550 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 dev_warn(dev, "CRQ Queue closed\n");
Juliet Kim7ed5b312019-09-20 16:11:23 -04003552 if (test_bit(0, &adapter->resetting))
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003553 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3554 }
3555
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 dev_warn(dev, "Send error (rc=%d)\n", rc);
3557 }
3558
3559 return rc;
3560}
3561
3562static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3563{
3564 union ibmvnic_crq crq;
3565
3566 memset(&crq, 0, sizeof(crq));
3567 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3568 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3569 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3570
3571 return ibmvnic_send_crq(adapter, &crq);
3572}
3573
Thomas Falcon032c5e82015-12-21 11:26:06 -06003574static int send_version_xchg(struct ibmvnic_adapter *adapter)
3575{
3576 union ibmvnic_crq crq;
3577
3578 memset(&crq, 0, sizeof(crq));
3579 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3580 crq.version_exchange.cmd = VERSION_EXCHANGE;
3581 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3582
3583 return ibmvnic_send_crq(adapter, &crq);
3584}
3585
Nathan Fontenot37798d02017-11-08 11:23:56 -06003586struct vnic_login_client_data {
3587 u8 type;
3588 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003589 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003590} __packed;
3591
3592static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3593{
3594 int len;
3595
3596 /* Calculate the amount of buffer space needed for the
3597 * vnic client data in the login buffer. There are four entries,
3598 * OS name, LPAR name, device name, and a null last entry.
3599 */
3600 len = 4 * sizeof(struct vnic_login_client_data);
3601 len += 6; /* "Linux" plus NULL */
3602 len += strlen(utsname()->nodename) + 1;
3603 len += strlen(adapter->netdev->name) + 1;
3604
3605 return len;
3606}
3607
3608static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3609 struct vnic_login_client_data *vlcd)
3610{
3611 const char *os_name = "Linux";
3612 int len;
3613
3614 /* Type 1 - LPAR OS */
3615 vlcd->type = 1;
3616 len = strlen(os_name) + 1;
3617 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003618 strncpy(vlcd->name, os_name, len);
3619 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003620
3621 /* Type 2 - LPAR name */
3622 vlcd->type = 2;
3623 len = strlen(utsname()->nodename) + 1;
3624 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003625 strncpy(vlcd->name, utsname()->nodename, len);
3626 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003627
3628 /* Type 3 - device name */
3629 vlcd->type = 3;
3630 len = strlen(adapter->netdev->name) + 1;
3631 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003632 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003633}
3634
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003635static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636{
3637 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3638 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 struct device *dev = &adapter->vdev->dev;
3640 dma_addr_t rsp_buffer_token;
3641 dma_addr_t buffer_token;
3642 size_t rsp_buffer_size;
3643 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003644 size_t buffer_size;
3645 __be64 *tx_list_p;
3646 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003647 int client_data_len;
3648 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003649 int i;
3650
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003651 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3652 netdev_err(adapter->netdev,
3653 "RX or TX queues are not allocated, device login failed\n");
3654 return -1;
3655 }
3656
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003657 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003658 client_data_len = vnic_client_data_len(adapter);
3659
Thomas Falcon032c5e82015-12-21 11:26:06 -06003660 buffer_size =
3661 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003662 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3663 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003664
Nathan Fontenot37798d02017-11-08 11:23:56 -06003665 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003666 if (!login_buffer)
3667 goto buf_alloc_failed;
3668
3669 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3670 DMA_TO_DEVICE);
3671 if (dma_mapping_error(dev, buffer_token)) {
3672 dev_err(dev, "Couldn't map login buffer\n");
3673 goto buf_map_failed;
3674 }
3675
John Allen498cd8e2016-04-06 11:49:55 -05003676 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3677 sizeof(u64) * adapter->req_tx_queues +
3678 sizeof(u64) * adapter->req_rx_queues +
3679 sizeof(u64) * adapter->req_rx_queues +
3680 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003681
3682 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3683 if (!login_rsp_buffer)
3684 goto buf_rsp_alloc_failed;
3685
3686 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3687 rsp_buffer_size, DMA_FROM_DEVICE);
3688 if (dma_mapping_error(dev, rsp_buffer_token)) {
3689 dev_err(dev, "Couldn't map login rsp buffer\n");
3690 goto buf_rsp_map_failed;
3691 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003692
Thomas Falcon032c5e82015-12-21 11:26:06 -06003693 adapter->login_buf = login_buffer;
3694 adapter->login_buf_token = buffer_token;
3695 adapter->login_buf_sz = buffer_size;
3696 adapter->login_rsp_buf = login_rsp_buffer;
3697 adapter->login_rsp_buf_token = rsp_buffer_token;
3698 adapter->login_rsp_buf_sz = rsp_buffer_size;
3699
3700 login_buffer->len = cpu_to_be32(buffer_size);
3701 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3702 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3703 login_buffer->off_txcomp_subcrqs =
3704 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3705 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3706 login_buffer->off_rxcomp_subcrqs =
3707 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3708 sizeof(u64) * adapter->req_tx_queues);
3709 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3710 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3711
3712 tx_list_p = (__be64 *)((char *)login_buffer +
3713 sizeof(struct ibmvnic_login_buffer));
3714 rx_list_p = (__be64 *)((char *)login_buffer +
3715 sizeof(struct ibmvnic_login_buffer) +
3716 sizeof(u64) * adapter->req_tx_queues);
3717
3718 for (i = 0; i < adapter->req_tx_queues; i++) {
3719 if (adapter->tx_scrq[i]) {
3720 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3721 crq_num);
3722 }
3723 }
3724
3725 for (i = 0; i < adapter->req_rx_queues; i++) {
3726 if (adapter->rx_scrq[i]) {
3727 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3728 crq_num);
3729 }
3730 }
3731
Nathan Fontenot37798d02017-11-08 11:23:56 -06003732 /* Insert vNIC login client data */
3733 vlcd = (struct vnic_login_client_data *)
3734 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3735 login_buffer->client_data_offset =
3736 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3737 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3738
3739 vnic_add_client_data(adapter, vlcd);
3740
Thomas Falcon032c5e82015-12-21 11:26:06 -06003741 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3742 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3743 netdev_dbg(adapter->netdev, "%016lx\n",
3744 ((unsigned long int *)(adapter->login_buf))[i]);
3745 }
3746
3747 memset(&crq, 0, sizeof(crq));
3748 crq.login.first = IBMVNIC_CRQ_CMD;
3749 crq.login.cmd = LOGIN;
3750 crq.login.ioba = cpu_to_be32(buffer_token);
3751 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003752 ibmvnic_send_crq(adapter, &crq);
3753
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003754 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003755
Thomas Falcon032c5e82015-12-21 11:26:06 -06003756buf_rsp_map_failed:
3757 kfree(login_rsp_buffer);
3758buf_rsp_alloc_failed:
3759 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3760buf_map_failed:
3761 kfree(login_buffer);
3762buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003763 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003764}
3765
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003766static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3767 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003768{
3769 union ibmvnic_crq crq;
3770
3771 memset(&crq, 0, sizeof(crq));
3772 crq.request_map.first = IBMVNIC_CRQ_CMD;
3773 crq.request_map.cmd = REQUEST_MAP;
3774 crq.request_map.map_id = map_id;
3775 crq.request_map.ioba = cpu_to_be32(addr);
3776 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003777 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003778}
3779
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003780static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003781{
3782 union ibmvnic_crq crq;
3783
3784 memset(&crq, 0, sizeof(crq));
3785 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3786 crq.request_unmap.cmd = REQUEST_UNMAP;
3787 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003788 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003789}
3790
3791static void send_map_query(struct ibmvnic_adapter *adapter)
3792{
3793 union ibmvnic_crq crq;
3794
3795 memset(&crq, 0, sizeof(crq));
3796 crq.query_map.first = IBMVNIC_CRQ_CMD;
3797 crq.query_map.cmd = QUERY_MAP;
3798 ibmvnic_send_crq(adapter, &crq);
3799}
3800
3801/* Send a series of CRQs requesting various capabilities of the VNIC server */
3802static void send_cap_queries(struct ibmvnic_adapter *adapter)
3803{
3804 union ibmvnic_crq crq;
3805
Thomas Falcon901e0402017-02-15 12:17:59 -06003806 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003807 memset(&crq, 0, sizeof(crq));
3808 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3809 crq.query_capability.cmd = QUERY_CAPABILITY;
3810
3811 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003812 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813 ibmvnic_send_crq(adapter, &crq);
3814
3815 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003816 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003817 ibmvnic_send_crq(adapter, &crq);
3818
3819 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003820 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003821 ibmvnic_send_crq(adapter, &crq);
3822
3823 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003824 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003825 ibmvnic_send_crq(adapter, &crq);
3826
3827 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003828 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003829 ibmvnic_send_crq(adapter, &crq);
3830
3831 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003832 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003833 ibmvnic_send_crq(adapter, &crq);
3834
3835 crq.query_capability.capability =
3836 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003837 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003838 ibmvnic_send_crq(adapter, &crq);
3839
3840 crq.query_capability.capability =
3841 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003842 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843 ibmvnic_send_crq(adapter, &crq);
3844
3845 crq.query_capability.capability =
3846 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003847 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003848 ibmvnic_send_crq(adapter, &crq);
3849
3850 crq.query_capability.capability =
3851 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
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(TCP_IP_OFFLOAD);
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(PROMISC_SUPPORTED);
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(MIN_MTU);
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 = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003868 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003869 ibmvnic_send_crq(adapter, &crq);
3870
3871 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003872 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003873 ibmvnic_send_crq(adapter, &crq);
3874
3875 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003876 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003877 ibmvnic_send_crq(adapter, &crq);
3878
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003879 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3880 atomic_inc(&adapter->running_cap_crqs);
3881 ibmvnic_send_crq(adapter, &crq);
3882
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
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(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
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(OPT_RX_COMP_QUEUES);
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 =
3900 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003901 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003902 ibmvnic_send_crq(adapter, &crq);
3903
3904 crq.query_capability.capability =
3905 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003906 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 ibmvnic_send_crq(adapter, &crq);
3908
3909 crq.query_capability.capability =
3910 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003911 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003912 ibmvnic_send_crq(adapter, &crq);
3913
3914 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003915 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003916 ibmvnic_send_crq(adapter, &crq);
3917}
3918
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003919static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3920 struct ibmvnic_adapter *adapter)
3921{
3922 struct device *dev = &adapter->vdev->dev;
3923
3924 if (crq->get_vpd_size_rsp.rc.code) {
3925 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3926 crq->get_vpd_size_rsp.rc.code);
3927 complete(&adapter->fw_done);
3928 return;
3929 }
3930
3931 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3932 complete(&adapter->fw_done);
3933}
3934
3935static void handle_vpd_rsp(union ibmvnic_crq *crq,
3936 struct ibmvnic_adapter *adapter)
3937{
3938 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003939 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003940 u8 fw_level_len = 0;
3941
3942 memset(adapter->fw_version, 0, 32);
3943
3944 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3945 DMA_FROM_DEVICE);
3946
3947 if (crq->get_vpd_rsp.rc.code) {
3948 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3949 crq->get_vpd_rsp.rc.code);
3950 goto complete;
3951 }
3952
3953 /* get the position of the firmware version info
3954 * located after the ASCII 'RM' substring in the buffer
3955 */
3956 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3957 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003958 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 -02003959 goto complete;
3960 }
3961
3962 /* get length of firmware level ASCII substring */
3963 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3964 fw_level_len = *(substr + 2);
3965 } else {
3966 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3967 goto complete;
3968 }
3969
3970 /* copy firmware version string from vpd into adapter */
3971 if ((substr + 3 + fw_level_len) <
3972 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003973 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003974 } else {
3975 dev_info(dev, "FW substr extrapolated VPD buff\n");
3976 }
3977
3978complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003979 if (adapter->fw_version[0] == '\0')
3980 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003981 complete(&adapter->fw_done);
3982}
3983
Thomas Falcon032c5e82015-12-21 11:26:06 -06003984static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3985{
3986 struct device *dev = &adapter->vdev->dev;
3987 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcondde746a2019-04-10 11:07:00 -05003988 netdev_features_t old_hw_features = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003989 union ibmvnic_crq crq;
3990 int i;
3991
3992 dma_unmap_single(dev, adapter->ip_offload_tok,
3993 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3994
3995 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3996 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3997 netdev_dbg(adapter->netdev, "%016lx\n",
3998 ((unsigned long int *)(buf))[i]);
3999
4000 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4001 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4002 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4003 buf->tcp_ipv4_chksum);
4004 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4005 buf->tcp_ipv6_chksum);
4006 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4007 buf->udp_ipv4_chksum);
4008 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4009 buf->udp_ipv6_chksum);
4010 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4011 buf->large_tx_ipv4);
4012 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4013 buf->large_tx_ipv6);
4014 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4015 buf->large_rx_ipv4);
4016 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4017 buf->large_rx_ipv6);
4018 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4019 buf->max_ipv4_header_size);
4020 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4021 buf->max_ipv6_header_size);
4022 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4023 buf->max_tcp_header_size);
4024 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4025 buf->max_udp_header_size);
4026 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4027 buf->max_large_tx_size);
4028 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4029 buf->max_large_rx_size);
4030 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4031 buf->ipv6_extension_header);
4032 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4033 buf->tcp_pseudosum_req);
4034 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4035 buf->num_ipv6_ext_headers);
4036 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4037 buf->off_ipv6_ext_headers);
4038
4039 adapter->ip_offload_ctrl_tok =
4040 dma_map_single(dev, &adapter->ip_offload_ctrl,
4041 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
4042
4043 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4044 dev_err(dev, "Couldn't map ip offload control buffer\n");
4045 return;
4046 }
4047
Thomas Falconf6897942018-01-18 19:05:01 -06004048 adapter->ip_offload_ctrl.len =
4049 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06004051 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
4052 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004053 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4054 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
4055 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4056 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05004057 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
4058 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004059
Thomas Falconfdb06102017-10-17 12:36:55 -05004060 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004061 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
4062 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
4063
Thomas Falcondde746a2019-04-10 11:07:00 -05004064 if (adapter->state != VNIC_PROBING) {
4065 old_hw_features = adapter->netdev->hw_features;
4066 adapter->netdev->hw_features = 0;
4067 }
4068
Thomas Falconb66b7bd2019-04-10 11:06:59 -05004069 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004070
4071 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05004072 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004073
4074 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05004075 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004076
Thomas Falcon9be02cd2016-04-01 17:20:35 -05004077 if ((adapter->netdev->features &
4078 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Thomas Falcondde746a2019-04-10 11:07:00 -05004079 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
Thomas Falcon9be02cd2016-04-01 17:20:35 -05004080
Thomas Falconfdb06102017-10-17 12:36:55 -05004081 if (buf->large_tx_ipv4)
Thomas Falcondde746a2019-04-10 11:07:00 -05004082 adapter->netdev->hw_features |= NETIF_F_TSO;
Thomas Falconfdb06102017-10-17 12:36:55 -05004083 if (buf->large_tx_ipv6)
Thomas Falcondde746a2019-04-10 11:07:00 -05004084 adapter->netdev->hw_features |= NETIF_F_TSO6;
Thomas Falconfdb06102017-10-17 12:36:55 -05004085
Thomas Falcondde746a2019-04-10 11:07:00 -05004086 if (adapter->state == VNIC_PROBING) {
4087 adapter->netdev->features |= adapter->netdev->hw_features;
4088 } else if (old_hw_features != adapter->netdev->hw_features) {
4089 netdev_features_t tmp = 0;
4090
4091 /* disable features no longer supported */
4092 adapter->netdev->features &= adapter->netdev->hw_features;
4093 /* turn on features now supported if previously enabled */
4094 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4095 adapter->netdev->hw_features;
4096 adapter->netdev->features |=
4097 tmp & adapter->netdev->wanted_features;
4098 }
Thomas Falconaa0bf852017-10-17 12:36:56 -05004099
Thomas Falcon032c5e82015-12-21 11:26:06 -06004100 memset(&crq, 0, sizeof(crq));
4101 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4102 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4103 crq.control_ip_offload.len =
4104 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4105 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4106 ibmvnic_send_crq(adapter, &crq);
4107}
4108
Thomas Falconc9008d32018-08-06 21:39:59 -05004109static const char *ibmvnic_fw_err_cause(u16 cause)
4110{
4111 switch (cause) {
4112 case ADAPTER_PROBLEM:
4113 return "adapter problem";
4114 case BUS_PROBLEM:
4115 return "bus problem";
4116 case FW_PROBLEM:
4117 return "firmware problem";
4118 case DD_PROBLEM:
4119 return "device driver problem";
4120 case EEH_RECOVERY:
4121 return "EEH recovery";
4122 case FW_UPDATED:
4123 return "firmware updated";
4124 case LOW_MEMORY:
4125 return "low Memory";
4126 default:
4127 return "unknown";
4128 }
4129}
4130
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004131static void handle_error_indication(union ibmvnic_crq *crq,
4132 struct ibmvnic_adapter *adapter)
4133{
4134 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05004135 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004136
Thomas Falconc9008d32018-08-06 21:39:59 -05004137 cause = be16_to_cpu(crq->error_indication.error_cause);
4138
4139 dev_warn_ratelimited(dev,
4140 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4141 crq->error_indication.flags
4142 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4143 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04004144
Nathan Fontenoted651a12017-05-03 14:04:38 -04004145 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4146 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04004147 else
4148 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149}
4150
Thomas Falconf8136142018-01-29 13:45:05 -06004151static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4152 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004153{
4154 struct net_device *netdev = adapter->netdev;
4155 struct device *dev = &adapter->vdev->dev;
4156 long rc;
4157
4158 rc = crq->change_mac_addr_rsp.rc.code;
4159 if (rc) {
4160 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06004161 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004162 }
Thomas Falcon62740e92019-05-09 23:13:43 -05004163 ether_addr_copy(netdev->dev_addr,
4164 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06004165out:
4166 complete(&adapter->fw_done);
4167 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168}
4169
4170static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4171 struct ibmvnic_adapter *adapter)
4172{
4173 struct device *dev = &adapter->vdev->dev;
4174 u64 *req_value;
4175 char *name;
4176
Thomas Falcon901e0402017-02-15 12:17:59 -06004177 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004178 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4179 case REQ_TX_QUEUES:
4180 req_value = &adapter->req_tx_queues;
4181 name = "tx";
4182 break;
4183 case REQ_RX_QUEUES:
4184 req_value = &adapter->req_rx_queues;
4185 name = "rx";
4186 break;
4187 case REQ_RX_ADD_QUEUES:
4188 req_value = &adapter->req_rx_add_queues;
4189 name = "rx_add";
4190 break;
4191 case REQ_TX_ENTRIES_PER_SUBCRQ:
4192 req_value = &adapter->req_tx_entries_per_subcrq;
4193 name = "tx_entries_per_subcrq";
4194 break;
4195 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4196 req_value = &adapter->req_rx_add_entries_per_subcrq;
4197 name = "rx_add_entries_per_subcrq";
4198 break;
4199 case REQ_MTU:
4200 req_value = &adapter->req_mtu;
4201 name = "mtu";
4202 break;
4203 case PROMISC_REQUESTED:
4204 req_value = &adapter->promisc;
4205 name = "promisc";
4206 break;
4207 default:
4208 dev_err(dev, "Got invalid cap request rsp %d\n",
4209 crq->request_capability.capability);
4210 return;
4211 }
4212
4213 switch (crq->request_capability_rsp.rc.code) {
4214 case SUCCESS:
4215 break;
4216 case PARTIALSUCCESS:
4217 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4218 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06004219 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06004220 number), name);
John Allene7913802018-01-18 16:27:12 -06004221
4222 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4223 REQ_MTU) {
4224 pr_err("mtu of %llu is not supported. Reverting.\n",
4225 *req_value);
4226 *req_value = adapter->fallback.mtu;
4227 } else {
4228 *req_value =
4229 be64_to_cpu(crq->request_capability_rsp.number);
4230 }
4231
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004232 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004233 return;
4234 default:
4235 dev_err(dev, "Error %d in request cap rsp\n",
4236 crq->request_capability_rsp.rc.code);
4237 return;
4238 }
4239
4240 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004241 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004242 union ibmvnic_crq newcrq;
4243 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4244 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4245 &adapter->ip_offload_buf;
4246
Thomas Falcon249168a2017-02-15 12:18:00 -06004247 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004248 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4249 buf_sz,
4250 DMA_FROM_DEVICE);
4251
4252 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4253 if (!firmware_has_feature(FW_FEATURE_CMO))
4254 dev_err(dev, "Couldn't map offload buffer\n");
4255 return;
4256 }
4257
4258 memset(&newcrq, 0, sizeof(newcrq));
4259 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4260 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4261 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4262 newcrq.query_ip_offload.ioba =
4263 cpu_to_be32(adapter->ip_offload_tok);
4264
4265 ibmvnic_send_crq(adapter, &newcrq);
4266 }
4267}
4268
4269static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4270 struct ibmvnic_adapter *adapter)
4271{
4272 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004273 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004274 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4275 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004276 int i;
4277
4278 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004279 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004280 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004281 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004282
John Allen498cd8e2016-04-06 11:49:55 -05004283 /* If the number of queues requested can't be allocated by the
4284 * server, the login response will return with code 1. We will need
4285 * to resend the login buffer with fewer queues requested.
4286 */
4287 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004288 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004289 complete(&adapter->init_done);
4290 return 0;
4291 }
4292
John Allenc26eba02017-10-26 16:23:25 -05004293 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4294
Thomas Falcon032c5e82015-12-21 11:26:06 -06004295 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4296 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4297 netdev_dbg(adapter->netdev, "%016lx\n",
4298 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4299 }
4300
4301 /* Sanity checks */
4302 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4303 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4304 adapter->req_rx_add_queues !=
4305 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4306 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4307 ibmvnic_remove(adapter->vdev);
4308 return -EIO;
4309 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06004310 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004311 complete(&adapter->init_done);
4312
Thomas Falcon032c5e82015-12-21 11:26:06 -06004313 return 0;
4314}
4315
Thomas Falcon032c5e82015-12-21 11:26:06 -06004316static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4317 struct ibmvnic_adapter *adapter)
4318{
4319 struct device *dev = &adapter->vdev->dev;
4320 long rc;
4321
4322 rc = crq->request_unmap_rsp.rc.code;
4323 if (rc)
4324 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4325}
4326
4327static void handle_query_map_rsp(union ibmvnic_crq *crq,
4328 struct ibmvnic_adapter *adapter)
4329{
4330 struct net_device *netdev = adapter->netdev;
4331 struct device *dev = &adapter->vdev->dev;
4332 long rc;
4333
4334 rc = crq->query_map_rsp.rc.code;
4335 if (rc) {
4336 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4337 return;
4338 }
4339 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4340 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4341 crq->query_map_rsp.free_pages);
4342}
4343
4344static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4345 struct ibmvnic_adapter *adapter)
4346{
4347 struct net_device *netdev = adapter->netdev;
4348 struct device *dev = &adapter->vdev->dev;
4349 long rc;
4350
Thomas Falcon901e0402017-02-15 12:17:59 -06004351 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004352 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004353 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004354 rc = crq->query_capability.rc.code;
4355 if (rc) {
4356 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4357 goto out;
4358 }
4359
4360 switch (be16_to_cpu(crq->query_capability.capability)) {
4361 case MIN_TX_QUEUES:
4362 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004363 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004364 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4365 adapter->min_tx_queues);
4366 break;
4367 case MIN_RX_QUEUES:
4368 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004369 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004370 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4371 adapter->min_rx_queues);
4372 break;
4373 case MIN_RX_ADD_QUEUES:
4374 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004375 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004376 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4377 adapter->min_rx_add_queues);
4378 break;
4379 case MAX_TX_QUEUES:
4380 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004381 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004382 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4383 adapter->max_tx_queues);
4384 break;
4385 case MAX_RX_QUEUES:
4386 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004387 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004388 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4389 adapter->max_rx_queues);
4390 break;
4391 case MAX_RX_ADD_QUEUES:
4392 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004393 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004394 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4395 adapter->max_rx_add_queues);
4396 break;
4397 case MIN_TX_ENTRIES_PER_SUBCRQ:
4398 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004399 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004400 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4401 adapter->min_tx_entries_per_subcrq);
4402 break;
4403 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4404 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004405 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004406 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4407 adapter->min_rx_add_entries_per_subcrq);
4408 break;
4409 case MAX_TX_ENTRIES_PER_SUBCRQ:
4410 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004411 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004412 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4413 adapter->max_tx_entries_per_subcrq);
4414 break;
4415 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4416 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004417 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004418 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4419 adapter->max_rx_add_entries_per_subcrq);
4420 break;
4421 case TCP_IP_OFFLOAD:
4422 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004423 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004424 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4425 adapter->tcp_ip_offload);
4426 break;
4427 case PROMISC_SUPPORTED:
4428 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004429 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004430 netdev_dbg(netdev, "promisc_supported = %lld\n",
4431 adapter->promisc_supported);
4432 break;
4433 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004434 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004435 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004436 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4437 break;
4438 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004439 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004440 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004441 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4442 break;
4443 case MAX_MULTICAST_FILTERS:
4444 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004445 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004446 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4447 adapter->max_multicast_filters);
4448 break;
4449 case VLAN_HEADER_INSERTION:
4450 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004451 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004452 if (adapter->vlan_header_insertion)
4453 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4454 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4455 adapter->vlan_header_insertion);
4456 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004457 case RX_VLAN_HEADER_INSERTION:
4458 adapter->rx_vlan_header_insertion =
4459 be64_to_cpu(crq->query_capability.number);
4460 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4461 adapter->rx_vlan_header_insertion);
4462 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004463 case MAX_TX_SG_ENTRIES:
4464 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004465 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004466 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4467 adapter->max_tx_sg_entries);
4468 break;
4469 case RX_SG_SUPPORTED:
4470 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004471 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004472 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4473 adapter->rx_sg_supported);
4474 break;
4475 case OPT_TX_COMP_SUB_QUEUES:
4476 adapter->opt_tx_comp_sub_queues =
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, "opt_tx_comp_sub_queues = %lld\n",
4479 adapter->opt_tx_comp_sub_queues);
4480 break;
4481 case OPT_RX_COMP_QUEUES:
4482 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004483 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004484 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4485 adapter->opt_rx_comp_queues);
4486 break;
4487 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4488 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004489 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004490 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4491 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4492 break;
4493 case OPT_TX_ENTRIES_PER_SUBCRQ:
4494 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004495 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004496 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4497 adapter->opt_tx_entries_per_subcrq);
4498 break;
4499 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4500 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004501 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004502 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4503 adapter->opt_rxba_entries_per_subcrq);
4504 break;
4505 case TX_RX_DESC_REQ:
4506 adapter->tx_rx_desc_req = crq->query_capability.number;
4507 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4508 adapter->tx_rx_desc_req);
4509 break;
4510
4511 default:
4512 netdev_err(netdev, "Got invalid cap rsp %d\n",
4513 crq->query_capability.capability);
4514 }
4515
4516out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004517 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4518 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004519 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004520 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004521}
4522
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004523static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4524{
4525 union ibmvnic_crq crq;
4526 int rc;
4527
4528 memset(&crq, 0, sizeof(crq));
4529 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4530 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004531
4532 mutex_lock(&adapter->fw_lock);
4533 adapter->fw_done_rc = 0;
Thomas Falcon070eca92019-11-25 17:12:53 -06004534 reinit_completion(&adapter->fw_done);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004535
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004536 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004537 if (rc) {
4538 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004539 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004540 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004541
4542 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
Thomas Falconff25dcb2019-11-25 17:12:56 -06004543 if (rc) {
4544 mutex_unlock(&adapter->fw_lock);
Thomas Falcon476d96c2019-11-25 17:12:55 -06004545 return rc;
Thomas Falconff25dcb2019-11-25 17:12:56 -06004546 }
Thomas Falcon476d96c2019-11-25 17:12:55 -06004547
Thomas Falconff25dcb2019-11-25 17:12:56 -06004548 mutex_unlock(&adapter->fw_lock);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004549 return adapter->fw_done_rc ? -EIO : 0;
4550}
4551
4552static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4553 struct ibmvnic_adapter *adapter)
4554{
4555 struct net_device *netdev = adapter->netdev;
4556 int rc;
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004557 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004558
4559 rc = crq->query_phys_parms_rsp.rc.code;
4560 if (rc) {
4561 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4562 return rc;
4563 }
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004564 switch (rspeed) {
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004565 case IBMVNIC_10MBPS:
4566 adapter->speed = SPEED_10;
4567 break;
4568 case IBMVNIC_100MBPS:
4569 adapter->speed = SPEED_100;
4570 break;
4571 case IBMVNIC_1GBPS:
4572 adapter->speed = SPEED_1000;
4573 break;
4574 case IBMVNIC_10GBP:
4575 adapter->speed = SPEED_10000;
4576 break;
4577 case IBMVNIC_25GBPS:
4578 adapter->speed = SPEED_25000;
4579 break;
4580 case IBMVNIC_40GBPS:
4581 adapter->speed = SPEED_40000;
4582 break;
4583 case IBMVNIC_50GBPS:
4584 adapter->speed = SPEED_50000;
4585 break;
4586 case IBMVNIC_100GBPS:
4587 adapter->speed = SPEED_100000;
4588 break;
4589 default:
Murilo Fossa Vicentinidd0f9d82019-09-16 11:50:37 -03004590 if (netif_carrier_ok(netdev))
4591 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004592 adapter->speed = SPEED_UNKNOWN;
4593 }
4594 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4595 adapter->duplex = DUPLEX_FULL;
4596 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4597 adapter->duplex = DUPLEX_HALF;
4598 else
4599 adapter->duplex = DUPLEX_UNKNOWN;
4600
4601 return rc;
4602}
4603
Thomas Falcon032c5e82015-12-21 11:26:06 -06004604static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4605 struct ibmvnic_adapter *adapter)
4606{
4607 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4608 struct net_device *netdev = adapter->netdev;
4609 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004610 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004611 long rc;
4612
4613 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004614 (unsigned long int)cpu_to_be64(u64_crq[0]),
4615 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004616 switch (gen_crq->first) {
4617 case IBMVNIC_CRQ_INIT_RSP:
4618 switch (gen_crq->cmd) {
4619 case IBMVNIC_CRQ_INIT:
4620 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004621 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004622 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004623 if (!completion_done(&adapter->init_done)) {
4624 complete(&adapter->init_done);
4625 adapter->init_done_rc = -EIO;
4626 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004627 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004628 break;
4629 case IBMVNIC_CRQ_INIT_COMPLETE:
4630 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004631 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004632 send_version_xchg(adapter);
4633 break;
4634 default:
4635 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4636 }
4637 return;
4638 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004639 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004640 adapter->crq.active = false;
Thomas Falcon2147e3d2019-11-25 17:12:54 -06004641 /* terminate any thread waiting for a response
4642 * from the device
4643 */
4644 if (!completion_done(&adapter->fw_done)) {
4645 adapter->fw_done_rc = -EIO;
4646 complete(&adapter->fw_done);
4647 }
4648 if (!completion_done(&adapter->stats_done))
4649 complete(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04004650 if (test_bit(0, &adapter->resetting))
Thomas Falcon2770a792018-05-23 13:38:02 -05004651 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004652 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004653 dev_info(dev, "Migrated, re-enabling adapter\n");
4654 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004655 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4656 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004657 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004658 } else {
4659 /* The adapter lost the connection */
4660 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4661 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004662 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004663 }
4664 return;
4665 case IBMVNIC_CRQ_CMD_RSP:
4666 break;
4667 default:
4668 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4669 gen_crq->first);
4670 return;
4671 }
4672
4673 switch (gen_crq->cmd) {
4674 case VERSION_EXCHANGE_RSP:
4675 rc = crq->version_exchange_rsp.rc.code;
4676 if (rc) {
4677 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4678 break;
4679 }
4680 dev_info(dev, "Partner protocol version is %d\n",
4681 crq->version_exchange_rsp.version);
4682 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4683 ibmvnic_version)
4684 ibmvnic_version =
4685 be16_to_cpu(crq->version_exchange_rsp.version);
4686 send_cap_queries(adapter);
4687 break;
4688 case QUERY_CAPABILITY_RSP:
4689 handle_query_cap_rsp(crq, adapter);
4690 break;
4691 case QUERY_MAP_RSP:
4692 handle_query_map_rsp(crq, adapter);
4693 break;
4694 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004695 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4696 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004697 break;
4698 case REQUEST_UNMAP_RSP:
4699 handle_request_unmap_rsp(crq, adapter);
4700 break;
4701 case REQUEST_CAPABILITY_RSP:
4702 handle_request_cap_rsp(crq, adapter);
4703 break;
4704 case LOGIN_RSP:
4705 netdev_dbg(netdev, "Got Login Response\n");
4706 handle_login_rsp(crq, adapter);
4707 break;
4708 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004709 netdev_dbg(netdev,
4710 "Got Logical Link State Response, state: %d rc: %d\n",
4711 crq->logical_link_state_rsp.link_state,
4712 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004713 adapter->logical_link_state =
4714 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004715 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4716 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004717 break;
4718 case LINK_STATE_INDICATION:
4719 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4720 adapter->phys_link_state =
4721 crq->link_state_indication.phys_link_state;
4722 adapter->logical_link_state =
4723 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004724 if (adapter->phys_link_state && adapter->logical_link_state)
4725 netif_carrier_on(netdev);
4726 else
4727 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004728 break;
4729 case CHANGE_MAC_ADDR_RSP:
4730 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004731 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004732 break;
4733 case ERROR_INDICATION:
4734 netdev_dbg(netdev, "Got Error Indication\n");
4735 handle_error_indication(crq, adapter);
4736 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004737 case REQUEST_STATISTICS_RSP:
4738 netdev_dbg(netdev, "Got Statistics Response\n");
4739 complete(&adapter->stats_done);
4740 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004741 case QUERY_IP_OFFLOAD_RSP:
4742 netdev_dbg(netdev, "Got Query IP offload Response\n");
4743 handle_query_ip_offload_rsp(adapter);
4744 break;
4745 case MULTICAST_CTRL_RSP:
4746 netdev_dbg(netdev, "Got multicast control Response\n");
4747 break;
4748 case CONTROL_IP_OFFLOAD_RSP:
4749 netdev_dbg(netdev, "Got Control IP offload Response\n");
4750 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4751 sizeof(adapter->ip_offload_ctrl),
4752 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004753 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004754 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004755 case COLLECT_FW_TRACE_RSP:
4756 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4757 complete(&adapter->fw_done);
4758 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004759 case GET_VPD_SIZE_RSP:
4760 handle_vpd_size_rsp(crq, adapter);
4761 break;
4762 case GET_VPD_RSP:
4763 handle_vpd_rsp(crq, adapter);
4764 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004765 case QUERY_PHYS_PARMS_RSP:
4766 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4767 complete(&adapter->fw_done);
4768 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004769 default:
4770 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4771 gen_crq->cmd);
4772 }
4773}
4774
4775static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4776{
4777 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004778
Thomas Falcon6c267b32017-02-15 12:17:58 -06004779 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004780 return IRQ_HANDLED;
4781}
4782
4783static void ibmvnic_tasklet(void *data)
4784{
4785 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004786 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004787 union ibmvnic_crq *crq;
4788 unsigned long flags;
4789 bool done = false;
4790
4791 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004792 while (!done) {
4793 /* Pull all the valid messages off the CRQ */
4794 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4795 ibmvnic_handle_crq(crq, adapter);
4796 crq->generic.first = 0;
4797 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004798
4799 /* remain in tasklet until all
4800 * capabilities responses are received
4801 */
4802 if (!adapter->wait_capability)
4803 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004804 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004805 /* if capabilities CRQ's were sent in this tasklet, the following
4806 * tasklet must wait until all responses are received
4807 */
4808 if (atomic_read(&adapter->running_cap_crqs) != 0)
4809 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004810 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004811}
4812
4813static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4814{
4815 struct vio_dev *vdev = adapter->vdev;
4816 int rc;
4817
4818 do {
4819 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4820 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4821
4822 if (rc)
4823 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4824
4825 return rc;
4826}
4827
4828static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4829{
4830 struct ibmvnic_crq_queue *crq = &adapter->crq;
4831 struct device *dev = &adapter->vdev->dev;
4832 struct vio_dev *vdev = adapter->vdev;
4833 int rc;
4834
4835 /* Close the CRQ */
4836 do {
4837 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4838 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4839
4840 /* Clean out the queue */
4841 memset(crq->msgs, 0, PAGE_SIZE);
4842 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004843 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004844
4845 /* And re-open it again */
4846 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4847 crq->msg_token, PAGE_SIZE);
4848
4849 if (rc == H_CLOSED)
4850 /* Adapter is good, but other end is not ready */
4851 dev_warn(dev, "Partner adapter not ready\n");
4852 else if (rc != 0)
4853 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4854
4855 return rc;
4856}
4857
Nathan Fontenotf9928872017-03-30 02:48:54 -04004858static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004859{
4860 struct ibmvnic_crq_queue *crq = &adapter->crq;
4861 struct vio_dev *vdev = adapter->vdev;
4862 long rc;
4863
Nathan Fontenotf9928872017-03-30 02:48:54 -04004864 if (!crq->msgs)
4865 return;
4866
Thomas Falcon032c5e82015-12-21 11:26:06 -06004867 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4868 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004869 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004870 do {
4871 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4872 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4873
4874 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4875 DMA_BIDIRECTIONAL);
4876 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004877 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004878 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004879}
4880
Nathan Fontenotf9928872017-03-30 02:48:54 -04004881static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004882{
4883 struct ibmvnic_crq_queue *crq = &adapter->crq;
4884 struct device *dev = &adapter->vdev->dev;
4885 struct vio_dev *vdev = adapter->vdev;
4886 int rc, retrc = -ENOMEM;
4887
Nathan Fontenotf9928872017-03-30 02:48:54 -04004888 if (crq->msgs)
4889 return 0;
4890
Thomas Falcon032c5e82015-12-21 11:26:06 -06004891 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4892 /* Should we allocate more than one page? */
4893
4894 if (!crq->msgs)
4895 return -ENOMEM;
4896
4897 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4898 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4899 DMA_BIDIRECTIONAL);
4900 if (dma_mapping_error(dev, crq->msg_token))
4901 goto map_failed;
4902
4903 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4904 crq->msg_token, PAGE_SIZE);
4905
4906 if (rc == H_RESOURCE)
4907 /* maybe kexecing and resource is busy. try a reset */
4908 rc = ibmvnic_reset_crq(adapter);
4909 retrc = rc;
4910
4911 if (rc == H_CLOSED) {
4912 dev_warn(dev, "Partner adapter not ready\n");
4913 } else if (rc) {
4914 dev_warn(dev, "Error %d opening adapter\n", rc);
4915 goto reg_crq_failed;
4916 }
4917
4918 retrc = 0;
4919
Thomas Falcon6c267b32017-02-15 12:17:58 -06004920 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4921 (unsigned long)adapter);
4922
Thomas Falcon032c5e82015-12-21 11:26:06 -06004923 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03004924 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
4925 adapter->vdev->unit_address);
4926 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004927 if (rc) {
4928 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4929 vdev->irq, rc);
4930 goto req_irq_failed;
4931 }
4932
4933 rc = vio_enable_interrupts(vdev);
4934 if (rc) {
4935 dev_err(dev, "Error %d enabling interrupts\n", rc);
4936 goto req_irq_failed;
4937 }
4938
4939 crq->cur = 0;
4940 spin_lock_init(&crq->lock);
4941
4942 return retrc;
4943
4944req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004945 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004946 do {
4947 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4948 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4949reg_crq_failed:
4950 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4951map_failed:
4952 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004953 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004954 return retrc;
4955}
4956
Thomas Falcon8a348452018-05-23 13:38:00 -05004957static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
John Allenf6ef6402017-03-17 17:13:42 -05004958{
4959 struct device *dev = &adapter->vdev->dev;
4960 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004961 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004962 int rc;
4963
John Allen017892c12017-05-26 10:30:19 -04004964 adapter->from_passive_init = false;
4965
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004966 old_num_rx_queues = adapter->req_rx_queues;
4967 old_num_tx_queues = adapter->req_tx_queues;
4968
Thomas Falconbbd669a2019-04-04 18:58:26 -05004969 reinit_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004970 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004971 ibmvnic_send_crq_init(adapter);
4972 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4973 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004974 return -1;
4975 }
4976
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004977 if (adapter->init_done_rc) {
4978 release_crq_queue(adapter);
4979 return adapter->init_done_rc;
4980 }
4981
John Allen017892c12017-05-26 10:30:19 -04004982 if (adapter->from_passive_init) {
4983 adapter->state = VNIC_OPEN;
4984 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004985 return -1;
4986 }
4987
Juliet Kim7ed5b312019-09-20 16:11:23 -04004988 if (test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05004989 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004990 if (adapter->req_rx_queues != old_num_rx_queues ||
4991 adapter->req_tx_queues != old_num_tx_queues) {
4992 release_sub_crqs(adapter, 0);
4993 rc = init_sub_crqs(adapter);
4994 } else {
4995 rc = reset_sub_crq_queues(adapter);
4996 }
4997 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004998 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004999 }
5000
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005001 if (rc) {
5002 dev_err(dev, "Initialization of sub crqs failed\n");
5003 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05005004 return rc;
5005 }
5006
5007 rc = init_sub_crq_irqs(adapter);
5008 if (rc) {
5009 dev_err(dev, "Failed to initialize sub crq irqs\n");
5010 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04005011 }
5012
5013 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05005014}
5015
Thomas Falcon8a348452018-05-23 13:38:00 -05005016static int ibmvnic_init(struct ibmvnic_adapter *adapter)
5017{
5018 struct device *dev = &adapter->vdev->dev;
5019 unsigned long timeout = msecs_to_jiffies(30000);
5020 int rc;
5021
5022 adapter->from_passive_init = false;
5023
Thomas Falcon8a348452018-05-23 13:38:00 -05005024 adapter->init_done_rc = 0;
5025 ibmvnic_send_crq_init(adapter);
5026 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5027 dev_err(dev, "Initialization sequence timed out\n");
5028 return -1;
5029 }
5030
5031 if (adapter->init_done_rc) {
5032 release_crq_queue(adapter);
5033 return adapter->init_done_rc;
5034 }
5035
5036 if (adapter->from_passive_init) {
5037 adapter->state = VNIC_OPEN;
5038 adapter->from_passive_init = false;
5039 return -1;
5040 }
5041
5042 rc = init_sub_crqs(adapter);
5043 if (rc) {
5044 dev_err(dev, "Initialization of sub crqs failed\n");
5045 release_crq_queue(adapter);
5046 return rc;
5047 }
5048
5049 rc = init_sub_crq_irqs(adapter);
5050 if (rc) {
5051 dev_err(dev, "Failed to initialize sub crq irqs\n");
5052 release_crq_queue(adapter);
5053 }
5054
5055 return rc;
5056}
5057
Thomas Falcon40c9db82017-06-12 12:35:04 -05005058static struct device_attribute dev_attr_failover;
5059
Thomas Falcon032c5e82015-12-21 11:26:06 -06005060static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5061{
5062 struct ibmvnic_adapter *adapter;
5063 struct net_device *netdev;
5064 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005065 int rc;
5066
5067 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5068 dev->unit_address);
5069
5070 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5071 VETH_MAC_ADDR, NULL);
5072 if (!mac_addr_p) {
5073 dev_err(&dev->dev,
5074 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5075 __FILE__, __LINE__);
5076 return 0;
5077 }
5078
5079 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06005080 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005081 if (!netdev)
5082 return -ENOMEM;
5083
5084 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04005085 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005086 dev_set_drvdata(&dev->dev, netdev);
5087 adapter->vdev = dev;
5088 adapter->netdev = netdev;
5089
5090 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5091 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5092 netdev->irq = dev->irq;
5093 netdev->netdev_ops = &ibmvnic_netdev_ops;
5094 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5095 SET_NETDEV_DEV(netdev, &dev->dev);
5096
5097 spin_lock_init(&adapter->stats_lock);
5098
Nathan Fontenoted651a12017-05-03 14:04:38 -04005099 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005100 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5101 __ibmvnic_delayed_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005102 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06005103 spin_lock_init(&adapter->rwi_lock);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005104 spin_lock_init(&adapter->state_lock);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005105 mutex_init(&adapter->fw_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05005106 init_completion(&adapter->init_done);
Thomas Falcon070eca92019-11-25 17:12:53 -06005107 init_completion(&adapter->fw_done);
5108 init_completion(&adapter->reset_done);
5109 init_completion(&adapter->stats_done);
Juliet Kim7ed5b312019-09-20 16:11:23 -04005110 clear_bit(0, &adapter->resetting);
Nathan Fontenoted651a12017-05-03 14:04:38 -04005111
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005112 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05005113 rc = init_crq_queue(adapter);
5114 if (rc) {
5115 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5116 rc);
5117 goto ibmvnic_init_fail;
5118 }
5119
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005120 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005121 if (rc && rc != EAGAIN)
5122 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04005123 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005124
Thomas Falcon07184212018-05-16 15:49:05 -05005125 rc = init_stats_buffers(adapter);
5126 if (rc)
5127 goto ibmvnic_init_fail;
5128
5129 rc = init_stats_token(adapter);
5130 if (rc)
5131 goto ibmvnic_stats_fail;
5132
Thomas Falconf39f0d12017-02-14 10:22:59 -06005133 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05005134 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5135 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005136
Thomas Falcon40c9db82017-06-12 12:35:04 -05005137 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005138 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05005139 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05005140
Mick Tarsele876a8a2017-09-28 13:53:18 -07005141 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005142 rc = register_netdev(netdev);
5143 if (rc) {
5144 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005145 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005146 }
5147 dev_info(&dev->dev, "ibmvnic registered\n");
5148
Nathan Fontenot90c80142017-05-03 14:04:32 -04005149 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05005150
5151 adapter->wait_for_reset = false;
5152
Thomas Falcon032c5e82015-12-21 11:26:06 -06005153 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005154
5155ibmvnic_register_fail:
5156 device_remove_file(&dev->dev, &dev_attr_failover);
5157
Thomas Falcon07184212018-05-16 15:49:05 -05005158ibmvnic_dev_file_err:
5159 release_stats_token(adapter);
5160
5161ibmvnic_stats_fail:
5162 release_stats_buffers(adapter);
5163
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005164ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005165 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005166 release_crq_queue(adapter);
Thomas Falconff25dcb2019-11-25 17:12:56 -06005167 mutex_destroy(&adapter->fw_lock);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05005168 free_netdev(netdev);
5169
5170 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06005171}
5172
5173static int ibmvnic_remove(struct vio_dev *dev)
5174{
5175 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005176 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Juliet Kim7d7195a2020-03-10 09:23:58 -05005177 unsigned long flags;
5178
5179 spin_lock_irqsave(&adapter->state_lock, flags);
5180 if (adapter->state == VNIC_RESETTING) {
5181 spin_unlock_irqrestore(&adapter->state_lock, flags);
5182 return -EBUSY;
5183 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06005184
Nathan Fontenot90c80142017-05-03 14:04:32 -04005185 adapter->state = VNIC_REMOVING;
Juliet Kim7d7195a2020-03-10 09:23:58 -05005186 spin_unlock_irqrestore(&adapter->state_lock, flags);
5187
Juliet Kima5681e22018-11-19 15:59:22 -06005188 rtnl_lock();
5189 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04005190
5191 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06005192 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04005193 release_crq_queue(adapter);
5194
Thomas Falcon53cc7722018-02-26 18:10:56 -06005195 release_stats_token(adapter);
5196 release_stats_buffers(adapter);
5197
Nathan Fontenot90c80142017-05-03 14:04:32 -04005198 adapter->state = VNIC_REMOVED;
5199
Juliet Kima5681e22018-11-19 15:59:22 -06005200 rtnl_unlock();
Thomas Falconff25dcb2019-11-25 17:12:56 -06005201 mutex_destroy(&adapter->fw_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005202 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005203 free_netdev(netdev);
5204 dev_set_drvdata(&dev->dev, NULL);
5205
5206 return 0;
5207}
5208
Thomas Falcon40c9db82017-06-12 12:35:04 -05005209static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5210 const char *buf, size_t count)
5211{
5212 struct net_device *netdev = dev_get_drvdata(dev);
5213 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5214 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5215 __be64 session_token;
5216 long rc;
5217
5218 if (!sysfs_streq(buf, "1"))
5219 return -EINVAL;
5220
5221 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5222 H_GET_SESSION_TOKEN, 0, 0, 0);
5223 if (rc) {
5224 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5225 rc);
5226 return -EINVAL;
5227 }
5228
5229 session_token = (__be64)retbuf[0];
5230 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5231 be64_to_cpu(session_token));
5232 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5233 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5234 if (rc) {
5235 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5236 rc);
5237 return -EINVAL;
5238 }
5239
5240 return count;
5241}
5242
Joe Perches6cbaefb2017-12-19 10:15:09 -08005243static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05005244
Thomas Falcon032c5e82015-12-21 11:26:06 -06005245static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5246{
5247 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5248 struct ibmvnic_adapter *adapter;
5249 struct iommu_table *tbl;
5250 unsigned long ret = 0;
5251 int i;
5252
5253 tbl = get_iommu_table_base(&vdev->dev);
5254
5255 /* netdev inits at probe time along with the structures we need below*/
5256 if (!netdev)
5257 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5258
5259 adapter = netdev_priv(netdev);
5260
5261 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06005262 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5263
5264 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5265 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5266
5267 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
5268 i++)
5269 ret += adapter->rx_pool[i].size *
5270 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5271
5272 return ret;
5273}
5274
5275static int ibmvnic_resume(struct device *dev)
5276{
5277 struct net_device *netdev = dev_get_drvdata(dev);
5278 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005279
John Allencb89ba22017-06-19 11:27:53 -05005280 if (adapter->state != VNIC_OPEN)
5281 return 0;
5282
John Allena2488782017-07-24 13:26:06 -05005283 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005284
5285 return 0;
5286}
5287
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305288static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005289 {"network", "IBM,vnic"},
5290 {"", "" }
5291};
5292MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5293
5294static const struct dev_pm_ops ibmvnic_pm_ops = {
5295 .resume = ibmvnic_resume
5296};
5297
5298static struct vio_driver ibmvnic_driver = {
5299 .id_table = ibmvnic_device_table,
5300 .probe = ibmvnic_probe,
5301 .remove = ibmvnic_remove,
5302 .get_desired_dma = ibmvnic_get_desired_dma,
5303 .name = ibmvnic_driver_name,
5304 .pm = &ibmvnic_pm_ops,
5305};
5306
5307/* module functions */
5308static int __init ibmvnic_module_init(void)
5309{
5310 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5311 IBMVNIC_DRIVER_VERSION);
5312
5313 return vio_register_driver(&ibmvnic_driver);
5314}
5315
5316static void __exit ibmvnic_module_exit(void)
5317{
5318 vio_unregister_driver(&ibmvnic_driver);
5319}
5320
5321module_init(ibmvnic_module_init);
5322module_exit(ibmvnic_module_exit);