blob: 9e9f4096db58274749b58b8527cb94b4570ff04f [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 Falcon032c5e82015-12-21 11:26:06 -0600162static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
163 struct ibmvnic_long_term_buff *ltb, int size)
164{
165 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500166 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600167
168 ltb->size = size;
169 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
170 GFP_KERNEL);
171
172 if (!ltb->buff) {
173 dev_err(dev, "Couldn't alloc long term buffer\n");
174 return -ENOMEM;
175 }
176 ltb->map_id = adapter->map_id;
177 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500178
179 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500180 rc = send_request_map(adapter, ltb->addr,
181 ltb->size, ltb->map_id);
182 if (rc) {
183 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
184 return rc;
185 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600186 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500187
188 if (adapter->fw_done_rc) {
189 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
190 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500191 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500192 return -1;
193 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600194 return 0;
195}
196
197static void free_long_term_buff(struct ibmvnic_adapter *adapter,
198 struct ibmvnic_long_term_buff *ltb)
199{
200 struct device *dev = &adapter->vdev->dev;
201
Nathan Fontenotc657e322017-03-30 02:49:06 -0400202 if (!ltb->buff)
203 return;
204
Nathan Fontenoted651a12017-05-03 14:04:38 -0400205 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
206 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500207 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400208 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600209}
210
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500211static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
212 struct ibmvnic_long_term_buff *ltb)
213{
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500214 int rc;
215
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500216 memset(ltb->buff, 0, ltb->size);
217
218 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500219 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 if (rc)
221 return rc;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500222 wait_for_completion(&adapter->fw_done);
223
224 if (adapter->fw_done_rc) {
225 dev_info(&adapter->vdev->dev,
226 "Reset failed, attempting to free and reallocate buffer\n");
227 free_long_term_buff(adapter, ltb);
228 return alloc_long_term_buff(adapter, ltb, ltb->size);
229 }
230 return 0;
231}
232
Thomas Falconf185a492017-05-26 10:30:48 -0400233static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
234{
235 int i;
236
237 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
238 i++)
239 adapter->rx_pool[i].active = 0;
240}
241
Thomas Falcon032c5e82015-12-21 11:26:06 -0600242static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
243 struct ibmvnic_rx_pool *pool)
244{
245 int count = pool->size - atomic_read(&pool->available);
246 struct device *dev = &adapter->vdev->dev;
247 int buffers_added = 0;
248 unsigned long lpar_rc;
249 union sub_crq sub_crq;
250 struct sk_buff *skb;
251 unsigned int offset;
252 dma_addr_t dma_addr;
253 unsigned char *dst;
254 u64 *handle_array;
255 int shift = 0;
256 int index;
257 int i;
258
Thomas Falconf185a492017-05-26 10:30:48 -0400259 if (!pool->active)
260 return;
261
Thomas Falcon032c5e82015-12-21 11:26:06 -0600262 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
263 be32_to_cpu(adapter->login_rsp_buf->
264 off_rxadd_subcrqs));
265
266 for (i = 0; i < count; ++i) {
267 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
268 if (!skb) {
269 dev_err(dev, "Couldn't replenish rx buff\n");
270 adapter->replenish_no_mem++;
271 break;
272 }
273
274 index = pool->free_map[pool->next_free];
275
276 if (pool->rx_buff[index].skb)
277 dev_err(dev, "Inconsistent free_map!\n");
278
279 /* Copy the skb to the long term mapped DMA buffer */
280 offset = index * pool->buff_size;
281 dst = pool->long_term_buff.buff + offset;
282 memset(dst, 0, pool->buff_size);
283 dma_addr = pool->long_term_buff.addr + offset;
284 pool->rx_buff[index].data = dst;
285
286 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
287 pool->rx_buff[index].dma = dma_addr;
288 pool->rx_buff[index].skb = skb;
289 pool->rx_buff[index].pool_index = pool->index;
290 pool->rx_buff[index].size = pool->buff_size;
291
292 memset(&sub_crq, 0, sizeof(sub_crq));
293 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
294 sub_crq.rx_add.correlator =
295 cpu_to_be64((u64)&pool->rx_buff[index]);
296 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
297 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
298
299 /* The length field of the sCRQ is defined to be 24 bits so the
300 * buffer size needs to be left shifted by a byte before it is
301 * converted to big endian to prevent the last byte from being
302 * truncated.
303 */
304#ifdef __LITTLE_ENDIAN__
305 shift = 8;
306#endif
307 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
308
309 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
310 &sub_crq);
311 if (lpar_rc != H_SUCCESS)
312 goto failure;
313
314 buffers_added++;
315 adapter->replenish_add_buff_success++;
316 pool->next_free = (pool->next_free + 1) % pool->size;
317 }
318 atomic_add(buffers_added, &pool->available);
319 return;
320
321failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500322 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
323 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -0600324 pool->free_map[pool->next_free] = index;
325 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600326
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400330
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500331 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400332 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500333 * queue is closed or pending failover.
334 * Firmware guarantees that a signal will be sent to the
335 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400336 */
337 deactivate_rx_pools(adapter);
338 netif_carrier_off(adapter->netdev);
339 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600340}
341
342static void replenish_pools(struct ibmvnic_adapter *adapter)
343{
344 int i;
345
Thomas Falcon032c5e82015-12-21 11:26:06 -0600346 adapter->replenish_task_cycles++;
347 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 i++) {
349 if (adapter->rx_pool[i].active)
350 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
351 }
352}
353
John Allen3d52b592017-08-02 16:44:14 -0500354static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355{
356 kfree(adapter->tx_stats_buffers);
357 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600358 adapter->tx_stats_buffers = NULL;
359 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500360}
361
362static int init_stats_buffers(struct ibmvnic_adapter *adapter)
363{
364 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600365 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500366 sizeof(struct ibmvnic_tx_queue_stats),
367 GFP_KERNEL);
368 if (!adapter->tx_stats_buffers)
369 return -ENOMEM;
370
371 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600372 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500373 sizeof(struct ibmvnic_rx_queue_stats),
374 GFP_KERNEL);
375 if (!adapter->rx_stats_buffers)
376 return -ENOMEM;
377
378 return 0;
379}
380
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400381static void release_stats_token(struct ibmvnic_adapter *adapter)
382{
383 struct device *dev = &adapter->vdev->dev;
384
385 if (!adapter->stats_token)
386 return;
387
388 dma_unmap_single(dev, adapter->stats_token,
389 sizeof(struct ibmvnic_statistics),
390 DMA_FROM_DEVICE);
391 adapter->stats_token = 0;
392}
393
394static int init_stats_token(struct ibmvnic_adapter *adapter)
395{
396 struct device *dev = &adapter->vdev->dev;
397 dma_addr_t stok;
398
399 stok = dma_map_single(dev, &adapter->stats,
400 sizeof(struct ibmvnic_statistics),
401 DMA_FROM_DEVICE);
402 if (dma_mapping_error(dev, stok)) {
403 dev_err(dev, "Couldn't map stats buffer\n");
404 return -1;
405 }
406
407 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500408 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400409 return 0;
410}
411
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400412static int reset_rx_pools(struct ibmvnic_adapter *adapter)
413{
414 struct ibmvnic_rx_pool *rx_pool;
415 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500416 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600417 u64 *size_array;
418
419 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
420 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400421
422 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
423 for (i = 0; i < rx_scrqs; i++) {
424 rx_pool = &adapter->rx_pool[i];
425
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500426 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
427
John Allen896d8692018-01-18 16:26:31 -0600428 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
429 free_long_term_buff(adapter, &rx_pool->long_term_buff);
430 rx_pool->buff_size = be64_to_cpu(size_array[i]);
431 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
432 rx_pool->size *
433 rx_pool->buff_size);
434 } else {
435 rc = reset_long_term_buff(adapter,
436 &rx_pool->long_term_buff);
437 }
438
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500439 if (rc)
440 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400441
442 for (j = 0; j < rx_pool->size; j++)
443 rx_pool->free_map[j] = j;
444
445 memset(rx_pool->rx_buff, 0,
446 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
447
448 atomic_set(&rx_pool->available, 0);
449 rx_pool->next_alloc = 0;
450 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500451 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400452 }
453
454 return 0;
455}
456
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400457static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600458{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400459 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400460 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600461
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400462 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600463 return;
464
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600465 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400466 rx_pool = &adapter->rx_pool[i];
467
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500468 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
469
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400470 kfree(rx_pool->free_map);
471 free_long_term_buff(adapter, &rx_pool->long_term_buff);
472
473 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400474 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400475
476 for (j = 0; j < rx_pool->size; j++) {
477 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600478 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
479 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400480 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600481 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400482
483 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600484 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400485
486 kfree(adapter->rx_pool);
487 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600488 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400489}
490
491static int init_rx_pools(struct net_device *netdev)
492{
493 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
494 struct device *dev = &adapter->vdev->dev;
495 struct ibmvnic_rx_pool *rx_pool;
496 int rxadd_subcrqs;
497 u64 *size_array;
498 int i, j;
499
500 rxadd_subcrqs =
501 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
502 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
503 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
504
505 adapter->rx_pool = kcalloc(rxadd_subcrqs,
506 sizeof(struct ibmvnic_rx_pool),
507 GFP_KERNEL);
508 if (!adapter->rx_pool) {
509 dev_err(dev, "Failed to allocate rx pools\n");
510 return -1;
511 }
512
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600513 adapter->num_active_rx_pools = rxadd_subcrqs;
514
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400515 for (i = 0; i < rxadd_subcrqs; i++) {
516 rx_pool = &adapter->rx_pool[i];
517
518 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500519 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400520 i, adapter->req_rx_add_entries_per_subcrq,
521 be64_to_cpu(size_array[i]));
522
523 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
524 rx_pool->index = i;
525 rx_pool->buff_size = be64_to_cpu(size_array[i]);
526 rx_pool->active = 1;
527
528 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
529 GFP_KERNEL);
530 if (!rx_pool->free_map) {
531 release_rx_pools(adapter);
532 return -1;
533 }
534
535 rx_pool->rx_buff = kcalloc(rx_pool->size,
536 sizeof(struct ibmvnic_rx_buff),
537 GFP_KERNEL);
538 if (!rx_pool->rx_buff) {
539 dev_err(dev, "Couldn't alloc rx buffers\n");
540 release_rx_pools(adapter);
541 return -1;
542 }
543
544 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
545 rx_pool->size * rx_pool->buff_size)) {
546 release_rx_pools(adapter);
547 return -1;
548 }
549
550 for (j = 0; j < rx_pool->size; ++j)
551 rx_pool->free_map[j] = j;
552
553 atomic_set(&rx_pool->available, 0);
554 rx_pool->next_alloc = 0;
555 rx_pool->next_free = 0;
556 }
557
558 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600559}
560
Thomas Falcone26dc252018-03-16 20:00:25 -0500561static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
562 struct ibmvnic_tx_pool *tx_pool)
563{
564 int rc, i;
565
566 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
567 if (rc)
568 return rc;
569
570 memset(tx_pool->tx_buff, 0,
571 tx_pool->num_buffers *
572 sizeof(struct ibmvnic_tx_buff));
573
574 for (i = 0; i < tx_pool->num_buffers; i++)
575 tx_pool->free_map[i] = i;
576
577 tx_pool->consumer_index = 0;
578 tx_pool->producer_index = 0;
579
580 return 0;
581}
582
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400583static int reset_tx_pools(struct ibmvnic_adapter *adapter)
584{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400585 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500586 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400587
588 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
589 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500590 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500591 if (rc)
592 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500593 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500594 if (rc)
595 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400596 }
597
598 return 0;
599}
600
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200601static void release_vpd_data(struct ibmvnic_adapter *adapter)
602{
603 if (!adapter->vpd)
604 return;
605
606 kfree(adapter->vpd->buff);
607 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600608
609 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200610}
611
Thomas Falconfb794212018-03-16 20:00:26 -0500612static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
613 struct ibmvnic_tx_pool *tx_pool)
614{
615 kfree(tx_pool->tx_buff);
616 kfree(tx_pool->free_map);
617 free_long_term_buff(adapter, &tx_pool->long_term_buff);
618}
619
Nathan Fontenotc657e322017-03-30 02:49:06 -0400620static void release_tx_pools(struct ibmvnic_adapter *adapter)
621{
John Allen896d8692018-01-18 16:26:31 -0600622 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400623
624 if (!adapter->tx_pool)
625 return;
626
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600627 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500628 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
629 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400630 }
631
632 kfree(adapter->tx_pool);
633 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500634 kfree(adapter->tso_pool);
635 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600636 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400637}
638
Thomas Falcon32053062018-03-16 20:00:27 -0500639static int init_one_tx_pool(struct net_device *netdev,
640 struct ibmvnic_tx_pool *tx_pool,
641 int num_entries, int buf_size)
642{
643 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
644 int i;
645
646 tx_pool->tx_buff = kcalloc(num_entries,
647 sizeof(struct ibmvnic_tx_buff),
648 GFP_KERNEL);
649 if (!tx_pool->tx_buff)
650 return -1;
651
652 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
653 num_entries * buf_size))
654 return -1;
655
656 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
657 if (!tx_pool->free_map)
658 return -1;
659
660 for (i = 0; i < num_entries; i++)
661 tx_pool->free_map[i] = i;
662
663 tx_pool->consumer_index = 0;
664 tx_pool->producer_index = 0;
665 tx_pool->num_buffers = num_entries;
666 tx_pool->buf_size = buf_size;
667
668 return 0;
669}
670
Nathan Fontenotc657e322017-03-30 02:49:06 -0400671static int init_tx_pools(struct net_device *netdev)
672{
673 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400674 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500675 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400676
677 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
678 adapter->tx_pool = kcalloc(tx_subcrqs,
679 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
680 if (!adapter->tx_pool)
681 return -1;
682
Thomas Falcon32053062018-03-16 20:00:27 -0500683 adapter->tso_pool = kcalloc(tx_subcrqs,
684 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
685 if (!adapter->tso_pool)
686 return -1;
687
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600688 adapter->num_active_tx_pools = tx_subcrqs;
689
Nathan Fontenotc657e322017-03-30 02:49:06 -0400690 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500691 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
692 adapter->req_tx_entries_per_subcrq,
693 adapter->req_mtu + VLAN_HLEN);
694 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400695 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500696 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400697 }
698
Thomas Falcon32053062018-03-16 20:00:27 -0500699 init_one_tx_pool(netdev, &adapter->tso_pool[i],
700 IBMVNIC_TSO_BUFS,
701 IBMVNIC_TSO_BUF_SZ);
702 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400703 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500704 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400705 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400706 }
707
708 return 0;
709}
710
John Allend944c3d62017-05-26 10:30:13 -0400711static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
712{
713 int i;
714
715 if (adapter->napi_enabled)
716 return;
717
718 for (i = 0; i < adapter->req_rx_queues; i++)
719 napi_enable(&adapter->napi[i]);
720
721 adapter->napi_enabled = true;
722}
723
724static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
725{
726 int i;
727
728 if (!adapter->napi_enabled)
729 return;
730
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500731 for (i = 0; i < adapter->req_rx_queues; i++) {
732 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400733 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500734 }
John Allend944c3d62017-05-26 10:30:13 -0400735
736 adapter->napi_enabled = false;
737}
738
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600739static int init_napi(struct ibmvnic_adapter *adapter)
740{
741 int i;
742
743 adapter->napi = kcalloc(adapter->req_rx_queues,
744 sizeof(struct napi_struct), GFP_KERNEL);
745 if (!adapter->napi)
746 return -ENOMEM;
747
748 for (i = 0; i < adapter->req_rx_queues; i++) {
749 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
750 netif_napi_add(adapter->netdev, &adapter->napi[i],
751 ibmvnic_poll, NAPI_POLL_WEIGHT);
752 }
753
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600754 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600755 return 0;
756}
757
758static void release_napi(struct ibmvnic_adapter *adapter)
759{
760 int i;
761
762 if (!adapter->napi)
763 return;
764
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600765 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800766 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
767 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600768 }
769
770 kfree(adapter->napi);
771 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600772 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500773 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600774}
775
John Allena57a5d22017-03-17 17:13:41 -0500776static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600777{
778 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500779 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500780 int retry_count = 0;
Thomas Falconeb110412018-05-24 14:37:53 -0500781 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500782 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600783
John Allenbd0b6722017-03-17 17:13:40 -0500784 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500785 retry = false;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500786 if (retry_count > IBMVNIC_MAX_QUEUES) {
787 netdev_warn(netdev, "Login attempts exceeded\n");
788 return -1;
789 }
790
791 adapter->init_done_rc = 0;
792 reinit_completion(&adapter->init_done);
793 rc = send_login(adapter);
794 if (rc) {
795 netdev_warn(netdev, "Unable to login\n");
796 return rc;
797 }
798
799 if (!wait_for_completion_timeout(&adapter->init_done,
800 timeout)) {
801 netdev_warn(netdev, "Login timed out\n");
802 return -1;
803 }
804
805 if (adapter->init_done_rc == PARTIALSUCCESS) {
806 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600807 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500808
Thomas Falconeb110412018-05-24 14:37:53 -0500809 retry = true;
810 netdev_dbg(netdev,
811 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500812 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500813 reinit_completion(&adapter->init_done);
814 send_cap_queries(adapter);
815 if (!wait_for_completion_timeout(&adapter->init_done,
816 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500817 netdev_warn(netdev,
818 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500819 return -1;
820 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500821
Thomas Falcon4d96f122017-08-01 15:04:36 -0500822 rc = init_sub_crqs(adapter);
823 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500824 netdev_warn(netdev,
825 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500826 return -1;
827 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500828
Thomas Falcon4d96f122017-08-01 15:04:36 -0500829 rc = init_sub_crq_irqs(adapter);
830 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500831 netdev_warn(netdev,
832 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500833 return -1;
834 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500835 } else if (adapter->init_done_rc) {
836 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500837 return -1;
838 }
Thomas Falconeb110412018-05-24 14:37:53 -0500839 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500840
Thomas Falcon62740e92019-05-09 23:13:43 -0500841 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600842
John Allena57a5d22017-03-17 17:13:41 -0500843 return 0;
844}
845
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600846static void release_login_buffer(struct ibmvnic_adapter *adapter)
847{
848 kfree(adapter->login_buf);
849 adapter->login_buf = NULL;
850}
851
852static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
853{
854 kfree(adapter->login_rsp_buf);
855 adapter->login_rsp_buf = NULL;
856}
857
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400858static void release_resources(struct ibmvnic_adapter *adapter)
859{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200860 release_vpd_data(adapter);
861
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400862 release_tx_pools(adapter);
863 release_rx_pools(adapter);
864
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600865 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600866 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400867}
868
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400869static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
870{
871 struct net_device *netdev = adapter->netdev;
872 unsigned long timeout = msecs_to_jiffies(30000);
873 union ibmvnic_crq crq;
874 bool resend;
875 int rc;
876
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500877 netdev_dbg(netdev, "setting link state %d\n", link_state);
878
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400879 memset(&crq, 0, sizeof(crq));
880 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
881 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
882 crq.logical_link_state.link_state = link_state;
883
884 do {
885 resend = false;
886
887 reinit_completion(&adapter->init_done);
888 rc = ibmvnic_send_crq(adapter, &crq);
889 if (rc) {
890 netdev_err(netdev, "Failed to set link state\n");
891 return rc;
892 }
893
894 if (!wait_for_completion_timeout(&adapter->init_done,
895 timeout)) {
896 netdev_err(netdev, "timeout setting link state\n");
897 return -1;
898 }
899
900 if (adapter->init_done_rc == 1) {
901 /* Partuial success, delay and re-send */
902 mdelay(1000);
903 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500904 } else if (adapter->init_done_rc) {
905 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
906 adapter->init_done_rc);
907 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400908 }
909 } while (resend);
910
911 return 0;
912}
913
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400914static int set_real_num_queues(struct net_device *netdev)
915{
916 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
917 int rc;
918
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500919 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
920 adapter->req_tx_queues, adapter->req_rx_queues);
921
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400922 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
923 if (rc) {
924 netdev_err(netdev, "failed to set the number of tx queues\n");
925 return rc;
926 }
927
928 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
929 if (rc)
930 netdev_err(netdev, "failed to set the number of rx queues\n");
931
932 return rc;
933}
934
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200935static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
936{
937 struct device *dev = &adapter->vdev->dev;
938 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200939 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500940 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200941
942 if (adapter->vpd->buff)
943 len = adapter->vpd->len;
944
John Allen69d08dc2018-01-18 16:27:58 -0600945 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200946 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
947 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500948 rc = ibmvnic_send_crq(adapter, &crq);
949 if (rc)
950 return rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200951 wait_for_completion(&adapter->fw_done);
952
953 if (!adapter->vpd->len)
954 return -ENODATA;
955
956 if (!adapter->vpd->buff)
957 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
958 else if (adapter->vpd->len != len)
959 adapter->vpd->buff =
960 krealloc(adapter->vpd->buff,
961 adapter->vpd->len, GFP_KERNEL);
962
963 if (!adapter->vpd->buff) {
964 dev_err(dev, "Could allocate VPD buffer\n");
965 return -ENOMEM;
966 }
967
968 adapter->vpd->dma_addr =
969 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
970 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200971 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200972 dev_err(dev, "Could not map VPD buffer\n");
973 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600974 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200975 return -ENOMEM;
976 }
977
978 reinit_completion(&adapter->fw_done);
979 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
980 crq.get_vpd.cmd = GET_VPD;
981 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
982 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500983 rc = ibmvnic_send_crq(adapter, &crq);
984 if (rc) {
985 kfree(adapter->vpd->buff);
986 adapter->vpd->buff = NULL;
987 return rc;
988 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200989 wait_for_completion(&adapter->fw_done);
990
991 return 0;
992}
993
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400994static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500995{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400996 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600997 int rc;
John Allena57a5d22017-03-17 17:13:41 -0500998
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400999 rc = set_real_num_queues(netdev);
1000 if (rc)
1001 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001002
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001003 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1004 if (!adapter->vpd)
1005 return -ENOMEM;
1006
John Allen69d08dc2018-01-18 16:27:58 -06001007 /* Vital Product Data (VPD) */
1008 rc = ibmvnic_get_vpd(adapter);
1009 if (rc) {
1010 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1011 return rc;
1012 }
1013
Thomas Falcon032c5e82015-12-21 11:26:06 -06001014 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001015
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001016 rc = init_napi(adapter);
1017 if (rc)
1018 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001019
Thomas Falcon032c5e82015-12-21 11:26:06 -06001020 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001021
1022 rc = init_rx_pools(netdev);
1023 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001024 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001025
Nathan Fontenotc657e322017-03-30 02:49:06 -04001026 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001027 return rc;
1028}
1029
Nathan Fontenoted651a12017-05-03 14:04:38 -04001030static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001031{
1032 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001033 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001034 int i, rc;
1035
Nathan Fontenot90c80142017-05-03 14:04:32 -04001036 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001037 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001038 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001039
Thomas Falcon032c5e82015-12-21 11:26:06 -06001040 /* We're ready to receive frames, enable the sub-crq interrupts and
1041 * set the logical link state to up
1042 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001043 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001044 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001045 if (prev_state == VNIC_CLOSED)
1046 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001047 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001048 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001049
Nathan Fontenoted651a12017-05-03 14:04:38 -04001050 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001051 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001052 if (prev_state == VNIC_CLOSED)
1053 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001054 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001055 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001056
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001057 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001058 if (rc) {
1059 for (i = 0; i < adapter->req_rx_queues; i++)
1060 napi_disable(&adapter->napi[i]);
1061 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001062 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001063 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001064
Nathan Fontenoted651a12017-05-03 14:04:38 -04001065 netif_tx_start_all_queues(netdev);
1066
1067 if (prev_state == VNIC_CLOSED) {
1068 for (i = 0; i < adapter->req_rx_queues; i++)
1069 napi_schedule(&adapter->napi[i]);
1070 }
1071
1072 adapter->state = VNIC_OPEN;
1073 return rc;
1074}
1075
1076static int ibmvnic_open(struct net_device *netdev)
1077{
1078 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001079 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001080
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001081 /* If device failover is pending, just set device state and return.
1082 * Device operation will be handled by reset routine.
1083 */
1084 if (adapter->failover_pending) {
1085 adapter->state = VNIC_OPEN;
1086 return 0;
1087 }
1088
Nathan Fontenoted651a12017-05-03 14:04:38 -04001089 if (adapter->state != VNIC_CLOSED) {
1090 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001091 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001092 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001093
1094 rc = init_resources(adapter);
1095 if (rc) {
1096 netdev_err(netdev, "failed to initialize resources\n");
1097 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001098 return rc;
1099 }
1100 }
1101
1102 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001103
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001104 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001105}
1106
Thomas Falcond0869c02018-02-13 18:23:43 -06001107static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1108{
1109 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001110 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001111 u64 rx_entries;
1112 int rx_scrqs;
1113 int i, j;
1114
1115 if (!adapter->rx_pool)
1116 return;
1117
Thomas Falcon660e3092018-04-20 14:25:32 -05001118 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001119 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1120
1121 /* Free any remaining skbs in the rx buffer pools */
1122 for (i = 0; i < rx_scrqs; i++) {
1123 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001124 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001125 continue;
1126
1127 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1128 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001129 rx_buff = &rx_pool->rx_buff[j];
1130 if (rx_buff && rx_buff->skb) {
1131 dev_kfree_skb_any(rx_buff->skb);
1132 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001133 }
1134 }
1135 }
1136}
1137
Thomas Falcone9e1e972018-03-16 20:00:30 -05001138static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1139 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001140{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001141 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001142 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001143 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001144
Dan Carpenter050e85c2018-03-23 14:36:15 +03001145 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001146 return;
1147
1148 tx_entries = tx_pool->num_buffers;
1149
1150 for (i = 0; i < tx_entries; i++) {
1151 tx_buff = &tx_pool->tx_buff[i];
1152 if (tx_buff && tx_buff->skb) {
1153 dev_kfree_skb_any(tx_buff->skb);
1154 tx_buff->skb = NULL;
1155 }
1156 }
1157}
1158
1159static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1160{
1161 int tx_scrqs;
1162 int i;
1163
1164 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001165 return;
1166
Thomas Falcon660e3092018-04-20 14:25:32 -05001167 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001168
1169 /* Free any remaining skbs in the tx buffer pools */
1170 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001171 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001172 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1173 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001174 }
1175}
1176
John Allen6095e592018-03-30 13:44:21 -05001177static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001178{
John Allen6095e592018-03-30 13:44:21 -05001179 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001180 int i;
1181
Nathan Fontenot46293b92017-05-03 14:05:02 -04001182 if (adapter->tx_scrq) {
1183 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001184 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001185 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001186 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001187 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001188 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001189 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001190 }
1191
Nathan Fontenot46293b92017-05-03 14:05:02 -04001192 if (adapter->rx_scrq) {
1193 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001194 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001195 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001196 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001197 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001198 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001199 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001200 }
1201 }
John Allen6095e592018-03-30 13:44:21 -05001202}
1203
1204static void ibmvnic_cleanup(struct net_device *netdev)
1205{
1206 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1207
1208 /* ensure that transmissions are stopped if called by do_reset */
1209 if (adapter->resetting)
1210 netif_tx_disable(netdev);
1211 else
1212 netif_tx_stop_all_queues(netdev);
1213
1214 ibmvnic_napi_disable(adapter);
1215 ibmvnic_disable_irqs(adapter);
1216
Thomas Falcond0869c02018-02-13 18:23:43 -06001217 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001218 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001219}
1220
1221static int __ibmvnic_close(struct net_device *netdev)
1222{
1223 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1224 int rc = 0;
1225
1226 adapter->state = VNIC_CLOSING;
1227 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1228 if (rc)
1229 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001230 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001231 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001232}
1233
Nathan Fontenoted651a12017-05-03 14:04:38 -04001234static int ibmvnic_close(struct net_device *netdev)
1235{
1236 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1237 int rc;
1238
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001239 /* If device failover is pending, just set device state and return.
1240 * Device operation will be handled by reset routine.
1241 */
1242 if (adapter->failover_pending) {
1243 adapter->state = VNIC_CLOSED;
1244 return 0;
1245 }
1246
Nathan Fontenoted651a12017-05-03 14:04:38 -04001247 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001248 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001249
1250 return rc;
1251}
1252
Thomas Falconad7775d2016-04-01 17:20:34 -05001253/**
1254 * build_hdr_data - creates L2/L3/L4 header data buffer
1255 * @hdr_field - bitfield determining needed headers
1256 * @skb - socket buffer
1257 * @hdr_len - array of header lengths
1258 * @tot_len - total length of data
1259 *
1260 * Reads hdr_field to determine which headers are needed by firmware.
1261 * Builds a buffer containing these headers. Saves individual header
1262 * lengths and total buffer length to be used to build descriptors.
1263 */
1264static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1265 int *hdr_len, u8 *hdr_data)
1266{
1267 int len = 0;
1268 u8 *hdr;
1269
Thomas Falconda75e3b2018-03-12 11:51:02 -05001270 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1271 hdr_len[0] = sizeof(struct vlan_ethhdr);
1272 else
1273 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001274
1275 if (skb->protocol == htons(ETH_P_IP)) {
1276 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1277 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1278 hdr_len[2] = tcp_hdrlen(skb);
1279 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1280 hdr_len[2] = sizeof(struct udphdr);
1281 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1282 hdr_len[1] = sizeof(struct ipv6hdr);
1283 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1284 hdr_len[2] = tcp_hdrlen(skb);
1285 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1286 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001287 } else if (skb->protocol == htons(ETH_P_ARP)) {
1288 hdr_len[1] = arp_hdr_len(skb->dev);
1289 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001290 }
1291
1292 memset(hdr_data, 0, 120);
1293 if ((hdr_field >> 6) & 1) {
1294 hdr = skb_mac_header(skb);
1295 memcpy(hdr_data, hdr, hdr_len[0]);
1296 len += hdr_len[0];
1297 }
1298
1299 if ((hdr_field >> 5) & 1) {
1300 hdr = skb_network_header(skb);
1301 memcpy(hdr_data + len, hdr, hdr_len[1]);
1302 len += hdr_len[1];
1303 }
1304
1305 if ((hdr_field >> 4) & 1) {
1306 hdr = skb_transport_header(skb);
1307 memcpy(hdr_data + len, hdr, hdr_len[2]);
1308 len += hdr_len[2];
1309 }
1310 return len;
1311}
1312
1313/**
1314 * create_hdr_descs - create header and header extension descriptors
1315 * @hdr_field - bitfield determining needed headers
1316 * @data - buffer containing header data
1317 * @len - length of data buffer
1318 * @hdr_len - array of individual header lengths
1319 * @scrq_arr - descriptor array
1320 *
1321 * Creates header and, if needed, header extension descriptors and
1322 * places them in a descriptor array, scrq_arr
1323 */
1324
Thomas Falcon2de09682017-10-16 10:02:11 -05001325static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1326 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001327{
1328 union sub_crq hdr_desc;
1329 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001330 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001331 u8 *data, *cur;
1332 int tmp;
1333
1334 while (tmp_len > 0) {
1335 cur = hdr_data + len - tmp_len;
1336
1337 memset(&hdr_desc, 0, sizeof(hdr_desc));
1338 if (cur != hdr_data) {
1339 data = hdr_desc.hdr_ext.data;
1340 tmp = tmp_len > 29 ? 29 : tmp_len;
1341 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1342 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1343 hdr_desc.hdr_ext.len = tmp;
1344 } else {
1345 data = hdr_desc.hdr.data;
1346 tmp = tmp_len > 24 ? 24 : tmp_len;
1347 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1348 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1349 hdr_desc.hdr.len = tmp;
1350 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1351 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1352 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1353 hdr_desc.hdr.flag = hdr_field << 1;
1354 }
1355 memcpy(data, cur, tmp);
1356 tmp_len -= tmp;
1357 *scrq_arr = hdr_desc;
1358 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001359 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001360 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001361
1362 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001363}
1364
1365/**
1366 * build_hdr_descs_arr - build a header descriptor array
1367 * @skb - socket buffer
1368 * @num_entries - number of descriptors to be sent
1369 * @subcrq - first TX descriptor
1370 * @hdr_field - bit field determining which headers will be sent
1371 *
1372 * This function will build a TX descriptor array with applicable
1373 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1374 */
1375
1376static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1377 int *num_entries, u8 hdr_field)
1378{
1379 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001380 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001381 u8 *hdr_data = txbuff->hdr_data;
1382
1383 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1384 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001385 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001386 txbuff->indir_arr + 1);
1387}
1388
Thomas Falcon1f247a62018-03-12 11:51:04 -05001389static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1390 struct net_device *netdev)
1391{
1392 /* For some backing devices, mishandling of small packets
1393 * can result in a loss of connection or TX stall. Device
1394 * architects recommend that no packet should be smaller
1395 * than the minimum MTU value provided to the driver, so
1396 * pad any packets to that length
1397 */
1398 if (skb->len < netdev->min_mtu)
1399 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001400
1401 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001402}
1403
YueHaibing94b2bb22018-09-18 14:35:47 +08001404static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001405{
1406 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1407 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001408 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001409 struct device *dev = &adapter->vdev->dev;
1410 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001411 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001412 struct ibmvnic_tx_pool *tx_pool;
1413 unsigned int tx_send_failed = 0;
1414 unsigned int tx_map_failed = 0;
1415 unsigned int tx_dropped = 0;
1416 unsigned int tx_packets = 0;
1417 unsigned int tx_bytes = 0;
1418 dma_addr_t data_dma_addr;
1419 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001420 unsigned long lpar_rc;
1421 union sub_crq tx_crq;
1422 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001423 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001424 unsigned char *dst;
1425 u64 *handle_array;
1426 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001427 u8 proto = 0;
YueHaibing94b2bb22018-09-18 14:35:47 +08001428 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001429
Nathan Fontenoted651a12017-05-03 14:04:38 -04001430 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001431 if (!netif_subqueue_stopped(netdev, skb))
1432 netif_stop_subqueue(netdev, queue_num);
1433 dev_kfree_skb_any(skb);
1434
Thomas Falcon032c5e82015-12-21 11:26:06 -06001435 tx_send_failed++;
1436 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001437 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001438 goto out;
1439 }
1440
Thomas Falcon7083a452018-03-12 21:05:26 -05001441 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001442 tx_dropped++;
1443 tx_send_failed++;
1444 ret = NETDEV_TX_OK;
1445 goto out;
1446 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001447 if (skb_is_gso(skb))
1448 tx_pool = &adapter->tso_pool[queue_num];
1449 else
1450 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001451
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001452 tx_scrq = adapter->tx_scrq[queue_num];
1453 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1454 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1455 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1456
Thomas Falcon032c5e82015-12-21 11:26:06 -06001457 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001458
Thomas Falcon86b61a52018-03-16 20:00:29 -05001459 if (index == IBMVNIC_INVALID_MAP) {
1460 dev_kfree_skb_any(skb);
1461 tx_send_failed++;
1462 tx_dropped++;
1463 ret = NETDEV_TX_OK;
1464 goto out;
1465 }
1466
1467 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1468
Thomas Falcon06b3e352018-03-16 20:00:28 -05001469 offset = index * tx_pool->buf_size;
1470 dst = tx_pool->long_term_buff.buff + offset;
1471 memset(dst, 0, tx_pool->buf_size);
1472 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001473
Thomas Falcon15482052017-10-17 12:36:54 -05001474 if (skb_shinfo(skb)->nr_frags) {
1475 int cur, i;
1476
1477 /* Copy the head */
1478 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1479 cur = skb_headlen(skb);
1480
1481 /* Copy the frags */
1482 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1483 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1484
1485 memcpy(dst + cur,
1486 page_address(skb_frag_page(frag)) +
1487 frag->page_offset, skb_frag_size(frag));
1488 cur += skb_frag_size(frag);
1489 }
1490 } else {
1491 skb_copy_from_linear_data(skb, dst, skb->len);
1492 }
1493
Thomas Falcon032c5e82015-12-21 11:26:06 -06001494 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001495 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001496
1497 tx_buff = &tx_pool->tx_buff[index];
1498 tx_buff->skb = skb;
1499 tx_buff->data_dma[0] = data_dma_addr;
1500 tx_buff->data_len[0] = skb->len;
1501 tx_buff->index = index;
1502 tx_buff->pool_index = queue_num;
1503 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001504
1505 memset(&tx_crq, 0, sizeof(tx_crq));
1506 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1507 tx_crq.v1.type = IBMVNIC_TX_DESC;
1508 tx_crq.v1.n_crq_elem = 1;
1509 tx_crq.v1.n_sge = 1;
1510 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001511
Thomas Falconfdb06102017-10-17 12:36:55 -05001512 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001513 tx_crq.v1.correlator =
1514 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001515 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001516 tx_crq.v1.correlator = cpu_to_be32(index);
1517 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001518 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1519 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1520
Michał Mirosławe84b4792018-11-07 17:50:52 +01001521 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001522 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1523 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1524 }
1525
1526 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001527 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1528 proto = ip_hdr(skb)->protocol;
1529 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1530 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1531 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001532 }
1533
Thomas Falcona0dca102018-01-18 19:29:48 -06001534 if (proto == IPPROTO_TCP)
1535 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1536 else if (proto == IPPROTO_UDP)
1537 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1538
Thomas Falconad7775d2016-04-01 17:20:34 -05001539 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001540 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001541 hdrs += 2;
1542 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001543 if (skb_is_gso(skb)) {
1544 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1545 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1546 hdrs += 2;
1547 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001548 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001549 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001550 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1551 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001552 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001553 tx_buff->indir_arr[0] = tx_crq;
1554 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1555 sizeof(tx_buff->indir_arr),
1556 DMA_TO_DEVICE);
1557 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001558 dev_kfree_skb_any(skb);
1559 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001560 if (!firmware_has_feature(FW_FEATURE_CMO))
1561 dev_err(dev, "tx: unable to map descriptor array\n");
1562 tx_map_failed++;
1563 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001564 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001565 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001566 }
John Allen498cd8e2016-04-06 11:49:55 -05001567 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001568 (u64)tx_buff->indir_dma,
1569 (u64)num_entries);
1570 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001571 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001572 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1573 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001574 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001575 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001576 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1577 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001578 dev_kfree_skb_any(skb);
1579 tx_buff->skb = NULL;
1580
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001581 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1582 /* Disable TX and report carrier off if queue is closed
1583 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001584 * Firmware guarantees that a signal will be sent to the
1585 * driver, triggering a reset or some other action.
1586 */
1587 netif_tx_stop_all_queues(netdev);
1588 netif_carrier_off(netdev);
1589 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001590
Thomas Falcon032c5e82015-12-21 11:26:06 -06001591 tx_send_failed++;
1592 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001593 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001594 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001595 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001596
Thomas Falconffc385b2018-02-18 10:08:41 -06001597 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001598 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001599 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001600 netif_stop_subqueue(netdev, queue_num);
1601 }
1602
Thomas Falcon032c5e82015-12-21 11:26:06 -06001603 tx_packets++;
1604 tx_bytes += skb->len;
1605 txq->trans_start = jiffies;
1606 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001607 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001608
Thomas Falcon86b61a52018-03-16 20:00:29 -05001609tx_err_out:
1610 /* roll back consumer index and map array*/
1611 if (tx_pool->consumer_index == 0)
1612 tx_pool->consumer_index =
1613 tx_pool->num_buffers - 1;
1614 else
1615 tx_pool->consumer_index--;
1616 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001617out:
1618 netdev->stats.tx_dropped += tx_dropped;
1619 netdev->stats.tx_bytes += tx_bytes;
1620 netdev->stats.tx_packets += tx_packets;
1621 adapter->tx_send_failed += tx_send_failed;
1622 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001623 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1624 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1625 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001626
1627 return ret;
1628}
1629
1630static void ibmvnic_set_multi(struct net_device *netdev)
1631{
1632 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1633 struct netdev_hw_addr *ha;
1634 union ibmvnic_crq crq;
1635
1636 memset(&crq, 0, sizeof(crq));
1637 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1638 crq.request_capability.cmd = REQUEST_CAPABILITY;
1639
1640 if (netdev->flags & IFF_PROMISC) {
1641 if (!adapter->promisc_supported)
1642 return;
1643 } else {
1644 if (netdev->flags & IFF_ALLMULTI) {
1645 /* Accept all multicast */
1646 memset(&crq, 0, sizeof(crq));
1647 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1648 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1649 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1650 ibmvnic_send_crq(adapter, &crq);
1651 } else if (netdev_mc_empty(netdev)) {
1652 /* Reject all multicast */
1653 memset(&crq, 0, sizeof(crq));
1654 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1655 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1656 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1657 ibmvnic_send_crq(adapter, &crq);
1658 } else {
1659 /* Accept one or more multicast(s) */
1660 netdev_for_each_mc_addr(ha, netdev) {
1661 memset(&crq, 0, sizeof(crq));
1662 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1663 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1664 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1665 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1666 ha->addr);
1667 ibmvnic_send_crq(adapter, &crq);
1668 }
1669 }
1670 }
1671}
1672
Thomas Falcon62740e92019-05-09 23:13:43 -05001673static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001674{
1675 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001676 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001677 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001678
Thomas Falcon62740e92019-05-09 23:13:43 -05001679 if (!is_valid_ether_addr(dev_addr)) {
1680 rc = -EADDRNOTAVAIL;
1681 goto err;
1682 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001683
1684 memset(&crq, 0, sizeof(crq));
1685 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1686 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001687 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001688
1689 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001690 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001691 if (rc) {
1692 rc = -EIO;
1693 goto err;
1694 }
1695
Thomas Falconf8136142018-01-29 13:45:05 -06001696 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001697 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon62740e92019-05-09 23:13:43 -05001698 if (adapter->fw_done_rc) {
1699 rc = -EIO;
1700 goto err;
1701 }
1702
1703 return 0;
1704err:
1705 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1706 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001707}
1708
John Allenc26eba02017-10-26 16:23:25 -05001709static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1710{
1711 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1712 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001713 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001714
Thomas Falcon62740e92019-05-09 23:13:43 -05001715 rc = 0;
1716 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1717 if (adapter->state != VNIC_PROBED)
1718 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001719
Thomas Falconf8136142018-01-29 13:45:05 -06001720 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001721}
1722
Nathan Fontenoted651a12017-05-03 14:04:38 -04001723/**
1724 * do_reset returns zero if we are able to keep processing reset events, or
1725 * non-zero if we hit a fatal error and must halt.
1726 */
1727static int do_reset(struct ibmvnic_adapter *adapter,
1728 struct ibmvnic_rwi *rwi, u32 reset_state)
1729{
John Allen896d8692018-01-18 16:26:31 -06001730 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001731 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001732 struct net_device *netdev = adapter->netdev;
1733 int i, rc;
1734
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001735 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1736 rwi->reset_reason);
1737
Nathan Fontenoted651a12017-05-03 14:04:38 -04001738 netif_carrier_off(netdev);
1739 adapter->reset_reason = rwi->reset_reason;
1740
John Allen896d8692018-01-18 16:26:31 -06001741 old_num_rx_queues = adapter->req_rx_queues;
1742 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001743 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1744 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001745
Nathan Fontenot30f79622018-04-06 18:37:06 -05001746 ibmvnic_cleanup(netdev);
1747
Thomas Falcon1f946082019-06-07 16:03:53 -05001748 if (reset_state == VNIC_OPEN &&
1749 adapter->reset_reason != VNIC_RESET_MOBILITY &&
Nathan Fontenot30f79622018-04-06 18:37:06 -05001750 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001751 rc = __ibmvnic_close(netdev);
1752 if (rc)
1753 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001754 }
1755
John Allenc26eba02017-10-26 16:23:25 -05001756 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1757 adapter->wait_for_reset) {
1758 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001759 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001760 release_crq_queue(adapter);
1761 }
1762
John Allen8cb31cf2017-05-26 10:30:37 -04001763 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1764 /* remove the closed state so when we call open it appears
1765 * we are coming from the probed state.
1766 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001767 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001768
Nathan Fontenot30f79622018-04-06 18:37:06 -05001769 if (adapter->wait_for_reset) {
1770 rc = init_crq_queue(adapter);
1771 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1772 rc = ibmvnic_reenable_crq_queue(adapter);
1773 release_sub_crqs(adapter, 1);
1774 } else {
1775 rc = ibmvnic_reset_crq(adapter);
1776 if (!rc)
1777 rc = vio_enable_interrupts(adapter->vdev);
1778 }
1779
1780 if (rc) {
1781 netdev_err(adapter->netdev,
1782 "Couldn't initialize crq. rc=%d\n", rc);
1783 return rc;
1784 }
1785
Thomas Falcon8a348452018-05-23 13:38:00 -05001786 rc = ibmvnic_reset_init(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001787 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001788 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001789
1790 /* If the adapter was in PROBE state prior to the reset,
1791 * exit here.
1792 */
1793 if (reset_state == VNIC_PROBED)
1794 return 0;
1795
1796 rc = ibmvnic_login(netdev);
1797 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05001798 adapter->state = reset_state;
1799 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001800 }
1801
John Allenc26eba02017-10-26 16:23:25 -05001802 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1803 adapter->wait_for_reset) {
1804 rc = init_resources(adapter);
1805 if (rc)
1806 return rc;
John Allen896d8692018-01-18 16:26:31 -06001807 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001808 adapter->req_tx_queues != old_num_tx_queues ||
1809 adapter->req_rx_add_entries_per_subcrq !=
1810 old_num_rx_slots ||
1811 adapter->req_tx_entries_per_subcrq !=
1812 old_num_tx_slots) {
John Allen896d8692018-01-18 16:26:31 -06001813 release_rx_pools(adapter);
1814 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06001815 release_napi(adapter);
1816 release_vpd_data(adapter);
1817
1818 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05001819 if (rc)
1820 return rc;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001821
John Allenc26eba02017-10-26 16:23:25 -05001822 } else {
1823 rc = reset_tx_pools(adapter);
1824 if (rc)
1825 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001826
John Allenc26eba02017-10-26 16:23:25 -05001827 rc = reset_rx_pools(adapter);
1828 if (rc)
1829 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001830 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05001831 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001832 }
John Allene676d812018-03-14 10:41:29 -05001833 adapter->state = VNIC_CLOSED;
1834
1835 if (reset_state == VNIC_CLOSED)
1836 return 0;
1837
Nathan Fontenoted651a12017-05-03 14:04:38 -04001838 rc = __ibmvnic_open(netdev);
1839 if (rc) {
1840 if (list_empty(&adapter->rwi_list))
1841 adapter->state = VNIC_CLOSED;
1842 else
1843 adapter->state = reset_state;
1844
1845 return 0;
1846 }
1847
Thomas Falconbe32a242019-06-07 16:03:54 -05001848 /* refresh device's multicast list */
1849 ibmvnic_set_multi(netdev);
1850
Nathan Fontenoted651a12017-05-03 14:04:38 -04001851 /* kick napi */
1852 for (i = 0; i < adapter->req_rx_queues; i++)
1853 napi_schedule(&adapter->napi[i]);
1854
Nathan Fontenotebc701b72018-04-11 10:09:38 -05001855 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1856 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
Thomas Falcon986103e2018-11-30 10:59:08 -06001857 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001858
Nathan Fontenoted651a12017-05-03 14:04:38 -04001859 return 0;
1860}
1861
Thomas Falcon2770a792018-05-23 13:38:02 -05001862static int do_hard_reset(struct ibmvnic_adapter *adapter,
1863 struct ibmvnic_rwi *rwi, u32 reset_state)
1864{
1865 struct net_device *netdev = adapter->netdev;
1866 int rc;
1867
1868 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
1869 rwi->reset_reason);
1870
1871 netif_carrier_off(netdev);
1872 adapter->reset_reason = rwi->reset_reason;
1873
1874 ibmvnic_cleanup(netdev);
1875 release_resources(adapter);
1876 release_sub_crqs(adapter, 0);
1877 release_crq_queue(adapter);
1878
1879 /* remove the closed state so when we call open it appears
1880 * we are coming from the probed state.
1881 */
1882 adapter->state = VNIC_PROBED;
1883
Thomas Falconbbd669a2019-04-04 18:58:26 -05001884 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05001885 rc = init_crq_queue(adapter);
1886 if (rc) {
1887 netdev_err(adapter->netdev,
1888 "Couldn't initialize crq. rc=%d\n", rc);
1889 return rc;
1890 }
1891
1892 rc = ibmvnic_init(adapter);
1893 if (rc)
1894 return rc;
1895
1896 /* If the adapter was in PROBE state prior to the reset,
1897 * exit here.
1898 */
1899 if (reset_state == VNIC_PROBED)
1900 return 0;
1901
1902 rc = ibmvnic_login(netdev);
1903 if (rc) {
1904 adapter->state = VNIC_PROBED;
1905 return 0;
1906 }
Juliet Kima5681e22018-11-19 15:59:22 -06001907
1908 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05001909 if (rc)
1910 return rc;
1911
1912 ibmvnic_disable_irqs(adapter);
1913 adapter->state = VNIC_CLOSED;
1914
1915 if (reset_state == VNIC_CLOSED)
1916 return 0;
1917
1918 rc = __ibmvnic_open(netdev);
1919 if (rc) {
1920 if (list_empty(&adapter->rwi_list))
1921 adapter->state = VNIC_CLOSED;
1922 else
1923 adapter->state = reset_state;
1924
1925 return 0;
1926 }
1927
Thomas Falcon2770a792018-05-23 13:38:02 -05001928 return 0;
1929}
1930
Nathan Fontenoted651a12017-05-03 14:04:38 -04001931static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1932{
1933 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001934 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001935
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001936 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001937
1938 if (!list_empty(&adapter->rwi_list)) {
1939 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1940 list);
1941 list_del(&rwi->list);
1942 } else {
1943 rwi = NULL;
1944 }
1945
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001946 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001947 return rwi;
1948}
1949
1950static void free_all_rwi(struct ibmvnic_adapter *adapter)
1951{
1952 struct ibmvnic_rwi *rwi;
1953
1954 rwi = get_next_rwi(adapter);
1955 while (rwi) {
1956 kfree(rwi);
1957 rwi = get_next_rwi(adapter);
1958 }
1959}
1960
1961static void __ibmvnic_reset(struct work_struct *work)
1962{
1963 struct ibmvnic_rwi *rwi;
1964 struct ibmvnic_adapter *adapter;
Juliet Kima5681e22018-11-19 15:59:22 -06001965 bool we_lock_rtnl = false;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001966 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001967 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001968
1969 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001970
Juliet Kima5681e22018-11-19 15:59:22 -06001971 /* netif_set_real_num_xx_queues needs to take rtnl lock here
1972 * unless wait_for_reset is set, in which case the rtnl lock
1973 * has already been taken before initializing the reset
1974 */
1975 if (!adapter->wait_for_reset) {
1976 rtnl_lock();
1977 we_lock_rtnl = true;
1978 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001979 reset_state = adapter->state;
1980
1981 rwi = get_next_rwi(adapter);
1982 while (rwi) {
Thomas Falcon2770a792018-05-23 13:38:02 -05001983 if (adapter->force_reset_recovery) {
1984 adapter->force_reset_recovery = false;
1985 rc = do_hard_reset(adapter, rwi, reset_state);
1986 } else {
1987 rc = do_reset(adapter, rwi, reset_state);
1988 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001989 kfree(rwi);
Thomas Falcon2770a792018-05-23 13:38:02 -05001990 if (rc && rc != IBMVNIC_INIT_FAILED &&
1991 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001992 break;
1993
1994 rwi = get_next_rwi(adapter);
1995 }
1996
John Allenc26eba02017-10-26 16:23:25 -05001997 if (adapter->wait_for_reset) {
1998 adapter->wait_for_reset = false;
1999 adapter->reset_done_rc = rc;
2000 complete(&adapter->reset_done);
2001 }
2002
Nathan Fontenoted651a12017-05-03 14:04:38 -04002003 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002004 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002005 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002006 }
2007
2008 adapter->resetting = false;
Juliet Kima5681e22018-11-19 15:59:22 -06002009 if (we_lock_rtnl)
2010 rtnl_unlock();
Nathan Fontenoted651a12017-05-03 14:04:38 -04002011}
2012
Thomas Falconaf894d22018-04-06 18:37:04 -05002013static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2014 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002015{
Thomas Falcon2770a792018-05-23 13:38:02 -05002016 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002017 struct ibmvnic_rwi *rwi, *tmp;
2018 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002019 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002020 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002021
2022 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002023 adapter->state == VNIC_REMOVED ||
2024 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002025 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002026 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002027 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002028 }
2029
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002030 if (adapter->state == VNIC_PROBING) {
2031 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002032 ret = adapter->init_done_rc = EAGAIN;
2033 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002034 }
2035
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002036 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002037
2038 list_for_each(entry, &adapter->rwi_list) {
2039 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2040 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002041 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002042 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002043 ret = EBUSY;
2044 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002045 }
2046 }
2047
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002048 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002049 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002050 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002051 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002052 ret = ENOMEM;
2053 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002054 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002055 /* if we just received a transport event,
2056 * flush reset queue and process this reset
2057 */
2058 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2059 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2060 list_del(entry);
2061 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002062 rwi->reset_reason = reason;
2063 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002064 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falcon06e43d72018-05-23 13:38:01 -05002065 adapter->resetting = true;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002066 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002067 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002068
2069 return 0;
2070err:
2071 if (adapter->wait_for_reset)
2072 adapter->wait_for_reset = false;
2073 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002074}
2075
Thomas Falcon032c5e82015-12-21 11:26:06 -06002076static void ibmvnic_tx_timeout(struct net_device *dev)
2077{
2078 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002079
Nathan Fontenoted651a12017-05-03 14:04:38 -04002080 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002081}
2082
2083static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2084 struct ibmvnic_rx_buff *rx_buff)
2085{
2086 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2087
2088 rx_buff->skb = NULL;
2089
2090 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2091 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2092
2093 atomic_dec(&pool->available);
2094}
2095
2096static int ibmvnic_poll(struct napi_struct *napi, int budget)
2097{
2098 struct net_device *netdev = napi->dev;
2099 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2100 int scrq_num = (int)(napi - adapter->napi);
2101 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002102
Thomas Falcon032c5e82015-12-21 11:26:06 -06002103restart_poll:
2104 while (frames_processed < budget) {
2105 struct sk_buff *skb;
2106 struct ibmvnic_rx_buff *rx_buff;
2107 union sub_crq *next;
2108 u32 length;
2109 u16 offset;
2110 u8 flags = 0;
2111
John Allen34686562018-02-06 16:21:49 -06002112 if (unlikely(adapter->resetting &&
2113 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002114 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2115 napi_complete_done(napi, frames_processed);
2116 return frames_processed;
2117 }
2118
Thomas Falcon032c5e82015-12-21 11:26:06 -06002119 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2120 break;
2121 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2122 rx_buff =
2123 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2124 rx_comp.correlator);
2125 /* do error checking */
2126 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002127 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2128 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002129 /* free the entry */
2130 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002131 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002132 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002133 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002134 } else if (!rx_buff->skb) {
2135 /* free the entry */
2136 next->rx_comp.first = 0;
2137 remove_buff_from_pool(adapter, rx_buff);
2138 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002139 }
2140
2141 length = be32_to_cpu(next->rx_comp.len);
2142 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2143 flags = next->rx_comp.flags;
2144 skb = rx_buff->skb;
2145 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2146 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002147
2148 /* VLAN Header has been stripped by the system firmware and
2149 * needs to be inserted by the driver
2150 */
2151 if (adapter->rx_vlan_header_insertion &&
2152 (flags & IBMVNIC_VLAN_STRIPPED))
2153 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2154 ntohs(next->rx_comp.vlan_tci));
2155
Thomas Falcon032c5e82015-12-21 11:26:06 -06002156 /* free the entry */
2157 next->rx_comp.first = 0;
2158 remove_buff_from_pool(adapter, rx_buff);
2159
2160 skb_put(skb, length);
2161 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002162 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002163
2164 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2165 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2166 skb->ip_summed = CHECKSUM_UNNECESSARY;
2167 }
2168
2169 length = skb->len;
2170 napi_gro_receive(napi, skb); /* send it up */
2171 netdev->stats.rx_packets++;
2172 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002173 adapter->rx_stats_buffers[scrq_num].packets++;
2174 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002175 frames_processed++;
2176 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002177
2178 if (adapter->state != VNIC_CLOSING)
2179 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002180
2181 if (frames_processed < budget) {
2182 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002183 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002184 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2185 napi_reschedule(napi)) {
2186 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2187 goto restart_poll;
2188 }
2189 }
2190 return frames_processed;
2191}
2192
John Allenc26eba02017-10-26 16:23:25 -05002193static int wait_for_reset(struct ibmvnic_adapter *adapter)
2194{
Thomas Falconaf894d22018-04-06 18:37:04 -05002195 int rc, ret;
2196
John Allenc26eba02017-10-26 16:23:25 -05002197 adapter->fallback.mtu = adapter->req_mtu;
2198 adapter->fallback.rx_queues = adapter->req_rx_queues;
2199 adapter->fallback.tx_queues = adapter->req_tx_queues;
2200 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2201 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2202
2203 init_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002204 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002205 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2206 if (rc)
2207 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002208 wait_for_completion(&adapter->reset_done);
2209
Thomas Falconaf894d22018-04-06 18:37:04 -05002210 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002211 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002212 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002213 adapter->desired.mtu = adapter->fallback.mtu;
2214 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2215 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2216 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2217 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2218
2219 init_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002220 adapter->wait_for_reset = true;
2221 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2222 if (rc)
2223 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002224 wait_for_completion(&adapter->reset_done);
2225 }
2226 adapter->wait_for_reset = false;
2227
Thomas Falconaf894d22018-04-06 18:37:04 -05002228 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002229}
2230
John Allen3a807b72017-06-06 16:55:52 -05002231static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2232{
John Allenc26eba02017-10-26 16:23:25 -05002233 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2234
2235 adapter->desired.mtu = new_mtu + ETH_HLEN;
2236
2237 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002238}
2239
Thomas Falconf10b09e2018-03-12 11:51:05 -05002240static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2241 struct net_device *dev,
2242 netdev_features_t features)
2243{
2244 /* Some backing hardware adapters can not
2245 * handle packets with a MSS less than 224
2246 * or with only one segment.
2247 */
2248 if (skb_is_gso(skb)) {
2249 if (skb_shinfo(skb)->gso_size < 224 ||
2250 skb_shinfo(skb)->gso_segs == 1)
2251 features &= ~NETIF_F_GSO_MASK;
2252 }
2253
2254 return features;
2255}
2256
Thomas Falcon032c5e82015-12-21 11:26:06 -06002257static const struct net_device_ops ibmvnic_netdev_ops = {
2258 .ndo_open = ibmvnic_open,
2259 .ndo_stop = ibmvnic_close,
2260 .ndo_start_xmit = ibmvnic_xmit,
2261 .ndo_set_rx_mode = ibmvnic_set_multi,
2262 .ndo_set_mac_address = ibmvnic_set_mac,
2263 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002264 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002265 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002266 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267};
2268
2269/* ethtool functions */
2270
Philippe Reynes8a433792017-01-07 22:37:29 +01002271static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2272 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002273{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002274 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2275 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002276
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002277 rc = send_query_phys_parms(adapter);
2278 if (rc) {
2279 adapter->speed = SPEED_UNKNOWN;
2280 adapter->duplex = DUPLEX_UNKNOWN;
2281 }
2282 cmd->base.speed = adapter->speed;
2283 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002284 cmd->base.port = PORT_FIBRE;
2285 cmd->base.phy_address = 0;
2286 cmd->base.autoneg = AUTONEG_ENABLE;
2287
Thomas Falcon032c5e82015-12-21 11:26:06 -06002288 return 0;
2289}
2290
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002291static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002292 struct ethtool_drvinfo *info)
2293{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002294 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2295
Thomas Falcon032c5e82015-12-21 11:26:06 -06002296 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2297 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002298 strlcpy(info->fw_version, adapter->fw_version,
2299 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002300}
2301
2302static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2303{
2304 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2305
2306 return adapter->msg_enable;
2307}
2308
2309static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2310{
2311 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2312
2313 adapter->msg_enable = data;
2314}
2315
2316static u32 ibmvnic_get_link(struct net_device *netdev)
2317{
2318 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2319
2320 /* Don't need to send a query because we request a logical link up at
2321 * init and then we wait for link state indications
2322 */
2323 return adapter->logical_link_state;
2324}
2325
2326static void ibmvnic_get_ringparam(struct net_device *netdev,
2327 struct ethtool_ringparam *ring)
2328{
John Allenbc131b32017-08-02 16:46:30 -05002329 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2330
Thomas Falcon723ad912018-09-28 18:38:26 -05002331 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2332 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2333 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2334 } else {
2335 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2336 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2337 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338 ring->rx_mini_max_pending = 0;
2339 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002340 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2341 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002342 ring->rx_mini_pending = 0;
2343 ring->rx_jumbo_pending = 0;
2344}
2345
John Allenc26eba02017-10-26 16:23:25 -05002346static int ibmvnic_set_ringparam(struct net_device *netdev,
2347 struct ethtool_ringparam *ring)
2348{
2349 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002350 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002351
Thomas Falcon723ad912018-09-28 18:38:26 -05002352 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002353 adapter->desired.rx_entries = ring->rx_pending;
2354 adapter->desired.tx_entries = ring->tx_pending;
2355
Thomas Falcon723ad912018-09-28 18:38:26 -05002356 ret = wait_for_reset(adapter);
2357
2358 if (!ret &&
2359 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2360 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2361 netdev_info(netdev,
2362 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2363 ring->rx_pending, ring->tx_pending,
2364 adapter->req_rx_add_entries_per_subcrq,
2365 adapter->req_tx_entries_per_subcrq);
2366 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002367}
2368
John Allenc2dbeb62017-08-02 16:47:17 -05002369static void ibmvnic_get_channels(struct net_device *netdev,
2370 struct ethtool_channels *channels)
2371{
2372 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2373
Thomas Falcon723ad912018-09-28 18:38:26 -05002374 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2375 channels->max_rx = adapter->max_rx_queues;
2376 channels->max_tx = adapter->max_tx_queues;
2377 } else {
2378 channels->max_rx = IBMVNIC_MAX_QUEUES;
2379 channels->max_tx = IBMVNIC_MAX_QUEUES;
2380 }
2381
John Allenc2dbeb62017-08-02 16:47:17 -05002382 channels->max_other = 0;
2383 channels->max_combined = 0;
2384 channels->rx_count = adapter->req_rx_queues;
2385 channels->tx_count = adapter->req_tx_queues;
2386 channels->other_count = 0;
2387 channels->combined_count = 0;
2388}
2389
John Allenc26eba02017-10-26 16:23:25 -05002390static int ibmvnic_set_channels(struct net_device *netdev,
2391 struct ethtool_channels *channels)
2392{
2393 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002394 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002395
Thomas Falcon723ad912018-09-28 18:38:26 -05002396 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002397 adapter->desired.rx_queues = channels->rx_count;
2398 adapter->desired.tx_queues = channels->tx_count;
2399
Thomas Falcon723ad912018-09-28 18:38:26 -05002400 ret = wait_for_reset(adapter);
2401
2402 if (!ret &&
2403 (adapter->req_rx_queues != channels->rx_count ||
2404 adapter->req_tx_queues != channels->tx_count))
2405 netdev_info(netdev,
2406 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2407 channels->rx_count, channels->tx_count,
2408 adapter->req_rx_queues, adapter->req_tx_queues);
2409 return ret;
2410
John Allenc26eba02017-10-26 16:23:25 -05002411}
2412
Thomas Falcon032c5e82015-12-21 11:26:06 -06002413static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2414{
John Allen3d52b592017-08-02 16:44:14 -05002415 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 int i;
2417
Thomas Falcon723ad912018-09-28 18:38:26 -05002418 switch (stringset) {
2419 case ETH_SS_STATS:
2420 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2421 i++, data += ETH_GSTRING_LEN)
2422 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2423
2424 for (i = 0; i < adapter->req_tx_queues; i++) {
2425 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2426 data += ETH_GSTRING_LEN;
2427
2428 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2429 data += ETH_GSTRING_LEN;
2430
2431 snprintf(data, ETH_GSTRING_LEN,
2432 "tx%d_dropped_packets", i);
2433 data += ETH_GSTRING_LEN;
2434 }
2435
2436 for (i = 0; i < adapter->req_rx_queues; i++) {
2437 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2438 data += ETH_GSTRING_LEN;
2439
2440 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2441 data += ETH_GSTRING_LEN;
2442
2443 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2444 data += ETH_GSTRING_LEN;
2445 }
2446 break;
2447
2448 case ETH_SS_PRIV_FLAGS:
2449 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2450 strcpy(data + i * ETH_GSTRING_LEN,
2451 ibmvnic_priv_flags[i]);
2452 break;
2453 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002454 return;
John Allen3d52b592017-08-02 16:44:14 -05002455 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002456}
2457
2458static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2459{
John Allen3d52b592017-08-02 16:44:14 -05002460 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2461
Thomas Falcon032c5e82015-12-21 11:26:06 -06002462 switch (sset) {
2463 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002464 return ARRAY_SIZE(ibmvnic_stats) +
2465 adapter->req_tx_queues * NUM_TX_STATS +
2466 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002467 case ETH_SS_PRIV_FLAGS:
2468 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002469 default:
2470 return -EOPNOTSUPP;
2471 }
2472}
2473
2474static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2475 struct ethtool_stats *stats, u64 *data)
2476{
2477 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2478 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002479 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002480 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002481
2482 memset(&crq, 0, sizeof(crq));
2483 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2484 crq.request_statistics.cmd = REQUEST_STATISTICS;
2485 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2486 crq.request_statistics.len =
2487 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002488
2489 /* Wait for data to be written */
2490 init_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002491 rc = ibmvnic_send_crq(adapter, &crq);
2492 if (rc)
2493 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002494 wait_for_completion(&adapter->stats_done);
2495
2496 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002497 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2498 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002499
2500 for (j = 0; j < adapter->req_tx_queues; j++) {
2501 data[i] = adapter->tx_stats_buffers[j].packets;
2502 i++;
2503 data[i] = adapter->tx_stats_buffers[j].bytes;
2504 i++;
2505 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2506 i++;
2507 }
2508
2509 for (j = 0; j < adapter->req_rx_queues; j++) {
2510 data[i] = adapter->rx_stats_buffers[j].packets;
2511 i++;
2512 data[i] = adapter->rx_stats_buffers[j].bytes;
2513 i++;
2514 data[i] = adapter->rx_stats_buffers[j].interrupts;
2515 i++;
2516 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002517}
2518
Thomas Falcon723ad912018-09-28 18:38:26 -05002519static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2520{
2521 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2522
2523 return adapter->priv_flags;
2524}
2525
2526static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2527{
2528 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2529 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2530
2531 if (which_maxes)
2532 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2533 else
2534 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2535
2536 return 0;
2537}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002538static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539 .get_drvinfo = ibmvnic_get_drvinfo,
2540 .get_msglevel = ibmvnic_get_msglevel,
2541 .set_msglevel = ibmvnic_set_msglevel,
2542 .get_link = ibmvnic_get_link,
2543 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002544 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002545 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002546 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002547 .get_strings = ibmvnic_get_strings,
2548 .get_sset_count = ibmvnic_get_sset_count,
2549 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002550 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002551 .get_priv_flags = ibmvnic_get_priv_flags,
2552 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002553};
2554
2555/* Routines for managing CRQs/sCRQs */
2556
Nathan Fontenot57a49432017-05-26 10:31:12 -04002557static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2558 struct ibmvnic_sub_crq_queue *scrq)
2559{
2560 int rc;
2561
2562 if (scrq->irq) {
2563 free_irq(scrq->irq, scrq);
2564 irq_dispose_mapping(scrq->irq);
2565 scrq->irq = 0;
2566 }
2567
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002568 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002569 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002570 scrq->cur = 0;
2571
2572 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2573 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2574 return rc;
2575}
2576
2577static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2578{
2579 int i, rc;
2580
2581 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002582 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002583 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2584 if (rc)
2585 return rc;
2586 }
2587
2588 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002589 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002590 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2591 if (rc)
2592 return rc;
2593 }
2594
Nathan Fontenot57a49432017-05-26 10:31:12 -04002595 return rc;
2596}
2597
Thomas Falcon032c5e82015-12-21 11:26:06 -06002598static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002599 struct ibmvnic_sub_crq_queue *scrq,
2600 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002601{
2602 struct device *dev = &adapter->vdev->dev;
2603 long rc;
2604
2605 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2606
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002607 if (do_h_free) {
2608 /* Close the sub-crqs */
2609 do {
2610 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2611 adapter->vdev->unit_address,
2612 scrq->crq_num);
2613 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002615 if (rc) {
2616 netdev_err(adapter->netdev,
2617 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2618 scrq->crq_num, rc);
2619 }
Thomas Falconffa73852017-04-19 13:44:29 -04002620 }
2621
Thomas Falcon032c5e82015-12-21 11:26:06 -06002622 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2623 DMA_BIDIRECTIONAL);
2624 free_pages((unsigned long)scrq->msgs, 2);
2625 kfree(scrq);
2626}
2627
2628static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2629 *adapter)
2630{
2631 struct device *dev = &adapter->vdev->dev;
2632 struct ibmvnic_sub_crq_queue *scrq;
2633 int rc;
2634
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002635 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002636 if (!scrq)
2637 return NULL;
2638
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002639 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002640 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002641 if (!scrq->msgs) {
2642 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2643 goto zero_page_failed;
2644 }
2645
2646 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2647 DMA_BIDIRECTIONAL);
2648 if (dma_mapping_error(dev, scrq->msg_token)) {
2649 dev_warn(dev, "Couldn't map crq queue messages page\n");
2650 goto map_failed;
2651 }
2652
2653 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2654 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2655
2656 if (rc == H_RESOURCE)
2657 rc = ibmvnic_reset_crq(adapter);
2658
2659 if (rc == H_CLOSED) {
2660 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2661 } else if (rc) {
2662 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2663 goto reg_failed;
2664 }
2665
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 scrq->adapter = adapter;
2667 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668 spin_lock_init(&scrq->lock);
2669
2670 netdev_dbg(adapter->netdev,
2671 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2672 scrq->crq_num, scrq->hw_irq, scrq->irq);
2673
2674 return scrq;
2675
Thomas Falcon032c5e82015-12-21 11:26:06 -06002676reg_failed:
2677 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2678 DMA_BIDIRECTIONAL);
2679map_failed:
2680 free_pages((unsigned long)scrq->msgs, 2);
2681zero_page_failed:
2682 kfree(scrq);
2683
2684 return NULL;
2685}
2686
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002687static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002688{
2689 int i;
2690
2691 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002692 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002693 if (!adapter->tx_scrq[i])
2694 continue;
2695
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002696 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2697 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002698 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002699 free_irq(adapter->tx_scrq[i]->irq,
2700 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002701 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002702 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002704
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002705 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2706 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002707 }
2708
Nathan Fontenot9501df32017-03-15 23:38:07 -04002709 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002710 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002711 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 }
2713
2714 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002715 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002716 if (!adapter->rx_scrq[i])
2717 continue;
2718
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002719 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2720 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002721 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002722 free_irq(adapter->rx_scrq[i]->irq,
2723 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002724 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002725 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002726 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002727
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002728 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2729 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002730 }
2731
Nathan Fontenot9501df32017-03-15 23:38:07 -04002732 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002733 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002734 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002735 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002736}
2737
2738static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2739 struct ibmvnic_sub_crq_queue *scrq)
2740{
2741 struct device *dev = &adapter->vdev->dev;
2742 unsigned long rc;
2743
2744 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2745 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2746 if (rc)
2747 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2748 scrq->hw_irq, rc);
2749 return rc;
2750}
2751
2752static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2753 struct ibmvnic_sub_crq_queue *scrq)
2754{
2755 struct device *dev = &adapter->vdev->dev;
2756 unsigned long rc;
2757
2758 if (scrq->hw_irq > 0x100000000ULL) {
2759 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2760 return 1;
2761 }
2762
Nathan Fontenot73f9d362018-05-22 11:21:10 -05002763 if (adapter->resetting &&
2764 adapter->reset_reason == VNIC_RESET_MOBILITY) {
2765 u64 val = (0xff000000) | scrq->hw_irq;
2766
2767 rc = plpar_hcall_norets(H_EOI, val);
2768 if (rc)
2769 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2770 val, rc);
2771 }
Thomas Falconf23e0642018-04-15 18:53:36 -05002772
Thomas Falcon032c5e82015-12-21 11:26:06 -06002773 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2774 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2775 if (rc)
2776 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2777 scrq->hw_irq, rc);
2778 return rc;
2779}
2780
2781static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2782 struct ibmvnic_sub_crq_queue *scrq)
2783{
2784 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05002785 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002786 struct ibmvnic_tx_buff *txbuff;
2787 union sub_crq *next;
2788 int index;
2789 int i, j;
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002790 u8 *first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002791
2792restart_loop:
2793 while (pending_scrq(adapter, scrq)) {
2794 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002795 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002796
2797 next = ibmvnic_next_scrq(adapter, scrq);
2798 for (i = 0; i < next->tx_comp.num_comps; i++) {
2799 if (next->tx_comp.rcs[i]) {
2800 dev_err(dev, "tx error %x\n",
2801 next->tx_comp.rcs[i]);
2802 continue;
2803 }
2804 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05002805 if (index & IBMVNIC_TSO_POOL_MASK) {
2806 tx_pool = &adapter->tso_pool[pool];
2807 index &= ~IBMVNIC_TSO_POOL_MASK;
2808 } else {
2809 tx_pool = &adapter->tx_pool[pool];
2810 }
2811
2812 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06002813
2814 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2815 if (!txbuff->data_dma[j])
2816 continue;
2817
2818 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002820 /* if sub_crq was sent indirectly */
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002821 first = &txbuff->indir_arr[0].generic.first;
2822 if (*first == IBMVNIC_CRQ_CMD) {
Thomas Falconad7775d2016-04-01 17:20:34 -05002823 dma_unmap_single(dev, txbuff->indir_dma,
2824 sizeof(txbuff->indir_arr),
2825 DMA_TO_DEVICE);
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002826 *first = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05002827 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002828
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002829 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002830 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002831 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002832 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002833
Thomas Falconffc385b2018-02-18 10:08:41 -06002834 num_entries += txbuff->num_entries;
2835
Thomas Falcon06b3e352018-03-16 20:00:28 -05002836 tx_pool->free_map[tx_pool->producer_index] = index;
2837 tx_pool->producer_index =
2838 (tx_pool->producer_index + 1) %
2839 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002840 }
2841 /* remove tx_comp scrq*/
2842 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002843
Thomas Falconffc385b2018-02-18 10:08:41 -06002844 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002845 (adapter->req_tx_entries_per_subcrq / 2) &&
2846 __netif_subqueue_stopped(adapter->netdev,
2847 scrq->pool_index)) {
2848 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002849 netdev_dbg(adapter->netdev, "Started queue %d\n",
2850 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002851 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002852 }
2853
2854 enable_scrq_irq(adapter, scrq);
2855
2856 if (pending_scrq(adapter, scrq)) {
2857 disable_scrq_irq(adapter, scrq);
2858 goto restart_loop;
2859 }
2860
2861 return 0;
2862}
2863
2864static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2865{
2866 struct ibmvnic_sub_crq_queue *scrq = instance;
2867 struct ibmvnic_adapter *adapter = scrq->adapter;
2868
2869 disable_scrq_irq(adapter, scrq);
2870 ibmvnic_complete_tx(adapter, scrq);
2871
2872 return IRQ_HANDLED;
2873}
2874
2875static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2876{
2877 struct ibmvnic_sub_crq_queue *scrq = instance;
2878 struct ibmvnic_adapter *adapter = scrq->adapter;
2879
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002880 /* When booting a kdump kernel we can hit pending interrupts
2881 * prior to completing driver initialization.
2882 */
2883 if (unlikely(adapter->state != VNIC_OPEN))
2884 return IRQ_NONE;
2885
John Allen3d52b592017-08-02 16:44:14 -05002886 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2887
Thomas Falcon032c5e82015-12-21 11:26:06 -06002888 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2889 disable_scrq_irq(adapter, scrq);
2890 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2891 }
2892
2893 return IRQ_HANDLED;
2894}
2895
Thomas Falconea22d512016-07-06 15:35:17 -05002896static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2897{
2898 struct device *dev = &adapter->vdev->dev;
2899 struct ibmvnic_sub_crq_queue *scrq;
2900 int i = 0, j = 0;
2901 int rc = 0;
2902
2903 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002904 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2905 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002906 scrq = adapter->tx_scrq[i];
2907 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2908
Michael Ellerman99c17902016-09-10 19:59:05 +10002909 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002910 rc = -EINVAL;
2911 dev_err(dev, "Error mapping irq\n");
2912 goto req_tx_irq_failed;
2913 }
2914
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002915 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
2916 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05002917 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002918 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05002919
2920 if (rc) {
2921 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2922 scrq->irq, rc);
2923 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002924 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002925 }
2926 }
2927
2928 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002929 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2930 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002931 scrq = adapter->rx_scrq[i];
2932 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002933 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002934 rc = -EINVAL;
2935 dev_err(dev, "Error mapping irq\n");
2936 goto req_rx_irq_failed;
2937 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002938 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
2939 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05002940 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002941 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05002942 if (rc) {
2943 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2944 scrq->irq, rc);
2945 irq_dispose_mapping(scrq->irq);
2946 goto req_rx_irq_failed;
2947 }
2948 }
2949 return rc;
2950
2951req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002952 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002953 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2954 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002955 }
Thomas Falconea22d512016-07-06 15:35:17 -05002956 i = adapter->req_tx_queues;
2957req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002958 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002959 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2960 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002961 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002962 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002963 return rc;
2964}
2965
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002966static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002967{
2968 struct device *dev = &adapter->vdev->dev;
2969 struct ibmvnic_sub_crq_queue **allqueues;
2970 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971 int total_queues;
2972 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002973 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002974
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2976
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002977 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002978 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002979 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002980
2981 for (i = 0; i < total_queues; i++) {
2982 allqueues[i] = init_sub_crq_queue(adapter);
2983 if (!allqueues[i]) {
2984 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2985 break;
2986 }
2987 registered_queues++;
2988 }
2989
2990 /* Make sure we were able to register the minimum number of queues */
2991 if (registered_queues <
2992 adapter->min_tx_queues + adapter->min_rx_queues) {
2993 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2994 goto tx_failed;
2995 }
2996
2997 /* Distribute the failed allocated queues*/
2998 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2999 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3000 switch (i % 3) {
3001 case 0:
3002 if (adapter->req_rx_queues > adapter->min_rx_queues)
3003 adapter->req_rx_queues--;
3004 else
3005 more++;
3006 break;
3007 case 1:
3008 if (adapter->req_tx_queues > adapter->min_tx_queues)
3009 adapter->req_tx_queues--;
3010 else
3011 more++;
3012 break;
3013 }
3014 }
3015
3016 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003017 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003018 if (!adapter->tx_scrq)
3019 goto tx_failed;
3020
3021 for (i = 0; i < adapter->req_tx_queues; i++) {
3022 adapter->tx_scrq[i] = allqueues[i];
3023 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003024 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 }
3026
3027 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003028 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003029 if (!adapter->rx_scrq)
3030 goto rx_failed;
3031
3032 for (i = 0; i < adapter->req_rx_queues; i++) {
3033 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3034 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003035 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003036 }
3037
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003038 kfree(allqueues);
3039 return 0;
3040
3041rx_failed:
3042 kfree(adapter->tx_scrq);
3043 adapter->tx_scrq = NULL;
3044tx_failed:
3045 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003046 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003047 kfree(allqueues);
3048 return -1;
3049}
3050
3051static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3052{
3053 struct device *dev = &adapter->vdev->dev;
3054 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003055 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003056
3057 if (!retry) {
3058 /* Sub-CRQ entries are 32 byte long */
3059 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3060
3061 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3062 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3063 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3064 return;
3065 }
3066
John Allenc26eba02017-10-26 16:23:25 -05003067 if (adapter->desired.mtu)
3068 adapter->req_mtu = adapter->desired.mtu;
3069 else
3070 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003071
John Allenc26eba02017-10-26 16:23:25 -05003072 if (!adapter->desired.tx_entries)
3073 adapter->desired.tx_entries =
3074 adapter->max_tx_entries_per_subcrq;
3075 if (!adapter->desired.rx_entries)
3076 adapter->desired.rx_entries =
3077 adapter->max_rx_add_entries_per_subcrq;
3078
3079 max_entries = IBMVNIC_MAX_LTB_SIZE /
3080 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3081
3082 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3083 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3084 adapter->desired.tx_entries = max_entries;
3085 }
3086
3087 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3088 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3089 adapter->desired.rx_entries = max_entries;
3090 }
3091
3092 if (adapter->desired.tx_entries)
3093 adapter->req_tx_entries_per_subcrq =
3094 adapter->desired.tx_entries;
3095 else
3096 adapter->req_tx_entries_per_subcrq =
3097 adapter->max_tx_entries_per_subcrq;
3098
3099 if (adapter->desired.rx_entries)
3100 adapter->req_rx_add_entries_per_subcrq =
3101 adapter->desired.rx_entries;
3102 else
3103 adapter->req_rx_add_entries_per_subcrq =
3104 adapter->max_rx_add_entries_per_subcrq;
3105
3106 if (adapter->desired.tx_queues)
3107 adapter->req_tx_queues =
3108 adapter->desired.tx_queues;
3109 else
3110 adapter->req_tx_queues =
3111 adapter->opt_tx_comp_sub_queues;
3112
3113 if (adapter->desired.rx_queues)
3114 adapter->req_rx_queues =
3115 adapter->desired.rx_queues;
3116 else
3117 adapter->req_rx_queues =
3118 adapter->opt_rx_comp_queues;
3119
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003120 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003121 }
3122
Thomas Falcon032c5e82015-12-21 11:26:06 -06003123 memset(&crq, 0, sizeof(crq));
3124 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3125 crq.request_capability.cmd = REQUEST_CAPABILITY;
3126
3127 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003128 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003129 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003130 ibmvnic_send_crq(adapter, &crq);
3131
3132 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003133 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003134 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135 ibmvnic_send_crq(adapter, &crq);
3136
3137 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003138 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003139 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003140 ibmvnic_send_crq(adapter, &crq);
3141
3142 crq.request_capability.capability =
3143 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3144 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003145 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003146 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147 ibmvnic_send_crq(adapter, &crq);
3148
3149 crq.request_capability.capability =
3150 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3151 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003152 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003153 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003154 ibmvnic_send_crq(adapter, &crq);
3155
3156 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003157 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003158 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159 ibmvnic_send_crq(adapter, &crq);
3160
3161 if (adapter->netdev->flags & IFF_PROMISC) {
3162 if (adapter->promisc_supported) {
3163 crq.request_capability.capability =
3164 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003165 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003166 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003167 ibmvnic_send_crq(adapter, &crq);
3168 }
3169 } else {
3170 crq.request_capability.capability =
3171 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003172 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003173 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003174 ibmvnic_send_crq(adapter, &crq);
3175 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003176}
3177
3178static int pending_scrq(struct ibmvnic_adapter *adapter,
3179 struct ibmvnic_sub_crq_queue *scrq)
3180{
3181 union sub_crq *entry = &scrq->msgs[scrq->cur];
3182
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003183 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003184 return 1;
3185 else
3186 return 0;
3187}
3188
3189static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3190 struct ibmvnic_sub_crq_queue *scrq)
3191{
3192 union sub_crq *entry;
3193 unsigned long flags;
3194
3195 spin_lock_irqsave(&scrq->lock, flags);
3196 entry = &scrq->msgs[scrq->cur];
3197 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3198 if (++scrq->cur == scrq->size)
3199 scrq->cur = 0;
3200 } else {
3201 entry = NULL;
3202 }
3203 spin_unlock_irqrestore(&scrq->lock, flags);
3204
3205 return entry;
3206}
3207
3208static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3209{
3210 struct ibmvnic_crq_queue *queue = &adapter->crq;
3211 union ibmvnic_crq *crq;
3212
3213 crq = &queue->msgs[queue->cur];
3214 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3215 if (++queue->cur == queue->size)
3216 queue->cur = 0;
3217 } else {
3218 crq = NULL;
3219 }
3220
3221 return crq;
3222}
3223
Thomas Falcon2d14d372018-07-13 12:03:32 -05003224static void print_subcrq_error(struct device *dev, int rc, const char *func)
3225{
3226 switch (rc) {
3227 case H_PARAMETER:
3228 dev_warn_ratelimited(dev,
3229 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3230 func, rc);
3231 break;
3232 case H_CLOSED:
3233 dev_warn_ratelimited(dev,
3234 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3235 func, rc);
3236 break;
3237 default:
3238 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3239 break;
3240 }
3241}
3242
Thomas Falcon032c5e82015-12-21 11:26:06 -06003243static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3244 union sub_crq *sub_crq)
3245{
3246 unsigned int ua = adapter->vdev->unit_address;
3247 struct device *dev = &adapter->vdev->dev;
3248 u64 *u64_crq = (u64 *)sub_crq;
3249 int rc;
3250
3251 netdev_dbg(adapter->netdev,
3252 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3253 (unsigned long int)cpu_to_be64(remote_handle),
3254 (unsigned long int)cpu_to_be64(u64_crq[0]),
3255 (unsigned long int)cpu_to_be64(u64_crq[1]),
3256 (unsigned long int)cpu_to_be64(u64_crq[2]),
3257 (unsigned long int)cpu_to_be64(u64_crq[3]));
3258
3259 /* Make sure the hypervisor sees the complete request */
3260 mb();
3261
3262 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3263 cpu_to_be64(remote_handle),
3264 cpu_to_be64(u64_crq[0]),
3265 cpu_to_be64(u64_crq[1]),
3266 cpu_to_be64(u64_crq[2]),
3267 cpu_to_be64(u64_crq[3]));
3268
Thomas Falcon2d14d372018-07-13 12:03:32 -05003269 if (rc)
3270 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003271
3272 return rc;
3273}
3274
Thomas Falconad7775d2016-04-01 17:20:34 -05003275static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3276 u64 remote_handle, u64 ioba, u64 num_entries)
3277{
3278 unsigned int ua = adapter->vdev->unit_address;
3279 struct device *dev = &adapter->vdev->dev;
3280 int rc;
3281
3282 /* Make sure the hypervisor sees the complete request */
3283 mb();
3284 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3285 cpu_to_be64(remote_handle),
3286 ioba, num_entries);
3287
Thomas Falcon2d14d372018-07-13 12:03:32 -05003288 if (rc)
3289 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003290
3291 return rc;
3292}
3293
Thomas Falcon032c5e82015-12-21 11:26:06 -06003294static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3295 union ibmvnic_crq *crq)
3296{
3297 unsigned int ua = adapter->vdev->unit_address;
3298 struct device *dev = &adapter->vdev->dev;
3299 u64 *u64_crq = (u64 *)crq;
3300 int rc;
3301
3302 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3303 (unsigned long int)cpu_to_be64(u64_crq[0]),
3304 (unsigned long int)cpu_to_be64(u64_crq[1]));
3305
Thomas Falcon51536982018-05-23 13:37:56 -05003306 if (!adapter->crq.active &&
3307 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3308 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3309 return -EINVAL;
3310 }
3311
Thomas Falcon032c5e82015-12-21 11:26:06 -06003312 /* Make sure the hypervisor sees the complete request */
3313 mb();
3314
3315 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3316 cpu_to_be64(u64_crq[0]),
3317 cpu_to_be64(u64_crq[1]));
3318
3319 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003320 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003321 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003322 if (adapter->resetting)
3323 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3324 }
3325
Thomas Falcon032c5e82015-12-21 11:26:06 -06003326 dev_warn(dev, "Send error (rc=%d)\n", rc);
3327 }
3328
3329 return rc;
3330}
3331
3332static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3333{
3334 union ibmvnic_crq crq;
3335
3336 memset(&crq, 0, sizeof(crq));
3337 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3338 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3339 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3340
3341 return ibmvnic_send_crq(adapter, &crq);
3342}
3343
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344static int send_version_xchg(struct ibmvnic_adapter *adapter)
3345{
3346 union ibmvnic_crq crq;
3347
3348 memset(&crq, 0, sizeof(crq));
3349 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3350 crq.version_exchange.cmd = VERSION_EXCHANGE;
3351 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3352
3353 return ibmvnic_send_crq(adapter, &crq);
3354}
3355
Nathan Fontenot37798d02017-11-08 11:23:56 -06003356struct vnic_login_client_data {
3357 u8 type;
3358 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003359 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003360} __packed;
3361
3362static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3363{
3364 int len;
3365
3366 /* Calculate the amount of buffer space needed for the
3367 * vnic client data in the login buffer. There are four entries,
3368 * OS name, LPAR name, device name, and a null last entry.
3369 */
3370 len = 4 * sizeof(struct vnic_login_client_data);
3371 len += 6; /* "Linux" plus NULL */
3372 len += strlen(utsname()->nodename) + 1;
3373 len += strlen(adapter->netdev->name) + 1;
3374
3375 return len;
3376}
3377
3378static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3379 struct vnic_login_client_data *vlcd)
3380{
3381 const char *os_name = "Linux";
3382 int len;
3383
3384 /* Type 1 - LPAR OS */
3385 vlcd->type = 1;
3386 len = strlen(os_name) + 1;
3387 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003388 strncpy(vlcd->name, os_name, len);
3389 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003390
3391 /* Type 2 - LPAR name */
3392 vlcd->type = 2;
3393 len = strlen(utsname()->nodename) + 1;
3394 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003395 strncpy(vlcd->name, utsname()->nodename, len);
3396 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003397
3398 /* Type 3 - device name */
3399 vlcd->type = 3;
3400 len = strlen(adapter->netdev->name) + 1;
3401 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003402 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003403}
3404
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003405static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003406{
3407 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3408 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 struct device *dev = &adapter->vdev->dev;
3410 dma_addr_t rsp_buffer_token;
3411 dma_addr_t buffer_token;
3412 size_t rsp_buffer_size;
3413 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003414 size_t buffer_size;
3415 __be64 *tx_list_p;
3416 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003417 int client_data_len;
3418 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003419 int i;
3420
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003421 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3422 netdev_err(adapter->netdev,
3423 "RX or TX queues are not allocated, device login failed\n");
3424 return -1;
3425 }
3426
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003427 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003428 client_data_len = vnic_client_data_len(adapter);
3429
Thomas Falcon032c5e82015-12-21 11:26:06 -06003430 buffer_size =
3431 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003432 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3433 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003434
Nathan Fontenot37798d02017-11-08 11:23:56 -06003435 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003436 if (!login_buffer)
3437 goto buf_alloc_failed;
3438
3439 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3440 DMA_TO_DEVICE);
3441 if (dma_mapping_error(dev, buffer_token)) {
3442 dev_err(dev, "Couldn't map login buffer\n");
3443 goto buf_map_failed;
3444 }
3445
John Allen498cd8e2016-04-06 11:49:55 -05003446 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3447 sizeof(u64) * adapter->req_tx_queues +
3448 sizeof(u64) * adapter->req_rx_queues +
3449 sizeof(u64) * adapter->req_rx_queues +
3450 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003451
3452 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3453 if (!login_rsp_buffer)
3454 goto buf_rsp_alloc_failed;
3455
3456 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3457 rsp_buffer_size, DMA_FROM_DEVICE);
3458 if (dma_mapping_error(dev, rsp_buffer_token)) {
3459 dev_err(dev, "Couldn't map login rsp buffer\n");
3460 goto buf_rsp_map_failed;
3461 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003462
Thomas Falcon032c5e82015-12-21 11:26:06 -06003463 adapter->login_buf = login_buffer;
3464 adapter->login_buf_token = buffer_token;
3465 adapter->login_buf_sz = buffer_size;
3466 adapter->login_rsp_buf = login_rsp_buffer;
3467 adapter->login_rsp_buf_token = rsp_buffer_token;
3468 adapter->login_rsp_buf_sz = rsp_buffer_size;
3469
3470 login_buffer->len = cpu_to_be32(buffer_size);
3471 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3472 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3473 login_buffer->off_txcomp_subcrqs =
3474 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3475 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3476 login_buffer->off_rxcomp_subcrqs =
3477 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3478 sizeof(u64) * adapter->req_tx_queues);
3479 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3480 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3481
3482 tx_list_p = (__be64 *)((char *)login_buffer +
3483 sizeof(struct ibmvnic_login_buffer));
3484 rx_list_p = (__be64 *)((char *)login_buffer +
3485 sizeof(struct ibmvnic_login_buffer) +
3486 sizeof(u64) * adapter->req_tx_queues);
3487
3488 for (i = 0; i < adapter->req_tx_queues; i++) {
3489 if (adapter->tx_scrq[i]) {
3490 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3491 crq_num);
3492 }
3493 }
3494
3495 for (i = 0; i < adapter->req_rx_queues; i++) {
3496 if (adapter->rx_scrq[i]) {
3497 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3498 crq_num);
3499 }
3500 }
3501
Nathan Fontenot37798d02017-11-08 11:23:56 -06003502 /* Insert vNIC login client data */
3503 vlcd = (struct vnic_login_client_data *)
3504 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3505 login_buffer->client_data_offset =
3506 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3507 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3508
3509 vnic_add_client_data(adapter, vlcd);
3510
Thomas Falcon032c5e82015-12-21 11:26:06 -06003511 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3512 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3513 netdev_dbg(adapter->netdev, "%016lx\n",
3514 ((unsigned long int *)(adapter->login_buf))[i]);
3515 }
3516
3517 memset(&crq, 0, sizeof(crq));
3518 crq.login.first = IBMVNIC_CRQ_CMD;
3519 crq.login.cmd = LOGIN;
3520 crq.login.ioba = cpu_to_be32(buffer_token);
3521 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003522 ibmvnic_send_crq(adapter, &crq);
3523
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003524 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003525
Thomas Falcon032c5e82015-12-21 11:26:06 -06003526buf_rsp_map_failed:
3527 kfree(login_rsp_buffer);
3528buf_rsp_alloc_failed:
3529 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3530buf_map_failed:
3531 kfree(login_buffer);
3532buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003533 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003534}
3535
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003536static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3537 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003538{
3539 union ibmvnic_crq crq;
3540
3541 memset(&crq, 0, sizeof(crq));
3542 crq.request_map.first = IBMVNIC_CRQ_CMD;
3543 crq.request_map.cmd = REQUEST_MAP;
3544 crq.request_map.map_id = map_id;
3545 crq.request_map.ioba = cpu_to_be32(addr);
3546 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003547 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003548}
3549
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003550static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551{
3552 union ibmvnic_crq crq;
3553
3554 memset(&crq, 0, sizeof(crq));
3555 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3556 crq.request_unmap.cmd = REQUEST_UNMAP;
3557 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003558 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003559}
3560
3561static void send_map_query(struct ibmvnic_adapter *adapter)
3562{
3563 union ibmvnic_crq crq;
3564
3565 memset(&crq, 0, sizeof(crq));
3566 crq.query_map.first = IBMVNIC_CRQ_CMD;
3567 crq.query_map.cmd = QUERY_MAP;
3568 ibmvnic_send_crq(adapter, &crq);
3569}
3570
3571/* Send a series of CRQs requesting various capabilities of the VNIC server */
3572static void send_cap_queries(struct ibmvnic_adapter *adapter)
3573{
3574 union ibmvnic_crq crq;
3575
Thomas Falcon901e0402017-02-15 12:17:59 -06003576 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003577 memset(&crq, 0, sizeof(crq));
3578 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3579 crq.query_capability.cmd = QUERY_CAPABILITY;
3580
3581 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003582 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 ibmvnic_send_crq(adapter, &crq);
3584
3585 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003586 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003587 ibmvnic_send_crq(adapter, &crq);
3588
3589 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003590 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 ibmvnic_send_crq(adapter, &crq);
3592
3593 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003594 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595 ibmvnic_send_crq(adapter, &crq);
3596
3597 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003598 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599 ibmvnic_send_crq(adapter, &crq);
3600
3601 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003602 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003603 ibmvnic_send_crq(adapter, &crq);
3604
3605 crq.query_capability.capability =
3606 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003607 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 ibmvnic_send_crq(adapter, &crq);
3609
3610 crq.query_capability.capability =
3611 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003612 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613 ibmvnic_send_crq(adapter, &crq);
3614
3615 crq.query_capability.capability =
3616 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003617 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003618 ibmvnic_send_crq(adapter, &crq);
3619
3620 crq.query_capability.capability =
3621 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003622 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003623 ibmvnic_send_crq(adapter, &crq);
3624
3625 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003626 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627 ibmvnic_send_crq(adapter, &crq);
3628
3629 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003630 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003631 ibmvnic_send_crq(adapter, &crq);
3632
3633 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003634 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003635 ibmvnic_send_crq(adapter, &crq);
3636
3637 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003638 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 ibmvnic_send_crq(adapter, &crq);
3640
3641 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003642 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003643 ibmvnic_send_crq(adapter, &crq);
3644
3645 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003646 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003647 ibmvnic_send_crq(adapter, &crq);
3648
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003649 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3650 atomic_inc(&adapter->running_cap_crqs);
3651 ibmvnic_send_crq(adapter, &crq);
3652
Thomas Falcon032c5e82015-12-21 11:26:06 -06003653 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003654 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003655 ibmvnic_send_crq(adapter, &crq);
3656
3657 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003658 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003659 ibmvnic_send_crq(adapter, &crq);
3660
3661 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003662 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003663 ibmvnic_send_crq(adapter, &crq);
3664
3665 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003666 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003667 ibmvnic_send_crq(adapter, &crq);
3668
3669 crq.query_capability.capability =
3670 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003671 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003672 ibmvnic_send_crq(adapter, &crq);
3673
3674 crq.query_capability.capability =
3675 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003676 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003677 ibmvnic_send_crq(adapter, &crq);
3678
3679 crq.query_capability.capability =
3680 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003681 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003682 ibmvnic_send_crq(adapter, &crq);
3683
3684 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003685 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003686 ibmvnic_send_crq(adapter, &crq);
3687}
3688
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003689static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3690 struct ibmvnic_adapter *adapter)
3691{
3692 struct device *dev = &adapter->vdev->dev;
3693
3694 if (crq->get_vpd_size_rsp.rc.code) {
3695 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3696 crq->get_vpd_size_rsp.rc.code);
3697 complete(&adapter->fw_done);
3698 return;
3699 }
3700
3701 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3702 complete(&adapter->fw_done);
3703}
3704
3705static void handle_vpd_rsp(union ibmvnic_crq *crq,
3706 struct ibmvnic_adapter *adapter)
3707{
3708 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003709 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003710 u8 fw_level_len = 0;
3711
3712 memset(adapter->fw_version, 0, 32);
3713
3714 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3715 DMA_FROM_DEVICE);
3716
3717 if (crq->get_vpd_rsp.rc.code) {
3718 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3719 crq->get_vpd_rsp.rc.code);
3720 goto complete;
3721 }
3722
3723 /* get the position of the firmware version info
3724 * located after the ASCII 'RM' substring in the buffer
3725 */
3726 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3727 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003728 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 -02003729 goto complete;
3730 }
3731
3732 /* get length of firmware level ASCII substring */
3733 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3734 fw_level_len = *(substr + 2);
3735 } else {
3736 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3737 goto complete;
3738 }
3739
3740 /* copy firmware version string from vpd into adapter */
3741 if ((substr + 3 + fw_level_len) <
3742 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003743 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003744 } else {
3745 dev_info(dev, "FW substr extrapolated VPD buff\n");
3746 }
3747
3748complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003749 if (adapter->fw_version[0] == '\0')
3750 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003751 complete(&adapter->fw_done);
3752}
3753
Thomas Falcon032c5e82015-12-21 11:26:06 -06003754static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3755{
3756 struct device *dev = &adapter->vdev->dev;
3757 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcondde746a2019-04-10 11:07:00 -05003758 netdev_features_t old_hw_features = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759 union ibmvnic_crq crq;
3760 int i;
3761
3762 dma_unmap_single(dev, adapter->ip_offload_tok,
3763 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3764
3765 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3766 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3767 netdev_dbg(adapter->netdev, "%016lx\n",
3768 ((unsigned long int *)(buf))[i]);
3769
3770 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3771 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3772 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3773 buf->tcp_ipv4_chksum);
3774 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3775 buf->tcp_ipv6_chksum);
3776 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3777 buf->udp_ipv4_chksum);
3778 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3779 buf->udp_ipv6_chksum);
3780 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3781 buf->large_tx_ipv4);
3782 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3783 buf->large_tx_ipv6);
3784 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3785 buf->large_rx_ipv4);
3786 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3787 buf->large_rx_ipv6);
3788 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3789 buf->max_ipv4_header_size);
3790 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3791 buf->max_ipv6_header_size);
3792 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3793 buf->max_tcp_header_size);
3794 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3795 buf->max_udp_header_size);
3796 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3797 buf->max_large_tx_size);
3798 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3799 buf->max_large_rx_size);
3800 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3801 buf->ipv6_extension_header);
3802 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3803 buf->tcp_pseudosum_req);
3804 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3805 buf->num_ipv6_ext_headers);
3806 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3807 buf->off_ipv6_ext_headers);
3808
3809 adapter->ip_offload_ctrl_tok =
3810 dma_map_single(dev, &adapter->ip_offload_ctrl,
3811 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3812
3813 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3814 dev_err(dev, "Couldn't map ip offload control buffer\n");
3815 return;
3816 }
3817
Thomas Falconf6897942018-01-18 19:05:01 -06003818 adapter->ip_offload_ctrl.len =
3819 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003820 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003821 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3822 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003823 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3824 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3825 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3826 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003827 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3828 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003829
Thomas Falconfdb06102017-10-17 12:36:55 -05003830 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003831 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3832 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3833
Thomas Falcondde746a2019-04-10 11:07:00 -05003834 if (adapter->state != VNIC_PROBING) {
3835 old_hw_features = adapter->netdev->hw_features;
3836 adapter->netdev->hw_features = 0;
3837 }
3838
Thomas Falconb66b7bd2019-04-10 11:06:59 -05003839 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003840
3841 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05003842 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843
3844 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05003845 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003846
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003847 if ((adapter->netdev->features &
3848 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Thomas Falcondde746a2019-04-10 11:07:00 -05003849 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003850
Thomas Falconfdb06102017-10-17 12:36:55 -05003851 if (buf->large_tx_ipv4)
Thomas Falcondde746a2019-04-10 11:07:00 -05003852 adapter->netdev->hw_features |= NETIF_F_TSO;
Thomas Falconfdb06102017-10-17 12:36:55 -05003853 if (buf->large_tx_ipv6)
Thomas Falcondde746a2019-04-10 11:07:00 -05003854 adapter->netdev->hw_features |= NETIF_F_TSO6;
Thomas Falconfdb06102017-10-17 12:36:55 -05003855
Thomas Falcondde746a2019-04-10 11:07:00 -05003856 if (adapter->state == VNIC_PROBING) {
3857 adapter->netdev->features |= adapter->netdev->hw_features;
3858 } else if (old_hw_features != adapter->netdev->hw_features) {
3859 netdev_features_t tmp = 0;
3860
3861 /* disable features no longer supported */
3862 adapter->netdev->features &= adapter->netdev->hw_features;
3863 /* turn on features now supported if previously enabled */
3864 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
3865 adapter->netdev->hw_features;
3866 adapter->netdev->features |=
3867 tmp & adapter->netdev->wanted_features;
3868 }
Thomas Falconaa0bf852017-10-17 12:36:56 -05003869
Thomas Falcon032c5e82015-12-21 11:26:06 -06003870 memset(&crq, 0, sizeof(crq));
3871 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3872 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3873 crq.control_ip_offload.len =
3874 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3875 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3876 ibmvnic_send_crq(adapter, &crq);
3877}
3878
Thomas Falconc9008d32018-08-06 21:39:59 -05003879static const char *ibmvnic_fw_err_cause(u16 cause)
3880{
3881 switch (cause) {
3882 case ADAPTER_PROBLEM:
3883 return "adapter problem";
3884 case BUS_PROBLEM:
3885 return "bus problem";
3886 case FW_PROBLEM:
3887 return "firmware problem";
3888 case DD_PROBLEM:
3889 return "device driver problem";
3890 case EEH_RECOVERY:
3891 return "EEH recovery";
3892 case FW_UPDATED:
3893 return "firmware updated";
3894 case LOW_MEMORY:
3895 return "low Memory";
3896 default:
3897 return "unknown";
3898 }
3899}
3900
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003901static void handle_error_indication(union ibmvnic_crq *crq,
3902 struct ibmvnic_adapter *adapter)
3903{
3904 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05003905 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003906
Thomas Falconc9008d32018-08-06 21:39:59 -05003907 cause = be16_to_cpu(crq->error_indication.error_cause);
3908
3909 dev_warn_ratelimited(dev,
3910 "Firmware reports %serror, cause: %s. Starting recovery...\n",
3911 crq->error_indication.flags
3912 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3913 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003914
Nathan Fontenoted651a12017-05-03 14:04:38 -04003915 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3916 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003917 else
3918 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003919}
3920
Thomas Falconf8136142018-01-29 13:45:05 -06003921static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3922 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003923{
3924 struct net_device *netdev = adapter->netdev;
3925 struct device *dev = &adapter->vdev->dev;
3926 long rc;
3927
3928 rc = crq->change_mac_addr_rsp.rc.code;
3929 if (rc) {
3930 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003931 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003932 }
Thomas Falcon62740e92019-05-09 23:13:43 -05003933 ether_addr_copy(netdev->dev_addr,
3934 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06003935out:
3936 complete(&adapter->fw_done);
3937 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003938}
3939
3940static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3941 struct ibmvnic_adapter *adapter)
3942{
3943 struct device *dev = &adapter->vdev->dev;
3944 u64 *req_value;
3945 char *name;
3946
Thomas Falcon901e0402017-02-15 12:17:59 -06003947 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003948 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3949 case REQ_TX_QUEUES:
3950 req_value = &adapter->req_tx_queues;
3951 name = "tx";
3952 break;
3953 case REQ_RX_QUEUES:
3954 req_value = &adapter->req_rx_queues;
3955 name = "rx";
3956 break;
3957 case REQ_RX_ADD_QUEUES:
3958 req_value = &adapter->req_rx_add_queues;
3959 name = "rx_add";
3960 break;
3961 case REQ_TX_ENTRIES_PER_SUBCRQ:
3962 req_value = &adapter->req_tx_entries_per_subcrq;
3963 name = "tx_entries_per_subcrq";
3964 break;
3965 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3966 req_value = &adapter->req_rx_add_entries_per_subcrq;
3967 name = "rx_add_entries_per_subcrq";
3968 break;
3969 case REQ_MTU:
3970 req_value = &adapter->req_mtu;
3971 name = "mtu";
3972 break;
3973 case PROMISC_REQUESTED:
3974 req_value = &adapter->promisc;
3975 name = "promisc";
3976 break;
3977 default:
3978 dev_err(dev, "Got invalid cap request rsp %d\n",
3979 crq->request_capability.capability);
3980 return;
3981 }
3982
3983 switch (crq->request_capability_rsp.rc.code) {
3984 case SUCCESS:
3985 break;
3986 case PARTIALSUCCESS:
3987 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3988 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003989 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003990 number), name);
John Allene7913802018-01-18 16:27:12 -06003991
3992 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3993 REQ_MTU) {
3994 pr_err("mtu of %llu is not supported. Reverting.\n",
3995 *req_value);
3996 *req_value = adapter->fallback.mtu;
3997 } else {
3998 *req_value =
3999 be64_to_cpu(crq->request_capability_rsp.number);
4000 }
4001
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004002 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003 return;
4004 default:
4005 dev_err(dev, "Error %d in request cap rsp\n",
4006 crq->request_capability_rsp.rc.code);
4007 return;
4008 }
4009
4010 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004011 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004012 union ibmvnic_crq newcrq;
4013 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4014 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4015 &adapter->ip_offload_buf;
4016
Thomas Falcon249168a2017-02-15 12:18:00 -06004017 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004018 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4019 buf_sz,
4020 DMA_FROM_DEVICE);
4021
4022 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4023 if (!firmware_has_feature(FW_FEATURE_CMO))
4024 dev_err(dev, "Couldn't map offload buffer\n");
4025 return;
4026 }
4027
4028 memset(&newcrq, 0, sizeof(newcrq));
4029 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4030 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4031 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4032 newcrq.query_ip_offload.ioba =
4033 cpu_to_be32(adapter->ip_offload_tok);
4034
4035 ibmvnic_send_crq(adapter, &newcrq);
4036 }
4037}
4038
4039static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4040 struct ibmvnic_adapter *adapter)
4041{
4042 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004043 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004044 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4045 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004046 int i;
4047
4048 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004049 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004051 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004052
John Allen498cd8e2016-04-06 11:49:55 -05004053 /* If the number of queues requested can't be allocated by the
4054 * server, the login response will return with code 1. We will need
4055 * to resend the login buffer with fewer queues requested.
4056 */
4057 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004058 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004059 complete(&adapter->init_done);
4060 return 0;
4061 }
4062
John Allenc26eba02017-10-26 16:23:25 -05004063 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4064
Thomas Falcon032c5e82015-12-21 11:26:06 -06004065 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4066 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4067 netdev_dbg(adapter->netdev, "%016lx\n",
4068 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4069 }
4070
4071 /* Sanity checks */
4072 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4073 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4074 adapter->req_rx_add_queues !=
4075 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4076 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4077 ibmvnic_remove(adapter->vdev);
4078 return -EIO;
4079 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06004080 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004081 complete(&adapter->init_done);
4082
Thomas Falcon032c5e82015-12-21 11:26:06 -06004083 return 0;
4084}
4085
Thomas Falcon032c5e82015-12-21 11:26:06 -06004086static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4087 struct ibmvnic_adapter *adapter)
4088{
4089 struct device *dev = &adapter->vdev->dev;
4090 long rc;
4091
4092 rc = crq->request_unmap_rsp.rc.code;
4093 if (rc)
4094 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4095}
4096
4097static void handle_query_map_rsp(union ibmvnic_crq *crq,
4098 struct ibmvnic_adapter *adapter)
4099{
4100 struct net_device *netdev = adapter->netdev;
4101 struct device *dev = &adapter->vdev->dev;
4102 long rc;
4103
4104 rc = crq->query_map_rsp.rc.code;
4105 if (rc) {
4106 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4107 return;
4108 }
4109 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4110 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4111 crq->query_map_rsp.free_pages);
4112}
4113
4114static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4115 struct ibmvnic_adapter *adapter)
4116{
4117 struct net_device *netdev = adapter->netdev;
4118 struct device *dev = &adapter->vdev->dev;
4119 long rc;
4120
Thomas Falcon901e0402017-02-15 12:17:59 -06004121 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004122 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004123 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004124 rc = crq->query_capability.rc.code;
4125 if (rc) {
4126 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4127 goto out;
4128 }
4129
4130 switch (be16_to_cpu(crq->query_capability.capability)) {
4131 case MIN_TX_QUEUES:
4132 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004133 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004134 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4135 adapter->min_tx_queues);
4136 break;
4137 case MIN_RX_QUEUES:
4138 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004139 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004140 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4141 adapter->min_rx_queues);
4142 break;
4143 case MIN_RX_ADD_QUEUES:
4144 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004145 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004146 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4147 adapter->min_rx_add_queues);
4148 break;
4149 case MAX_TX_QUEUES:
4150 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004151 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004152 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4153 adapter->max_tx_queues);
4154 break;
4155 case MAX_RX_QUEUES:
4156 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004157 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004158 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4159 adapter->max_rx_queues);
4160 break;
4161 case MAX_RX_ADD_QUEUES:
4162 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004163 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004164 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4165 adapter->max_rx_add_queues);
4166 break;
4167 case MIN_TX_ENTRIES_PER_SUBCRQ:
4168 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004169 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004170 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4171 adapter->min_tx_entries_per_subcrq);
4172 break;
4173 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4174 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004175 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004176 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4177 adapter->min_rx_add_entries_per_subcrq);
4178 break;
4179 case MAX_TX_ENTRIES_PER_SUBCRQ:
4180 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004181 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004182 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4183 adapter->max_tx_entries_per_subcrq);
4184 break;
4185 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4186 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004187 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004188 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4189 adapter->max_rx_add_entries_per_subcrq);
4190 break;
4191 case TCP_IP_OFFLOAD:
4192 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004193 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004194 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4195 adapter->tcp_ip_offload);
4196 break;
4197 case PROMISC_SUPPORTED:
4198 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004199 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004200 netdev_dbg(netdev, "promisc_supported = %lld\n",
4201 adapter->promisc_supported);
4202 break;
4203 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004204 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004205 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004206 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4207 break;
4208 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004209 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004210 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004211 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4212 break;
4213 case MAX_MULTICAST_FILTERS:
4214 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004215 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004216 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4217 adapter->max_multicast_filters);
4218 break;
4219 case VLAN_HEADER_INSERTION:
4220 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004221 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004222 if (adapter->vlan_header_insertion)
4223 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4224 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4225 adapter->vlan_header_insertion);
4226 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004227 case RX_VLAN_HEADER_INSERTION:
4228 adapter->rx_vlan_header_insertion =
4229 be64_to_cpu(crq->query_capability.number);
4230 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4231 adapter->rx_vlan_header_insertion);
4232 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004233 case MAX_TX_SG_ENTRIES:
4234 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004235 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004236 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4237 adapter->max_tx_sg_entries);
4238 break;
4239 case RX_SG_SUPPORTED:
4240 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004241 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004242 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4243 adapter->rx_sg_supported);
4244 break;
4245 case OPT_TX_COMP_SUB_QUEUES:
4246 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004247 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004248 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4249 adapter->opt_tx_comp_sub_queues);
4250 break;
4251 case OPT_RX_COMP_QUEUES:
4252 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004253 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004254 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4255 adapter->opt_rx_comp_queues);
4256 break;
4257 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4258 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004259 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004260 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4261 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4262 break;
4263 case OPT_TX_ENTRIES_PER_SUBCRQ:
4264 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004265 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004266 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4267 adapter->opt_tx_entries_per_subcrq);
4268 break;
4269 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4270 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004271 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004272 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4273 adapter->opt_rxba_entries_per_subcrq);
4274 break;
4275 case TX_RX_DESC_REQ:
4276 adapter->tx_rx_desc_req = crq->query_capability.number;
4277 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4278 adapter->tx_rx_desc_req);
4279 break;
4280
4281 default:
4282 netdev_err(netdev, "Got invalid cap rsp %d\n",
4283 crq->query_capability.capability);
4284 }
4285
4286out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004287 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4288 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004289 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004290 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004291}
4292
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004293static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4294{
4295 union ibmvnic_crq crq;
4296 int rc;
4297
4298 memset(&crq, 0, sizeof(crq));
4299 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4300 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
4301 init_completion(&adapter->fw_done);
4302 rc = ibmvnic_send_crq(adapter, &crq);
4303 if (rc)
4304 return rc;
4305 wait_for_completion(&adapter->fw_done);
4306 return adapter->fw_done_rc ? -EIO : 0;
4307}
4308
4309static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4310 struct ibmvnic_adapter *adapter)
4311{
4312 struct net_device *netdev = adapter->netdev;
4313 int rc;
4314
4315 rc = crq->query_phys_parms_rsp.rc.code;
4316 if (rc) {
4317 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4318 return rc;
4319 }
4320 switch (cpu_to_be32(crq->query_phys_parms_rsp.speed)) {
4321 case IBMVNIC_10MBPS:
4322 adapter->speed = SPEED_10;
4323 break;
4324 case IBMVNIC_100MBPS:
4325 adapter->speed = SPEED_100;
4326 break;
4327 case IBMVNIC_1GBPS:
4328 adapter->speed = SPEED_1000;
4329 break;
4330 case IBMVNIC_10GBP:
4331 adapter->speed = SPEED_10000;
4332 break;
4333 case IBMVNIC_25GBPS:
4334 adapter->speed = SPEED_25000;
4335 break;
4336 case IBMVNIC_40GBPS:
4337 adapter->speed = SPEED_40000;
4338 break;
4339 case IBMVNIC_50GBPS:
4340 adapter->speed = SPEED_50000;
4341 break;
4342 case IBMVNIC_100GBPS:
4343 adapter->speed = SPEED_100000;
4344 break;
4345 default:
4346 netdev_warn(netdev, "Unknown speed 0x%08x\n",
4347 cpu_to_be32(crq->query_phys_parms_rsp.speed));
4348 adapter->speed = SPEED_UNKNOWN;
4349 }
4350 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4351 adapter->duplex = DUPLEX_FULL;
4352 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4353 adapter->duplex = DUPLEX_HALF;
4354 else
4355 adapter->duplex = DUPLEX_UNKNOWN;
4356
4357 return rc;
4358}
4359
Thomas Falcon032c5e82015-12-21 11:26:06 -06004360static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4361 struct ibmvnic_adapter *adapter)
4362{
4363 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4364 struct net_device *netdev = adapter->netdev;
4365 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004366 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004367 long rc;
4368
4369 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004370 (unsigned long int)cpu_to_be64(u64_crq[0]),
4371 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004372 switch (gen_crq->first) {
4373 case IBMVNIC_CRQ_INIT_RSP:
4374 switch (gen_crq->cmd) {
4375 case IBMVNIC_CRQ_INIT:
4376 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004377 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004378 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004379 if (!completion_done(&adapter->init_done)) {
4380 complete(&adapter->init_done);
4381 adapter->init_done_rc = -EIO;
4382 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004383 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004384 break;
4385 case IBMVNIC_CRQ_INIT_COMPLETE:
4386 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004387 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004388 send_version_xchg(adapter);
4389 break;
4390 default:
4391 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4392 }
4393 return;
4394 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004395 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004396 adapter->crq.active = false;
Thomas Falcon2770a792018-05-23 13:38:02 -05004397 if (adapter->resetting)
4398 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004399 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004400 dev_info(dev, "Migrated, re-enabling adapter\n");
4401 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004402 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4403 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004404 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004405 } else {
4406 /* The adapter lost the connection */
4407 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4408 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004409 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004410 }
4411 return;
4412 case IBMVNIC_CRQ_CMD_RSP:
4413 break;
4414 default:
4415 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4416 gen_crq->first);
4417 return;
4418 }
4419
4420 switch (gen_crq->cmd) {
4421 case VERSION_EXCHANGE_RSP:
4422 rc = crq->version_exchange_rsp.rc.code;
4423 if (rc) {
4424 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4425 break;
4426 }
4427 dev_info(dev, "Partner protocol version is %d\n",
4428 crq->version_exchange_rsp.version);
4429 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4430 ibmvnic_version)
4431 ibmvnic_version =
4432 be16_to_cpu(crq->version_exchange_rsp.version);
4433 send_cap_queries(adapter);
4434 break;
4435 case QUERY_CAPABILITY_RSP:
4436 handle_query_cap_rsp(crq, adapter);
4437 break;
4438 case QUERY_MAP_RSP:
4439 handle_query_map_rsp(crq, adapter);
4440 break;
4441 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004442 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4443 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004444 break;
4445 case REQUEST_UNMAP_RSP:
4446 handle_request_unmap_rsp(crq, adapter);
4447 break;
4448 case REQUEST_CAPABILITY_RSP:
4449 handle_request_cap_rsp(crq, adapter);
4450 break;
4451 case LOGIN_RSP:
4452 netdev_dbg(netdev, "Got Login Response\n");
4453 handle_login_rsp(crq, adapter);
4454 break;
4455 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004456 netdev_dbg(netdev,
4457 "Got Logical Link State Response, state: %d rc: %d\n",
4458 crq->logical_link_state_rsp.link_state,
4459 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004460 adapter->logical_link_state =
4461 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004462 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4463 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004464 break;
4465 case LINK_STATE_INDICATION:
4466 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4467 adapter->phys_link_state =
4468 crq->link_state_indication.phys_link_state;
4469 adapter->logical_link_state =
4470 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004471 if (adapter->phys_link_state && adapter->logical_link_state)
4472 netif_carrier_on(netdev);
4473 else
4474 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004475 break;
4476 case CHANGE_MAC_ADDR_RSP:
4477 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004478 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004479 break;
4480 case ERROR_INDICATION:
4481 netdev_dbg(netdev, "Got Error Indication\n");
4482 handle_error_indication(crq, adapter);
4483 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004484 case REQUEST_STATISTICS_RSP:
4485 netdev_dbg(netdev, "Got Statistics Response\n");
4486 complete(&adapter->stats_done);
4487 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004488 case QUERY_IP_OFFLOAD_RSP:
4489 netdev_dbg(netdev, "Got Query IP offload Response\n");
4490 handle_query_ip_offload_rsp(adapter);
4491 break;
4492 case MULTICAST_CTRL_RSP:
4493 netdev_dbg(netdev, "Got multicast control Response\n");
4494 break;
4495 case CONTROL_IP_OFFLOAD_RSP:
4496 netdev_dbg(netdev, "Got Control IP offload Response\n");
4497 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4498 sizeof(adapter->ip_offload_ctrl),
4499 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004500 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004501 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004502 case COLLECT_FW_TRACE_RSP:
4503 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4504 complete(&adapter->fw_done);
4505 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004506 case GET_VPD_SIZE_RSP:
4507 handle_vpd_size_rsp(crq, adapter);
4508 break;
4509 case GET_VPD_RSP:
4510 handle_vpd_rsp(crq, adapter);
4511 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004512 case QUERY_PHYS_PARMS_RSP:
4513 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4514 complete(&adapter->fw_done);
4515 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 default:
4517 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4518 gen_crq->cmd);
4519 }
4520}
4521
4522static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4523{
4524 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004525
Thomas Falcon6c267b32017-02-15 12:17:58 -06004526 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004527 return IRQ_HANDLED;
4528}
4529
4530static void ibmvnic_tasklet(void *data)
4531{
4532 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004533 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004534 union ibmvnic_crq *crq;
4535 unsigned long flags;
4536 bool done = false;
4537
4538 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004539 while (!done) {
4540 /* Pull all the valid messages off the CRQ */
4541 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4542 ibmvnic_handle_crq(crq, adapter);
4543 crq->generic.first = 0;
4544 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004545
4546 /* remain in tasklet until all
4547 * capabilities responses are received
4548 */
4549 if (!adapter->wait_capability)
4550 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004551 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004552 /* if capabilities CRQ's were sent in this tasklet, the following
4553 * tasklet must wait until all responses are received
4554 */
4555 if (atomic_read(&adapter->running_cap_crqs) != 0)
4556 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004557 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558}
4559
4560static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4561{
4562 struct vio_dev *vdev = adapter->vdev;
4563 int rc;
4564
4565 do {
4566 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4567 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4568
4569 if (rc)
4570 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4571
4572 return rc;
4573}
4574
4575static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4576{
4577 struct ibmvnic_crq_queue *crq = &adapter->crq;
4578 struct device *dev = &adapter->vdev->dev;
4579 struct vio_dev *vdev = adapter->vdev;
4580 int rc;
4581
4582 /* Close the CRQ */
4583 do {
4584 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4585 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4586
4587 /* Clean out the queue */
4588 memset(crq->msgs, 0, PAGE_SIZE);
4589 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004590 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004591
4592 /* And re-open it again */
4593 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4594 crq->msg_token, PAGE_SIZE);
4595
4596 if (rc == H_CLOSED)
4597 /* Adapter is good, but other end is not ready */
4598 dev_warn(dev, "Partner adapter not ready\n");
4599 else if (rc != 0)
4600 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4601
4602 return rc;
4603}
4604
Nathan Fontenotf9928872017-03-30 02:48:54 -04004605static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004606{
4607 struct ibmvnic_crq_queue *crq = &adapter->crq;
4608 struct vio_dev *vdev = adapter->vdev;
4609 long rc;
4610
Nathan Fontenotf9928872017-03-30 02:48:54 -04004611 if (!crq->msgs)
4612 return;
4613
Thomas Falcon032c5e82015-12-21 11:26:06 -06004614 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4615 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004616 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004617 do {
4618 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4619 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4620
4621 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4622 DMA_BIDIRECTIONAL);
4623 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004624 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004625 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004626}
4627
Nathan Fontenotf9928872017-03-30 02:48:54 -04004628static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004629{
4630 struct ibmvnic_crq_queue *crq = &adapter->crq;
4631 struct device *dev = &adapter->vdev->dev;
4632 struct vio_dev *vdev = adapter->vdev;
4633 int rc, retrc = -ENOMEM;
4634
Nathan Fontenotf9928872017-03-30 02:48:54 -04004635 if (crq->msgs)
4636 return 0;
4637
Thomas Falcon032c5e82015-12-21 11:26:06 -06004638 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4639 /* Should we allocate more than one page? */
4640
4641 if (!crq->msgs)
4642 return -ENOMEM;
4643
4644 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4645 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4646 DMA_BIDIRECTIONAL);
4647 if (dma_mapping_error(dev, crq->msg_token))
4648 goto map_failed;
4649
4650 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4651 crq->msg_token, PAGE_SIZE);
4652
4653 if (rc == H_RESOURCE)
4654 /* maybe kexecing and resource is busy. try a reset */
4655 rc = ibmvnic_reset_crq(adapter);
4656 retrc = rc;
4657
4658 if (rc == H_CLOSED) {
4659 dev_warn(dev, "Partner adapter not ready\n");
4660 } else if (rc) {
4661 dev_warn(dev, "Error %d opening adapter\n", rc);
4662 goto reg_crq_failed;
4663 }
4664
4665 retrc = 0;
4666
Thomas Falcon6c267b32017-02-15 12:17:58 -06004667 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4668 (unsigned long)adapter);
4669
Thomas Falcon032c5e82015-12-21 11:26:06 -06004670 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03004671 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
4672 adapter->vdev->unit_address);
4673 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004674 if (rc) {
4675 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4676 vdev->irq, rc);
4677 goto req_irq_failed;
4678 }
4679
4680 rc = vio_enable_interrupts(vdev);
4681 if (rc) {
4682 dev_err(dev, "Error %d enabling interrupts\n", rc);
4683 goto req_irq_failed;
4684 }
4685
4686 crq->cur = 0;
4687 spin_lock_init(&crq->lock);
4688
4689 return retrc;
4690
4691req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004692 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004693 do {
4694 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4695 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4696reg_crq_failed:
4697 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4698map_failed:
4699 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004700 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004701 return retrc;
4702}
4703
Thomas Falcon8a348452018-05-23 13:38:00 -05004704static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
John Allenf6ef6402017-03-17 17:13:42 -05004705{
4706 struct device *dev = &adapter->vdev->dev;
4707 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004708 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004709 int rc;
4710
John Allen017892c12017-05-26 10:30:19 -04004711 adapter->from_passive_init = false;
4712
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004713 old_num_rx_queues = adapter->req_rx_queues;
4714 old_num_tx_queues = adapter->req_tx_queues;
4715
Thomas Falconbbd669a2019-04-04 18:58:26 -05004716 reinit_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004717 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004718 ibmvnic_send_crq_init(adapter);
4719 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4720 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004721 return -1;
4722 }
4723
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004724 if (adapter->init_done_rc) {
4725 release_crq_queue(adapter);
4726 return adapter->init_done_rc;
4727 }
4728
John Allen017892c12017-05-26 10:30:19 -04004729 if (adapter->from_passive_init) {
4730 adapter->state = VNIC_OPEN;
4731 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004732 return -1;
4733 }
4734
Nathan Fontenot30f79622018-04-06 18:37:06 -05004735 if (adapter->resetting && !adapter->wait_for_reset &&
4736 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004737 if (adapter->req_rx_queues != old_num_rx_queues ||
4738 adapter->req_tx_queues != old_num_tx_queues) {
4739 release_sub_crqs(adapter, 0);
4740 rc = init_sub_crqs(adapter);
4741 } else {
4742 rc = reset_sub_crq_queues(adapter);
4743 }
4744 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004745 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004746 }
4747
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004748 if (rc) {
4749 dev_err(dev, "Initialization of sub crqs failed\n");
4750 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004751 return rc;
4752 }
4753
4754 rc = init_sub_crq_irqs(adapter);
4755 if (rc) {
4756 dev_err(dev, "Failed to initialize sub crq irqs\n");
4757 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004758 }
4759
4760 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004761}
4762
Thomas Falcon8a348452018-05-23 13:38:00 -05004763static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4764{
4765 struct device *dev = &adapter->vdev->dev;
4766 unsigned long timeout = msecs_to_jiffies(30000);
4767 int rc;
4768
4769 adapter->from_passive_init = false;
4770
Thomas Falcon8a348452018-05-23 13:38:00 -05004771 adapter->init_done_rc = 0;
4772 ibmvnic_send_crq_init(adapter);
4773 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4774 dev_err(dev, "Initialization sequence timed out\n");
4775 return -1;
4776 }
4777
4778 if (adapter->init_done_rc) {
4779 release_crq_queue(adapter);
4780 return adapter->init_done_rc;
4781 }
4782
4783 if (adapter->from_passive_init) {
4784 adapter->state = VNIC_OPEN;
4785 adapter->from_passive_init = false;
4786 return -1;
4787 }
4788
4789 rc = init_sub_crqs(adapter);
4790 if (rc) {
4791 dev_err(dev, "Initialization of sub crqs failed\n");
4792 release_crq_queue(adapter);
4793 return rc;
4794 }
4795
4796 rc = init_sub_crq_irqs(adapter);
4797 if (rc) {
4798 dev_err(dev, "Failed to initialize sub crq irqs\n");
4799 release_crq_queue(adapter);
4800 }
4801
4802 return rc;
4803}
4804
Thomas Falcon40c9db82017-06-12 12:35:04 -05004805static struct device_attribute dev_attr_failover;
4806
Thomas Falcon032c5e82015-12-21 11:26:06 -06004807static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4808{
4809 struct ibmvnic_adapter *adapter;
4810 struct net_device *netdev;
4811 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004812 int rc;
4813
4814 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4815 dev->unit_address);
4816
4817 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4818 VETH_MAC_ADDR, NULL);
4819 if (!mac_addr_p) {
4820 dev_err(&dev->dev,
4821 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4822 __FILE__, __LINE__);
4823 return 0;
4824 }
4825
4826 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004827 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004828 if (!netdev)
4829 return -ENOMEM;
4830
4831 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004832 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004833 dev_set_drvdata(&dev->dev, netdev);
4834 adapter->vdev = dev;
4835 adapter->netdev = netdev;
4836
4837 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4838 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4839 netdev->irq = dev->irq;
4840 netdev->netdev_ops = &ibmvnic_netdev_ops;
4841 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4842 SET_NETDEV_DEV(netdev, &dev->dev);
4843
4844 spin_lock_init(&adapter->stats_lock);
4845
Nathan Fontenoted651a12017-05-03 14:04:38 -04004846 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4847 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06004848 spin_lock_init(&adapter->rwi_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05004849 init_completion(&adapter->init_done);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004850 adapter->resetting = false;
4851
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004852 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05004853 rc = init_crq_queue(adapter);
4854 if (rc) {
4855 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4856 rc);
4857 goto ibmvnic_init_fail;
4858 }
4859
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004860 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004861 if (rc && rc != EAGAIN)
4862 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004863 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004864
Thomas Falcon07184212018-05-16 15:49:05 -05004865 rc = init_stats_buffers(adapter);
4866 if (rc)
4867 goto ibmvnic_init_fail;
4868
4869 rc = init_stats_token(adapter);
4870 if (rc)
4871 goto ibmvnic_stats_fail;
4872
Thomas Falconf39f0d12017-02-14 10:22:59 -06004873 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004874 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4875 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004876
Thomas Falcon40c9db82017-06-12 12:35:04 -05004877 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004878 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05004879 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004880
Mick Tarsele876a8a2017-09-28 13:53:18 -07004881 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004882 rc = register_netdev(netdev);
4883 if (rc) {
4884 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004885 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004886 }
4887 dev_info(&dev->dev, "ibmvnic registered\n");
4888
Nathan Fontenot90c80142017-05-03 14:04:32 -04004889 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004890
4891 adapter->wait_for_reset = false;
4892
Thomas Falcon032c5e82015-12-21 11:26:06 -06004893 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004894
4895ibmvnic_register_fail:
4896 device_remove_file(&dev->dev, &dev_attr_failover);
4897
Thomas Falcon07184212018-05-16 15:49:05 -05004898ibmvnic_dev_file_err:
4899 release_stats_token(adapter);
4900
4901ibmvnic_stats_fail:
4902 release_stats_buffers(adapter);
4903
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004904ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004905 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004906 release_crq_queue(adapter);
4907 free_netdev(netdev);
4908
4909 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004910}
4911
4912static int ibmvnic_remove(struct vio_dev *dev)
4913{
4914 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004915 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004916
Nathan Fontenot90c80142017-05-03 14:04:32 -04004917 adapter->state = VNIC_REMOVING;
Juliet Kima5681e22018-11-19 15:59:22 -06004918 rtnl_lock();
4919 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004920
4921 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004922 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004923 release_crq_queue(adapter);
4924
Thomas Falcon53cc7722018-02-26 18:10:56 -06004925 release_stats_token(adapter);
4926 release_stats_buffers(adapter);
4927
Nathan Fontenot90c80142017-05-03 14:04:32 -04004928 adapter->state = VNIC_REMOVED;
4929
Juliet Kima5681e22018-11-19 15:59:22 -06004930 rtnl_unlock();
Thomas Falcon40c9db82017-06-12 12:35:04 -05004931 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004932 free_netdev(netdev);
4933 dev_set_drvdata(&dev->dev, NULL);
4934
4935 return 0;
4936}
4937
Thomas Falcon40c9db82017-06-12 12:35:04 -05004938static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4939 const char *buf, size_t count)
4940{
4941 struct net_device *netdev = dev_get_drvdata(dev);
4942 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4943 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4944 __be64 session_token;
4945 long rc;
4946
4947 if (!sysfs_streq(buf, "1"))
4948 return -EINVAL;
4949
4950 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4951 H_GET_SESSION_TOKEN, 0, 0, 0);
4952 if (rc) {
4953 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4954 rc);
4955 return -EINVAL;
4956 }
4957
4958 session_token = (__be64)retbuf[0];
4959 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4960 be64_to_cpu(session_token));
4961 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4962 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4963 if (rc) {
4964 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4965 rc);
4966 return -EINVAL;
4967 }
4968
4969 return count;
4970}
4971
Joe Perches6cbaefb2017-12-19 10:15:09 -08004972static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004973
Thomas Falcon032c5e82015-12-21 11:26:06 -06004974static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4975{
4976 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4977 struct ibmvnic_adapter *adapter;
4978 struct iommu_table *tbl;
4979 unsigned long ret = 0;
4980 int i;
4981
4982 tbl = get_iommu_table_base(&vdev->dev);
4983
4984 /* netdev inits at probe time along with the structures we need below*/
4985 if (!netdev)
4986 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4987
4988 adapter = netdev_priv(netdev);
4989
4990 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004991 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4992
4993 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4994 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4995
4996 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4997 i++)
4998 ret += adapter->rx_pool[i].size *
4999 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5000
5001 return ret;
5002}
5003
5004static int ibmvnic_resume(struct device *dev)
5005{
5006 struct net_device *netdev = dev_get_drvdata(dev);
5007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005008
John Allencb89ba22017-06-19 11:27:53 -05005009 if (adapter->state != VNIC_OPEN)
5010 return 0;
5011
John Allena2488782017-07-24 13:26:06 -05005012 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005013
5014 return 0;
5015}
5016
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305017static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005018 {"network", "IBM,vnic"},
5019 {"", "" }
5020};
5021MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5022
5023static const struct dev_pm_ops ibmvnic_pm_ops = {
5024 .resume = ibmvnic_resume
5025};
5026
5027static struct vio_driver ibmvnic_driver = {
5028 .id_table = ibmvnic_device_table,
5029 .probe = ibmvnic_probe,
5030 .remove = ibmvnic_remove,
5031 .get_desired_dma = ibmvnic_get_desired_dma,
5032 .name = ibmvnic_driver_name,
5033 .pm = &ibmvnic_pm_ops,
5034};
5035
5036/* module functions */
5037static int __init ibmvnic_module_init(void)
5038{
5039 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5040 IBMVNIC_DRIVER_VERSION);
5041
5042 return vio_register_driver(&ibmvnic_driver);
5043}
5044
5045static void __exit ibmvnic_module_exit(void)
5046{
5047 vio_unregister_driver(&ibmvnic_driver);
5048}
5049
5050module_init(ibmvnic_module_init);
5051module_exit(ibmvnic_module_exit);