blob: 3dcd9c3d8781dec039e624c9ac9d32537cca230e [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
Thomas Falcon4eb50ce2017-12-18 12:52:40 -060062#include <linux/if_arp.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060063#include <linux/in.h>
64#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050065#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060066#include <linux/irq.h>
67#include <linux/kthread.h>
68#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060069#include <linux/interrupt.h>
70#include <net/net_namespace.h>
71#include <asm/hvcall.h>
72#include <linux/atomic.h>
73#include <asm/vio.h>
74#include <asm/iommu.h>
75#include <linux/uaccess.h>
76#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050077#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040078#include <linux/if_vlan.h>
Nathan Fontenot37798d02017-11-08 11:23:56 -060079#include <linux/utsname.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060080
81#include "ibmvnic.h"
82
83static const char ibmvnic_driver_name[] = "ibmvnic";
84static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
85
Thomas Falcon78b07ac2017-06-01 15:32:34 -050086MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060087MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
90
91static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92static int ibmvnic_remove(struct vio_dev *);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -060093static void release_sub_crqs(struct ibmvnic_adapter *, bool);
Thomas Falcon032c5e82015-12-21 11:26:06 -060094static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500100static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600101static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110static int ibmvnic_poll(struct napi_struct *napi, int data);
111static void send_map_query(struct ibmvnic_adapter *adapter);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500112static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113static int send_request_unmap(struct ibmvnic_adapter *, u8);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600114static int send_login(struct ibmvnic_adapter *adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500115static void send_cap_queries(struct ibmvnic_adapter *adapter);
Thomas Falcon4d96f122017-08-01 15:04:36 -0500116static int init_sub_crqs(struct ibmvnic_adapter *);
John Allenbd0b6722017-03-17 17:13:40 -0500117static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500118static int ibmvnic_init(struct ibmvnic_adapter *);
Thomas Falcon8a348452018-05-23 13:38:00 -0500119static int ibmvnic_reset_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400120static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon62740e92019-05-09 23:13:43 -0500121static int __ibmvnic_set_mac(struct net_device *, u8 *);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500122static int init_crq_queue(struct ibmvnic_adapter *adapter);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300123static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600124
125struct ibmvnic_stat {
126 char name[ETH_GSTRING_LEN];
127 int offset;
128};
129
130#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
131 offsetof(struct ibmvnic_statistics, stat))
132#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
133
134static const struct ibmvnic_stat ibmvnic_stats[] = {
135 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
136 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
137 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
138 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
139 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
140 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
141 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
142 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
143 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
144 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
145 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
146 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
147 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
148 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
149 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
150 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
151 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
152 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
153 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
154 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
155 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
156 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
157};
158
159static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
160 unsigned long length, unsigned long *number,
161 unsigned long *irq)
162{
163 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
164 long rc;
165
166 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
167 *number = retbuf[0];
168 *irq = retbuf[1];
169
170 return rc;
171}
172
Thomas Falcon032c5e82015-12-21 11:26:06 -0600173static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
174 struct ibmvnic_long_term_buff *ltb, int size)
175{
176 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500177 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600178
179 ltb->size = size;
180 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
181 GFP_KERNEL);
182
183 if (!ltb->buff) {
184 dev_err(dev, "Couldn't alloc long term buffer\n");
185 return -ENOMEM;
186 }
187 ltb->map_id = adapter->map_id;
188 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500189
190 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500191 rc = send_request_map(adapter, ltb->addr,
192 ltb->size, ltb->map_id);
193 if (rc) {
194 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
195 return rc;
196 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600197 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500198
199 if (adapter->fw_done_rc) {
200 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
201 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500202 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500203 return -1;
204 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600205 return 0;
206}
207
208static void free_long_term_buff(struct ibmvnic_adapter *adapter,
209 struct ibmvnic_long_term_buff *ltb)
210{
211 struct device *dev = &adapter->vdev->dev;
212
Nathan Fontenotc657e322017-03-30 02:49:06 -0400213 if (!ltb->buff)
214 return;
215
Nathan Fontenoted651a12017-05-03 14:04:38 -0400216 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
217 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500218 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400219 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600220}
221
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500222static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
223 struct ibmvnic_long_term_buff *ltb)
224{
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500225 int rc;
226
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500227 memset(ltb->buff, 0, ltb->size);
228
229 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500230 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
231 if (rc)
232 return rc;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500233 wait_for_completion(&adapter->fw_done);
234
235 if (adapter->fw_done_rc) {
236 dev_info(&adapter->vdev->dev,
237 "Reset failed, attempting to free and reallocate buffer\n");
238 free_long_term_buff(adapter, ltb);
239 return alloc_long_term_buff(adapter, ltb, ltb->size);
240 }
241 return 0;
242}
243
Thomas Falconf185a492017-05-26 10:30:48 -0400244static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
245{
246 int i;
247
248 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
249 i++)
250 adapter->rx_pool[i].active = 0;
251}
252
Thomas Falcon032c5e82015-12-21 11:26:06 -0600253static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
254 struct ibmvnic_rx_pool *pool)
255{
256 int count = pool->size - atomic_read(&pool->available);
257 struct device *dev = &adapter->vdev->dev;
258 int buffers_added = 0;
259 unsigned long lpar_rc;
260 union sub_crq sub_crq;
261 struct sk_buff *skb;
262 unsigned int offset;
263 dma_addr_t dma_addr;
264 unsigned char *dst;
265 u64 *handle_array;
266 int shift = 0;
267 int index;
268 int i;
269
Thomas Falconf185a492017-05-26 10:30:48 -0400270 if (!pool->active)
271 return;
272
Thomas Falcon032c5e82015-12-21 11:26:06 -0600273 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
274 be32_to_cpu(adapter->login_rsp_buf->
275 off_rxadd_subcrqs));
276
277 for (i = 0; i < count; ++i) {
278 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
279 if (!skb) {
280 dev_err(dev, "Couldn't replenish rx buff\n");
281 adapter->replenish_no_mem++;
282 break;
283 }
284
285 index = pool->free_map[pool->next_free];
286
287 if (pool->rx_buff[index].skb)
288 dev_err(dev, "Inconsistent free_map!\n");
289
290 /* Copy the skb to the long term mapped DMA buffer */
291 offset = index * pool->buff_size;
292 dst = pool->long_term_buff.buff + offset;
293 memset(dst, 0, pool->buff_size);
294 dma_addr = pool->long_term_buff.addr + offset;
295 pool->rx_buff[index].data = dst;
296
297 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
298 pool->rx_buff[index].dma = dma_addr;
299 pool->rx_buff[index].skb = skb;
300 pool->rx_buff[index].pool_index = pool->index;
301 pool->rx_buff[index].size = pool->buff_size;
302
303 memset(&sub_crq, 0, sizeof(sub_crq));
304 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
305 sub_crq.rx_add.correlator =
306 cpu_to_be64((u64)&pool->rx_buff[index]);
307 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
308 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
309
310 /* The length field of the sCRQ is defined to be 24 bits so the
311 * buffer size needs to be left shifted by a byte before it is
312 * converted to big endian to prevent the last byte from being
313 * truncated.
314 */
315#ifdef __LITTLE_ENDIAN__
316 shift = 8;
317#endif
318 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
319
320 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
321 &sub_crq);
322 if (lpar_rc != H_SUCCESS)
323 goto failure;
324
325 buffers_added++;
326 adapter->replenish_add_buff_success++;
327 pool->next_free = (pool->next_free + 1) % pool->size;
328 }
329 atomic_add(buffers_added, &pool->available);
330 return;
331
332failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500333 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
334 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -0600335 pool->free_map[pool->next_free] = index;
336 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600337
338 dev_kfree_skb_any(skb);
339 adapter->replenish_add_buff_failure++;
340 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400341
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500342 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400343 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500344 * queue is closed or pending failover.
345 * Firmware guarantees that a signal will be sent to the
346 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400347 */
348 deactivate_rx_pools(adapter);
349 netif_carrier_off(adapter->netdev);
350 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600351}
352
353static void replenish_pools(struct ibmvnic_adapter *adapter)
354{
355 int i;
356
Thomas Falcon032c5e82015-12-21 11:26:06 -0600357 adapter->replenish_task_cycles++;
358 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
359 i++) {
360 if (adapter->rx_pool[i].active)
361 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
362 }
363}
364
John Allen3d52b592017-08-02 16:44:14 -0500365static void release_stats_buffers(struct ibmvnic_adapter *adapter)
366{
367 kfree(adapter->tx_stats_buffers);
368 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600369 adapter->tx_stats_buffers = NULL;
370 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500371}
372
373static int init_stats_buffers(struct ibmvnic_adapter *adapter)
374{
375 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600376 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500377 sizeof(struct ibmvnic_tx_queue_stats),
378 GFP_KERNEL);
379 if (!adapter->tx_stats_buffers)
380 return -ENOMEM;
381
382 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600383 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500384 sizeof(struct ibmvnic_rx_queue_stats),
385 GFP_KERNEL);
386 if (!adapter->rx_stats_buffers)
387 return -ENOMEM;
388
389 return 0;
390}
391
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400392static void release_stats_token(struct ibmvnic_adapter *adapter)
393{
394 struct device *dev = &adapter->vdev->dev;
395
396 if (!adapter->stats_token)
397 return;
398
399 dma_unmap_single(dev, adapter->stats_token,
400 sizeof(struct ibmvnic_statistics),
401 DMA_FROM_DEVICE);
402 adapter->stats_token = 0;
403}
404
405static int init_stats_token(struct ibmvnic_adapter *adapter)
406{
407 struct device *dev = &adapter->vdev->dev;
408 dma_addr_t stok;
409
410 stok = dma_map_single(dev, &adapter->stats,
411 sizeof(struct ibmvnic_statistics),
412 DMA_FROM_DEVICE);
413 if (dma_mapping_error(dev, stok)) {
414 dev_err(dev, "Couldn't map stats buffer\n");
415 return -1;
416 }
417
418 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500419 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400420 return 0;
421}
422
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400423static int reset_rx_pools(struct ibmvnic_adapter *adapter)
424{
425 struct ibmvnic_rx_pool *rx_pool;
426 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500427 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600428 u64 *size_array;
429
430 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
431 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400432
433 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
434 for (i = 0; i < rx_scrqs; i++) {
435 rx_pool = &adapter->rx_pool[i];
436
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500437 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
438
John Allen896d8692018-01-18 16:26:31 -0600439 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
440 free_long_term_buff(adapter, &rx_pool->long_term_buff);
441 rx_pool->buff_size = be64_to_cpu(size_array[i]);
442 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
443 rx_pool->size *
444 rx_pool->buff_size);
445 } else {
446 rc = reset_long_term_buff(adapter,
447 &rx_pool->long_term_buff);
448 }
449
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500450 if (rc)
451 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400452
453 for (j = 0; j < rx_pool->size; j++)
454 rx_pool->free_map[j] = j;
455
456 memset(rx_pool->rx_buff, 0,
457 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
458
459 atomic_set(&rx_pool->available, 0);
460 rx_pool->next_alloc = 0;
461 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500462 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400463 }
464
465 return 0;
466}
467
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400468static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600469{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400470 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400471 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600472
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400473 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600474 return;
475
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600476 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400477 rx_pool = &adapter->rx_pool[i];
478
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500479 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
480
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400481 kfree(rx_pool->free_map);
482 free_long_term_buff(adapter, &rx_pool->long_term_buff);
483
484 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400485 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400486
487 for (j = 0; j < rx_pool->size; j++) {
488 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600489 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
490 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400491 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600492 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400493
494 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600495 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400496
497 kfree(adapter->rx_pool);
498 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600499 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400500}
501
502static int init_rx_pools(struct net_device *netdev)
503{
504 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
505 struct device *dev = &adapter->vdev->dev;
506 struct ibmvnic_rx_pool *rx_pool;
507 int rxadd_subcrqs;
508 u64 *size_array;
509 int i, j;
510
511 rxadd_subcrqs =
512 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
513 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
514 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
515
516 adapter->rx_pool = kcalloc(rxadd_subcrqs,
517 sizeof(struct ibmvnic_rx_pool),
518 GFP_KERNEL);
519 if (!adapter->rx_pool) {
520 dev_err(dev, "Failed to allocate rx pools\n");
521 return -1;
522 }
523
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600524 adapter->num_active_rx_pools = rxadd_subcrqs;
525
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400526 for (i = 0; i < rxadd_subcrqs; i++) {
527 rx_pool = &adapter->rx_pool[i];
528
529 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500530 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400531 i, adapter->req_rx_add_entries_per_subcrq,
532 be64_to_cpu(size_array[i]));
533
534 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
535 rx_pool->index = i;
536 rx_pool->buff_size = be64_to_cpu(size_array[i]);
537 rx_pool->active = 1;
538
539 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
540 GFP_KERNEL);
541 if (!rx_pool->free_map) {
542 release_rx_pools(adapter);
543 return -1;
544 }
545
546 rx_pool->rx_buff = kcalloc(rx_pool->size,
547 sizeof(struct ibmvnic_rx_buff),
548 GFP_KERNEL);
549 if (!rx_pool->rx_buff) {
550 dev_err(dev, "Couldn't alloc rx buffers\n");
551 release_rx_pools(adapter);
552 return -1;
553 }
554
555 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
556 rx_pool->size * rx_pool->buff_size)) {
557 release_rx_pools(adapter);
558 return -1;
559 }
560
561 for (j = 0; j < rx_pool->size; ++j)
562 rx_pool->free_map[j] = j;
563
564 atomic_set(&rx_pool->available, 0);
565 rx_pool->next_alloc = 0;
566 rx_pool->next_free = 0;
567 }
568
569 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600570}
571
Thomas Falcone26dc252018-03-16 20:00:25 -0500572static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
573 struct ibmvnic_tx_pool *tx_pool)
574{
575 int rc, i;
576
577 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
578 if (rc)
579 return rc;
580
581 memset(tx_pool->tx_buff, 0,
582 tx_pool->num_buffers *
583 sizeof(struct ibmvnic_tx_buff));
584
585 for (i = 0; i < tx_pool->num_buffers; i++)
586 tx_pool->free_map[i] = i;
587
588 tx_pool->consumer_index = 0;
589 tx_pool->producer_index = 0;
590
591 return 0;
592}
593
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400594static int reset_tx_pools(struct ibmvnic_adapter *adapter)
595{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400596 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500597 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400598
599 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
600 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500601 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500602 if (rc)
603 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500604 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500605 if (rc)
606 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400607 }
608
609 return 0;
610}
611
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200612static void release_vpd_data(struct ibmvnic_adapter *adapter)
613{
614 if (!adapter->vpd)
615 return;
616
617 kfree(adapter->vpd->buff);
618 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600619
620 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200621}
622
Thomas Falconfb794212018-03-16 20:00:26 -0500623static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
624 struct ibmvnic_tx_pool *tx_pool)
625{
626 kfree(tx_pool->tx_buff);
627 kfree(tx_pool->free_map);
628 free_long_term_buff(adapter, &tx_pool->long_term_buff);
629}
630
Nathan Fontenotc657e322017-03-30 02:49:06 -0400631static void release_tx_pools(struct ibmvnic_adapter *adapter)
632{
John Allen896d8692018-01-18 16:26:31 -0600633 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400634
635 if (!adapter->tx_pool)
636 return;
637
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600638 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500639 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
640 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400641 }
642
643 kfree(adapter->tx_pool);
644 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500645 kfree(adapter->tso_pool);
646 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600647 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400648}
649
Thomas Falcon32053062018-03-16 20:00:27 -0500650static int init_one_tx_pool(struct net_device *netdev,
651 struct ibmvnic_tx_pool *tx_pool,
652 int num_entries, int buf_size)
653{
654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
655 int i;
656
657 tx_pool->tx_buff = kcalloc(num_entries,
658 sizeof(struct ibmvnic_tx_buff),
659 GFP_KERNEL);
660 if (!tx_pool->tx_buff)
661 return -1;
662
663 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
664 num_entries * buf_size))
665 return -1;
666
667 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
668 if (!tx_pool->free_map)
669 return -1;
670
671 for (i = 0; i < num_entries; i++)
672 tx_pool->free_map[i] = i;
673
674 tx_pool->consumer_index = 0;
675 tx_pool->producer_index = 0;
676 tx_pool->num_buffers = num_entries;
677 tx_pool->buf_size = buf_size;
678
679 return 0;
680}
681
Nathan Fontenotc657e322017-03-30 02:49:06 -0400682static int init_tx_pools(struct net_device *netdev)
683{
684 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400685 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500686 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400687
688 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
689 adapter->tx_pool = kcalloc(tx_subcrqs,
690 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
691 if (!adapter->tx_pool)
692 return -1;
693
Thomas Falcon32053062018-03-16 20:00:27 -0500694 adapter->tso_pool = kcalloc(tx_subcrqs,
695 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
696 if (!adapter->tso_pool)
697 return -1;
698
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600699 adapter->num_active_tx_pools = tx_subcrqs;
700
Nathan Fontenotc657e322017-03-30 02:49:06 -0400701 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500702 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
703 adapter->req_tx_entries_per_subcrq,
704 adapter->req_mtu + VLAN_HLEN);
705 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400706 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500707 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400708 }
709
Thomas Falcon32053062018-03-16 20:00:27 -0500710 init_one_tx_pool(netdev, &adapter->tso_pool[i],
711 IBMVNIC_TSO_BUFS,
712 IBMVNIC_TSO_BUF_SZ);
713 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400714 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500715 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400716 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400717 }
718
719 return 0;
720}
721
John Allend944c3d62017-05-26 10:30:13 -0400722static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
723{
724 int i;
725
726 if (adapter->napi_enabled)
727 return;
728
729 for (i = 0; i < adapter->req_rx_queues; i++)
730 napi_enable(&adapter->napi[i]);
731
732 adapter->napi_enabled = true;
733}
734
735static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
736{
737 int i;
738
739 if (!adapter->napi_enabled)
740 return;
741
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500742 for (i = 0; i < adapter->req_rx_queues; i++) {
743 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400744 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500745 }
John Allend944c3d62017-05-26 10:30:13 -0400746
747 adapter->napi_enabled = false;
748}
749
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600750static int init_napi(struct ibmvnic_adapter *adapter)
751{
752 int i;
753
754 adapter->napi = kcalloc(adapter->req_rx_queues,
755 sizeof(struct napi_struct), GFP_KERNEL);
756 if (!adapter->napi)
757 return -ENOMEM;
758
759 for (i = 0; i < adapter->req_rx_queues; i++) {
760 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
761 netif_napi_add(adapter->netdev, &adapter->napi[i],
762 ibmvnic_poll, NAPI_POLL_WEIGHT);
763 }
764
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600765 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600766 return 0;
767}
768
769static void release_napi(struct ibmvnic_adapter *adapter)
770{
771 int i;
772
773 if (!adapter->napi)
774 return;
775
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600776 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800777 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
778 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600779 }
780
781 kfree(adapter->napi);
782 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600783 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500784 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600785}
786
John Allena57a5d22017-03-17 17:13:41 -0500787static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600788{
789 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500790 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500791 int retry_count = 0;
Thomas Falconeb110412018-05-24 14:37:53 -0500792 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500793 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600794
John Allenbd0b6722017-03-17 17:13:40 -0500795 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500796 retry = false;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500797 if (retry_count > IBMVNIC_MAX_QUEUES) {
798 netdev_warn(netdev, "Login attempts exceeded\n");
799 return -1;
800 }
801
802 adapter->init_done_rc = 0;
803 reinit_completion(&adapter->init_done);
804 rc = send_login(adapter);
805 if (rc) {
806 netdev_warn(netdev, "Unable to login\n");
807 return rc;
808 }
809
810 if (!wait_for_completion_timeout(&adapter->init_done,
811 timeout)) {
812 netdev_warn(netdev, "Login timed out\n");
813 return -1;
814 }
815
816 if (adapter->init_done_rc == PARTIALSUCCESS) {
817 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600818 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500819
Thomas Falconeb110412018-05-24 14:37:53 -0500820 retry = true;
821 netdev_dbg(netdev,
822 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500823 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500824 reinit_completion(&adapter->init_done);
825 send_cap_queries(adapter);
826 if (!wait_for_completion_timeout(&adapter->init_done,
827 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500828 netdev_warn(netdev,
829 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500830 return -1;
831 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500832
Thomas Falcon4d96f122017-08-01 15:04:36 -0500833 rc = init_sub_crqs(adapter);
834 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500835 netdev_warn(netdev,
836 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500837 return -1;
838 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500839
Thomas Falcon4d96f122017-08-01 15:04:36 -0500840 rc = init_sub_crq_irqs(adapter);
841 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500842 netdev_warn(netdev,
843 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500844 return -1;
845 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500846 } else if (adapter->init_done_rc) {
847 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500848 return -1;
849 }
Thomas Falconeb110412018-05-24 14:37:53 -0500850 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500851
Thomas Falcon62740e92019-05-09 23:13:43 -0500852 __ibmvnic_set_mac(netdev, adapter->mac_addr);
Thomas Falcon3d166132018-01-10 19:39:52 -0600853
John Allena57a5d22017-03-17 17:13:41 -0500854 return 0;
855}
856
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600857static void release_login_buffer(struct ibmvnic_adapter *adapter)
858{
859 kfree(adapter->login_buf);
860 adapter->login_buf = NULL;
861}
862
863static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
864{
865 kfree(adapter->login_rsp_buf);
866 adapter->login_rsp_buf = NULL;
867}
868
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400869static void release_resources(struct ibmvnic_adapter *adapter)
870{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200871 release_vpd_data(adapter);
872
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400873 release_tx_pools(adapter);
874 release_rx_pools(adapter);
875
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600876 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600877 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400878}
879
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400880static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
881{
882 struct net_device *netdev = adapter->netdev;
883 unsigned long timeout = msecs_to_jiffies(30000);
884 union ibmvnic_crq crq;
885 bool resend;
886 int rc;
887
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500888 netdev_dbg(netdev, "setting link state %d\n", link_state);
889
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400890 memset(&crq, 0, sizeof(crq));
891 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
892 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
893 crq.logical_link_state.link_state = link_state;
894
895 do {
896 resend = false;
897
898 reinit_completion(&adapter->init_done);
899 rc = ibmvnic_send_crq(adapter, &crq);
900 if (rc) {
901 netdev_err(netdev, "Failed to set link state\n");
902 return rc;
903 }
904
905 if (!wait_for_completion_timeout(&adapter->init_done,
906 timeout)) {
907 netdev_err(netdev, "timeout setting link state\n");
908 return -1;
909 }
910
911 if (adapter->init_done_rc == 1) {
912 /* Partuial success, delay and re-send */
913 mdelay(1000);
914 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500915 } else if (adapter->init_done_rc) {
916 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
917 adapter->init_done_rc);
918 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400919 }
920 } while (resend);
921
922 return 0;
923}
924
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400925static int set_real_num_queues(struct net_device *netdev)
926{
927 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
928 int rc;
929
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500930 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
931 adapter->req_tx_queues, adapter->req_rx_queues);
932
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400933 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
934 if (rc) {
935 netdev_err(netdev, "failed to set the number of tx queues\n");
936 return rc;
937 }
938
939 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
940 if (rc)
941 netdev_err(netdev, "failed to set the number of rx queues\n");
942
943 return rc;
944}
945
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200946static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
947{
948 struct device *dev = &adapter->vdev->dev;
949 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200950 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500951 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200952
953 if (adapter->vpd->buff)
954 len = adapter->vpd->len;
955
John Allen69d08dc2018-01-18 16:27:58 -0600956 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200957 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
958 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500959 rc = ibmvnic_send_crq(adapter, &crq);
960 if (rc)
961 return rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200962 wait_for_completion(&adapter->fw_done);
963
964 if (!adapter->vpd->len)
965 return -ENODATA;
966
967 if (!adapter->vpd->buff)
968 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
969 else if (adapter->vpd->len != len)
970 adapter->vpd->buff =
971 krealloc(adapter->vpd->buff,
972 adapter->vpd->len, GFP_KERNEL);
973
974 if (!adapter->vpd->buff) {
975 dev_err(dev, "Could allocate VPD buffer\n");
976 return -ENOMEM;
977 }
978
979 adapter->vpd->dma_addr =
980 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
981 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200982 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200983 dev_err(dev, "Could not map VPD buffer\n");
984 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600985 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200986 return -ENOMEM;
987 }
988
989 reinit_completion(&adapter->fw_done);
990 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
991 crq.get_vpd.cmd = GET_VPD;
992 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
993 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500994 rc = ibmvnic_send_crq(adapter, &crq);
995 if (rc) {
996 kfree(adapter->vpd->buff);
997 adapter->vpd->buff = NULL;
998 return rc;
999 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001000 wait_for_completion(&adapter->fw_done);
1001
1002 return 0;
1003}
1004
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001005static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001006{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001007 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001008 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001009
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001010 rc = set_real_num_queues(netdev);
1011 if (rc)
1012 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001013
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001014 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1015 if (!adapter->vpd)
1016 return -ENOMEM;
1017
John Allen69d08dc2018-01-18 16:27:58 -06001018 /* Vital Product Data (VPD) */
1019 rc = ibmvnic_get_vpd(adapter);
1020 if (rc) {
1021 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1022 return rc;
1023 }
1024
Thomas Falcon032c5e82015-12-21 11:26:06 -06001025 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001026
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001027 rc = init_napi(adapter);
1028 if (rc)
1029 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001030
Thomas Falcon032c5e82015-12-21 11:26:06 -06001031 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001032
1033 rc = init_rx_pools(netdev);
1034 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001035 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001036
Nathan Fontenotc657e322017-03-30 02:49:06 -04001037 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001038 return rc;
1039}
1040
Nathan Fontenoted651a12017-05-03 14:04:38 -04001041static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001042{
1043 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001044 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001045 int i, rc;
1046
Nathan Fontenot90c80142017-05-03 14:04:32 -04001047 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001048 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001049 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001050
Thomas Falcon032c5e82015-12-21 11:26:06 -06001051 /* We're ready to receive frames, enable the sub-crq interrupts and
1052 * set the logical link state to up
1053 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001054 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001055 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001056 if (prev_state == VNIC_CLOSED)
1057 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001058 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001059 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001060
Nathan Fontenoted651a12017-05-03 14:04:38 -04001061 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001062 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001063 if (prev_state == VNIC_CLOSED)
1064 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001065 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001066 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001067
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001068 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001069 if (rc) {
1070 for (i = 0; i < adapter->req_rx_queues; i++)
1071 napi_disable(&adapter->napi[i]);
1072 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001073 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001074 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001075
Nathan Fontenoted651a12017-05-03 14:04:38 -04001076 netif_tx_start_all_queues(netdev);
1077
1078 if (prev_state == VNIC_CLOSED) {
1079 for (i = 0; i < adapter->req_rx_queues; i++)
1080 napi_schedule(&adapter->napi[i]);
1081 }
1082
1083 adapter->state = VNIC_OPEN;
1084 return rc;
1085}
1086
1087static int ibmvnic_open(struct net_device *netdev)
1088{
1089 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001090 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001091
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001092 /* If device failover is pending, just set device state and return.
1093 * Device operation will be handled by reset routine.
1094 */
1095 if (adapter->failover_pending) {
1096 adapter->state = VNIC_OPEN;
1097 return 0;
1098 }
1099
Nathan Fontenoted651a12017-05-03 14:04:38 -04001100 if (adapter->state != VNIC_CLOSED) {
1101 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001102 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001103 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001104
1105 rc = init_resources(adapter);
1106 if (rc) {
1107 netdev_err(netdev, "failed to initialize resources\n");
1108 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001109 return rc;
1110 }
1111 }
1112
1113 rc = __ibmvnic_open(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001114
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001115 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001116}
1117
Thomas Falcond0869c02018-02-13 18:23:43 -06001118static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1119{
1120 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001121 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001122 u64 rx_entries;
1123 int rx_scrqs;
1124 int i, j;
1125
1126 if (!adapter->rx_pool)
1127 return;
1128
Thomas Falcon660e3092018-04-20 14:25:32 -05001129 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001130 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1131
1132 /* Free any remaining skbs in the rx buffer pools */
1133 for (i = 0; i < rx_scrqs; i++) {
1134 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001135 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001136 continue;
1137
1138 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1139 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001140 rx_buff = &rx_pool->rx_buff[j];
1141 if (rx_buff && rx_buff->skb) {
1142 dev_kfree_skb_any(rx_buff->skb);
1143 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001144 }
1145 }
1146 }
1147}
1148
Thomas Falcone9e1e972018-03-16 20:00:30 -05001149static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1150 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001151{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001152 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001153 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001154 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001155
Dan Carpenter050e85c2018-03-23 14:36:15 +03001156 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001157 return;
1158
1159 tx_entries = tx_pool->num_buffers;
1160
1161 for (i = 0; i < tx_entries; i++) {
1162 tx_buff = &tx_pool->tx_buff[i];
1163 if (tx_buff && tx_buff->skb) {
1164 dev_kfree_skb_any(tx_buff->skb);
1165 tx_buff->skb = NULL;
1166 }
1167 }
1168}
1169
1170static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1171{
1172 int tx_scrqs;
1173 int i;
1174
1175 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001176 return;
1177
Thomas Falcon660e3092018-04-20 14:25:32 -05001178 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001179
1180 /* Free any remaining skbs in the tx buffer pools */
1181 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001182 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001183 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1184 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001185 }
1186}
1187
John Allen6095e592018-03-30 13:44:21 -05001188static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001189{
John Allen6095e592018-03-30 13:44:21 -05001190 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001191 int i;
1192
Nathan Fontenot46293b92017-05-03 14:05:02 -04001193 if (adapter->tx_scrq) {
1194 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001195 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001196 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001197 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001198 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001199 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001200 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001201 }
1202
Nathan Fontenot46293b92017-05-03 14:05:02 -04001203 if (adapter->rx_scrq) {
1204 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001205 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001206 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001207 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001208 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001209 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001210 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001211 }
1212 }
John Allen6095e592018-03-30 13:44:21 -05001213}
1214
1215static void ibmvnic_cleanup(struct net_device *netdev)
1216{
1217 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1218
1219 /* ensure that transmissions are stopped if called by do_reset */
1220 if (adapter->resetting)
1221 netif_tx_disable(netdev);
1222 else
1223 netif_tx_stop_all_queues(netdev);
1224
1225 ibmvnic_napi_disable(adapter);
1226 ibmvnic_disable_irqs(adapter);
1227
Thomas Falcond0869c02018-02-13 18:23:43 -06001228 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001229 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001230}
1231
1232static int __ibmvnic_close(struct net_device *netdev)
1233{
1234 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1235 int rc = 0;
1236
1237 adapter->state = VNIC_CLOSING;
1238 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1239 if (rc)
1240 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001241 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001242 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001243}
1244
Nathan Fontenoted651a12017-05-03 14:04:38 -04001245static int ibmvnic_close(struct net_device *netdev)
1246{
1247 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1248 int rc;
1249
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001250 /* If device failover is pending, just set device state and return.
1251 * Device operation will be handled by reset routine.
1252 */
1253 if (adapter->failover_pending) {
1254 adapter->state = VNIC_CLOSED;
1255 return 0;
1256 }
1257
Nathan Fontenoted651a12017-05-03 14:04:38 -04001258 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001259 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001260
1261 return rc;
1262}
1263
Thomas Falconad7775d2016-04-01 17:20:34 -05001264/**
1265 * build_hdr_data - creates L2/L3/L4 header data buffer
1266 * @hdr_field - bitfield determining needed headers
1267 * @skb - socket buffer
1268 * @hdr_len - array of header lengths
1269 * @tot_len - total length of data
1270 *
1271 * Reads hdr_field to determine which headers are needed by firmware.
1272 * Builds a buffer containing these headers. Saves individual header
1273 * lengths and total buffer length to be used to build descriptors.
1274 */
1275static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1276 int *hdr_len, u8 *hdr_data)
1277{
1278 int len = 0;
1279 u8 *hdr;
1280
Thomas Falconda75e3b2018-03-12 11:51:02 -05001281 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1282 hdr_len[0] = sizeof(struct vlan_ethhdr);
1283 else
1284 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001285
1286 if (skb->protocol == htons(ETH_P_IP)) {
1287 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1288 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1289 hdr_len[2] = tcp_hdrlen(skb);
1290 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1291 hdr_len[2] = sizeof(struct udphdr);
1292 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1293 hdr_len[1] = sizeof(struct ipv6hdr);
1294 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1295 hdr_len[2] = tcp_hdrlen(skb);
1296 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1297 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001298 } else if (skb->protocol == htons(ETH_P_ARP)) {
1299 hdr_len[1] = arp_hdr_len(skb->dev);
1300 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001301 }
1302
1303 memset(hdr_data, 0, 120);
1304 if ((hdr_field >> 6) & 1) {
1305 hdr = skb_mac_header(skb);
1306 memcpy(hdr_data, hdr, hdr_len[0]);
1307 len += hdr_len[0];
1308 }
1309
1310 if ((hdr_field >> 5) & 1) {
1311 hdr = skb_network_header(skb);
1312 memcpy(hdr_data + len, hdr, hdr_len[1]);
1313 len += hdr_len[1];
1314 }
1315
1316 if ((hdr_field >> 4) & 1) {
1317 hdr = skb_transport_header(skb);
1318 memcpy(hdr_data + len, hdr, hdr_len[2]);
1319 len += hdr_len[2];
1320 }
1321 return len;
1322}
1323
1324/**
1325 * create_hdr_descs - create header and header extension descriptors
1326 * @hdr_field - bitfield determining needed headers
1327 * @data - buffer containing header data
1328 * @len - length of data buffer
1329 * @hdr_len - array of individual header lengths
1330 * @scrq_arr - descriptor array
1331 *
1332 * Creates header and, if needed, header extension descriptors and
1333 * places them in a descriptor array, scrq_arr
1334 */
1335
Thomas Falcon2de09682017-10-16 10:02:11 -05001336static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1337 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001338{
1339 union sub_crq hdr_desc;
1340 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001341 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001342 u8 *data, *cur;
1343 int tmp;
1344
1345 while (tmp_len > 0) {
1346 cur = hdr_data + len - tmp_len;
1347
1348 memset(&hdr_desc, 0, sizeof(hdr_desc));
1349 if (cur != hdr_data) {
1350 data = hdr_desc.hdr_ext.data;
1351 tmp = tmp_len > 29 ? 29 : tmp_len;
1352 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1353 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1354 hdr_desc.hdr_ext.len = tmp;
1355 } else {
1356 data = hdr_desc.hdr.data;
1357 tmp = tmp_len > 24 ? 24 : tmp_len;
1358 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1359 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1360 hdr_desc.hdr.len = tmp;
1361 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1362 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1363 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1364 hdr_desc.hdr.flag = hdr_field << 1;
1365 }
1366 memcpy(data, cur, tmp);
1367 tmp_len -= tmp;
1368 *scrq_arr = hdr_desc;
1369 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001370 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001371 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001372
1373 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001374}
1375
1376/**
1377 * build_hdr_descs_arr - build a header descriptor array
1378 * @skb - socket buffer
1379 * @num_entries - number of descriptors to be sent
1380 * @subcrq - first TX descriptor
1381 * @hdr_field - bit field determining which headers will be sent
1382 *
1383 * This function will build a TX descriptor array with applicable
1384 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1385 */
1386
1387static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1388 int *num_entries, u8 hdr_field)
1389{
1390 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001391 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001392 u8 *hdr_data = txbuff->hdr_data;
1393
1394 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1395 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001396 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001397 txbuff->indir_arr + 1);
1398}
1399
Thomas Falcon1f247a62018-03-12 11:51:04 -05001400static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1401 struct net_device *netdev)
1402{
1403 /* For some backing devices, mishandling of small packets
1404 * can result in a loss of connection or TX stall. Device
1405 * architects recommend that no packet should be smaller
1406 * than the minimum MTU value provided to the driver, so
1407 * pad any packets to that length
1408 */
1409 if (skb->len < netdev->min_mtu)
1410 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001411
1412 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001413}
1414
YueHaibing94b2bb22018-09-18 14:35:47 +08001415static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001416{
1417 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1418 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001419 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001420 struct device *dev = &adapter->vdev->dev;
1421 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001422 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001423 struct ibmvnic_tx_pool *tx_pool;
1424 unsigned int tx_send_failed = 0;
1425 unsigned int tx_map_failed = 0;
1426 unsigned int tx_dropped = 0;
1427 unsigned int tx_packets = 0;
1428 unsigned int tx_bytes = 0;
1429 dma_addr_t data_dma_addr;
1430 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001431 unsigned long lpar_rc;
1432 union sub_crq tx_crq;
1433 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001434 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001435 unsigned char *dst;
1436 u64 *handle_array;
1437 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001438 u8 proto = 0;
YueHaibing94b2bb22018-09-18 14:35:47 +08001439 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001440
Nathan Fontenoted651a12017-05-03 14:04:38 -04001441 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001442 if (!netif_subqueue_stopped(netdev, skb))
1443 netif_stop_subqueue(netdev, queue_num);
1444 dev_kfree_skb_any(skb);
1445
Thomas Falcon032c5e82015-12-21 11:26:06 -06001446 tx_send_failed++;
1447 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001448 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001449 goto out;
1450 }
1451
Thomas Falcon7083a452018-03-12 21:05:26 -05001452 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001453 tx_dropped++;
1454 tx_send_failed++;
1455 ret = NETDEV_TX_OK;
1456 goto out;
1457 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001458 if (skb_is_gso(skb))
1459 tx_pool = &adapter->tso_pool[queue_num];
1460 else
1461 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001462
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001463 tx_scrq = adapter->tx_scrq[queue_num];
1464 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1465 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1466 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1467
Thomas Falcon032c5e82015-12-21 11:26:06 -06001468 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001469
Thomas Falcon86b61a52018-03-16 20:00:29 -05001470 if (index == IBMVNIC_INVALID_MAP) {
1471 dev_kfree_skb_any(skb);
1472 tx_send_failed++;
1473 tx_dropped++;
1474 ret = NETDEV_TX_OK;
1475 goto out;
1476 }
1477
1478 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1479
Thomas Falcon06b3e352018-03-16 20:00:28 -05001480 offset = index * tx_pool->buf_size;
1481 dst = tx_pool->long_term_buff.buff + offset;
1482 memset(dst, 0, tx_pool->buf_size);
1483 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001484
Thomas Falcon15482052017-10-17 12:36:54 -05001485 if (skb_shinfo(skb)->nr_frags) {
1486 int cur, i;
1487
1488 /* Copy the head */
1489 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1490 cur = skb_headlen(skb);
1491
1492 /* Copy the frags */
1493 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1494 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1495
1496 memcpy(dst + cur,
1497 page_address(skb_frag_page(frag)) +
1498 frag->page_offset, skb_frag_size(frag));
1499 cur += skb_frag_size(frag);
1500 }
1501 } else {
1502 skb_copy_from_linear_data(skb, dst, skb->len);
1503 }
1504
Thomas Falcon032c5e82015-12-21 11:26:06 -06001505 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001506 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001507
1508 tx_buff = &tx_pool->tx_buff[index];
1509 tx_buff->skb = skb;
1510 tx_buff->data_dma[0] = data_dma_addr;
1511 tx_buff->data_len[0] = skb->len;
1512 tx_buff->index = index;
1513 tx_buff->pool_index = queue_num;
1514 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001515
1516 memset(&tx_crq, 0, sizeof(tx_crq));
1517 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1518 tx_crq.v1.type = IBMVNIC_TX_DESC;
1519 tx_crq.v1.n_crq_elem = 1;
1520 tx_crq.v1.n_sge = 1;
1521 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001522
Thomas Falconfdb06102017-10-17 12:36:55 -05001523 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001524 tx_crq.v1.correlator =
1525 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001526 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001527 tx_crq.v1.correlator = cpu_to_be32(index);
1528 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001529 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1530 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1531
Michał Mirosławe84b4792018-11-07 17:50:52 +01001532 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001533 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1534 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1535 }
1536
1537 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001538 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1539 proto = ip_hdr(skb)->protocol;
1540 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1541 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1542 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001543 }
1544
Thomas Falcona0dca102018-01-18 19:29:48 -06001545 if (proto == IPPROTO_TCP)
1546 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1547 else if (proto == IPPROTO_UDP)
1548 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1549
Thomas Falconad7775d2016-04-01 17:20:34 -05001550 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001551 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001552 hdrs += 2;
1553 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001554 if (skb_is_gso(skb)) {
1555 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1556 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1557 hdrs += 2;
1558 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001559 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001560 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001561 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1562 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001563 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001564 tx_buff->indir_arr[0] = tx_crq;
1565 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1566 sizeof(tx_buff->indir_arr),
1567 DMA_TO_DEVICE);
1568 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001569 dev_kfree_skb_any(skb);
1570 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001571 if (!firmware_has_feature(FW_FEATURE_CMO))
1572 dev_err(dev, "tx: unable to map descriptor array\n");
1573 tx_map_failed++;
1574 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001575 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001576 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001577 }
John Allen498cd8e2016-04-06 11:49:55 -05001578 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001579 (u64)tx_buff->indir_dma,
1580 (u64)num_entries);
1581 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001582 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001583 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1584 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001585 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001586 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001587 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1588 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001589 dev_kfree_skb_any(skb);
1590 tx_buff->skb = NULL;
1591
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001592 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1593 /* Disable TX and report carrier off if queue is closed
1594 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001595 * Firmware guarantees that a signal will be sent to the
1596 * driver, triggering a reset or some other action.
1597 */
1598 netif_tx_stop_all_queues(netdev);
1599 netif_carrier_off(netdev);
1600 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001601
Thomas Falcon032c5e82015-12-21 11:26:06 -06001602 tx_send_failed++;
1603 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001604 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001605 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001606 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001607
Thomas Falconffc385b2018-02-18 10:08:41 -06001608 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001609 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001610 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001611 netif_stop_subqueue(netdev, queue_num);
1612 }
1613
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 tx_packets++;
1615 tx_bytes += skb->len;
1616 txq->trans_start = jiffies;
1617 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001618 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001619
Thomas Falcon86b61a52018-03-16 20:00:29 -05001620tx_err_out:
1621 /* roll back consumer index and map array*/
1622 if (tx_pool->consumer_index == 0)
1623 tx_pool->consumer_index =
1624 tx_pool->num_buffers - 1;
1625 else
1626 tx_pool->consumer_index--;
1627 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001628out:
1629 netdev->stats.tx_dropped += tx_dropped;
1630 netdev->stats.tx_bytes += tx_bytes;
1631 netdev->stats.tx_packets += tx_packets;
1632 adapter->tx_send_failed += tx_send_failed;
1633 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001634 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1635 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1636 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001637
1638 return ret;
1639}
1640
1641static void ibmvnic_set_multi(struct net_device *netdev)
1642{
1643 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1644 struct netdev_hw_addr *ha;
1645 union ibmvnic_crq crq;
1646
1647 memset(&crq, 0, sizeof(crq));
1648 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1649 crq.request_capability.cmd = REQUEST_CAPABILITY;
1650
1651 if (netdev->flags & IFF_PROMISC) {
1652 if (!adapter->promisc_supported)
1653 return;
1654 } else {
1655 if (netdev->flags & IFF_ALLMULTI) {
1656 /* Accept all multicast */
1657 memset(&crq, 0, sizeof(crq));
1658 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1659 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1660 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1661 ibmvnic_send_crq(adapter, &crq);
1662 } else if (netdev_mc_empty(netdev)) {
1663 /* Reject all multicast */
1664 memset(&crq, 0, sizeof(crq));
1665 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1666 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1667 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1668 ibmvnic_send_crq(adapter, &crq);
1669 } else {
1670 /* Accept one or more multicast(s) */
1671 netdev_for_each_mc_addr(ha, netdev) {
1672 memset(&crq, 0, sizeof(crq));
1673 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1674 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1675 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1676 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1677 ha->addr);
1678 ibmvnic_send_crq(adapter, &crq);
1679 }
1680 }
1681 }
1682}
1683
Thomas Falcon62740e92019-05-09 23:13:43 -05001684static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001685{
1686 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001687 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001688 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001689
Thomas Falcon62740e92019-05-09 23:13:43 -05001690 if (!is_valid_ether_addr(dev_addr)) {
1691 rc = -EADDRNOTAVAIL;
1692 goto err;
1693 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001694
1695 memset(&crq, 0, sizeof(crq));
1696 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1697 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
Thomas Falcon62740e92019-05-09 23:13:43 -05001698 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
Thomas Falconf8136142018-01-29 13:45:05 -06001699
1700 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001701 rc = ibmvnic_send_crq(adapter, &crq);
Thomas Falcon62740e92019-05-09 23:13:43 -05001702 if (rc) {
1703 rc = -EIO;
1704 goto err;
1705 }
1706
Thomas Falconf8136142018-01-29 13:45:05 -06001707 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001708 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falcon62740e92019-05-09 23:13:43 -05001709 if (adapter->fw_done_rc) {
1710 rc = -EIO;
1711 goto err;
1712 }
1713
1714 return 0;
1715err:
1716 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1717 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001718}
1719
John Allenc26eba02017-10-26 16:23:25 -05001720static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1721{
1722 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1723 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001724 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001725
Thomas Falcon62740e92019-05-09 23:13:43 -05001726 rc = 0;
1727 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1728 if (adapter->state != VNIC_PROBED)
1729 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
John Allenc26eba02017-10-26 16:23:25 -05001730
Thomas Falconf8136142018-01-29 13:45:05 -06001731 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001732}
1733
Nathan Fontenoted651a12017-05-03 14:04:38 -04001734/**
1735 * do_reset returns zero if we are able to keep processing reset events, or
1736 * non-zero if we hit a fatal error and must halt.
1737 */
1738static int do_reset(struct ibmvnic_adapter *adapter,
1739 struct ibmvnic_rwi *rwi, u32 reset_state)
1740{
John Allen896d8692018-01-18 16:26:31 -06001741 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001742 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001743 struct net_device *netdev = adapter->netdev;
1744 int i, rc;
1745
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001746 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1747 rwi->reset_reason);
1748
Nathan Fontenoted651a12017-05-03 14:04:38 -04001749 netif_carrier_off(netdev);
1750 adapter->reset_reason = rwi->reset_reason;
1751
John Allen896d8692018-01-18 16:26:31 -06001752 old_num_rx_queues = adapter->req_rx_queues;
1753 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001754 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1755 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001756
Nathan Fontenot30f79622018-04-06 18:37:06 -05001757 ibmvnic_cleanup(netdev);
1758
1759 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1760 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001761 rc = __ibmvnic_close(netdev);
1762 if (rc)
1763 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001764 }
1765
John Allenc26eba02017-10-26 16:23:25 -05001766 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1767 adapter->wait_for_reset) {
1768 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001769 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001770 release_crq_queue(adapter);
1771 }
1772
John Allen8cb31cf2017-05-26 10:30:37 -04001773 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1774 /* remove the closed state so when we call open it appears
1775 * we are coming from the probed state.
1776 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001777 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001778
Nathan Fontenot30f79622018-04-06 18:37:06 -05001779 if (adapter->wait_for_reset) {
1780 rc = init_crq_queue(adapter);
1781 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1782 rc = ibmvnic_reenable_crq_queue(adapter);
1783 release_sub_crqs(adapter, 1);
1784 } else {
1785 rc = ibmvnic_reset_crq(adapter);
1786 if (!rc)
1787 rc = vio_enable_interrupts(adapter->vdev);
1788 }
1789
1790 if (rc) {
1791 netdev_err(adapter->netdev,
1792 "Couldn't initialize crq. rc=%d\n", rc);
1793 return rc;
1794 }
1795
Thomas Falcon8a348452018-05-23 13:38:00 -05001796 rc = ibmvnic_reset_init(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001797 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001798 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001799
1800 /* If the adapter was in PROBE state prior to the reset,
1801 * exit here.
1802 */
1803 if (reset_state == VNIC_PROBED)
1804 return 0;
1805
1806 rc = ibmvnic_login(netdev);
1807 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05001808 adapter->state = reset_state;
1809 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001810 }
1811
John Allenc26eba02017-10-26 16:23:25 -05001812 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1813 adapter->wait_for_reset) {
1814 rc = init_resources(adapter);
1815 if (rc)
1816 return rc;
John Allen896d8692018-01-18 16:26:31 -06001817 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001818 adapter->req_tx_queues != old_num_tx_queues ||
1819 adapter->req_rx_add_entries_per_subcrq !=
1820 old_num_rx_slots ||
1821 adapter->req_tx_entries_per_subcrq !=
1822 old_num_tx_slots) {
John Allen896d8692018-01-18 16:26:31 -06001823 release_rx_pools(adapter);
1824 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06001825 release_napi(adapter);
1826 release_vpd_data(adapter);
1827
1828 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05001829 if (rc)
1830 return rc;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001831
John Allenc26eba02017-10-26 16:23:25 -05001832 } else {
1833 rc = reset_tx_pools(adapter);
1834 if (rc)
1835 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001836
John Allenc26eba02017-10-26 16:23:25 -05001837 rc = reset_rx_pools(adapter);
1838 if (rc)
1839 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001840 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05001841 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001842 }
John Allene676d812018-03-14 10:41:29 -05001843 adapter->state = VNIC_CLOSED;
1844
1845 if (reset_state == VNIC_CLOSED)
1846 return 0;
1847
Nathan Fontenoted651a12017-05-03 14:04:38 -04001848 rc = __ibmvnic_open(netdev);
1849 if (rc) {
1850 if (list_empty(&adapter->rwi_list))
1851 adapter->state = VNIC_CLOSED;
1852 else
1853 adapter->state = reset_state;
1854
1855 return 0;
1856 }
1857
Nathan Fontenoted651a12017-05-03 14:04:38 -04001858 /* kick napi */
1859 for (i = 0; i < adapter->req_rx_queues; i++)
1860 napi_schedule(&adapter->napi[i]);
1861
Nathan Fontenotebc701b72018-04-11 10:09:38 -05001862 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1863 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
Thomas Falcon986103e2018-11-30 10:59:08 -06001864 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001865
Nathan Fontenoted651a12017-05-03 14:04:38 -04001866 return 0;
1867}
1868
Thomas Falcon2770a792018-05-23 13:38:02 -05001869static int do_hard_reset(struct ibmvnic_adapter *adapter,
1870 struct ibmvnic_rwi *rwi, u32 reset_state)
1871{
1872 struct net_device *netdev = adapter->netdev;
1873 int rc;
1874
1875 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
1876 rwi->reset_reason);
1877
1878 netif_carrier_off(netdev);
1879 adapter->reset_reason = rwi->reset_reason;
1880
1881 ibmvnic_cleanup(netdev);
1882 release_resources(adapter);
1883 release_sub_crqs(adapter, 0);
1884 release_crq_queue(adapter);
1885
1886 /* remove the closed state so when we call open it appears
1887 * we are coming from the probed state.
1888 */
1889 adapter->state = VNIC_PROBED;
1890
Thomas Falconbbd669a2019-04-04 18:58:26 -05001891 reinit_completion(&adapter->init_done);
Thomas Falcon2770a792018-05-23 13:38:02 -05001892 rc = init_crq_queue(adapter);
1893 if (rc) {
1894 netdev_err(adapter->netdev,
1895 "Couldn't initialize crq. rc=%d\n", rc);
1896 return rc;
1897 }
1898
1899 rc = ibmvnic_init(adapter);
1900 if (rc)
1901 return rc;
1902
1903 /* If the adapter was in PROBE state prior to the reset,
1904 * exit here.
1905 */
1906 if (reset_state == VNIC_PROBED)
1907 return 0;
1908
1909 rc = ibmvnic_login(netdev);
1910 if (rc) {
1911 adapter->state = VNIC_PROBED;
1912 return 0;
1913 }
Juliet Kima5681e22018-11-19 15:59:22 -06001914
1915 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05001916 if (rc)
1917 return rc;
1918
1919 ibmvnic_disable_irqs(adapter);
1920 adapter->state = VNIC_CLOSED;
1921
1922 if (reset_state == VNIC_CLOSED)
1923 return 0;
1924
1925 rc = __ibmvnic_open(netdev);
1926 if (rc) {
1927 if (list_empty(&adapter->rwi_list))
1928 adapter->state = VNIC_CLOSED;
1929 else
1930 adapter->state = reset_state;
1931
1932 return 0;
1933 }
1934
Thomas Falcon2770a792018-05-23 13:38:02 -05001935 return 0;
1936}
1937
Nathan Fontenoted651a12017-05-03 14:04:38 -04001938static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1939{
1940 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001941 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001942
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001943 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001944
1945 if (!list_empty(&adapter->rwi_list)) {
1946 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1947 list);
1948 list_del(&rwi->list);
1949 } else {
1950 rwi = NULL;
1951 }
1952
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001953 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001954 return rwi;
1955}
1956
1957static void free_all_rwi(struct ibmvnic_adapter *adapter)
1958{
1959 struct ibmvnic_rwi *rwi;
1960
1961 rwi = get_next_rwi(adapter);
1962 while (rwi) {
1963 kfree(rwi);
1964 rwi = get_next_rwi(adapter);
1965 }
1966}
1967
1968static void __ibmvnic_reset(struct work_struct *work)
1969{
1970 struct ibmvnic_rwi *rwi;
1971 struct ibmvnic_adapter *adapter;
Juliet Kima5681e22018-11-19 15:59:22 -06001972 bool we_lock_rtnl = false;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001973 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001974 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001975
1976 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001977
Juliet Kima5681e22018-11-19 15:59:22 -06001978 /* netif_set_real_num_xx_queues needs to take rtnl lock here
1979 * unless wait_for_reset is set, in which case the rtnl lock
1980 * has already been taken before initializing the reset
1981 */
1982 if (!adapter->wait_for_reset) {
1983 rtnl_lock();
1984 we_lock_rtnl = true;
1985 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001986 reset_state = adapter->state;
1987
1988 rwi = get_next_rwi(adapter);
1989 while (rwi) {
Thomas Falcon2770a792018-05-23 13:38:02 -05001990 if (adapter->force_reset_recovery) {
1991 adapter->force_reset_recovery = false;
1992 rc = do_hard_reset(adapter, rwi, reset_state);
1993 } else {
1994 rc = do_reset(adapter, rwi, reset_state);
1995 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001996 kfree(rwi);
Thomas Falcon2770a792018-05-23 13:38:02 -05001997 if (rc && rc != IBMVNIC_INIT_FAILED &&
1998 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001999 break;
2000
2001 rwi = get_next_rwi(adapter);
2002 }
2003
John Allenc26eba02017-10-26 16:23:25 -05002004 if (adapter->wait_for_reset) {
2005 adapter->wait_for_reset = false;
2006 adapter->reset_done_rc = rc;
2007 complete(&adapter->reset_done);
2008 }
2009
Nathan Fontenoted651a12017-05-03 14:04:38 -04002010 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002011 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002012 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002013 }
2014
2015 adapter->resetting = false;
Juliet Kima5681e22018-11-19 15:59:22 -06002016 if (we_lock_rtnl)
2017 rtnl_unlock();
Nathan Fontenoted651a12017-05-03 14:04:38 -04002018}
2019
Thomas Falconaf894d22018-04-06 18:37:04 -05002020static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2021 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002022{
Thomas Falcon2770a792018-05-23 13:38:02 -05002023 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002024 struct ibmvnic_rwi *rwi, *tmp;
2025 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002026 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002027 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002028
2029 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002030 adapter->state == VNIC_REMOVED ||
2031 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002032 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002033 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002034 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002035 }
2036
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002037 if (adapter->state == VNIC_PROBING) {
2038 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002039 ret = adapter->init_done_rc = EAGAIN;
2040 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002041 }
2042
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002043 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002044
2045 list_for_each(entry, &adapter->rwi_list) {
2046 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2047 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002048 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002049 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002050 ret = EBUSY;
2051 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002052 }
2053 }
2054
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002055 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002056 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002057 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002058 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002059 ret = ENOMEM;
2060 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002061 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002062 /* if we just received a transport event,
2063 * flush reset queue and process this reset
2064 */
2065 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2066 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2067 list_del(entry);
2068 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002069 rwi->reset_reason = reason;
2070 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002071 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falcon06e43d72018-05-23 13:38:01 -05002072 adapter->resetting = true;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002073 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002074 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002075
2076 return 0;
2077err:
2078 if (adapter->wait_for_reset)
2079 adapter->wait_for_reset = false;
2080 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002081}
2082
Thomas Falcon032c5e82015-12-21 11:26:06 -06002083static void ibmvnic_tx_timeout(struct net_device *dev)
2084{
2085 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002086
Nathan Fontenoted651a12017-05-03 14:04:38 -04002087 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088}
2089
2090static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2091 struct ibmvnic_rx_buff *rx_buff)
2092{
2093 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2094
2095 rx_buff->skb = NULL;
2096
2097 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2098 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2099
2100 atomic_dec(&pool->available);
2101}
2102
2103static int ibmvnic_poll(struct napi_struct *napi, int budget)
2104{
2105 struct net_device *netdev = napi->dev;
2106 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2107 int scrq_num = (int)(napi - adapter->napi);
2108 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002109
Thomas Falcon032c5e82015-12-21 11:26:06 -06002110restart_poll:
2111 while (frames_processed < budget) {
2112 struct sk_buff *skb;
2113 struct ibmvnic_rx_buff *rx_buff;
2114 union sub_crq *next;
2115 u32 length;
2116 u16 offset;
2117 u8 flags = 0;
2118
John Allen34686562018-02-06 16:21:49 -06002119 if (unlikely(adapter->resetting &&
2120 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002121 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2122 napi_complete_done(napi, frames_processed);
2123 return frames_processed;
2124 }
2125
Thomas Falcon032c5e82015-12-21 11:26:06 -06002126 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2127 break;
2128 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2129 rx_buff =
2130 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2131 rx_comp.correlator);
2132 /* do error checking */
2133 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002134 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2135 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002136 /* free the entry */
2137 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002138 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002139 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002140 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002141 } else if (!rx_buff->skb) {
2142 /* free the entry */
2143 next->rx_comp.first = 0;
2144 remove_buff_from_pool(adapter, rx_buff);
2145 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002146 }
2147
2148 length = be32_to_cpu(next->rx_comp.len);
2149 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2150 flags = next->rx_comp.flags;
2151 skb = rx_buff->skb;
2152 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2153 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002154
2155 /* VLAN Header has been stripped by the system firmware and
2156 * needs to be inserted by the driver
2157 */
2158 if (adapter->rx_vlan_header_insertion &&
2159 (flags & IBMVNIC_VLAN_STRIPPED))
2160 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2161 ntohs(next->rx_comp.vlan_tci));
2162
Thomas Falcon032c5e82015-12-21 11:26:06 -06002163 /* free the entry */
2164 next->rx_comp.first = 0;
2165 remove_buff_from_pool(adapter, rx_buff);
2166
2167 skb_put(skb, length);
2168 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002169 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002170
2171 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2172 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2173 skb->ip_summed = CHECKSUM_UNNECESSARY;
2174 }
2175
2176 length = skb->len;
2177 napi_gro_receive(napi, skb); /* send it up */
2178 netdev->stats.rx_packets++;
2179 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002180 adapter->rx_stats_buffers[scrq_num].packets++;
2181 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002182 frames_processed++;
2183 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002184
2185 if (adapter->state != VNIC_CLOSING)
2186 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002187
2188 if (frames_processed < budget) {
2189 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002190 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002191 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2192 napi_reschedule(napi)) {
2193 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2194 goto restart_poll;
2195 }
2196 }
2197 return frames_processed;
2198}
2199
John Allenc26eba02017-10-26 16:23:25 -05002200static int wait_for_reset(struct ibmvnic_adapter *adapter)
2201{
Thomas Falconaf894d22018-04-06 18:37:04 -05002202 int rc, ret;
2203
John Allenc26eba02017-10-26 16:23:25 -05002204 adapter->fallback.mtu = adapter->req_mtu;
2205 adapter->fallback.rx_queues = adapter->req_rx_queues;
2206 adapter->fallback.tx_queues = adapter->req_tx_queues;
2207 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2208 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2209
2210 init_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002211 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002212 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2213 if (rc)
2214 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002215 wait_for_completion(&adapter->reset_done);
2216
Thomas Falconaf894d22018-04-06 18:37:04 -05002217 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002218 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002219 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002220 adapter->desired.mtu = adapter->fallback.mtu;
2221 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2222 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2223 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2224 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2225
2226 init_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002227 adapter->wait_for_reset = true;
2228 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2229 if (rc)
2230 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002231 wait_for_completion(&adapter->reset_done);
2232 }
2233 adapter->wait_for_reset = false;
2234
Thomas Falconaf894d22018-04-06 18:37:04 -05002235 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002236}
2237
John Allen3a807b72017-06-06 16:55:52 -05002238static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2239{
John Allenc26eba02017-10-26 16:23:25 -05002240 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2241
2242 adapter->desired.mtu = new_mtu + ETH_HLEN;
2243
2244 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002245}
2246
Thomas Falconf10b09e2018-03-12 11:51:05 -05002247static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2248 struct net_device *dev,
2249 netdev_features_t features)
2250{
2251 /* Some backing hardware adapters can not
2252 * handle packets with a MSS less than 224
2253 * or with only one segment.
2254 */
2255 if (skb_is_gso(skb)) {
2256 if (skb_shinfo(skb)->gso_size < 224 ||
2257 skb_shinfo(skb)->gso_segs == 1)
2258 features &= ~NETIF_F_GSO_MASK;
2259 }
2260
2261 return features;
2262}
2263
Thomas Falcon032c5e82015-12-21 11:26:06 -06002264static const struct net_device_ops ibmvnic_netdev_ops = {
2265 .ndo_open = ibmvnic_open,
2266 .ndo_stop = ibmvnic_close,
2267 .ndo_start_xmit = ibmvnic_xmit,
2268 .ndo_set_rx_mode = ibmvnic_set_multi,
2269 .ndo_set_mac_address = ibmvnic_set_mac,
2270 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002271 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002272 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002273 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002274};
2275
2276/* ethtool functions */
2277
Philippe Reynes8a433792017-01-07 22:37:29 +01002278static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2279 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002280{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002281 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2282 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002283
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002284 rc = send_query_phys_parms(adapter);
2285 if (rc) {
2286 adapter->speed = SPEED_UNKNOWN;
2287 adapter->duplex = DUPLEX_UNKNOWN;
2288 }
2289 cmd->base.speed = adapter->speed;
2290 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002291 cmd->base.port = PORT_FIBRE;
2292 cmd->base.phy_address = 0;
2293 cmd->base.autoneg = AUTONEG_ENABLE;
2294
Thomas Falcon032c5e82015-12-21 11:26:06 -06002295 return 0;
2296}
2297
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002298static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002299 struct ethtool_drvinfo *info)
2300{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002301 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2302
Thomas Falcon032c5e82015-12-21 11:26:06 -06002303 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2304 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002305 strlcpy(info->fw_version, adapter->fw_version,
2306 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002307}
2308
2309static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2310{
2311 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2312
2313 return adapter->msg_enable;
2314}
2315
2316static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2317{
2318 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2319
2320 adapter->msg_enable = data;
2321}
2322
2323static u32 ibmvnic_get_link(struct net_device *netdev)
2324{
2325 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2326
2327 /* Don't need to send a query because we request a logical link up at
2328 * init and then we wait for link state indications
2329 */
2330 return adapter->logical_link_state;
2331}
2332
2333static void ibmvnic_get_ringparam(struct net_device *netdev,
2334 struct ethtool_ringparam *ring)
2335{
John Allenbc131b32017-08-02 16:46:30 -05002336 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2337
Thomas Falcon723ad912018-09-28 18:38:26 -05002338 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2339 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2340 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2341 } else {
2342 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2343 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2344 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002345 ring->rx_mini_max_pending = 0;
2346 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002347 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2348 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002349 ring->rx_mini_pending = 0;
2350 ring->rx_jumbo_pending = 0;
2351}
2352
John Allenc26eba02017-10-26 16:23:25 -05002353static int ibmvnic_set_ringparam(struct net_device *netdev,
2354 struct ethtool_ringparam *ring)
2355{
2356 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002357 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002358
Thomas Falcon723ad912018-09-28 18:38:26 -05002359 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002360 adapter->desired.rx_entries = ring->rx_pending;
2361 adapter->desired.tx_entries = ring->tx_pending;
2362
Thomas Falcon723ad912018-09-28 18:38:26 -05002363 ret = wait_for_reset(adapter);
2364
2365 if (!ret &&
2366 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2367 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2368 netdev_info(netdev,
2369 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2370 ring->rx_pending, ring->tx_pending,
2371 adapter->req_rx_add_entries_per_subcrq,
2372 adapter->req_tx_entries_per_subcrq);
2373 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002374}
2375
John Allenc2dbeb62017-08-02 16:47:17 -05002376static void ibmvnic_get_channels(struct net_device *netdev,
2377 struct ethtool_channels *channels)
2378{
2379 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2380
Thomas Falcon723ad912018-09-28 18:38:26 -05002381 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2382 channels->max_rx = adapter->max_rx_queues;
2383 channels->max_tx = adapter->max_tx_queues;
2384 } else {
2385 channels->max_rx = IBMVNIC_MAX_QUEUES;
2386 channels->max_tx = IBMVNIC_MAX_QUEUES;
2387 }
2388
John Allenc2dbeb62017-08-02 16:47:17 -05002389 channels->max_other = 0;
2390 channels->max_combined = 0;
2391 channels->rx_count = adapter->req_rx_queues;
2392 channels->tx_count = adapter->req_tx_queues;
2393 channels->other_count = 0;
2394 channels->combined_count = 0;
2395}
2396
John Allenc26eba02017-10-26 16:23:25 -05002397static int ibmvnic_set_channels(struct net_device *netdev,
2398 struct ethtool_channels *channels)
2399{
2400 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002401 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002402
Thomas Falcon723ad912018-09-28 18:38:26 -05002403 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002404 adapter->desired.rx_queues = channels->rx_count;
2405 adapter->desired.tx_queues = channels->tx_count;
2406
Thomas Falcon723ad912018-09-28 18:38:26 -05002407 ret = wait_for_reset(adapter);
2408
2409 if (!ret &&
2410 (adapter->req_rx_queues != channels->rx_count ||
2411 adapter->req_tx_queues != channels->tx_count))
2412 netdev_info(netdev,
2413 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2414 channels->rx_count, channels->tx_count,
2415 adapter->req_rx_queues, adapter->req_tx_queues);
2416 return ret;
2417
John Allenc26eba02017-10-26 16:23:25 -05002418}
2419
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2421{
John Allen3d52b592017-08-02 16:44:14 -05002422 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002423 int i;
2424
Thomas Falcon723ad912018-09-28 18:38:26 -05002425 switch (stringset) {
2426 case ETH_SS_STATS:
2427 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2428 i++, data += ETH_GSTRING_LEN)
2429 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2430
2431 for (i = 0; i < adapter->req_tx_queues; i++) {
2432 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2433 data += ETH_GSTRING_LEN;
2434
2435 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2436 data += ETH_GSTRING_LEN;
2437
2438 snprintf(data, ETH_GSTRING_LEN,
2439 "tx%d_dropped_packets", i);
2440 data += ETH_GSTRING_LEN;
2441 }
2442
2443 for (i = 0; i < adapter->req_rx_queues; i++) {
2444 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2445 data += ETH_GSTRING_LEN;
2446
2447 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2448 data += ETH_GSTRING_LEN;
2449
2450 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2451 data += ETH_GSTRING_LEN;
2452 }
2453 break;
2454
2455 case ETH_SS_PRIV_FLAGS:
2456 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2457 strcpy(data + i * ETH_GSTRING_LEN,
2458 ibmvnic_priv_flags[i]);
2459 break;
2460 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002461 return;
John Allen3d52b592017-08-02 16:44:14 -05002462 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002463}
2464
2465static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2466{
John Allen3d52b592017-08-02 16:44:14 -05002467 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2468
Thomas Falcon032c5e82015-12-21 11:26:06 -06002469 switch (sset) {
2470 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002471 return ARRAY_SIZE(ibmvnic_stats) +
2472 adapter->req_tx_queues * NUM_TX_STATS +
2473 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002474 case ETH_SS_PRIV_FLAGS:
2475 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002476 default:
2477 return -EOPNOTSUPP;
2478 }
2479}
2480
2481static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2482 struct ethtool_stats *stats, u64 *data)
2483{
2484 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2485 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002486 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002487 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002488
2489 memset(&crq, 0, sizeof(crq));
2490 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2491 crq.request_statistics.cmd = REQUEST_STATISTICS;
2492 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2493 crq.request_statistics.len =
2494 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002495
2496 /* Wait for data to be written */
2497 init_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002498 rc = ibmvnic_send_crq(adapter, &crq);
2499 if (rc)
2500 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501 wait_for_completion(&adapter->stats_done);
2502
2503 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002504 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2505 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002506
2507 for (j = 0; j < adapter->req_tx_queues; j++) {
2508 data[i] = adapter->tx_stats_buffers[j].packets;
2509 i++;
2510 data[i] = adapter->tx_stats_buffers[j].bytes;
2511 i++;
2512 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2513 i++;
2514 }
2515
2516 for (j = 0; j < adapter->req_rx_queues; j++) {
2517 data[i] = adapter->rx_stats_buffers[j].packets;
2518 i++;
2519 data[i] = adapter->rx_stats_buffers[j].bytes;
2520 i++;
2521 data[i] = adapter->rx_stats_buffers[j].interrupts;
2522 i++;
2523 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002524}
2525
Thomas Falcon723ad912018-09-28 18:38:26 -05002526static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2527{
2528 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2529
2530 return adapter->priv_flags;
2531}
2532
2533static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2534{
2535 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2536 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2537
2538 if (which_maxes)
2539 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2540 else
2541 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2542
2543 return 0;
2544}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002545static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546 .get_drvinfo = ibmvnic_get_drvinfo,
2547 .get_msglevel = ibmvnic_get_msglevel,
2548 .set_msglevel = ibmvnic_set_msglevel,
2549 .get_link = ibmvnic_get_link,
2550 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002551 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002552 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002553 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002554 .get_strings = ibmvnic_get_strings,
2555 .get_sset_count = ibmvnic_get_sset_count,
2556 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002557 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002558 .get_priv_flags = ibmvnic_get_priv_flags,
2559 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560};
2561
2562/* Routines for managing CRQs/sCRQs */
2563
Nathan Fontenot57a49432017-05-26 10:31:12 -04002564static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2565 struct ibmvnic_sub_crq_queue *scrq)
2566{
2567 int rc;
2568
2569 if (scrq->irq) {
2570 free_irq(scrq->irq, scrq);
2571 irq_dispose_mapping(scrq->irq);
2572 scrq->irq = 0;
2573 }
2574
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002575 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002576 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002577 scrq->cur = 0;
2578
2579 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2580 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2581 return rc;
2582}
2583
2584static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2585{
2586 int i, rc;
2587
2588 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002589 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002590 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2591 if (rc)
2592 return rc;
2593 }
2594
2595 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002596 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002597 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2598 if (rc)
2599 return rc;
2600 }
2601
Nathan Fontenot57a49432017-05-26 10:31:12 -04002602 return rc;
2603}
2604
Thomas Falcon032c5e82015-12-21 11:26:06 -06002605static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002606 struct ibmvnic_sub_crq_queue *scrq,
2607 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002608{
2609 struct device *dev = &adapter->vdev->dev;
2610 long rc;
2611
2612 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2613
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002614 if (do_h_free) {
2615 /* Close the sub-crqs */
2616 do {
2617 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2618 adapter->vdev->unit_address,
2619 scrq->crq_num);
2620 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002621
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002622 if (rc) {
2623 netdev_err(adapter->netdev,
2624 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2625 scrq->crq_num, rc);
2626 }
Thomas Falconffa73852017-04-19 13:44:29 -04002627 }
2628
Thomas Falcon032c5e82015-12-21 11:26:06 -06002629 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2630 DMA_BIDIRECTIONAL);
2631 free_pages((unsigned long)scrq->msgs, 2);
2632 kfree(scrq);
2633}
2634
2635static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2636 *adapter)
2637{
2638 struct device *dev = &adapter->vdev->dev;
2639 struct ibmvnic_sub_crq_queue *scrq;
2640 int rc;
2641
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002642 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002643 if (!scrq)
2644 return NULL;
2645
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002646 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002647 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002648 if (!scrq->msgs) {
2649 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2650 goto zero_page_failed;
2651 }
2652
2653 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2654 DMA_BIDIRECTIONAL);
2655 if (dma_mapping_error(dev, scrq->msg_token)) {
2656 dev_warn(dev, "Couldn't map crq queue messages page\n");
2657 goto map_failed;
2658 }
2659
2660 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2661 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2662
2663 if (rc == H_RESOURCE)
2664 rc = ibmvnic_reset_crq(adapter);
2665
2666 if (rc == H_CLOSED) {
2667 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2668 } else if (rc) {
2669 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2670 goto reg_failed;
2671 }
2672
Thomas Falcon032c5e82015-12-21 11:26:06 -06002673 scrq->adapter = adapter;
2674 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002675 spin_lock_init(&scrq->lock);
2676
2677 netdev_dbg(adapter->netdev,
2678 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2679 scrq->crq_num, scrq->hw_irq, scrq->irq);
2680
2681 return scrq;
2682
Thomas Falcon032c5e82015-12-21 11:26:06 -06002683reg_failed:
2684 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2685 DMA_BIDIRECTIONAL);
2686map_failed:
2687 free_pages((unsigned long)scrq->msgs, 2);
2688zero_page_failed:
2689 kfree(scrq);
2690
2691 return NULL;
2692}
2693
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002694static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695{
2696 int i;
2697
2698 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002699 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002700 if (!adapter->tx_scrq[i])
2701 continue;
2702
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002703 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2704 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002705 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002706 free_irq(adapter->tx_scrq[i]->irq,
2707 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002708 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002709 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002710 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002711
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002712 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2713 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002714 }
2715
Nathan Fontenot9501df32017-03-15 23:38:07 -04002716 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002717 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002718 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 }
2720
2721 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002722 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002723 if (!adapter->rx_scrq[i])
2724 continue;
2725
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002726 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2727 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002728 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002729 free_irq(adapter->rx_scrq[i]->irq,
2730 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002731 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002732 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002733 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002734
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002735 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2736 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002737 }
2738
Nathan Fontenot9501df32017-03-15 23:38:07 -04002739 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002741 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002742 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002743}
2744
2745static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2746 struct ibmvnic_sub_crq_queue *scrq)
2747{
2748 struct device *dev = &adapter->vdev->dev;
2749 unsigned long rc;
2750
2751 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2752 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2753 if (rc)
2754 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2755 scrq->hw_irq, rc);
2756 return rc;
2757}
2758
2759static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2760 struct ibmvnic_sub_crq_queue *scrq)
2761{
2762 struct device *dev = &adapter->vdev->dev;
2763 unsigned long rc;
2764
2765 if (scrq->hw_irq > 0x100000000ULL) {
2766 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2767 return 1;
2768 }
2769
Nathan Fontenot73f9d362018-05-22 11:21:10 -05002770 if (adapter->resetting &&
2771 adapter->reset_reason == VNIC_RESET_MOBILITY) {
2772 u64 val = (0xff000000) | scrq->hw_irq;
2773
2774 rc = plpar_hcall_norets(H_EOI, val);
2775 if (rc)
2776 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2777 val, rc);
2778 }
Thomas Falconf23e0642018-04-15 18:53:36 -05002779
Thomas Falcon032c5e82015-12-21 11:26:06 -06002780 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2781 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2782 if (rc)
2783 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2784 scrq->hw_irq, rc);
2785 return rc;
2786}
2787
2788static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2789 struct ibmvnic_sub_crq_queue *scrq)
2790{
2791 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05002792 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002793 struct ibmvnic_tx_buff *txbuff;
2794 union sub_crq *next;
2795 int index;
2796 int i, j;
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002797 u8 *first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002798
2799restart_loop:
2800 while (pending_scrq(adapter, scrq)) {
2801 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002802 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002803
2804 next = ibmvnic_next_scrq(adapter, scrq);
2805 for (i = 0; i < next->tx_comp.num_comps; i++) {
2806 if (next->tx_comp.rcs[i]) {
2807 dev_err(dev, "tx error %x\n",
2808 next->tx_comp.rcs[i]);
2809 continue;
2810 }
2811 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05002812 if (index & IBMVNIC_TSO_POOL_MASK) {
2813 tx_pool = &adapter->tso_pool[pool];
2814 index &= ~IBMVNIC_TSO_POOL_MASK;
2815 } else {
2816 tx_pool = &adapter->tx_pool[pool];
2817 }
2818
2819 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06002820
2821 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2822 if (!txbuff->data_dma[j])
2823 continue;
2824
2825 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002827 /* if sub_crq was sent indirectly */
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002828 first = &txbuff->indir_arr[0].generic.first;
2829 if (*first == IBMVNIC_CRQ_CMD) {
Thomas Falconad7775d2016-04-01 17:20:34 -05002830 dma_unmap_single(dev, txbuff->indir_dma,
2831 sizeof(txbuff->indir_arr),
2832 DMA_TO_DEVICE);
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002833 *first = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05002834 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002835
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002836 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002837 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002838 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002839 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002840
Thomas Falconffc385b2018-02-18 10:08:41 -06002841 num_entries += txbuff->num_entries;
2842
Thomas Falcon06b3e352018-03-16 20:00:28 -05002843 tx_pool->free_map[tx_pool->producer_index] = index;
2844 tx_pool->producer_index =
2845 (tx_pool->producer_index + 1) %
2846 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002847 }
2848 /* remove tx_comp scrq*/
2849 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002850
Thomas Falconffc385b2018-02-18 10:08:41 -06002851 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002852 (adapter->req_tx_entries_per_subcrq / 2) &&
2853 __netif_subqueue_stopped(adapter->netdev,
2854 scrq->pool_index)) {
2855 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002856 netdev_dbg(adapter->netdev, "Started queue %d\n",
2857 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002858 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002859 }
2860
2861 enable_scrq_irq(adapter, scrq);
2862
2863 if (pending_scrq(adapter, scrq)) {
2864 disable_scrq_irq(adapter, scrq);
2865 goto restart_loop;
2866 }
2867
2868 return 0;
2869}
2870
2871static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2872{
2873 struct ibmvnic_sub_crq_queue *scrq = instance;
2874 struct ibmvnic_adapter *adapter = scrq->adapter;
2875
2876 disable_scrq_irq(adapter, scrq);
2877 ibmvnic_complete_tx(adapter, scrq);
2878
2879 return IRQ_HANDLED;
2880}
2881
2882static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2883{
2884 struct ibmvnic_sub_crq_queue *scrq = instance;
2885 struct ibmvnic_adapter *adapter = scrq->adapter;
2886
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002887 /* When booting a kdump kernel we can hit pending interrupts
2888 * prior to completing driver initialization.
2889 */
2890 if (unlikely(adapter->state != VNIC_OPEN))
2891 return IRQ_NONE;
2892
John Allen3d52b592017-08-02 16:44:14 -05002893 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2894
Thomas Falcon032c5e82015-12-21 11:26:06 -06002895 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2896 disable_scrq_irq(adapter, scrq);
2897 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2898 }
2899
2900 return IRQ_HANDLED;
2901}
2902
Thomas Falconea22d512016-07-06 15:35:17 -05002903static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2904{
2905 struct device *dev = &adapter->vdev->dev;
2906 struct ibmvnic_sub_crq_queue *scrq;
2907 int i = 0, j = 0;
2908 int rc = 0;
2909
2910 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002911 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2912 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002913 scrq = adapter->tx_scrq[i];
2914 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2915
Michael Ellerman99c17902016-09-10 19:59:05 +10002916 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002917 rc = -EINVAL;
2918 dev_err(dev, "Error mapping irq\n");
2919 goto req_tx_irq_failed;
2920 }
2921
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002922 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
2923 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05002924 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002925 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05002926
2927 if (rc) {
2928 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2929 scrq->irq, rc);
2930 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002931 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002932 }
2933 }
2934
2935 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002936 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2937 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002938 scrq = adapter->rx_scrq[i];
2939 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002940 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002941 rc = -EINVAL;
2942 dev_err(dev, "Error mapping irq\n");
2943 goto req_rx_irq_failed;
2944 }
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002945 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
2946 adapter->vdev->unit_address, i);
Thomas Falconea22d512016-07-06 15:35:17 -05002947 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03002948 0, scrq->name, scrq);
Thomas Falconea22d512016-07-06 15:35:17 -05002949 if (rc) {
2950 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2951 scrq->irq, rc);
2952 irq_dispose_mapping(scrq->irq);
2953 goto req_rx_irq_failed;
2954 }
2955 }
2956 return rc;
2957
2958req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002959 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002960 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2961 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002962 }
Thomas Falconea22d512016-07-06 15:35:17 -05002963 i = adapter->req_tx_queues;
2964req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002965 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002966 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2967 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002968 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002969 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002970 return rc;
2971}
2972
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002973static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002974{
2975 struct device *dev = &adapter->vdev->dev;
2976 struct ibmvnic_sub_crq_queue **allqueues;
2977 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002978 int total_queues;
2979 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002980 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002981
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2983
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002984 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002985 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002986 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002987
2988 for (i = 0; i < total_queues; i++) {
2989 allqueues[i] = init_sub_crq_queue(adapter);
2990 if (!allqueues[i]) {
2991 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2992 break;
2993 }
2994 registered_queues++;
2995 }
2996
2997 /* Make sure we were able to register the minimum number of queues */
2998 if (registered_queues <
2999 adapter->min_tx_queues + adapter->min_rx_queues) {
3000 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3001 goto tx_failed;
3002 }
3003
3004 /* Distribute the failed allocated queues*/
3005 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3006 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3007 switch (i % 3) {
3008 case 0:
3009 if (adapter->req_rx_queues > adapter->min_rx_queues)
3010 adapter->req_rx_queues--;
3011 else
3012 more++;
3013 break;
3014 case 1:
3015 if (adapter->req_tx_queues > adapter->min_tx_queues)
3016 adapter->req_tx_queues--;
3017 else
3018 more++;
3019 break;
3020 }
3021 }
3022
3023 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003024 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 if (!adapter->tx_scrq)
3026 goto tx_failed;
3027
3028 for (i = 0; i < adapter->req_tx_queues; i++) {
3029 adapter->tx_scrq[i] = allqueues[i];
3030 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003031 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003032 }
3033
3034 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003035 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003036 if (!adapter->rx_scrq)
3037 goto rx_failed;
3038
3039 for (i = 0; i < adapter->req_rx_queues; i++) {
3040 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3041 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003042 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003043 }
3044
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003045 kfree(allqueues);
3046 return 0;
3047
3048rx_failed:
3049 kfree(adapter->tx_scrq);
3050 adapter->tx_scrq = NULL;
3051tx_failed:
3052 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003053 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003054 kfree(allqueues);
3055 return -1;
3056}
3057
3058static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3059{
3060 struct device *dev = &adapter->vdev->dev;
3061 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003062 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003063
3064 if (!retry) {
3065 /* Sub-CRQ entries are 32 byte long */
3066 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3067
3068 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3069 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3070 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3071 return;
3072 }
3073
John Allenc26eba02017-10-26 16:23:25 -05003074 if (adapter->desired.mtu)
3075 adapter->req_mtu = adapter->desired.mtu;
3076 else
3077 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003078
John Allenc26eba02017-10-26 16:23:25 -05003079 if (!adapter->desired.tx_entries)
3080 adapter->desired.tx_entries =
3081 adapter->max_tx_entries_per_subcrq;
3082 if (!adapter->desired.rx_entries)
3083 adapter->desired.rx_entries =
3084 adapter->max_rx_add_entries_per_subcrq;
3085
3086 max_entries = IBMVNIC_MAX_LTB_SIZE /
3087 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3088
3089 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3090 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3091 adapter->desired.tx_entries = max_entries;
3092 }
3093
3094 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3095 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3096 adapter->desired.rx_entries = max_entries;
3097 }
3098
3099 if (adapter->desired.tx_entries)
3100 adapter->req_tx_entries_per_subcrq =
3101 adapter->desired.tx_entries;
3102 else
3103 adapter->req_tx_entries_per_subcrq =
3104 adapter->max_tx_entries_per_subcrq;
3105
3106 if (adapter->desired.rx_entries)
3107 adapter->req_rx_add_entries_per_subcrq =
3108 adapter->desired.rx_entries;
3109 else
3110 adapter->req_rx_add_entries_per_subcrq =
3111 adapter->max_rx_add_entries_per_subcrq;
3112
3113 if (adapter->desired.tx_queues)
3114 adapter->req_tx_queues =
3115 adapter->desired.tx_queues;
3116 else
3117 adapter->req_tx_queues =
3118 adapter->opt_tx_comp_sub_queues;
3119
3120 if (adapter->desired.rx_queues)
3121 adapter->req_rx_queues =
3122 adapter->desired.rx_queues;
3123 else
3124 adapter->req_rx_queues =
3125 adapter->opt_rx_comp_queues;
3126
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003127 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003128 }
3129
Thomas Falcon032c5e82015-12-21 11:26:06 -06003130 memset(&crq, 0, sizeof(crq));
3131 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3132 crq.request_capability.cmd = REQUEST_CAPABILITY;
3133
3134 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003135 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003136 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003137 ibmvnic_send_crq(adapter, &crq);
3138
3139 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003140 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003141 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003142 ibmvnic_send_crq(adapter, &crq);
3143
3144 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003145 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
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_TX_ENTRIES_PER_SUBCRQ);
3151 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003152 cpu_to_be64(adapter->req_tx_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 =
3157 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3158 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003159 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003160 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003161 ibmvnic_send_crq(adapter, &crq);
3162
3163 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003164 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003165 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003166 ibmvnic_send_crq(adapter, &crq);
3167
3168 if (adapter->netdev->flags & IFF_PROMISC) {
3169 if (adapter->promisc_supported) {
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(1);
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 }
3176 } else {
3177 crq.request_capability.capability =
3178 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003179 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003180 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003181 ibmvnic_send_crq(adapter, &crq);
3182 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003183}
3184
3185static int pending_scrq(struct ibmvnic_adapter *adapter,
3186 struct ibmvnic_sub_crq_queue *scrq)
3187{
3188 union sub_crq *entry = &scrq->msgs[scrq->cur];
3189
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003190 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003191 return 1;
3192 else
3193 return 0;
3194}
3195
3196static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3197 struct ibmvnic_sub_crq_queue *scrq)
3198{
3199 union sub_crq *entry;
3200 unsigned long flags;
3201
3202 spin_lock_irqsave(&scrq->lock, flags);
3203 entry = &scrq->msgs[scrq->cur];
3204 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3205 if (++scrq->cur == scrq->size)
3206 scrq->cur = 0;
3207 } else {
3208 entry = NULL;
3209 }
3210 spin_unlock_irqrestore(&scrq->lock, flags);
3211
3212 return entry;
3213}
3214
3215static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3216{
3217 struct ibmvnic_crq_queue *queue = &adapter->crq;
3218 union ibmvnic_crq *crq;
3219
3220 crq = &queue->msgs[queue->cur];
3221 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3222 if (++queue->cur == queue->size)
3223 queue->cur = 0;
3224 } else {
3225 crq = NULL;
3226 }
3227
3228 return crq;
3229}
3230
Thomas Falcon2d14d372018-07-13 12:03:32 -05003231static void print_subcrq_error(struct device *dev, int rc, const char *func)
3232{
3233 switch (rc) {
3234 case H_PARAMETER:
3235 dev_warn_ratelimited(dev,
3236 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3237 func, rc);
3238 break;
3239 case H_CLOSED:
3240 dev_warn_ratelimited(dev,
3241 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3242 func, rc);
3243 break;
3244 default:
3245 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3246 break;
3247 }
3248}
3249
Thomas Falcon032c5e82015-12-21 11:26:06 -06003250static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3251 union sub_crq *sub_crq)
3252{
3253 unsigned int ua = adapter->vdev->unit_address;
3254 struct device *dev = &adapter->vdev->dev;
3255 u64 *u64_crq = (u64 *)sub_crq;
3256 int rc;
3257
3258 netdev_dbg(adapter->netdev,
3259 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3260 (unsigned long int)cpu_to_be64(remote_handle),
3261 (unsigned long int)cpu_to_be64(u64_crq[0]),
3262 (unsigned long int)cpu_to_be64(u64_crq[1]),
3263 (unsigned long int)cpu_to_be64(u64_crq[2]),
3264 (unsigned long int)cpu_to_be64(u64_crq[3]));
3265
3266 /* Make sure the hypervisor sees the complete request */
3267 mb();
3268
3269 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3270 cpu_to_be64(remote_handle),
3271 cpu_to_be64(u64_crq[0]),
3272 cpu_to_be64(u64_crq[1]),
3273 cpu_to_be64(u64_crq[2]),
3274 cpu_to_be64(u64_crq[3]));
3275
Thomas Falcon2d14d372018-07-13 12:03:32 -05003276 if (rc)
3277 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278
3279 return rc;
3280}
3281
Thomas Falconad7775d2016-04-01 17:20:34 -05003282static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3283 u64 remote_handle, u64 ioba, u64 num_entries)
3284{
3285 unsigned int ua = adapter->vdev->unit_address;
3286 struct device *dev = &adapter->vdev->dev;
3287 int rc;
3288
3289 /* Make sure the hypervisor sees the complete request */
3290 mb();
3291 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3292 cpu_to_be64(remote_handle),
3293 ioba, num_entries);
3294
Thomas Falcon2d14d372018-07-13 12:03:32 -05003295 if (rc)
3296 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003297
3298 return rc;
3299}
3300
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3302 union ibmvnic_crq *crq)
3303{
3304 unsigned int ua = adapter->vdev->unit_address;
3305 struct device *dev = &adapter->vdev->dev;
3306 u64 *u64_crq = (u64 *)crq;
3307 int rc;
3308
3309 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3310 (unsigned long int)cpu_to_be64(u64_crq[0]),
3311 (unsigned long int)cpu_to_be64(u64_crq[1]));
3312
Thomas Falcon51536982018-05-23 13:37:56 -05003313 if (!adapter->crq.active &&
3314 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3315 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3316 return -EINVAL;
3317 }
3318
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 /* Make sure the hypervisor sees the complete request */
3320 mb();
3321
3322 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3323 cpu_to_be64(u64_crq[0]),
3324 cpu_to_be64(u64_crq[1]));
3325
3326 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003327 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003329 if (adapter->resetting)
3330 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3331 }
3332
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333 dev_warn(dev, "Send error (rc=%d)\n", rc);
3334 }
3335
3336 return rc;
3337}
3338
3339static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3340{
3341 union ibmvnic_crq crq;
3342
3343 memset(&crq, 0, sizeof(crq));
3344 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3345 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3346 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3347
3348 return ibmvnic_send_crq(adapter, &crq);
3349}
3350
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351static int send_version_xchg(struct ibmvnic_adapter *adapter)
3352{
3353 union ibmvnic_crq crq;
3354
3355 memset(&crq, 0, sizeof(crq));
3356 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3357 crq.version_exchange.cmd = VERSION_EXCHANGE;
3358 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3359
3360 return ibmvnic_send_crq(adapter, &crq);
3361}
3362
Nathan Fontenot37798d02017-11-08 11:23:56 -06003363struct vnic_login_client_data {
3364 u8 type;
3365 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003366 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003367} __packed;
3368
3369static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3370{
3371 int len;
3372
3373 /* Calculate the amount of buffer space needed for the
3374 * vnic client data in the login buffer. There are four entries,
3375 * OS name, LPAR name, device name, and a null last entry.
3376 */
3377 len = 4 * sizeof(struct vnic_login_client_data);
3378 len += 6; /* "Linux" plus NULL */
3379 len += strlen(utsname()->nodename) + 1;
3380 len += strlen(adapter->netdev->name) + 1;
3381
3382 return len;
3383}
3384
3385static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3386 struct vnic_login_client_data *vlcd)
3387{
3388 const char *os_name = "Linux";
3389 int len;
3390
3391 /* Type 1 - LPAR OS */
3392 vlcd->type = 1;
3393 len = strlen(os_name) + 1;
3394 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003395 strncpy(vlcd->name, os_name, len);
3396 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003397
3398 /* Type 2 - LPAR name */
3399 vlcd->type = 2;
3400 len = strlen(utsname()->nodename) + 1;
3401 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003402 strncpy(vlcd->name, utsname()->nodename, len);
3403 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003404
3405 /* Type 3 - device name */
3406 vlcd->type = 3;
3407 len = strlen(adapter->netdev->name) + 1;
3408 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003409 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003410}
3411
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003412static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003413{
3414 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3415 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003416 struct device *dev = &adapter->vdev->dev;
3417 dma_addr_t rsp_buffer_token;
3418 dma_addr_t buffer_token;
3419 size_t rsp_buffer_size;
3420 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003421 size_t buffer_size;
3422 __be64 *tx_list_p;
3423 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003424 int client_data_len;
3425 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003426 int i;
3427
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003428 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3429 netdev_err(adapter->netdev,
3430 "RX or TX queues are not allocated, device login failed\n");
3431 return -1;
3432 }
3433
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003434 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003435 client_data_len = vnic_client_data_len(adapter);
3436
Thomas Falcon032c5e82015-12-21 11:26:06 -06003437 buffer_size =
3438 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003439 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3440 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003441
Nathan Fontenot37798d02017-11-08 11:23:56 -06003442 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443 if (!login_buffer)
3444 goto buf_alloc_failed;
3445
3446 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3447 DMA_TO_DEVICE);
3448 if (dma_mapping_error(dev, buffer_token)) {
3449 dev_err(dev, "Couldn't map login buffer\n");
3450 goto buf_map_failed;
3451 }
3452
John Allen498cd8e2016-04-06 11:49:55 -05003453 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3454 sizeof(u64) * adapter->req_tx_queues +
3455 sizeof(u64) * adapter->req_rx_queues +
3456 sizeof(u64) * adapter->req_rx_queues +
3457 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003458
3459 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3460 if (!login_rsp_buffer)
3461 goto buf_rsp_alloc_failed;
3462
3463 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3464 rsp_buffer_size, DMA_FROM_DEVICE);
3465 if (dma_mapping_error(dev, rsp_buffer_token)) {
3466 dev_err(dev, "Couldn't map login rsp buffer\n");
3467 goto buf_rsp_map_failed;
3468 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003469
Thomas Falcon032c5e82015-12-21 11:26:06 -06003470 adapter->login_buf = login_buffer;
3471 adapter->login_buf_token = buffer_token;
3472 adapter->login_buf_sz = buffer_size;
3473 adapter->login_rsp_buf = login_rsp_buffer;
3474 adapter->login_rsp_buf_token = rsp_buffer_token;
3475 adapter->login_rsp_buf_sz = rsp_buffer_size;
3476
3477 login_buffer->len = cpu_to_be32(buffer_size);
3478 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3479 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3480 login_buffer->off_txcomp_subcrqs =
3481 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3482 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3483 login_buffer->off_rxcomp_subcrqs =
3484 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3485 sizeof(u64) * adapter->req_tx_queues);
3486 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3487 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3488
3489 tx_list_p = (__be64 *)((char *)login_buffer +
3490 sizeof(struct ibmvnic_login_buffer));
3491 rx_list_p = (__be64 *)((char *)login_buffer +
3492 sizeof(struct ibmvnic_login_buffer) +
3493 sizeof(u64) * adapter->req_tx_queues);
3494
3495 for (i = 0; i < adapter->req_tx_queues; i++) {
3496 if (adapter->tx_scrq[i]) {
3497 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3498 crq_num);
3499 }
3500 }
3501
3502 for (i = 0; i < adapter->req_rx_queues; i++) {
3503 if (adapter->rx_scrq[i]) {
3504 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3505 crq_num);
3506 }
3507 }
3508
Nathan Fontenot37798d02017-11-08 11:23:56 -06003509 /* Insert vNIC login client data */
3510 vlcd = (struct vnic_login_client_data *)
3511 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3512 login_buffer->client_data_offset =
3513 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3514 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3515
3516 vnic_add_client_data(adapter, vlcd);
3517
Thomas Falcon032c5e82015-12-21 11:26:06 -06003518 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3519 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3520 netdev_dbg(adapter->netdev, "%016lx\n",
3521 ((unsigned long int *)(adapter->login_buf))[i]);
3522 }
3523
3524 memset(&crq, 0, sizeof(crq));
3525 crq.login.first = IBMVNIC_CRQ_CMD;
3526 crq.login.cmd = LOGIN;
3527 crq.login.ioba = cpu_to_be32(buffer_token);
3528 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003529 ibmvnic_send_crq(adapter, &crq);
3530
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003531 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003532
Thomas Falcon032c5e82015-12-21 11:26:06 -06003533buf_rsp_map_failed:
3534 kfree(login_rsp_buffer);
3535buf_rsp_alloc_failed:
3536 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3537buf_map_failed:
3538 kfree(login_buffer);
3539buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003540 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003541}
3542
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003543static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3544 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003545{
3546 union ibmvnic_crq crq;
3547
3548 memset(&crq, 0, sizeof(crq));
3549 crq.request_map.first = IBMVNIC_CRQ_CMD;
3550 crq.request_map.cmd = REQUEST_MAP;
3551 crq.request_map.map_id = map_id;
3552 crq.request_map.ioba = cpu_to_be32(addr);
3553 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003554 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003555}
3556
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003557static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003558{
3559 union ibmvnic_crq crq;
3560
3561 memset(&crq, 0, sizeof(crq));
3562 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3563 crq.request_unmap.cmd = REQUEST_UNMAP;
3564 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003565 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003566}
3567
3568static void send_map_query(struct ibmvnic_adapter *adapter)
3569{
3570 union ibmvnic_crq crq;
3571
3572 memset(&crq, 0, sizeof(crq));
3573 crq.query_map.first = IBMVNIC_CRQ_CMD;
3574 crq.query_map.cmd = QUERY_MAP;
3575 ibmvnic_send_crq(adapter, &crq);
3576}
3577
3578/* Send a series of CRQs requesting various capabilities of the VNIC server */
3579static void send_cap_queries(struct ibmvnic_adapter *adapter)
3580{
3581 union ibmvnic_crq crq;
3582
Thomas Falcon901e0402017-02-15 12:17:59 -06003583 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003584 memset(&crq, 0, sizeof(crq));
3585 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3586 crq.query_capability.cmd = QUERY_CAPABILITY;
3587
3588 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003589 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 ibmvnic_send_crq(adapter, &crq);
3591
3592 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003593 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003594 ibmvnic_send_crq(adapter, &crq);
3595
3596 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003597 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003598 ibmvnic_send_crq(adapter, &crq);
3599
3600 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003601 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003602 ibmvnic_send_crq(adapter, &crq);
3603
3604 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003605 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003606 ibmvnic_send_crq(adapter, &crq);
3607
3608 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003609 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003610 ibmvnic_send_crq(adapter, &crq);
3611
3612 crq.query_capability.capability =
3613 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003614 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003615 ibmvnic_send_crq(adapter, &crq);
3616
3617 crq.query_capability.capability =
3618 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003619 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003620 ibmvnic_send_crq(adapter, &crq);
3621
3622 crq.query_capability.capability =
3623 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003624 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003625 ibmvnic_send_crq(adapter, &crq);
3626
3627 crq.query_capability.capability =
3628 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003629 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003630 ibmvnic_send_crq(adapter, &crq);
3631
3632 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003633 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 ibmvnic_send_crq(adapter, &crq);
3635
3636 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003637 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003638 ibmvnic_send_crq(adapter, &crq);
3639
3640 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003641 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003642 ibmvnic_send_crq(adapter, &crq);
3643
3644 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003645 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003646 ibmvnic_send_crq(adapter, &crq);
3647
3648 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003649 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003650 ibmvnic_send_crq(adapter, &crq);
3651
3652 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003653 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003654 ibmvnic_send_crq(adapter, &crq);
3655
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003656 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3657 atomic_inc(&adapter->running_cap_crqs);
3658 ibmvnic_send_crq(adapter, &crq);
3659
Thomas Falcon032c5e82015-12-21 11:26:06 -06003660 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003661 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003662 ibmvnic_send_crq(adapter, &crq);
3663
3664 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003665 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003666 ibmvnic_send_crq(adapter, &crq);
3667
3668 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003669 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003670 ibmvnic_send_crq(adapter, &crq);
3671
3672 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003673 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003674 ibmvnic_send_crq(adapter, &crq);
3675
3676 crq.query_capability.capability =
3677 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003678 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003679 ibmvnic_send_crq(adapter, &crq);
3680
3681 crq.query_capability.capability =
3682 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003683 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003684 ibmvnic_send_crq(adapter, &crq);
3685
3686 crq.query_capability.capability =
3687 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003688 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003689 ibmvnic_send_crq(adapter, &crq);
3690
3691 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003692 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003693 ibmvnic_send_crq(adapter, &crq);
3694}
3695
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003696static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3697 struct ibmvnic_adapter *adapter)
3698{
3699 struct device *dev = &adapter->vdev->dev;
3700
3701 if (crq->get_vpd_size_rsp.rc.code) {
3702 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3703 crq->get_vpd_size_rsp.rc.code);
3704 complete(&adapter->fw_done);
3705 return;
3706 }
3707
3708 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3709 complete(&adapter->fw_done);
3710}
3711
3712static void handle_vpd_rsp(union ibmvnic_crq *crq,
3713 struct ibmvnic_adapter *adapter)
3714{
3715 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003716 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003717 u8 fw_level_len = 0;
3718
3719 memset(adapter->fw_version, 0, 32);
3720
3721 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3722 DMA_FROM_DEVICE);
3723
3724 if (crq->get_vpd_rsp.rc.code) {
3725 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3726 crq->get_vpd_rsp.rc.code);
3727 goto complete;
3728 }
3729
3730 /* get the position of the firmware version info
3731 * located after the ASCII 'RM' substring in the buffer
3732 */
3733 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3734 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003735 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 -02003736 goto complete;
3737 }
3738
3739 /* get length of firmware level ASCII substring */
3740 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3741 fw_level_len = *(substr + 2);
3742 } else {
3743 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3744 goto complete;
3745 }
3746
3747 /* copy firmware version string from vpd into adapter */
3748 if ((substr + 3 + fw_level_len) <
3749 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003750 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003751 } else {
3752 dev_info(dev, "FW substr extrapolated VPD buff\n");
3753 }
3754
3755complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003756 if (adapter->fw_version[0] == '\0')
3757 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003758 complete(&adapter->fw_done);
3759}
3760
Thomas Falcon032c5e82015-12-21 11:26:06 -06003761static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3762{
3763 struct device *dev = &adapter->vdev->dev;
3764 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
Thomas Falcondde746a2019-04-10 11:07:00 -05003765 netdev_features_t old_hw_features = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003766 union ibmvnic_crq crq;
3767 int i;
3768
3769 dma_unmap_single(dev, adapter->ip_offload_tok,
3770 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3771
3772 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3773 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3774 netdev_dbg(adapter->netdev, "%016lx\n",
3775 ((unsigned long int *)(buf))[i]);
3776
3777 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3778 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3779 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3780 buf->tcp_ipv4_chksum);
3781 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3782 buf->tcp_ipv6_chksum);
3783 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3784 buf->udp_ipv4_chksum);
3785 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3786 buf->udp_ipv6_chksum);
3787 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3788 buf->large_tx_ipv4);
3789 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3790 buf->large_tx_ipv6);
3791 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3792 buf->large_rx_ipv4);
3793 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3794 buf->large_rx_ipv6);
3795 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3796 buf->max_ipv4_header_size);
3797 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3798 buf->max_ipv6_header_size);
3799 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3800 buf->max_tcp_header_size);
3801 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3802 buf->max_udp_header_size);
3803 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3804 buf->max_large_tx_size);
3805 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3806 buf->max_large_rx_size);
3807 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3808 buf->ipv6_extension_header);
3809 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3810 buf->tcp_pseudosum_req);
3811 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3812 buf->num_ipv6_ext_headers);
3813 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3814 buf->off_ipv6_ext_headers);
3815
3816 adapter->ip_offload_ctrl_tok =
3817 dma_map_single(dev, &adapter->ip_offload_ctrl,
3818 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3819
3820 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3821 dev_err(dev, "Couldn't map ip offload control buffer\n");
3822 return;
3823 }
3824
Thomas Falconf6897942018-01-18 19:05:01 -06003825 adapter->ip_offload_ctrl.len =
3826 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003827 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003828 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3829 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3831 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3832 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3833 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003834 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3835 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003836
Thomas Falconfdb06102017-10-17 12:36:55 -05003837 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003838 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3839 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3840
Thomas Falcondde746a2019-04-10 11:07:00 -05003841 if (adapter->state != VNIC_PROBING) {
3842 old_hw_features = adapter->netdev->hw_features;
3843 adapter->netdev->hw_features = 0;
3844 }
3845
Thomas Falconb66b7bd2019-04-10 11:06:59 -05003846 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003847
3848 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05003849 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003850
3851 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
Thomas Falcondde746a2019-04-10 11:07:00 -05003852 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003853
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003854 if ((adapter->netdev->features &
3855 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
Thomas Falcondde746a2019-04-10 11:07:00 -05003856 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003857
Thomas Falconfdb06102017-10-17 12:36:55 -05003858 if (buf->large_tx_ipv4)
Thomas Falcondde746a2019-04-10 11:07:00 -05003859 adapter->netdev->hw_features |= NETIF_F_TSO;
Thomas Falconfdb06102017-10-17 12:36:55 -05003860 if (buf->large_tx_ipv6)
Thomas Falcondde746a2019-04-10 11:07:00 -05003861 adapter->netdev->hw_features |= NETIF_F_TSO6;
Thomas Falconfdb06102017-10-17 12:36:55 -05003862
Thomas Falcondde746a2019-04-10 11:07:00 -05003863 if (adapter->state == VNIC_PROBING) {
3864 adapter->netdev->features |= adapter->netdev->hw_features;
3865 } else if (old_hw_features != adapter->netdev->hw_features) {
3866 netdev_features_t tmp = 0;
3867
3868 /* disable features no longer supported */
3869 adapter->netdev->features &= adapter->netdev->hw_features;
3870 /* turn on features now supported if previously enabled */
3871 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
3872 adapter->netdev->hw_features;
3873 adapter->netdev->features |=
3874 tmp & adapter->netdev->wanted_features;
3875 }
Thomas Falconaa0bf852017-10-17 12:36:56 -05003876
Thomas Falcon032c5e82015-12-21 11:26:06 -06003877 memset(&crq, 0, sizeof(crq));
3878 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3879 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3880 crq.control_ip_offload.len =
3881 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3882 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3883 ibmvnic_send_crq(adapter, &crq);
3884}
3885
Thomas Falconc9008d32018-08-06 21:39:59 -05003886static const char *ibmvnic_fw_err_cause(u16 cause)
3887{
3888 switch (cause) {
3889 case ADAPTER_PROBLEM:
3890 return "adapter problem";
3891 case BUS_PROBLEM:
3892 return "bus problem";
3893 case FW_PROBLEM:
3894 return "firmware problem";
3895 case DD_PROBLEM:
3896 return "device driver problem";
3897 case EEH_RECOVERY:
3898 return "EEH recovery";
3899 case FW_UPDATED:
3900 return "firmware updated";
3901 case LOW_MEMORY:
3902 return "low Memory";
3903 default:
3904 return "unknown";
3905 }
3906}
3907
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003908static void handle_error_indication(union ibmvnic_crq *crq,
3909 struct ibmvnic_adapter *adapter)
3910{
3911 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05003912 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003913
Thomas Falconc9008d32018-08-06 21:39:59 -05003914 cause = be16_to_cpu(crq->error_indication.error_cause);
3915
3916 dev_warn_ratelimited(dev,
3917 "Firmware reports %serror, cause: %s. Starting recovery...\n",
3918 crq->error_indication.flags
3919 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3920 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003921
Nathan Fontenoted651a12017-05-03 14:04:38 -04003922 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3923 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003924 else
3925 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003926}
3927
Thomas Falconf8136142018-01-29 13:45:05 -06003928static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3929 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003930{
3931 struct net_device *netdev = adapter->netdev;
3932 struct device *dev = &adapter->vdev->dev;
3933 long rc;
3934
3935 rc = crq->change_mac_addr_rsp.rc.code;
3936 if (rc) {
3937 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003938 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 }
Thomas Falcon62740e92019-05-09 23:13:43 -05003940 ether_addr_copy(netdev->dev_addr,
3941 &crq->change_mac_addr_rsp.mac_addr[0]);
Thomas Falconf8136142018-01-29 13:45:05 -06003942out:
3943 complete(&adapter->fw_done);
3944 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003945}
3946
3947static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3948 struct ibmvnic_adapter *adapter)
3949{
3950 struct device *dev = &adapter->vdev->dev;
3951 u64 *req_value;
3952 char *name;
3953
Thomas Falcon901e0402017-02-15 12:17:59 -06003954 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003955 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3956 case REQ_TX_QUEUES:
3957 req_value = &adapter->req_tx_queues;
3958 name = "tx";
3959 break;
3960 case REQ_RX_QUEUES:
3961 req_value = &adapter->req_rx_queues;
3962 name = "rx";
3963 break;
3964 case REQ_RX_ADD_QUEUES:
3965 req_value = &adapter->req_rx_add_queues;
3966 name = "rx_add";
3967 break;
3968 case REQ_TX_ENTRIES_PER_SUBCRQ:
3969 req_value = &adapter->req_tx_entries_per_subcrq;
3970 name = "tx_entries_per_subcrq";
3971 break;
3972 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3973 req_value = &adapter->req_rx_add_entries_per_subcrq;
3974 name = "rx_add_entries_per_subcrq";
3975 break;
3976 case REQ_MTU:
3977 req_value = &adapter->req_mtu;
3978 name = "mtu";
3979 break;
3980 case PROMISC_REQUESTED:
3981 req_value = &adapter->promisc;
3982 name = "promisc";
3983 break;
3984 default:
3985 dev_err(dev, "Got invalid cap request rsp %d\n",
3986 crq->request_capability.capability);
3987 return;
3988 }
3989
3990 switch (crq->request_capability_rsp.rc.code) {
3991 case SUCCESS:
3992 break;
3993 case PARTIALSUCCESS:
3994 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3995 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003996 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003997 number), name);
John Allene7913802018-01-18 16:27:12 -06003998
3999 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4000 REQ_MTU) {
4001 pr_err("mtu of %llu is not supported. Reverting.\n",
4002 *req_value);
4003 *req_value = adapter->fallback.mtu;
4004 } else {
4005 *req_value =
4006 be64_to_cpu(crq->request_capability_rsp.number);
4007 }
4008
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004009 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004010 return;
4011 default:
4012 dev_err(dev, "Error %d in request cap rsp\n",
4013 crq->request_capability_rsp.rc.code);
4014 return;
4015 }
4016
4017 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004018 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004019 union ibmvnic_crq newcrq;
4020 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4021 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4022 &adapter->ip_offload_buf;
4023
Thomas Falcon249168a2017-02-15 12:18:00 -06004024 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004025 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4026 buf_sz,
4027 DMA_FROM_DEVICE);
4028
4029 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4030 if (!firmware_has_feature(FW_FEATURE_CMO))
4031 dev_err(dev, "Couldn't map offload buffer\n");
4032 return;
4033 }
4034
4035 memset(&newcrq, 0, sizeof(newcrq));
4036 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4037 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4038 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4039 newcrq.query_ip_offload.ioba =
4040 cpu_to_be32(adapter->ip_offload_tok);
4041
4042 ibmvnic_send_crq(adapter, &newcrq);
4043 }
4044}
4045
4046static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4047 struct ibmvnic_adapter *adapter)
4048{
4049 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004050 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004051 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4052 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004053 int i;
4054
4055 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004056 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004057 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004058 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004059
John Allen498cd8e2016-04-06 11:49:55 -05004060 /* If the number of queues requested can't be allocated by the
4061 * server, the login response will return with code 1. We will need
4062 * to resend the login buffer with fewer queues requested.
4063 */
4064 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004065 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004066 complete(&adapter->init_done);
4067 return 0;
4068 }
4069
John Allenc26eba02017-10-26 16:23:25 -05004070 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4071
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4073 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4074 netdev_dbg(adapter->netdev, "%016lx\n",
4075 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4076 }
4077
4078 /* Sanity checks */
4079 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4080 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4081 adapter->req_rx_add_queues !=
4082 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4083 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4084 ibmvnic_remove(adapter->vdev);
4085 return -EIO;
4086 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06004087 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004088 complete(&adapter->init_done);
4089
Thomas Falcon032c5e82015-12-21 11:26:06 -06004090 return 0;
4091}
4092
Thomas Falcon032c5e82015-12-21 11:26:06 -06004093static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4094 struct ibmvnic_adapter *adapter)
4095{
4096 struct device *dev = &adapter->vdev->dev;
4097 long rc;
4098
4099 rc = crq->request_unmap_rsp.rc.code;
4100 if (rc)
4101 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4102}
4103
4104static void handle_query_map_rsp(union ibmvnic_crq *crq,
4105 struct ibmvnic_adapter *adapter)
4106{
4107 struct net_device *netdev = adapter->netdev;
4108 struct device *dev = &adapter->vdev->dev;
4109 long rc;
4110
4111 rc = crq->query_map_rsp.rc.code;
4112 if (rc) {
4113 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4114 return;
4115 }
4116 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4117 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4118 crq->query_map_rsp.free_pages);
4119}
4120
4121static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4122 struct ibmvnic_adapter *adapter)
4123{
4124 struct net_device *netdev = adapter->netdev;
4125 struct device *dev = &adapter->vdev->dev;
4126 long rc;
4127
Thomas Falcon901e0402017-02-15 12:17:59 -06004128 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004129 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004130 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004131 rc = crq->query_capability.rc.code;
4132 if (rc) {
4133 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4134 goto out;
4135 }
4136
4137 switch (be16_to_cpu(crq->query_capability.capability)) {
4138 case MIN_TX_QUEUES:
4139 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004140 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004141 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4142 adapter->min_tx_queues);
4143 break;
4144 case MIN_RX_QUEUES:
4145 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004146 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004147 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4148 adapter->min_rx_queues);
4149 break;
4150 case MIN_RX_ADD_QUEUES:
4151 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004152 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004153 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4154 adapter->min_rx_add_queues);
4155 break;
4156 case MAX_TX_QUEUES:
4157 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004158 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004159 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4160 adapter->max_tx_queues);
4161 break;
4162 case MAX_RX_QUEUES:
4163 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004164 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004165 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4166 adapter->max_rx_queues);
4167 break;
4168 case MAX_RX_ADD_QUEUES:
4169 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004170 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004171 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4172 adapter->max_rx_add_queues);
4173 break;
4174 case MIN_TX_ENTRIES_PER_SUBCRQ:
4175 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004176 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004177 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4178 adapter->min_tx_entries_per_subcrq);
4179 break;
4180 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4181 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004182 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004183 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4184 adapter->min_rx_add_entries_per_subcrq);
4185 break;
4186 case MAX_TX_ENTRIES_PER_SUBCRQ:
4187 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004188 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004189 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4190 adapter->max_tx_entries_per_subcrq);
4191 break;
4192 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4193 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004194 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004195 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4196 adapter->max_rx_add_entries_per_subcrq);
4197 break;
4198 case TCP_IP_OFFLOAD:
4199 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004200 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004201 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4202 adapter->tcp_ip_offload);
4203 break;
4204 case PROMISC_SUPPORTED:
4205 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004206 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004207 netdev_dbg(netdev, "promisc_supported = %lld\n",
4208 adapter->promisc_supported);
4209 break;
4210 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004211 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004212 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004213 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4214 break;
4215 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004216 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004217 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004218 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4219 break;
4220 case MAX_MULTICAST_FILTERS:
4221 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004222 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004223 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4224 adapter->max_multicast_filters);
4225 break;
4226 case VLAN_HEADER_INSERTION:
4227 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004228 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004229 if (adapter->vlan_header_insertion)
4230 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4231 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4232 adapter->vlan_header_insertion);
4233 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004234 case RX_VLAN_HEADER_INSERTION:
4235 adapter->rx_vlan_header_insertion =
4236 be64_to_cpu(crq->query_capability.number);
4237 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4238 adapter->rx_vlan_header_insertion);
4239 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004240 case MAX_TX_SG_ENTRIES:
4241 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004242 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004243 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4244 adapter->max_tx_sg_entries);
4245 break;
4246 case RX_SG_SUPPORTED:
4247 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004248 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004249 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4250 adapter->rx_sg_supported);
4251 break;
4252 case OPT_TX_COMP_SUB_QUEUES:
4253 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004254 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004255 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4256 adapter->opt_tx_comp_sub_queues);
4257 break;
4258 case OPT_RX_COMP_QUEUES:
4259 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004260 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004261 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4262 adapter->opt_rx_comp_queues);
4263 break;
4264 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4265 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004266 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004267 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4268 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4269 break;
4270 case OPT_TX_ENTRIES_PER_SUBCRQ:
4271 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004272 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004273 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4274 adapter->opt_tx_entries_per_subcrq);
4275 break;
4276 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4277 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004278 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004279 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4280 adapter->opt_rxba_entries_per_subcrq);
4281 break;
4282 case TX_RX_DESC_REQ:
4283 adapter->tx_rx_desc_req = crq->query_capability.number;
4284 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4285 adapter->tx_rx_desc_req);
4286 break;
4287
4288 default:
4289 netdev_err(netdev, "Got invalid cap rsp %d\n",
4290 crq->query_capability.capability);
4291 }
4292
4293out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004294 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4295 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004296 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004297 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004298}
4299
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004300static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4301{
4302 union ibmvnic_crq crq;
4303 int rc;
4304
4305 memset(&crq, 0, sizeof(crq));
4306 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4307 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
4308 init_completion(&adapter->fw_done);
4309 rc = ibmvnic_send_crq(adapter, &crq);
4310 if (rc)
4311 return rc;
4312 wait_for_completion(&adapter->fw_done);
4313 return adapter->fw_done_rc ? -EIO : 0;
4314}
4315
4316static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4317 struct ibmvnic_adapter *adapter)
4318{
4319 struct net_device *netdev = adapter->netdev;
4320 int rc;
4321
4322 rc = crq->query_phys_parms_rsp.rc.code;
4323 if (rc) {
4324 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4325 return rc;
4326 }
4327 switch (cpu_to_be32(crq->query_phys_parms_rsp.speed)) {
4328 case IBMVNIC_10MBPS:
4329 adapter->speed = SPEED_10;
4330 break;
4331 case IBMVNIC_100MBPS:
4332 adapter->speed = SPEED_100;
4333 break;
4334 case IBMVNIC_1GBPS:
4335 adapter->speed = SPEED_1000;
4336 break;
4337 case IBMVNIC_10GBP:
4338 adapter->speed = SPEED_10000;
4339 break;
4340 case IBMVNIC_25GBPS:
4341 adapter->speed = SPEED_25000;
4342 break;
4343 case IBMVNIC_40GBPS:
4344 adapter->speed = SPEED_40000;
4345 break;
4346 case IBMVNIC_50GBPS:
4347 adapter->speed = SPEED_50000;
4348 break;
4349 case IBMVNIC_100GBPS:
4350 adapter->speed = SPEED_100000;
4351 break;
4352 default:
4353 netdev_warn(netdev, "Unknown speed 0x%08x\n",
4354 cpu_to_be32(crq->query_phys_parms_rsp.speed));
4355 adapter->speed = SPEED_UNKNOWN;
4356 }
4357 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4358 adapter->duplex = DUPLEX_FULL;
4359 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4360 adapter->duplex = DUPLEX_HALF;
4361 else
4362 adapter->duplex = DUPLEX_UNKNOWN;
4363
4364 return rc;
4365}
4366
Thomas Falcon032c5e82015-12-21 11:26:06 -06004367static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4368 struct ibmvnic_adapter *adapter)
4369{
4370 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4371 struct net_device *netdev = adapter->netdev;
4372 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004373 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004374 long rc;
4375
4376 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004377 (unsigned long int)cpu_to_be64(u64_crq[0]),
4378 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004379 switch (gen_crq->first) {
4380 case IBMVNIC_CRQ_INIT_RSP:
4381 switch (gen_crq->cmd) {
4382 case IBMVNIC_CRQ_INIT:
4383 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004384 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004385 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004386 if (!completion_done(&adapter->init_done)) {
4387 complete(&adapter->init_done);
4388 adapter->init_done_rc = -EIO;
4389 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004390 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004391 break;
4392 case IBMVNIC_CRQ_INIT_COMPLETE:
4393 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004394 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004395 send_version_xchg(adapter);
4396 break;
4397 default:
4398 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4399 }
4400 return;
4401 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004402 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004403 adapter->crq.active = false;
Thomas Falcon2770a792018-05-23 13:38:02 -05004404 if (adapter->resetting)
4405 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004406 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004407 dev_info(dev, "Migrated, re-enabling adapter\n");
4408 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004409 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4410 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004411 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004412 } else {
4413 /* The adapter lost the connection */
4414 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4415 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004416 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004417 }
4418 return;
4419 case IBMVNIC_CRQ_CMD_RSP:
4420 break;
4421 default:
4422 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4423 gen_crq->first);
4424 return;
4425 }
4426
4427 switch (gen_crq->cmd) {
4428 case VERSION_EXCHANGE_RSP:
4429 rc = crq->version_exchange_rsp.rc.code;
4430 if (rc) {
4431 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4432 break;
4433 }
4434 dev_info(dev, "Partner protocol version is %d\n",
4435 crq->version_exchange_rsp.version);
4436 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4437 ibmvnic_version)
4438 ibmvnic_version =
4439 be16_to_cpu(crq->version_exchange_rsp.version);
4440 send_cap_queries(adapter);
4441 break;
4442 case QUERY_CAPABILITY_RSP:
4443 handle_query_cap_rsp(crq, adapter);
4444 break;
4445 case QUERY_MAP_RSP:
4446 handle_query_map_rsp(crq, adapter);
4447 break;
4448 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004449 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4450 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004451 break;
4452 case REQUEST_UNMAP_RSP:
4453 handle_request_unmap_rsp(crq, adapter);
4454 break;
4455 case REQUEST_CAPABILITY_RSP:
4456 handle_request_cap_rsp(crq, adapter);
4457 break;
4458 case LOGIN_RSP:
4459 netdev_dbg(netdev, "Got Login Response\n");
4460 handle_login_rsp(crq, adapter);
4461 break;
4462 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004463 netdev_dbg(netdev,
4464 "Got Logical Link State Response, state: %d rc: %d\n",
4465 crq->logical_link_state_rsp.link_state,
4466 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004467 adapter->logical_link_state =
4468 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004469 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4470 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471 break;
4472 case LINK_STATE_INDICATION:
4473 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4474 adapter->phys_link_state =
4475 crq->link_state_indication.phys_link_state;
4476 adapter->logical_link_state =
4477 crq->link_state_indication.logical_link_state;
Thomas Falcon0655f992019-05-09 23:13:44 -05004478 if (adapter->phys_link_state && adapter->logical_link_state)
4479 netif_carrier_on(netdev);
4480 else
4481 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004482 break;
4483 case CHANGE_MAC_ADDR_RSP:
4484 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004485 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004486 break;
4487 case ERROR_INDICATION:
4488 netdev_dbg(netdev, "Got Error Indication\n");
4489 handle_error_indication(crq, adapter);
4490 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004491 case REQUEST_STATISTICS_RSP:
4492 netdev_dbg(netdev, "Got Statistics Response\n");
4493 complete(&adapter->stats_done);
4494 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004495 case QUERY_IP_OFFLOAD_RSP:
4496 netdev_dbg(netdev, "Got Query IP offload Response\n");
4497 handle_query_ip_offload_rsp(adapter);
4498 break;
4499 case MULTICAST_CTRL_RSP:
4500 netdev_dbg(netdev, "Got multicast control Response\n");
4501 break;
4502 case CONTROL_IP_OFFLOAD_RSP:
4503 netdev_dbg(netdev, "Got Control IP offload Response\n");
4504 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4505 sizeof(adapter->ip_offload_ctrl),
4506 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004507 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004508 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004509 case COLLECT_FW_TRACE_RSP:
4510 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4511 complete(&adapter->fw_done);
4512 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004513 case GET_VPD_SIZE_RSP:
4514 handle_vpd_size_rsp(crq, adapter);
4515 break;
4516 case GET_VPD_RSP:
4517 handle_vpd_rsp(crq, adapter);
4518 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004519 case QUERY_PHYS_PARMS_RSP:
4520 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4521 complete(&adapter->fw_done);
4522 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004523 default:
4524 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4525 gen_crq->cmd);
4526 }
4527}
4528
4529static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4530{
4531 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004532
Thomas Falcon6c267b32017-02-15 12:17:58 -06004533 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004534 return IRQ_HANDLED;
4535}
4536
4537static void ibmvnic_tasklet(void *data)
4538{
4539 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004540 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004541 union ibmvnic_crq *crq;
4542 unsigned long flags;
4543 bool done = false;
4544
4545 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004546 while (!done) {
4547 /* Pull all the valid messages off the CRQ */
4548 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4549 ibmvnic_handle_crq(crq, adapter);
4550 crq->generic.first = 0;
4551 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004552
4553 /* remain in tasklet until all
4554 * capabilities responses are received
4555 */
4556 if (!adapter->wait_capability)
4557 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004559 /* if capabilities CRQ's were sent in this tasklet, the following
4560 * tasklet must wait until all responses are received
4561 */
4562 if (atomic_read(&adapter->running_cap_crqs) != 0)
4563 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004564 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004565}
4566
4567static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4568{
4569 struct vio_dev *vdev = adapter->vdev;
4570 int rc;
4571
4572 do {
4573 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4574 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4575
4576 if (rc)
4577 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4578
4579 return rc;
4580}
4581
4582static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4583{
4584 struct ibmvnic_crq_queue *crq = &adapter->crq;
4585 struct device *dev = &adapter->vdev->dev;
4586 struct vio_dev *vdev = adapter->vdev;
4587 int rc;
4588
4589 /* Close the CRQ */
4590 do {
4591 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4592 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4593
4594 /* Clean out the queue */
4595 memset(crq->msgs, 0, PAGE_SIZE);
4596 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004597 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004598
4599 /* And re-open it again */
4600 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4601 crq->msg_token, PAGE_SIZE);
4602
4603 if (rc == H_CLOSED)
4604 /* Adapter is good, but other end is not ready */
4605 dev_warn(dev, "Partner adapter not ready\n");
4606 else if (rc != 0)
4607 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4608
4609 return rc;
4610}
4611
Nathan Fontenotf9928872017-03-30 02:48:54 -04004612static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004613{
4614 struct ibmvnic_crq_queue *crq = &adapter->crq;
4615 struct vio_dev *vdev = adapter->vdev;
4616 long rc;
4617
Nathan Fontenotf9928872017-03-30 02:48:54 -04004618 if (!crq->msgs)
4619 return;
4620
Thomas Falcon032c5e82015-12-21 11:26:06 -06004621 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4622 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004623 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004624 do {
4625 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4626 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4627
4628 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4629 DMA_BIDIRECTIONAL);
4630 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004631 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004632 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004633}
4634
Nathan Fontenotf9928872017-03-30 02:48:54 -04004635static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004636{
4637 struct ibmvnic_crq_queue *crq = &adapter->crq;
4638 struct device *dev = &adapter->vdev->dev;
4639 struct vio_dev *vdev = adapter->vdev;
4640 int rc, retrc = -ENOMEM;
4641
Nathan Fontenotf9928872017-03-30 02:48:54 -04004642 if (crq->msgs)
4643 return 0;
4644
Thomas Falcon032c5e82015-12-21 11:26:06 -06004645 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4646 /* Should we allocate more than one page? */
4647
4648 if (!crq->msgs)
4649 return -ENOMEM;
4650
4651 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4652 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4653 DMA_BIDIRECTIONAL);
4654 if (dma_mapping_error(dev, crq->msg_token))
4655 goto map_failed;
4656
4657 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4658 crq->msg_token, PAGE_SIZE);
4659
4660 if (rc == H_RESOURCE)
4661 /* maybe kexecing and resource is busy. try a reset */
4662 rc = ibmvnic_reset_crq(adapter);
4663 retrc = rc;
4664
4665 if (rc == H_CLOSED) {
4666 dev_warn(dev, "Partner adapter not ready\n");
4667 } else if (rc) {
4668 dev_warn(dev, "Error %d opening adapter\n", rc);
4669 goto reg_crq_failed;
4670 }
4671
4672 retrc = 0;
4673
Thomas Falcon6c267b32017-02-15 12:17:58 -06004674 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4675 (unsigned long)adapter);
4676
Thomas Falcon032c5e82015-12-21 11:26:06 -06004677 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
Murilo Fossa Vicentinie56e2512019-04-25 11:02:33 -03004678 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
4679 adapter->vdev->unit_address);
4680 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004681 if (rc) {
4682 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4683 vdev->irq, rc);
4684 goto req_irq_failed;
4685 }
4686
4687 rc = vio_enable_interrupts(vdev);
4688 if (rc) {
4689 dev_err(dev, "Error %d enabling interrupts\n", rc);
4690 goto req_irq_failed;
4691 }
4692
4693 crq->cur = 0;
4694 spin_lock_init(&crq->lock);
4695
4696 return retrc;
4697
4698req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004699 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004700 do {
4701 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4702 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4703reg_crq_failed:
4704 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4705map_failed:
4706 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004707 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004708 return retrc;
4709}
4710
Thomas Falcon8a348452018-05-23 13:38:00 -05004711static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
John Allenf6ef6402017-03-17 17:13:42 -05004712{
4713 struct device *dev = &adapter->vdev->dev;
4714 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004715 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004716 int rc;
4717
John Allen017892c12017-05-26 10:30:19 -04004718 adapter->from_passive_init = false;
4719
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004720 old_num_rx_queues = adapter->req_rx_queues;
4721 old_num_tx_queues = adapter->req_tx_queues;
4722
Thomas Falconbbd669a2019-04-04 18:58:26 -05004723 reinit_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004724 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004725 ibmvnic_send_crq_init(adapter);
4726 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4727 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004728 return -1;
4729 }
4730
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004731 if (adapter->init_done_rc) {
4732 release_crq_queue(adapter);
4733 return adapter->init_done_rc;
4734 }
4735
John Allen017892c12017-05-26 10:30:19 -04004736 if (adapter->from_passive_init) {
4737 adapter->state = VNIC_OPEN;
4738 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004739 return -1;
4740 }
4741
Nathan Fontenot30f79622018-04-06 18:37:06 -05004742 if (adapter->resetting && !adapter->wait_for_reset &&
4743 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004744 if (adapter->req_rx_queues != old_num_rx_queues ||
4745 adapter->req_tx_queues != old_num_tx_queues) {
4746 release_sub_crqs(adapter, 0);
4747 rc = init_sub_crqs(adapter);
4748 } else {
4749 rc = reset_sub_crq_queues(adapter);
4750 }
4751 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004752 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004753 }
4754
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004755 if (rc) {
4756 dev_err(dev, "Initialization of sub crqs failed\n");
4757 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004758 return rc;
4759 }
4760
4761 rc = init_sub_crq_irqs(adapter);
4762 if (rc) {
4763 dev_err(dev, "Failed to initialize sub crq irqs\n");
4764 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004765 }
4766
4767 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004768}
4769
Thomas Falcon8a348452018-05-23 13:38:00 -05004770static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4771{
4772 struct device *dev = &adapter->vdev->dev;
4773 unsigned long timeout = msecs_to_jiffies(30000);
4774 int rc;
4775
4776 adapter->from_passive_init = false;
4777
Thomas Falcon8a348452018-05-23 13:38:00 -05004778 adapter->init_done_rc = 0;
4779 ibmvnic_send_crq_init(adapter);
4780 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4781 dev_err(dev, "Initialization sequence timed out\n");
4782 return -1;
4783 }
4784
4785 if (adapter->init_done_rc) {
4786 release_crq_queue(adapter);
4787 return adapter->init_done_rc;
4788 }
4789
4790 if (adapter->from_passive_init) {
4791 adapter->state = VNIC_OPEN;
4792 adapter->from_passive_init = false;
4793 return -1;
4794 }
4795
4796 rc = init_sub_crqs(adapter);
4797 if (rc) {
4798 dev_err(dev, "Initialization of sub crqs failed\n");
4799 release_crq_queue(adapter);
4800 return rc;
4801 }
4802
4803 rc = init_sub_crq_irqs(adapter);
4804 if (rc) {
4805 dev_err(dev, "Failed to initialize sub crq irqs\n");
4806 release_crq_queue(adapter);
4807 }
4808
4809 return rc;
4810}
4811
Thomas Falcon40c9db82017-06-12 12:35:04 -05004812static struct device_attribute dev_attr_failover;
4813
Thomas Falcon032c5e82015-12-21 11:26:06 -06004814static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4815{
4816 struct ibmvnic_adapter *adapter;
4817 struct net_device *netdev;
4818 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004819 int rc;
4820
4821 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4822 dev->unit_address);
4823
4824 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4825 VETH_MAC_ADDR, NULL);
4826 if (!mac_addr_p) {
4827 dev_err(&dev->dev,
4828 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4829 __FILE__, __LINE__);
4830 return 0;
4831 }
4832
4833 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004834 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004835 if (!netdev)
4836 return -ENOMEM;
4837
4838 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004839 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004840 dev_set_drvdata(&dev->dev, netdev);
4841 adapter->vdev = dev;
4842 adapter->netdev = netdev;
4843
4844 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4845 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4846 netdev->irq = dev->irq;
4847 netdev->netdev_ops = &ibmvnic_netdev_ops;
4848 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4849 SET_NETDEV_DEV(netdev, &dev->dev);
4850
4851 spin_lock_init(&adapter->stats_lock);
4852
Nathan Fontenoted651a12017-05-03 14:04:38 -04004853 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4854 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06004855 spin_lock_init(&adapter->rwi_lock);
Thomas Falconbbd669a2019-04-04 18:58:26 -05004856 init_completion(&adapter->init_done);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004857 adapter->resetting = false;
4858
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004859 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05004860 rc = init_crq_queue(adapter);
4861 if (rc) {
4862 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4863 rc);
4864 goto ibmvnic_init_fail;
4865 }
4866
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004867 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004868 if (rc && rc != EAGAIN)
4869 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004870 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004871
Thomas Falcon07184212018-05-16 15:49:05 -05004872 rc = init_stats_buffers(adapter);
4873 if (rc)
4874 goto ibmvnic_init_fail;
4875
4876 rc = init_stats_token(adapter);
4877 if (rc)
4878 goto ibmvnic_stats_fail;
4879
Thomas Falconf39f0d12017-02-14 10:22:59 -06004880 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004881 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4882 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004883
Thomas Falcon40c9db82017-06-12 12:35:04 -05004884 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004885 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05004886 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004887
Mick Tarsele876a8a2017-09-28 13:53:18 -07004888 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004889 rc = register_netdev(netdev);
4890 if (rc) {
4891 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004892 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004893 }
4894 dev_info(&dev->dev, "ibmvnic registered\n");
4895
Nathan Fontenot90c80142017-05-03 14:04:32 -04004896 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004897
4898 adapter->wait_for_reset = false;
4899
Thomas Falcon032c5e82015-12-21 11:26:06 -06004900 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004901
4902ibmvnic_register_fail:
4903 device_remove_file(&dev->dev, &dev_attr_failover);
4904
Thomas Falcon07184212018-05-16 15:49:05 -05004905ibmvnic_dev_file_err:
4906 release_stats_token(adapter);
4907
4908ibmvnic_stats_fail:
4909 release_stats_buffers(adapter);
4910
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004911ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004912 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004913 release_crq_queue(adapter);
4914 free_netdev(netdev);
4915
4916 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004917}
4918
4919static int ibmvnic_remove(struct vio_dev *dev)
4920{
4921 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004922 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004923
Nathan Fontenot90c80142017-05-03 14:04:32 -04004924 adapter->state = VNIC_REMOVING;
Juliet Kima5681e22018-11-19 15:59:22 -06004925 rtnl_lock();
4926 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004927
4928 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004929 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004930 release_crq_queue(adapter);
4931
Thomas Falcon53cc7722018-02-26 18:10:56 -06004932 release_stats_token(adapter);
4933 release_stats_buffers(adapter);
4934
Nathan Fontenot90c80142017-05-03 14:04:32 -04004935 adapter->state = VNIC_REMOVED;
4936
Juliet Kima5681e22018-11-19 15:59:22 -06004937 rtnl_unlock();
Thomas Falcon40c9db82017-06-12 12:35:04 -05004938 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004939 free_netdev(netdev);
4940 dev_set_drvdata(&dev->dev, NULL);
4941
4942 return 0;
4943}
4944
Thomas Falcon40c9db82017-06-12 12:35:04 -05004945static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4946 const char *buf, size_t count)
4947{
4948 struct net_device *netdev = dev_get_drvdata(dev);
4949 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4950 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4951 __be64 session_token;
4952 long rc;
4953
4954 if (!sysfs_streq(buf, "1"))
4955 return -EINVAL;
4956
4957 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4958 H_GET_SESSION_TOKEN, 0, 0, 0);
4959 if (rc) {
4960 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4961 rc);
4962 return -EINVAL;
4963 }
4964
4965 session_token = (__be64)retbuf[0];
4966 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4967 be64_to_cpu(session_token));
4968 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4969 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4970 if (rc) {
4971 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4972 rc);
4973 return -EINVAL;
4974 }
4975
4976 return count;
4977}
4978
Joe Perches6cbaefb2017-12-19 10:15:09 -08004979static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004980
Thomas Falcon032c5e82015-12-21 11:26:06 -06004981static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4982{
4983 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4984 struct ibmvnic_adapter *adapter;
4985 struct iommu_table *tbl;
4986 unsigned long ret = 0;
4987 int i;
4988
4989 tbl = get_iommu_table_base(&vdev->dev);
4990
4991 /* netdev inits at probe time along with the structures we need below*/
4992 if (!netdev)
4993 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4994
4995 adapter = netdev_priv(netdev);
4996
4997 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004998 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4999
5000 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5001 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5002
5003 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
5004 i++)
5005 ret += adapter->rx_pool[i].size *
5006 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5007
5008 return ret;
5009}
5010
5011static int ibmvnic_resume(struct device *dev)
5012{
5013 struct net_device *netdev = dev_get_drvdata(dev);
5014 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005015
John Allencb89ba22017-06-19 11:27:53 -05005016 if (adapter->state != VNIC_OPEN)
5017 return 0;
5018
John Allena2488782017-07-24 13:26:06 -05005019 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06005020
5021 return 0;
5022}
5023
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305024static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005025 {"network", "IBM,vnic"},
5026 {"", "" }
5027};
5028MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5029
5030static const struct dev_pm_ops ibmvnic_pm_ops = {
5031 .resume = ibmvnic_resume
5032};
5033
5034static struct vio_driver ibmvnic_driver = {
5035 .id_table = ibmvnic_device_table,
5036 .probe = ibmvnic_probe,
5037 .remove = ibmvnic_remove,
5038 .get_desired_dma = ibmvnic_get_desired_dma,
5039 .name = ibmvnic_driver_name,
5040 .pm = &ibmvnic_pm_ops,
5041};
5042
5043/* module functions */
5044static int __init ibmvnic_module_init(void)
5045{
5046 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5047 IBMVNIC_DRIVER_VERSION);
5048
5049 return vio_register_driver(&ibmvnic_driver);
5050}
5051
5052static void __exit ibmvnic_module_exit(void)
5053{
5054 vio_unregister_driver(&ibmvnic_driver);
5055}
5056
5057module_init(ibmvnic_module_init);
5058module_exit(ibmvnic_module_exit);