blob: 67cc6d9c8fd7257af3e3c592fd96a636cc8be326 [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 *);
John Allenc26eba02017-10-26 16:23:25 -0500121static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
Nathan Fontenot30f79622018-04-06 18:37:06 -0500122static int init_crq_queue(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600123
124struct ibmvnic_stat {
125 char name[ETH_GSTRING_LEN];
126 int offset;
127};
128
129#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
130 offsetof(struct ibmvnic_statistics, stat))
131#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
132
133static const struct ibmvnic_stat ibmvnic_stats[] = {
134 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
135 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
136 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
137 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
138 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
139 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
140 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
141 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
142 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
143 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
144 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
145 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
146 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
147 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
148 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
149 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
150 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
151 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
152 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
153 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
154 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
155 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156};
157
158static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
159 unsigned long length, unsigned long *number,
160 unsigned long *irq)
161{
162 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
163 long rc;
164
165 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
166 *number = retbuf[0];
167 *irq = retbuf[1];
168
169 return rc;
170}
171
Thomas Falcon032c5e82015-12-21 11:26:06 -0600172static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
173 struct ibmvnic_long_term_buff *ltb, int size)
174{
175 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500176 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600177
178 ltb->size = size;
179 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
180 GFP_KERNEL);
181
182 if (!ltb->buff) {
183 dev_err(dev, "Couldn't alloc long term buffer\n");
184 return -ENOMEM;
185 }
186 ltb->map_id = adapter->map_id;
187 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500188
189 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500190 rc = send_request_map(adapter, ltb->addr,
191 ltb->size, ltb->map_id);
192 if (rc) {
193 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
194 return rc;
195 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600196 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500197
198 if (adapter->fw_done_rc) {
199 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
200 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500201 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500202 return -1;
203 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600204 return 0;
205}
206
207static void free_long_term_buff(struct ibmvnic_adapter *adapter,
208 struct ibmvnic_long_term_buff *ltb)
209{
210 struct device *dev = &adapter->vdev->dev;
211
Nathan Fontenotc657e322017-03-30 02:49:06 -0400212 if (!ltb->buff)
213 return;
214
Nathan Fontenoted651a12017-05-03 14:04:38 -0400215 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
216 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500217 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400218 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600219}
220
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500221static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
222 struct ibmvnic_long_term_buff *ltb)
223{
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500224 int rc;
225
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500226 memset(ltb->buff, 0, ltb->size);
227
228 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500229 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
230 if (rc)
231 return rc;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500232 wait_for_completion(&adapter->fw_done);
233
234 if (adapter->fw_done_rc) {
235 dev_info(&adapter->vdev->dev,
236 "Reset failed, attempting to free and reallocate buffer\n");
237 free_long_term_buff(adapter, ltb);
238 return alloc_long_term_buff(adapter, ltb, ltb->size);
239 }
240 return 0;
241}
242
Thomas Falconf185a492017-05-26 10:30:48 -0400243static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
244{
245 int i;
246
247 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
248 i++)
249 adapter->rx_pool[i].active = 0;
250}
251
Thomas Falcon032c5e82015-12-21 11:26:06 -0600252static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
253 struct ibmvnic_rx_pool *pool)
254{
255 int count = pool->size - atomic_read(&pool->available);
256 struct device *dev = &adapter->vdev->dev;
257 int buffers_added = 0;
258 unsigned long lpar_rc;
259 union sub_crq sub_crq;
260 struct sk_buff *skb;
261 unsigned int offset;
262 dma_addr_t dma_addr;
263 unsigned char *dst;
264 u64 *handle_array;
265 int shift = 0;
266 int index;
267 int i;
268
Thomas Falconf185a492017-05-26 10:30:48 -0400269 if (!pool->active)
270 return;
271
Thomas Falcon032c5e82015-12-21 11:26:06 -0600272 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
273 be32_to_cpu(adapter->login_rsp_buf->
274 off_rxadd_subcrqs));
275
276 for (i = 0; i < count; ++i) {
277 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
278 if (!skb) {
279 dev_err(dev, "Couldn't replenish rx buff\n");
280 adapter->replenish_no_mem++;
281 break;
282 }
283
284 index = pool->free_map[pool->next_free];
285
286 if (pool->rx_buff[index].skb)
287 dev_err(dev, "Inconsistent free_map!\n");
288
289 /* Copy the skb to the long term mapped DMA buffer */
290 offset = index * pool->buff_size;
291 dst = pool->long_term_buff.buff + offset;
292 memset(dst, 0, pool->buff_size);
293 dma_addr = pool->long_term_buff.addr + offset;
294 pool->rx_buff[index].data = dst;
295
296 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
297 pool->rx_buff[index].dma = dma_addr;
298 pool->rx_buff[index].skb = skb;
299 pool->rx_buff[index].pool_index = pool->index;
300 pool->rx_buff[index].size = pool->buff_size;
301
302 memset(&sub_crq, 0, sizeof(sub_crq));
303 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
304 sub_crq.rx_add.correlator =
305 cpu_to_be64((u64)&pool->rx_buff[index]);
306 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
307 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
308
309 /* The length field of the sCRQ is defined to be 24 bits so the
310 * buffer size needs to be left shifted by a byte before it is
311 * converted to big endian to prevent the last byte from being
312 * truncated.
313 */
314#ifdef __LITTLE_ENDIAN__
315 shift = 8;
316#endif
317 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
318
319 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
320 &sub_crq);
321 if (lpar_rc != H_SUCCESS)
322 goto failure;
323
324 buffers_added++;
325 adapter->replenish_add_buff_success++;
326 pool->next_free = (pool->next_free + 1) % pool->size;
327 }
328 atomic_add(buffers_added, &pool->available);
329 return;
330
331failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500332 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
333 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -0600334 pool->free_map[pool->next_free] = index;
335 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600336
337 dev_kfree_skb_any(skb);
338 adapter->replenish_add_buff_failure++;
339 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400340
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500341 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400342 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500343 * queue is closed or pending failover.
344 * Firmware guarantees that a signal will be sent to the
345 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400346 */
347 deactivate_rx_pools(adapter);
348 netif_carrier_off(adapter->netdev);
349 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600350}
351
352static void replenish_pools(struct ibmvnic_adapter *adapter)
353{
354 int i;
355
Thomas Falcon032c5e82015-12-21 11:26:06 -0600356 adapter->replenish_task_cycles++;
357 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
358 i++) {
359 if (adapter->rx_pool[i].active)
360 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
361 }
362}
363
John Allen3d52b592017-08-02 16:44:14 -0500364static void release_stats_buffers(struct ibmvnic_adapter *adapter)
365{
366 kfree(adapter->tx_stats_buffers);
367 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600368 adapter->tx_stats_buffers = NULL;
369 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500370}
371
372static int init_stats_buffers(struct ibmvnic_adapter *adapter)
373{
374 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600375 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500376 sizeof(struct ibmvnic_tx_queue_stats),
377 GFP_KERNEL);
378 if (!adapter->tx_stats_buffers)
379 return -ENOMEM;
380
381 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600382 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500383 sizeof(struct ibmvnic_rx_queue_stats),
384 GFP_KERNEL);
385 if (!adapter->rx_stats_buffers)
386 return -ENOMEM;
387
388 return 0;
389}
390
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400391static void release_stats_token(struct ibmvnic_adapter *adapter)
392{
393 struct device *dev = &adapter->vdev->dev;
394
395 if (!adapter->stats_token)
396 return;
397
398 dma_unmap_single(dev, adapter->stats_token,
399 sizeof(struct ibmvnic_statistics),
400 DMA_FROM_DEVICE);
401 adapter->stats_token = 0;
402}
403
404static int init_stats_token(struct ibmvnic_adapter *adapter)
405{
406 struct device *dev = &adapter->vdev->dev;
407 dma_addr_t stok;
408
409 stok = dma_map_single(dev, &adapter->stats,
410 sizeof(struct ibmvnic_statistics),
411 DMA_FROM_DEVICE);
412 if (dma_mapping_error(dev, stok)) {
413 dev_err(dev, "Couldn't map stats buffer\n");
414 return -1;
415 }
416
417 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500418 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400419 return 0;
420}
421
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400422static int reset_rx_pools(struct ibmvnic_adapter *adapter)
423{
424 struct ibmvnic_rx_pool *rx_pool;
425 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500426 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600427 u64 *size_array;
428
429 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
430 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400431
432 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
433 for (i = 0; i < rx_scrqs; i++) {
434 rx_pool = &adapter->rx_pool[i];
435
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500436 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
437
John Allen896d8692018-01-18 16:26:31 -0600438 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
439 free_long_term_buff(adapter, &rx_pool->long_term_buff);
440 rx_pool->buff_size = be64_to_cpu(size_array[i]);
441 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
442 rx_pool->size *
443 rx_pool->buff_size);
444 } else {
445 rc = reset_long_term_buff(adapter,
446 &rx_pool->long_term_buff);
447 }
448
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500449 if (rc)
450 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400451
452 for (j = 0; j < rx_pool->size; j++)
453 rx_pool->free_map[j] = j;
454
455 memset(rx_pool->rx_buff, 0,
456 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
457
458 atomic_set(&rx_pool->available, 0);
459 rx_pool->next_alloc = 0;
460 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500461 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400462 }
463
464 return 0;
465}
466
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400467static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600468{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400469 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400470 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600471
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400472 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600473 return;
474
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600475 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400476 rx_pool = &adapter->rx_pool[i];
477
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500478 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
479
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400480 kfree(rx_pool->free_map);
481 free_long_term_buff(adapter, &rx_pool->long_term_buff);
482
483 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400484 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400485
486 for (j = 0; j < rx_pool->size; j++) {
487 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600488 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
489 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400490 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600491 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400492
493 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600494 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400495
496 kfree(adapter->rx_pool);
497 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600498 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400499}
500
501static int init_rx_pools(struct net_device *netdev)
502{
503 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
504 struct device *dev = &adapter->vdev->dev;
505 struct ibmvnic_rx_pool *rx_pool;
506 int rxadd_subcrqs;
507 u64 *size_array;
508 int i, j;
509
510 rxadd_subcrqs =
511 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
512 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
513 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
514
515 adapter->rx_pool = kcalloc(rxadd_subcrqs,
516 sizeof(struct ibmvnic_rx_pool),
517 GFP_KERNEL);
518 if (!adapter->rx_pool) {
519 dev_err(dev, "Failed to allocate rx pools\n");
520 return -1;
521 }
522
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600523 adapter->num_active_rx_pools = rxadd_subcrqs;
524
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400525 for (i = 0; i < rxadd_subcrqs; i++) {
526 rx_pool = &adapter->rx_pool[i];
527
528 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500529 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400530 i, adapter->req_rx_add_entries_per_subcrq,
531 be64_to_cpu(size_array[i]));
532
533 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
534 rx_pool->index = i;
535 rx_pool->buff_size = be64_to_cpu(size_array[i]);
536 rx_pool->active = 1;
537
538 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
539 GFP_KERNEL);
540 if (!rx_pool->free_map) {
541 release_rx_pools(adapter);
542 return -1;
543 }
544
545 rx_pool->rx_buff = kcalloc(rx_pool->size,
546 sizeof(struct ibmvnic_rx_buff),
547 GFP_KERNEL);
548 if (!rx_pool->rx_buff) {
549 dev_err(dev, "Couldn't alloc rx buffers\n");
550 release_rx_pools(adapter);
551 return -1;
552 }
553
554 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
555 rx_pool->size * rx_pool->buff_size)) {
556 release_rx_pools(adapter);
557 return -1;
558 }
559
560 for (j = 0; j < rx_pool->size; ++j)
561 rx_pool->free_map[j] = j;
562
563 atomic_set(&rx_pool->available, 0);
564 rx_pool->next_alloc = 0;
565 rx_pool->next_free = 0;
566 }
567
568 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600569}
570
Thomas Falcone26dc252018-03-16 20:00:25 -0500571static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
572 struct ibmvnic_tx_pool *tx_pool)
573{
574 int rc, i;
575
576 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
577 if (rc)
578 return rc;
579
580 memset(tx_pool->tx_buff, 0,
581 tx_pool->num_buffers *
582 sizeof(struct ibmvnic_tx_buff));
583
584 for (i = 0; i < tx_pool->num_buffers; i++)
585 tx_pool->free_map[i] = i;
586
587 tx_pool->consumer_index = 0;
588 tx_pool->producer_index = 0;
589
590 return 0;
591}
592
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400593static int reset_tx_pools(struct ibmvnic_adapter *adapter)
594{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400595 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500596 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400597
598 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
599 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500600 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500601 if (rc)
602 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500603 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500604 if (rc)
605 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400606 }
607
608 return 0;
609}
610
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200611static void release_vpd_data(struct ibmvnic_adapter *adapter)
612{
613 if (!adapter->vpd)
614 return;
615
616 kfree(adapter->vpd->buff);
617 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600618
619 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200620}
621
Thomas Falconfb794212018-03-16 20:00:26 -0500622static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
623 struct ibmvnic_tx_pool *tx_pool)
624{
625 kfree(tx_pool->tx_buff);
626 kfree(tx_pool->free_map);
627 free_long_term_buff(adapter, &tx_pool->long_term_buff);
628}
629
Nathan Fontenotc657e322017-03-30 02:49:06 -0400630static void release_tx_pools(struct ibmvnic_adapter *adapter)
631{
John Allen896d8692018-01-18 16:26:31 -0600632 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400633
634 if (!adapter->tx_pool)
635 return;
636
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600637 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500638 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
639 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400640 }
641
642 kfree(adapter->tx_pool);
643 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500644 kfree(adapter->tso_pool);
645 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600646 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400647}
648
Thomas Falcon32053062018-03-16 20:00:27 -0500649static int init_one_tx_pool(struct net_device *netdev,
650 struct ibmvnic_tx_pool *tx_pool,
651 int num_entries, int buf_size)
652{
653 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
654 int i;
655
656 tx_pool->tx_buff = kcalloc(num_entries,
657 sizeof(struct ibmvnic_tx_buff),
658 GFP_KERNEL);
659 if (!tx_pool->tx_buff)
660 return -1;
661
662 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
663 num_entries * buf_size))
664 return -1;
665
666 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
667 if (!tx_pool->free_map)
668 return -1;
669
670 for (i = 0; i < num_entries; i++)
671 tx_pool->free_map[i] = i;
672
673 tx_pool->consumer_index = 0;
674 tx_pool->producer_index = 0;
675 tx_pool->num_buffers = num_entries;
676 tx_pool->buf_size = buf_size;
677
678 return 0;
679}
680
Nathan Fontenotc657e322017-03-30 02:49:06 -0400681static int init_tx_pools(struct net_device *netdev)
682{
683 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400684 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500685 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400686
687 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
688 adapter->tx_pool = kcalloc(tx_subcrqs,
689 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
690 if (!adapter->tx_pool)
691 return -1;
692
Thomas Falcon32053062018-03-16 20:00:27 -0500693 adapter->tso_pool = kcalloc(tx_subcrqs,
694 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
695 if (!adapter->tso_pool)
696 return -1;
697
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600698 adapter->num_active_tx_pools = tx_subcrqs;
699
Nathan Fontenotc657e322017-03-30 02:49:06 -0400700 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500701 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
702 adapter->req_tx_entries_per_subcrq,
703 adapter->req_mtu + VLAN_HLEN);
704 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400705 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500706 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400707 }
708
Thomas Falcon32053062018-03-16 20:00:27 -0500709 init_one_tx_pool(netdev, &adapter->tso_pool[i],
710 IBMVNIC_TSO_BUFS,
711 IBMVNIC_TSO_BUF_SZ);
712 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400713 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500714 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400715 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400716 }
717
718 return 0;
719}
720
John Allend944c3d62017-05-26 10:30:13 -0400721static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
722{
723 int i;
724
725 if (adapter->napi_enabled)
726 return;
727
728 for (i = 0; i < adapter->req_rx_queues; i++)
729 napi_enable(&adapter->napi[i]);
730
731 adapter->napi_enabled = true;
732}
733
734static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
735{
736 int i;
737
738 if (!adapter->napi_enabled)
739 return;
740
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500741 for (i = 0; i < adapter->req_rx_queues; i++) {
742 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400743 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500744 }
John Allend944c3d62017-05-26 10:30:13 -0400745
746 adapter->napi_enabled = false;
747}
748
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600749static int init_napi(struct ibmvnic_adapter *adapter)
750{
751 int i;
752
753 adapter->napi = kcalloc(adapter->req_rx_queues,
754 sizeof(struct napi_struct), GFP_KERNEL);
755 if (!adapter->napi)
756 return -ENOMEM;
757
758 for (i = 0; i < adapter->req_rx_queues; i++) {
759 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
760 netif_napi_add(adapter->netdev, &adapter->napi[i],
761 ibmvnic_poll, NAPI_POLL_WEIGHT);
762 }
763
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600764 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600765 return 0;
766}
767
768static void release_napi(struct ibmvnic_adapter *adapter)
769{
770 int i;
771
772 if (!adapter->napi)
773 return;
774
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600775 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600776 if (&adapter->napi[i]) {
777 netdev_dbg(adapter->netdev,
778 "Releasing napi[%d]\n", i);
779 netif_napi_del(&adapter->napi[i]);
780 }
781 }
782
783 kfree(adapter->napi);
784 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600785 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500786 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600787}
788
John Allena57a5d22017-03-17 17:13:41 -0500789static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600790{
791 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500792 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500793 int retry_count = 0;
Thomas Falconeb110412018-05-24 14:37:53 -0500794 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500795 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600796
John Allenbd0b6722017-03-17 17:13:40 -0500797 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500798 retry = false;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500799 if (retry_count > IBMVNIC_MAX_QUEUES) {
800 netdev_warn(netdev, "Login attempts exceeded\n");
801 return -1;
802 }
803
804 adapter->init_done_rc = 0;
805 reinit_completion(&adapter->init_done);
806 rc = send_login(adapter);
807 if (rc) {
808 netdev_warn(netdev, "Unable to login\n");
809 return rc;
810 }
811
812 if (!wait_for_completion_timeout(&adapter->init_done,
813 timeout)) {
814 netdev_warn(netdev, "Login timed out\n");
815 return -1;
816 }
817
818 if (adapter->init_done_rc == PARTIALSUCCESS) {
819 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600820 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500821
Thomas Falconeb110412018-05-24 14:37:53 -0500822 retry = true;
823 netdev_dbg(netdev,
824 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500825 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500826 reinit_completion(&adapter->init_done);
827 send_cap_queries(adapter);
828 if (!wait_for_completion_timeout(&adapter->init_done,
829 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500830 netdev_warn(netdev,
831 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500832 return -1;
833 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500834
Thomas Falcon4d96f122017-08-01 15:04:36 -0500835 rc = init_sub_crqs(adapter);
836 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500837 netdev_warn(netdev,
838 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500839 return -1;
840 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500841
Thomas Falcon4d96f122017-08-01 15:04:36 -0500842 rc = init_sub_crq_irqs(adapter);
843 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500844 netdev_warn(netdev,
845 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500846 return -1;
847 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500848 } else if (adapter->init_done_rc) {
849 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500850 return -1;
851 }
Thomas Falconeb110412018-05-24 14:37:53 -0500852 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500853
Thomas Falcon3d166132018-01-10 19:39:52 -0600854 /* handle pending MAC address changes after successful login */
855 if (adapter->mac_change_pending) {
856 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
857 adapter->mac_change_pending = false;
858 }
859
John Allena57a5d22017-03-17 17:13:41 -0500860 return 0;
861}
862
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600863static void release_login_buffer(struct ibmvnic_adapter *adapter)
864{
865 kfree(adapter->login_buf);
866 adapter->login_buf = NULL;
867}
868
869static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
870{
871 kfree(adapter->login_rsp_buf);
872 adapter->login_rsp_buf = NULL;
873}
874
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400875static void release_resources(struct ibmvnic_adapter *adapter)
876{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200877 release_vpd_data(adapter);
878
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400879 release_tx_pools(adapter);
880 release_rx_pools(adapter);
881
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600882 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600883 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400884}
885
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400886static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
887{
888 struct net_device *netdev = adapter->netdev;
889 unsigned long timeout = msecs_to_jiffies(30000);
890 union ibmvnic_crq crq;
891 bool resend;
892 int rc;
893
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500894 netdev_dbg(netdev, "setting link state %d\n", link_state);
895
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400896 memset(&crq, 0, sizeof(crq));
897 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
898 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
899 crq.logical_link_state.link_state = link_state;
900
901 do {
902 resend = false;
903
904 reinit_completion(&adapter->init_done);
905 rc = ibmvnic_send_crq(adapter, &crq);
906 if (rc) {
907 netdev_err(netdev, "Failed to set link state\n");
908 return rc;
909 }
910
911 if (!wait_for_completion_timeout(&adapter->init_done,
912 timeout)) {
913 netdev_err(netdev, "timeout setting link state\n");
914 return -1;
915 }
916
917 if (adapter->init_done_rc == 1) {
918 /* Partuial success, delay and re-send */
919 mdelay(1000);
920 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500921 } else if (adapter->init_done_rc) {
922 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
923 adapter->init_done_rc);
924 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400925 }
926 } while (resend);
927
928 return 0;
929}
930
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400931static int set_real_num_queues(struct net_device *netdev)
932{
933 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
934 int rc;
935
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500936 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
937 adapter->req_tx_queues, adapter->req_rx_queues);
938
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400939 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
940 if (rc) {
941 netdev_err(netdev, "failed to set the number of tx queues\n");
942 return rc;
943 }
944
945 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
946 if (rc)
947 netdev_err(netdev, "failed to set the number of rx queues\n");
948
949 return rc;
950}
951
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200952static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
953{
954 struct device *dev = &adapter->vdev->dev;
955 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200956 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500957 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200958
959 if (adapter->vpd->buff)
960 len = adapter->vpd->len;
961
John Allen69d08dc2018-01-18 16:27:58 -0600962 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200963 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
964 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500965 rc = ibmvnic_send_crq(adapter, &crq);
966 if (rc)
967 return rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200968 wait_for_completion(&adapter->fw_done);
969
970 if (!adapter->vpd->len)
971 return -ENODATA;
972
973 if (!adapter->vpd->buff)
974 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
975 else if (adapter->vpd->len != len)
976 adapter->vpd->buff =
977 krealloc(adapter->vpd->buff,
978 adapter->vpd->len, GFP_KERNEL);
979
980 if (!adapter->vpd->buff) {
981 dev_err(dev, "Could allocate VPD buffer\n");
982 return -ENOMEM;
983 }
984
985 adapter->vpd->dma_addr =
986 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
987 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200988 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200989 dev_err(dev, "Could not map VPD buffer\n");
990 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600991 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200992 return -ENOMEM;
993 }
994
995 reinit_completion(&adapter->fw_done);
996 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
997 crq.get_vpd.cmd = GET_VPD;
998 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
999 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001000 rc = ibmvnic_send_crq(adapter, &crq);
1001 if (rc) {
1002 kfree(adapter->vpd->buff);
1003 adapter->vpd->buff = NULL;
1004 return rc;
1005 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001006 wait_for_completion(&adapter->fw_done);
1007
1008 return 0;
1009}
1010
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001011static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001012{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001013 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001014 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001015
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001016 rc = set_real_num_queues(netdev);
1017 if (rc)
1018 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001019
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001020 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1021 if (!adapter->vpd)
1022 return -ENOMEM;
1023
John Allen69d08dc2018-01-18 16:27:58 -06001024 /* Vital Product Data (VPD) */
1025 rc = ibmvnic_get_vpd(adapter);
1026 if (rc) {
1027 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1028 return rc;
1029 }
1030
Thomas Falcon032c5e82015-12-21 11:26:06 -06001031 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001032
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001033 rc = init_napi(adapter);
1034 if (rc)
1035 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001036
Thomas Falcon032c5e82015-12-21 11:26:06 -06001037 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001038
1039 rc = init_rx_pools(netdev);
1040 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001041 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001042
Nathan Fontenotc657e322017-03-30 02:49:06 -04001043 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001044 return rc;
1045}
1046
Nathan Fontenoted651a12017-05-03 14:04:38 -04001047static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001048{
1049 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001050 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001051 int i, rc;
1052
Nathan Fontenot90c80142017-05-03 14:04:32 -04001053 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001054 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001055 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001056
Thomas Falcon032c5e82015-12-21 11:26:06 -06001057 /* We're ready to receive frames, enable the sub-crq interrupts and
1058 * set the logical link state to up
1059 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001060 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001061 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001062 if (prev_state == VNIC_CLOSED)
1063 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001064 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001065 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001066
Nathan Fontenoted651a12017-05-03 14:04:38 -04001067 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001068 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001069 if (prev_state == VNIC_CLOSED)
1070 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001071 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001072 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001073
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001074 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001075 if (rc) {
1076 for (i = 0; i < adapter->req_rx_queues; i++)
1077 napi_disable(&adapter->napi[i]);
1078 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001079 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001080 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001081
Nathan Fontenoted651a12017-05-03 14:04:38 -04001082 netif_tx_start_all_queues(netdev);
1083
1084 if (prev_state == VNIC_CLOSED) {
1085 for (i = 0; i < adapter->req_rx_queues; i++)
1086 napi_schedule(&adapter->napi[i]);
1087 }
1088
1089 adapter->state = VNIC_OPEN;
1090 return rc;
1091}
1092
1093static int ibmvnic_open(struct net_device *netdev)
1094{
1095 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001096 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001097
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001098 /* If device failover is pending, just set device state and return.
1099 * Device operation will be handled by reset routine.
1100 */
1101 if (adapter->failover_pending) {
1102 adapter->state = VNIC_OPEN;
1103 return 0;
1104 }
1105
Nathan Fontenoted651a12017-05-03 14:04:38 -04001106 if (adapter->state != VNIC_CLOSED) {
1107 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001108 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001109 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001110
1111 rc = init_resources(adapter);
1112 if (rc) {
1113 netdev_err(netdev, "failed to initialize resources\n");
1114 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001115 return rc;
1116 }
1117 }
1118
1119 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001120 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001121
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001122 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001123}
1124
Thomas Falcond0869c02018-02-13 18:23:43 -06001125static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1126{
1127 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001128 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001129 u64 rx_entries;
1130 int rx_scrqs;
1131 int i, j;
1132
1133 if (!adapter->rx_pool)
1134 return;
1135
Thomas Falcon660e3092018-04-20 14:25:32 -05001136 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001137 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1138
1139 /* Free any remaining skbs in the rx buffer pools */
1140 for (i = 0; i < rx_scrqs; i++) {
1141 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001142 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001143 continue;
1144
1145 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1146 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001147 rx_buff = &rx_pool->rx_buff[j];
1148 if (rx_buff && rx_buff->skb) {
1149 dev_kfree_skb_any(rx_buff->skb);
1150 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001151 }
1152 }
1153 }
1154}
1155
Thomas Falcone9e1e972018-03-16 20:00:30 -05001156static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1157 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001158{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001159 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001160 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001161 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001162
Dan Carpenter050e85c2018-03-23 14:36:15 +03001163 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001164 return;
1165
1166 tx_entries = tx_pool->num_buffers;
1167
1168 for (i = 0; i < tx_entries; i++) {
1169 tx_buff = &tx_pool->tx_buff[i];
1170 if (tx_buff && tx_buff->skb) {
1171 dev_kfree_skb_any(tx_buff->skb);
1172 tx_buff->skb = NULL;
1173 }
1174 }
1175}
1176
1177static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1178{
1179 int tx_scrqs;
1180 int i;
1181
1182 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001183 return;
1184
Thomas Falcon660e3092018-04-20 14:25:32 -05001185 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001186
1187 /* Free any remaining skbs in the tx buffer pools */
1188 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001189 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001190 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1191 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001192 }
1193}
1194
John Allen6095e592018-03-30 13:44:21 -05001195static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001196{
John Allen6095e592018-03-30 13:44:21 -05001197 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001198 int i;
1199
Nathan Fontenot46293b92017-05-03 14:05:02 -04001200 if (adapter->tx_scrq) {
1201 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001202 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001203 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001204 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001205 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001206 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001207 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001208 }
1209
Nathan Fontenot46293b92017-05-03 14:05:02 -04001210 if (adapter->rx_scrq) {
1211 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001212 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001213 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001214 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001215 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001216 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001217 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001218 }
1219 }
John Allen6095e592018-03-30 13:44:21 -05001220}
1221
1222static void ibmvnic_cleanup(struct net_device *netdev)
1223{
1224 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1225
1226 /* ensure that transmissions are stopped if called by do_reset */
1227 if (adapter->resetting)
1228 netif_tx_disable(netdev);
1229 else
1230 netif_tx_stop_all_queues(netdev);
1231
1232 ibmvnic_napi_disable(adapter);
1233 ibmvnic_disable_irqs(adapter);
1234
Thomas Falcond0869c02018-02-13 18:23:43 -06001235 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001236 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001237}
1238
1239static int __ibmvnic_close(struct net_device *netdev)
1240{
1241 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1242 int rc = 0;
1243
1244 adapter->state = VNIC_CLOSING;
1245 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1246 if (rc)
1247 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001248 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001249 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001250}
1251
Nathan Fontenoted651a12017-05-03 14:04:38 -04001252static int ibmvnic_close(struct net_device *netdev)
1253{
1254 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1255 int rc;
1256
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001257 /* If device failover is pending, just set device state and return.
1258 * Device operation will be handled by reset routine.
1259 */
1260 if (adapter->failover_pending) {
1261 adapter->state = VNIC_CLOSED;
1262 return 0;
1263 }
1264
Nathan Fontenoted651a12017-05-03 14:04:38 -04001265 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001266 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001267
1268 return rc;
1269}
1270
Thomas Falconad7775d2016-04-01 17:20:34 -05001271/**
1272 * build_hdr_data - creates L2/L3/L4 header data buffer
1273 * @hdr_field - bitfield determining needed headers
1274 * @skb - socket buffer
1275 * @hdr_len - array of header lengths
1276 * @tot_len - total length of data
1277 *
1278 * Reads hdr_field to determine which headers are needed by firmware.
1279 * Builds a buffer containing these headers. Saves individual header
1280 * lengths and total buffer length to be used to build descriptors.
1281 */
1282static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1283 int *hdr_len, u8 *hdr_data)
1284{
1285 int len = 0;
1286 u8 *hdr;
1287
Thomas Falconda75e3b2018-03-12 11:51:02 -05001288 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1289 hdr_len[0] = sizeof(struct vlan_ethhdr);
1290 else
1291 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001292
1293 if (skb->protocol == htons(ETH_P_IP)) {
1294 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1295 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1296 hdr_len[2] = tcp_hdrlen(skb);
1297 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1298 hdr_len[2] = sizeof(struct udphdr);
1299 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1300 hdr_len[1] = sizeof(struct ipv6hdr);
1301 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1302 hdr_len[2] = tcp_hdrlen(skb);
1303 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1304 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001305 } else if (skb->protocol == htons(ETH_P_ARP)) {
1306 hdr_len[1] = arp_hdr_len(skb->dev);
1307 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001308 }
1309
1310 memset(hdr_data, 0, 120);
1311 if ((hdr_field >> 6) & 1) {
1312 hdr = skb_mac_header(skb);
1313 memcpy(hdr_data, hdr, hdr_len[0]);
1314 len += hdr_len[0];
1315 }
1316
1317 if ((hdr_field >> 5) & 1) {
1318 hdr = skb_network_header(skb);
1319 memcpy(hdr_data + len, hdr, hdr_len[1]);
1320 len += hdr_len[1];
1321 }
1322
1323 if ((hdr_field >> 4) & 1) {
1324 hdr = skb_transport_header(skb);
1325 memcpy(hdr_data + len, hdr, hdr_len[2]);
1326 len += hdr_len[2];
1327 }
1328 return len;
1329}
1330
1331/**
1332 * create_hdr_descs - create header and header extension descriptors
1333 * @hdr_field - bitfield determining needed headers
1334 * @data - buffer containing header data
1335 * @len - length of data buffer
1336 * @hdr_len - array of individual header lengths
1337 * @scrq_arr - descriptor array
1338 *
1339 * Creates header and, if needed, header extension descriptors and
1340 * places them in a descriptor array, scrq_arr
1341 */
1342
Thomas Falcon2de09682017-10-16 10:02:11 -05001343static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1344 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001345{
1346 union sub_crq hdr_desc;
1347 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001348 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001349 u8 *data, *cur;
1350 int tmp;
1351
1352 while (tmp_len > 0) {
1353 cur = hdr_data + len - tmp_len;
1354
1355 memset(&hdr_desc, 0, sizeof(hdr_desc));
1356 if (cur != hdr_data) {
1357 data = hdr_desc.hdr_ext.data;
1358 tmp = tmp_len > 29 ? 29 : tmp_len;
1359 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1360 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1361 hdr_desc.hdr_ext.len = tmp;
1362 } else {
1363 data = hdr_desc.hdr.data;
1364 tmp = tmp_len > 24 ? 24 : tmp_len;
1365 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1366 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1367 hdr_desc.hdr.len = tmp;
1368 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1369 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1370 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1371 hdr_desc.hdr.flag = hdr_field << 1;
1372 }
1373 memcpy(data, cur, tmp);
1374 tmp_len -= tmp;
1375 *scrq_arr = hdr_desc;
1376 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001377 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001378 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001379
1380 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001381}
1382
1383/**
1384 * build_hdr_descs_arr - build a header descriptor array
1385 * @skb - socket buffer
1386 * @num_entries - number of descriptors to be sent
1387 * @subcrq - first TX descriptor
1388 * @hdr_field - bit field determining which headers will be sent
1389 *
1390 * This function will build a TX descriptor array with applicable
1391 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1392 */
1393
1394static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1395 int *num_entries, u8 hdr_field)
1396{
1397 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001398 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001399 u8 *hdr_data = txbuff->hdr_data;
1400
1401 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1402 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001403 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001404 txbuff->indir_arr + 1);
1405}
1406
Thomas Falcon1f247a62018-03-12 11:51:04 -05001407static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1408 struct net_device *netdev)
1409{
1410 /* For some backing devices, mishandling of small packets
1411 * can result in a loss of connection or TX stall. Device
1412 * architects recommend that no packet should be smaller
1413 * than the minimum MTU value provided to the driver, so
1414 * pad any packets to that length
1415 */
1416 if (skb->len < netdev->min_mtu)
1417 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001418
1419 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001420}
1421
YueHaibing94b2bb22018-09-18 14:35:47 +08001422static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001423{
1424 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1425 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001426 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001427 struct device *dev = &adapter->vdev->dev;
1428 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001429 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001430 struct ibmvnic_tx_pool *tx_pool;
1431 unsigned int tx_send_failed = 0;
1432 unsigned int tx_map_failed = 0;
1433 unsigned int tx_dropped = 0;
1434 unsigned int tx_packets = 0;
1435 unsigned int tx_bytes = 0;
1436 dma_addr_t data_dma_addr;
1437 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001438 unsigned long lpar_rc;
1439 union sub_crq tx_crq;
1440 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001441 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001442 unsigned char *dst;
1443 u64 *handle_array;
1444 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001445 u8 proto = 0;
YueHaibing94b2bb22018-09-18 14:35:47 +08001446 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001447
Nathan Fontenoted651a12017-05-03 14:04:38 -04001448 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001449 if (!netif_subqueue_stopped(netdev, skb))
1450 netif_stop_subqueue(netdev, queue_num);
1451 dev_kfree_skb_any(skb);
1452
Thomas Falcon032c5e82015-12-21 11:26:06 -06001453 tx_send_failed++;
1454 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001455 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001456 goto out;
1457 }
1458
Thomas Falcon7083a452018-03-12 21:05:26 -05001459 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001460 tx_dropped++;
1461 tx_send_failed++;
1462 ret = NETDEV_TX_OK;
1463 goto out;
1464 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001465 if (skb_is_gso(skb))
1466 tx_pool = &adapter->tso_pool[queue_num];
1467 else
1468 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001469
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001470 tx_scrq = adapter->tx_scrq[queue_num];
1471 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1472 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1473 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1474
Thomas Falcon032c5e82015-12-21 11:26:06 -06001475 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001476
Thomas Falcon86b61a52018-03-16 20:00:29 -05001477 if (index == IBMVNIC_INVALID_MAP) {
1478 dev_kfree_skb_any(skb);
1479 tx_send_failed++;
1480 tx_dropped++;
1481 ret = NETDEV_TX_OK;
1482 goto out;
1483 }
1484
1485 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1486
Thomas Falcon06b3e352018-03-16 20:00:28 -05001487 offset = index * tx_pool->buf_size;
1488 dst = tx_pool->long_term_buff.buff + offset;
1489 memset(dst, 0, tx_pool->buf_size);
1490 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001491
Thomas Falcon15482052017-10-17 12:36:54 -05001492 if (skb_shinfo(skb)->nr_frags) {
1493 int cur, i;
1494
1495 /* Copy the head */
1496 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1497 cur = skb_headlen(skb);
1498
1499 /* Copy the frags */
1500 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1501 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1502
1503 memcpy(dst + cur,
1504 page_address(skb_frag_page(frag)) +
1505 frag->page_offset, skb_frag_size(frag));
1506 cur += skb_frag_size(frag);
1507 }
1508 } else {
1509 skb_copy_from_linear_data(skb, dst, skb->len);
1510 }
1511
Thomas Falcon032c5e82015-12-21 11:26:06 -06001512 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001513 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001514
1515 tx_buff = &tx_pool->tx_buff[index];
1516 tx_buff->skb = skb;
1517 tx_buff->data_dma[0] = data_dma_addr;
1518 tx_buff->data_len[0] = skb->len;
1519 tx_buff->index = index;
1520 tx_buff->pool_index = queue_num;
1521 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001522
1523 memset(&tx_crq, 0, sizeof(tx_crq));
1524 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1525 tx_crq.v1.type = IBMVNIC_TX_DESC;
1526 tx_crq.v1.n_crq_elem = 1;
1527 tx_crq.v1.n_sge = 1;
1528 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001529
Thomas Falconfdb06102017-10-17 12:36:55 -05001530 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001531 tx_crq.v1.correlator =
1532 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001533 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001534 tx_crq.v1.correlator = cpu_to_be32(index);
1535 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001536 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1537 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1538
Michał Mirosławe84b4792018-11-07 17:50:52 +01001539 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001540 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1541 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1542 }
1543
1544 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001545 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1546 proto = ip_hdr(skb)->protocol;
1547 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1548 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1549 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001550 }
1551
Thomas Falcona0dca102018-01-18 19:29:48 -06001552 if (proto == IPPROTO_TCP)
1553 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1554 else if (proto == IPPROTO_UDP)
1555 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1556
Thomas Falconad7775d2016-04-01 17:20:34 -05001557 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001558 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001559 hdrs += 2;
1560 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001561 if (skb_is_gso(skb)) {
1562 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1563 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1564 hdrs += 2;
1565 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001566 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001567 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001568 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1569 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001570 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001571 tx_buff->indir_arr[0] = tx_crq;
1572 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1573 sizeof(tx_buff->indir_arr),
1574 DMA_TO_DEVICE);
1575 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001576 dev_kfree_skb_any(skb);
1577 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001578 if (!firmware_has_feature(FW_FEATURE_CMO))
1579 dev_err(dev, "tx: unable to map descriptor array\n");
1580 tx_map_failed++;
1581 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001582 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001583 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001584 }
John Allen498cd8e2016-04-06 11:49:55 -05001585 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001586 (u64)tx_buff->indir_dma,
1587 (u64)num_entries);
1588 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001589 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001590 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1591 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001592 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001593 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001594 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1595 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001596 dev_kfree_skb_any(skb);
1597 tx_buff->skb = NULL;
1598
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001599 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1600 /* Disable TX and report carrier off if queue is closed
1601 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001602 * Firmware guarantees that a signal will be sent to the
1603 * driver, triggering a reset or some other action.
1604 */
1605 netif_tx_stop_all_queues(netdev);
1606 netif_carrier_off(netdev);
1607 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001608
Thomas Falcon032c5e82015-12-21 11:26:06 -06001609 tx_send_failed++;
1610 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001611 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001612 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001613 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001614
Thomas Falconffc385b2018-02-18 10:08:41 -06001615 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001616 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001617 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001618 netif_stop_subqueue(netdev, queue_num);
1619 }
1620
Thomas Falcon032c5e82015-12-21 11:26:06 -06001621 tx_packets++;
1622 tx_bytes += skb->len;
1623 txq->trans_start = jiffies;
1624 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001625 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001626
Thomas Falcon86b61a52018-03-16 20:00:29 -05001627tx_err_out:
1628 /* roll back consumer index and map array*/
1629 if (tx_pool->consumer_index == 0)
1630 tx_pool->consumer_index =
1631 tx_pool->num_buffers - 1;
1632 else
1633 tx_pool->consumer_index--;
1634 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001635out:
1636 netdev->stats.tx_dropped += tx_dropped;
1637 netdev->stats.tx_bytes += tx_bytes;
1638 netdev->stats.tx_packets += tx_packets;
1639 adapter->tx_send_failed += tx_send_failed;
1640 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001641 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1642 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1643 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001644
1645 return ret;
1646}
1647
1648static void ibmvnic_set_multi(struct net_device *netdev)
1649{
1650 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1651 struct netdev_hw_addr *ha;
1652 union ibmvnic_crq crq;
1653
1654 memset(&crq, 0, sizeof(crq));
1655 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1656 crq.request_capability.cmd = REQUEST_CAPABILITY;
1657
1658 if (netdev->flags & IFF_PROMISC) {
1659 if (!adapter->promisc_supported)
1660 return;
1661 } else {
1662 if (netdev->flags & IFF_ALLMULTI) {
1663 /* Accept 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_ENABLE_ALL;
1668 ibmvnic_send_crq(adapter, &crq);
1669 } else if (netdev_mc_empty(netdev)) {
1670 /* Reject all multicast */
1671 memset(&crq, 0, sizeof(crq));
1672 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1673 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1674 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1675 ibmvnic_send_crq(adapter, &crq);
1676 } else {
1677 /* Accept one or more multicast(s) */
1678 netdev_for_each_mc_addr(ha, netdev) {
1679 memset(&crq, 0, sizeof(crq));
1680 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1681 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1682 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1683 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1684 ha->addr);
1685 ibmvnic_send_crq(adapter, &crq);
1686 }
1687 }
1688 }
1689}
1690
John Allenc26eba02017-10-26 16:23:25 -05001691static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692{
1693 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1694 struct sockaddr *addr = p;
1695 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001696 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001697
1698 if (!is_valid_ether_addr(addr->sa_data))
1699 return -EADDRNOTAVAIL;
1700
1701 memset(&crq, 0, sizeof(crq));
1702 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1703 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1704 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001705
1706 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001707 rc = ibmvnic_send_crq(adapter, &crq);
1708 if (rc)
1709 return rc;
Thomas Falconf8136142018-01-29 13:45:05 -06001710 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001712 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001713}
1714
John Allenc26eba02017-10-26 16:23:25 -05001715static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1716{
1717 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1718 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001719 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001720
Thomas Falcon3d166132018-01-10 19:39:52 -06001721 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001722 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1723 adapter->mac_change_pending = true;
1724 return 0;
1725 }
1726
Thomas Falconf8136142018-01-29 13:45:05 -06001727 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001728
Thomas Falconf8136142018-01-29 13:45:05 -06001729 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001730}
1731
Nathan Fontenoted651a12017-05-03 14:04:38 -04001732/**
1733 * do_reset returns zero if we are able to keep processing reset events, or
1734 * non-zero if we hit a fatal error and must halt.
1735 */
1736static int do_reset(struct ibmvnic_adapter *adapter,
1737 struct ibmvnic_rwi *rwi, u32 reset_state)
1738{
John Allen896d8692018-01-18 16:26:31 -06001739 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001740 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001741 struct net_device *netdev = adapter->netdev;
1742 int i, rc;
1743
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001744 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1745 rwi->reset_reason);
1746
Nathan Fontenoted651a12017-05-03 14:04:38 -04001747 netif_carrier_off(netdev);
1748 adapter->reset_reason = rwi->reset_reason;
1749
John Allen896d8692018-01-18 16:26:31 -06001750 old_num_rx_queues = adapter->req_rx_queues;
1751 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001752 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1753 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001754
Nathan Fontenot30f79622018-04-06 18:37:06 -05001755 ibmvnic_cleanup(netdev);
1756
1757 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1758 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001759 rc = __ibmvnic_close(netdev);
1760 if (rc)
1761 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001762 }
1763
John Allenc26eba02017-10-26 16:23:25 -05001764 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1765 adapter->wait_for_reset) {
1766 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001767 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001768 release_crq_queue(adapter);
1769 }
1770
John Allen8cb31cf2017-05-26 10:30:37 -04001771 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1772 /* remove the closed state so when we call open it appears
1773 * we are coming from the probed state.
1774 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001775 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001776
Nathan Fontenot30f79622018-04-06 18:37:06 -05001777 if (adapter->wait_for_reset) {
1778 rc = init_crq_queue(adapter);
1779 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1780 rc = ibmvnic_reenable_crq_queue(adapter);
1781 release_sub_crqs(adapter, 1);
1782 } else {
1783 rc = ibmvnic_reset_crq(adapter);
1784 if (!rc)
1785 rc = vio_enable_interrupts(adapter->vdev);
1786 }
1787
1788 if (rc) {
1789 netdev_err(adapter->netdev,
1790 "Couldn't initialize crq. rc=%d\n", rc);
1791 return rc;
1792 }
1793
Thomas Falcon8a348452018-05-23 13:38:00 -05001794 rc = ibmvnic_reset_init(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001795 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001796 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001797
1798 /* If the adapter was in PROBE state prior to the reset,
1799 * exit here.
1800 */
1801 if (reset_state == VNIC_PROBED)
1802 return 0;
1803
1804 rc = ibmvnic_login(netdev);
1805 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05001806 adapter->state = reset_state;
1807 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001808 }
1809
John Allenc26eba02017-10-26 16:23:25 -05001810 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1811 adapter->wait_for_reset) {
1812 rc = init_resources(adapter);
1813 if (rc)
1814 return rc;
John Allen896d8692018-01-18 16:26:31 -06001815 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001816 adapter->req_tx_queues != old_num_tx_queues ||
1817 adapter->req_rx_add_entries_per_subcrq !=
1818 old_num_rx_slots ||
1819 adapter->req_tx_entries_per_subcrq !=
1820 old_num_tx_slots) {
John Allen896d8692018-01-18 16:26:31 -06001821 release_rx_pools(adapter);
1822 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06001823 release_napi(adapter);
1824 release_vpd_data(adapter);
1825
1826 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05001827 if (rc)
1828 return rc;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001829
John Allenc26eba02017-10-26 16:23:25 -05001830 } else {
1831 rc = reset_tx_pools(adapter);
1832 if (rc)
1833 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001834
John Allenc26eba02017-10-26 16:23:25 -05001835 rc = reset_rx_pools(adapter);
1836 if (rc)
1837 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001838 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05001839 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001840 }
John Allene676d812018-03-14 10:41:29 -05001841 adapter->state = VNIC_CLOSED;
1842
1843 if (reset_state == VNIC_CLOSED)
1844 return 0;
1845
Nathan Fontenoted651a12017-05-03 14:04:38 -04001846 rc = __ibmvnic_open(netdev);
1847 if (rc) {
1848 if (list_empty(&adapter->rwi_list))
1849 adapter->state = VNIC_CLOSED;
1850 else
1851 adapter->state = reset_state;
1852
1853 return 0;
1854 }
1855
Nathan Fontenoted651a12017-05-03 14:04:38 -04001856 /* kick napi */
1857 for (i = 0; i < adapter->req_rx_queues; i++)
1858 napi_schedule(&adapter->napi[i]);
1859
Nathan Fontenotebc701b72018-04-11 10:09:38 -05001860 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1861 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
Thomas Falcon986103e2018-11-30 10:59:08 -06001862 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001863
Thomas Falconcc85c022018-02-13 15:32:50 -06001864 netif_carrier_on(netdev);
1865
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
1891 rc = init_crq_queue(adapter);
1892 if (rc) {
1893 netdev_err(adapter->netdev,
1894 "Couldn't initialize crq. rc=%d\n", rc);
1895 return rc;
1896 }
1897
1898 rc = ibmvnic_init(adapter);
1899 if (rc)
1900 return rc;
1901
1902 /* If the adapter was in PROBE state prior to the reset,
1903 * exit here.
1904 */
1905 if (reset_state == VNIC_PROBED)
1906 return 0;
1907
1908 rc = ibmvnic_login(netdev);
1909 if (rc) {
1910 adapter->state = VNIC_PROBED;
1911 return 0;
1912 }
Juliet Kima5681e22018-11-19 15:59:22 -06001913
1914 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05001915 if (rc)
1916 return rc;
1917
1918 ibmvnic_disable_irqs(adapter);
1919 adapter->state = VNIC_CLOSED;
1920
1921 if (reset_state == VNIC_CLOSED)
1922 return 0;
1923
1924 rc = __ibmvnic_open(netdev);
1925 if (rc) {
1926 if (list_empty(&adapter->rwi_list))
1927 adapter->state = VNIC_CLOSED;
1928 else
1929 adapter->state = reset_state;
1930
1931 return 0;
1932 }
1933
1934 netif_carrier_on(netdev);
1935
1936 return 0;
1937}
1938
Nathan Fontenoted651a12017-05-03 14:04:38 -04001939static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1940{
1941 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001942 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001943
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001944 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001945
1946 if (!list_empty(&adapter->rwi_list)) {
1947 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1948 list);
1949 list_del(&rwi->list);
1950 } else {
1951 rwi = NULL;
1952 }
1953
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001954 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001955 return rwi;
1956}
1957
1958static void free_all_rwi(struct ibmvnic_adapter *adapter)
1959{
1960 struct ibmvnic_rwi *rwi;
1961
1962 rwi = get_next_rwi(adapter);
1963 while (rwi) {
1964 kfree(rwi);
1965 rwi = get_next_rwi(adapter);
1966 }
1967}
1968
1969static void __ibmvnic_reset(struct work_struct *work)
1970{
1971 struct ibmvnic_rwi *rwi;
1972 struct ibmvnic_adapter *adapter;
1973 struct net_device *netdev;
Juliet Kima5681e22018-11-19 15:59:22 -06001974 bool we_lock_rtnl = false;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001975 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001976 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001977
1978 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1979 netdev = adapter->netdev;
1980
Juliet Kima5681e22018-11-19 15:59:22 -06001981 /* netif_set_real_num_xx_queues needs to take rtnl lock here
1982 * unless wait_for_reset is set, in which case the rtnl lock
1983 * has already been taken before initializing the reset
1984 */
1985 if (!adapter->wait_for_reset) {
1986 rtnl_lock();
1987 we_lock_rtnl = true;
1988 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001989 reset_state = adapter->state;
1990
1991 rwi = get_next_rwi(adapter);
1992 while (rwi) {
Thomas Falcon2770a792018-05-23 13:38:02 -05001993 if (adapter->force_reset_recovery) {
1994 adapter->force_reset_recovery = false;
1995 rc = do_hard_reset(adapter, rwi, reset_state);
1996 } else {
1997 rc = do_reset(adapter, rwi, reset_state);
1998 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001999 kfree(rwi);
Thomas Falcon2770a792018-05-23 13:38:02 -05002000 if (rc && rc != IBMVNIC_INIT_FAILED &&
2001 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002002 break;
2003
2004 rwi = get_next_rwi(adapter);
2005 }
2006
John Allenc26eba02017-10-26 16:23:25 -05002007 if (adapter->wait_for_reset) {
2008 adapter->wait_for_reset = false;
2009 adapter->reset_done_rc = rc;
2010 complete(&adapter->reset_done);
2011 }
2012
Nathan Fontenoted651a12017-05-03 14:04:38 -04002013 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002014 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002015 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002016 }
2017
2018 adapter->resetting = false;
Juliet Kima5681e22018-11-19 15:59:22 -06002019 if (we_lock_rtnl)
2020 rtnl_unlock();
Nathan Fontenoted651a12017-05-03 14:04:38 -04002021}
2022
Thomas Falconaf894d22018-04-06 18:37:04 -05002023static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2024 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002025{
Thomas Falcon2770a792018-05-23 13:38:02 -05002026 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002027 struct ibmvnic_rwi *rwi, *tmp;
2028 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002029 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002030 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002031
2032 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002033 adapter->state == VNIC_REMOVED ||
2034 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002035 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002036 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002037 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002038 }
2039
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002040 if (adapter->state == VNIC_PROBING) {
2041 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002042 ret = adapter->init_done_rc = EAGAIN;
2043 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002044 }
2045
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002046 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002047
2048 list_for_each(entry, &adapter->rwi_list) {
2049 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2050 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002051 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002052 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002053 ret = EBUSY;
2054 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002055 }
2056 }
2057
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002058 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002059 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002060 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002061 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002062 ret = ENOMEM;
2063 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002064 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002065 /* if we just received a transport event,
2066 * flush reset queue and process this reset
2067 */
2068 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2069 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2070 list_del(entry);
2071 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002072 rwi->reset_reason = reason;
2073 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002074 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falcon06e43d72018-05-23 13:38:01 -05002075 adapter->resetting = true;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002076 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002077 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002078
2079 return 0;
2080err:
2081 if (adapter->wait_for_reset)
2082 adapter->wait_for_reset = false;
2083 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002084}
2085
Thomas Falcon032c5e82015-12-21 11:26:06 -06002086static void ibmvnic_tx_timeout(struct net_device *dev)
2087{
2088 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002089
Nathan Fontenoted651a12017-05-03 14:04:38 -04002090 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002091}
2092
2093static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2094 struct ibmvnic_rx_buff *rx_buff)
2095{
2096 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2097
2098 rx_buff->skb = NULL;
2099
2100 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2101 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2102
2103 atomic_dec(&pool->available);
2104}
2105
2106static int ibmvnic_poll(struct napi_struct *napi, int budget)
2107{
2108 struct net_device *netdev = napi->dev;
2109 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2110 int scrq_num = (int)(napi - adapter->napi);
2111 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002112
Thomas Falcon032c5e82015-12-21 11:26:06 -06002113restart_poll:
2114 while (frames_processed < budget) {
2115 struct sk_buff *skb;
2116 struct ibmvnic_rx_buff *rx_buff;
2117 union sub_crq *next;
2118 u32 length;
2119 u16 offset;
2120 u8 flags = 0;
2121
John Allen34686562018-02-06 16:21:49 -06002122 if (unlikely(adapter->resetting &&
2123 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002124 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2125 napi_complete_done(napi, frames_processed);
2126 return frames_processed;
2127 }
2128
Thomas Falcon032c5e82015-12-21 11:26:06 -06002129 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2130 break;
2131 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2132 rx_buff =
2133 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2134 rx_comp.correlator);
2135 /* do error checking */
2136 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002137 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2138 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002139 /* free the entry */
2140 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002141 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002142 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002143 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002144 } else if (!rx_buff->skb) {
2145 /* free the entry */
2146 next->rx_comp.first = 0;
2147 remove_buff_from_pool(adapter, rx_buff);
2148 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002149 }
2150
2151 length = be32_to_cpu(next->rx_comp.len);
2152 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2153 flags = next->rx_comp.flags;
2154 skb = rx_buff->skb;
2155 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2156 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002157
2158 /* VLAN Header has been stripped by the system firmware and
2159 * needs to be inserted by the driver
2160 */
2161 if (adapter->rx_vlan_header_insertion &&
2162 (flags & IBMVNIC_VLAN_STRIPPED))
2163 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2164 ntohs(next->rx_comp.vlan_tci));
2165
Thomas Falcon032c5e82015-12-21 11:26:06 -06002166 /* free the entry */
2167 next->rx_comp.first = 0;
2168 remove_buff_from_pool(adapter, rx_buff);
2169
2170 skb_put(skb, length);
2171 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002172 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002173
2174 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2175 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2176 skb->ip_summed = CHECKSUM_UNNECESSARY;
2177 }
2178
2179 length = skb->len;
2180 napi_gro_receive(napi, skb); /* send it up */
2181 netdev->stats.rx_packets++;
2182 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002183 adapter->rx_stats_buffers[scrq_num].packets++;
2184 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002185 frames_processed++;
2186 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002187
2188 if (adapter->state != VNIC_CLOSING)
2189 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002190
2191 if (frames_processed < budget) {
2192 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002193 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002194 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2195 napi_reschedule(napi)) {
2196 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2197 goto restart_poll;
2198 }
2199 }
2200 return frames_processed;
2201}
2202
John Allenc26eba02017-10-26 16:23:25 -05002203static int wait_for_reset(struct ibmvnic_adapter *adapter)
2204{
Thomas Falconaf894d22018-04-06 18:37:04 -05002205 int rc, ret;
2206
John Allenc26eba02017-10-26 16:23:25 -05002207 adapter->fallback.mtu = adapter->req_mtu;
2208 adapter->fallback.rx_queues = adapter->req_rx_queues;
2209 adapter->fallback.tx_queues = adapter->req_tx_queues;
2210 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2211 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2212
2213 init_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002214 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002215 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2216 if (rc)
2217 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002218 wait_for_completion(&adapter->reset_done);
2219
Thomas Falconaf894d22018-04-06 18:37:04 -05002220 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002221 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002222 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002223 adapter->desired.mtu = adapter->fallback.mtu;
2224 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2225 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2226 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2227 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2228
2229 init_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002230 adapter->wait_for_reset = true;
2231 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2232 if (rc)
2233 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002234 wait_for_completion(&adapter->reset_done);
2235 }
2236 adapter->wait_for_reset = false;
2237
Thomas Falconaf894d22018-04-06 18:37:04 -05002238 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002239}
2240
John Allen3a807b72017-06-06 16:55:52 -05002241static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2242{
John Allenc26eba02017-10-26 16:23:25 -05002243 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2244
2245 adapter->desired.mtu = new_mtu + ETH_HLEN;
2246
2247 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002248}
2249
Thomas Falconf10b09e2018-03-12 11:51:05 -05002250static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2251 struct net_device *dev,
2252 netdev_features_t features)
2253{
2254 /* Some backing hardware adapters can not
2255 * handle packets with a MSS less than 224
2256 * or with only one segment.
2257 */
2258 if (skb_is_gso(skb)) {
2259 if (skb_shinfo(skb)->gso_size < 224 ||
2260 skb_shinfo(skb)->gso_segs == 1)
2261 features &= ~NETIF_F_GSO_MASK;
2262 }
2263
2264 return features;
2265}
2266
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267static const struct net_device_ops ibmvnic_netdev_ops = {
2268 .ndo_open = ibmvnic_open,
2269 .ndo_stop = ibmvnic_close,
2270 .ndo_start_xmit = ibmvnic_xmit,
2271 .ndo_set_rx_mode = ibmvnic_set_multi,
2272 .ndo_set_mac_address = ibmvnic_set_mac,
2273 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002274 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002275 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002276 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002277};
2278
2279/* ethtool functions */
2280
Philippe Reynes8a433792017-01-07 22:37:29 +01002281static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2282 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002283{
Philippe Reynes8a433792017-01-07 22:37:29 +01002284 u32 supported, advertising;
2285
2286 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002287 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002288 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002289 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002290 cmd->base.speed = SPEED_1000;
2291 cmd->base.duplex = DUPLEX_FULL;
2292 cmd->base.port = PORT_FIBRE;
2293 cmd->base.phy_address = 0;
2294 cmd->base.autoneg = AUTONEG_ENABLE;
2295
2296 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2297 supported);
2298 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2299 advertising);
2300
Thomas Falcon032c5e82015-12-21 11:26:06 -06002301 return 0;
2302}
2303
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002304static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002305 struct ethtool_drvinfo *info)
2306{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002307 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2308
Thomas Falcon032c5e82015-12-21 11:26:06 -06002309 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2310 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002311 strlcpy(info->fw_version, adapter->fw_version,
2312 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002313}
2314
2315static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2316{
2317 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2318
2319 return adapter->msg_enable;
2320}
2321
2322static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2323{
2324 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2325
2326 adapter->msg_enable = data;
2327}
2328
2329static u32 ibmvnic_get_link(struct net_device *netdev)
2330{
2331 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2332
2333 /* Don't need to send a query because we request a logical link up at
2334 * init and then we wait for link state indications
2335 */
2336 return adapter->logical_link_state;
2337}
2338
2339static void ibmvnic_get_ringparam(struct net_device *netdev,
2340 struct ethtool_ringparam *ring)
2341{
John Allenbc131b32017-08-02 16:46:30 -05002342 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2343
Thomas Falcon723ad912018-09-28 18:38:26 -05002344 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2345 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2346 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2347 } else {
2348 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2349 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2350 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002351 ring->rx_mini_max_pending = 0;
2352 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002353 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2354 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002355 ring->rx_mini_pending = 0;
2356 ring->rx_jumbo_pending = 0;
2357}
2358
John Allenc26eba02017-10-26 16:23:25 -05002359static int ibmvnic_set_ringparam(struct net_device *netdev,
2360 struct ethtool_ringparam *ring)
2361{
2362 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002363 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002364
Thomas Falcon723ad912018-09-28 18:38:26 -05002365 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002366 adapter->desired.rx_entries = ring->rx_pending;
2367 adapter->desired.tx_entries = ring->tx_pending;
2368
Thomas Falcon723ad912018-09-28 18:38:26 -05002369 ret = wait_for_reset(adapter);
2370
2371 if (!ret &&
2372 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2373 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2374 netdev_info(netdev,
2375 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2376 ring->rx_pending, ring->tx_pending,
2377 adapter->req_rx_add_entries_per_subcrq,
2378 adapter->req_tx_entries_per_subcrq);
2379 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002380}
2381
John Allenc2dbeb62017-08-02 16:47:17 -05002382static void ibmvnic_get_channels(struct net_device *netdev,
2383 struct ethtool_channels *channels)
2384{
2385 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2386
Thomas Falcon723ad912018-09-28 18:38:26 -05002387 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2388 channels->max_rx = adapter->max_rx_queues;
2389 channels->max_tx = adapter->max_tx_queues;
2390 } else {
2391 channels->max_rx = IBMVNIC_MAX_QUEUES;
2392 channels->max_tx = IBMVNIC_MAX_QUEUES;
2393 }
2394
John Allenc2dbeb62017-08-02 16:47:17 -05002395 channels->max_other = 0;
2396 channels->max_combined = 0;
2397 channels->rx_count = adapter->req_rx_queues;
2398 channels->tx_count = adapter->req_tx_queues;
2399 channels->other_count = 0;
2400 channels->combined_count = 0;
2401}
2402
John Allenc26eba02017-10-26 16:23:25 -05002403static int ibmvnic_set_channels(struct net_device *netdev,
2404 struct ethtool_channels *channels)
2405{
2406 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002407 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002408
Thomas Falcon723ad912018-09-28 18:38:26 -05002409 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002410 adapter->desired.rx_queues = channels->rx_count;
2411 adapter->desired.tx_queues = channels->tx_count;
2412
Thomas Falcon723ad912018-09-28 18:38:26 -05002413 ret = wait_for_reset(adapter);
2414
2415 if (!ret &&
2416 (adapter->req_rx_queues != channels->rx_count ||
2417 adapter->req_tx_queues != channels->tx_count))
2418 netdev_info(netdev,
2419 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2420 channels->rx_count, channels->tx_count,
2421 adapter->req_rx_queues, adapter->req_tx_queues);
2422 return ret;
2423
John Allenc26eba02017-10-26 16:23:25 -05002424}
2425
Thomas Falcon032c5e82015-12-21 11:26:06 -06002426static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2427{
John Allen3d52b592017-08-02 16:44:14 -05002428 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002429 int i;
2430
Thomas Falcon723ad912018-09-28 18:38:26 -05002431 switch (stringset) {
2432 case ETH_SS_STATS:
2433 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2434 i++, data += ETH_GSTRING_LEN)
2435 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2436
2437 for (i = 0; i < adapter->req_tx_queues; i++) {
2438 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2439 data += ETH_GSTRING_LEN;
2440
2441 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2442 data += ETH_GSTRING_LEN;
2443
2444 snprintf(data, ETH_GSTRING_LEN,
2445 "tx%d_dropped_packets", i);
2446 data += ETH_GSTRING_LEN;
2447 }
2448
2449 for (i = 0; i < adapter->req_rx_queues; i++) {
2450 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2451 data += ETH_GSTRING_LEN;
2452
2453 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2454 data += ETH_GSTRING_LEN;
2455
2456 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2457 data += ETH_GSTRING_LEN;
2458 }
2459 break;
2460
2461 case ETH_SS_PRIV_FLAGS:
2462 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2463 strcpy(data + i * ETH_GSTRING_LEN,
2464 ibmvnic_priv_flags[i]);
2465 break;
2466 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002467 return;
John Allen3d52b592017-08-02 16:44:14 -05002468 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002469}
2470
2471static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2472{
John Allen3d52b592017-08-02 16:44:14 -05002473 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2474
Thomas Falcon032c5e82015-12-21 11:26:06 -06002475 switch (sset) {
2476 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002477 return ARRAY_SIZE(ibmvnic_stats) +
2478 adapter->req_tx_queues * NUM_TX_STATS +
2479 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002480 case ETH_SS_PRIV_FLAGS:
2481 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002482 default:
2483 return -EOPNOTSUPP;
2484 }
2485}
2486
2487static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2488 struct ethtool_stats *stats, u64 *data)
2489{
2490 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2491 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002492 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002493 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002494
2495 memset(&crq, 0, sizeof(crq));
2496 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2497 crq.request_statistics.cmd = REQUEST_STATISTICS;
2498 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2499 crq.request_statistics.len =
2500 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501
2502 /* Wait for data to be written */
2503 init_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002504 rc = ibmvnic_send_crq(adapter, &crq);
2505 if (rc)
2506 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002507 wait_for_completion(&adapter->stats_done);
2508
2509 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002510 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2511 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002512
2513 for (j = 0; j < adapter->req_tx_queues; j++) {
2514 data[i] = adapter->tx_stats_buffers[j].packets;
2515 i++;
2516 data[i] = adapter->tx_stats_buffers[j].bytes;
2517 i++;
2518 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2519 i++;
2520 }
2521
2522 for (j = 0; j < adapter->req_rx_queues; j++) {
2523 data[i] = adapter->rx_stats_buffers[j].packets;
2524 i++;
2525 data[i] = adapter->rx_stats_buffers[j].bytes;
2526 i++;
2527 data[i] = adapter->rx_stats_buffers[j].interrupts;
2528 i++;
2529 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530}
2531
Thomas Falcon723ad912018-09-28 18:38:26 -05002532static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2533{
2534 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2535
2536 return adapter->priv_flags;
2537}
2538
2539static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2540{
2541 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2542 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2543
2544 if (which_maxes)
2545 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2546 else
2547 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2548
2549 return 0;
2550}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002551static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002552 .get_drvinfo = ibmvnic_get_drvinfo,
2553 .get_msglevel = ibmvnic_get_msglevel,
2554 .set_msglevel = ibmvnic_set_msglevel,
2555 .get_link = ibmvnic_get_link,
2556 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002557 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002558 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002559 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560 .get_strings = ibmvnic_get_strings,
2561 .get_sset_count = ibmvnic_get_sset_count,
2562 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002563 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002564 .get_priv_flags = ibmvnic_get_priv_flags,
2565 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002566};
2567
2568/* Routines for managing CRQs/sCRQs */
2569
Nathan Fontenot57a49432017-05-26 10:31:12 -04002570static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2571 struct ibmvnic_sub_crq_queue *scrq)
2572{
2573 int rc;
2574
2575 if (scrq->irq) {
2576 free_irq(scrq->irq, scrq);
2577 irq_dispose_mapping(scrq->irq);
2578 scrq->irq = 0;
2579 }
2580
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002581 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002582 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002583 scrq->cur = 0;
2584
2585 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2586 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2587 return rc;
2588}
2589
2590static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2591{
2592 int i, rc;
2593
2594 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002595 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002596 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2597 if (rc)
2598 return rc;
2599 }
2600
2601 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002602 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002603 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2604 if (rc)
2605 return rc;
2606 }
2607
Nathan Fontenot57a49432017-05-26 10:31:12 -04002608 return rc;
2609}
2610
Thomas Falcon032c5e82015-12-21 11:26:06 -06002611static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002612 struct ibmvnic_sub_crq_queue *scrq,
2613 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614{
2615 struct device *dev = &adapter->vdev->dev;
2616 long rc;
2617
2618 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2619
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002620 if (do_h_free) {
2621 /* Close the sub-crqs */
2622 do {
2623 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2624 adapter->vdev->unit_address,
2625 scrq->crq_num);
2626 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002627
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002628 if (rc) {
2629 netdev_err(adapter->netdev,
2630 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2631 scrq->crq_num, rc);
2632 }
Thomas Falconffa73852017-04-19 13:44:29 -04002633 }
2634
Thomas Falcon032c5e82015-12-21 11:26:06 -06002635 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2636 DMA_BIDIRECTIONAL);
2637 free_pages((unsigned long)scrq->msgs, 2);
2638 kfree(scrq);
2639}
2640
2641static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2642 *adapter)
2643{
2644 struct device *dev = &adapter->vdev->dev;
2645 struct ibmvnic_sub_crq_queue *scrq;
2646 int rc;
2647
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002648 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002649 if (!scrq)
2650 return NULL;
2651
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002652 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002653 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654 if (!scrq->msgs) {
2655 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2656 goto zero_page_failed;
2657 }
2658
2659 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2660 DMA_BIDIRECTIONAL);
2661 if (dma_mapping_error(dev, scrq->msg_token)) {
2662 dev_warn(dev, "Couldn't map crq queue messages page\n");
2663 goto map_failed;
2664 }
2665
2666 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2667 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2668
2669 if (rc == H_RESOURCE)
2670 rc = ibmvnic_reset_crq(adapter);
2671
2672 if (rc == H_CLOSED) {
2673 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2674 } else if (rc) {
2675 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2676 goto reg_failed;
2677 }
2678
Thomas Falcon032c5e82015-12-21 11:26:06 -06002679 scrq->adapter = adapter;
2680 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002681 spin_lock_init(&scrq->lock);
2682
2683 netdev_dbg(adapter->netdev,
2684 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2685 scrq->crq_num, scrq->hw_irq, scrq->irq);
2686
2687 return scrq;
2688
Thomas Falcon032c5e82015-12-21 11:26:06 -06002689reg_failed:
2690 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2691 DMA_BIDIRECTIONAL);
2692map_failed:
2693 free_pages((unsigned long)scrq->msgs, 2);
2694zero_page_failed:
2695 kfree(scrq);
2696
2697 return NULL;
2698}
2699
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002700static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002701{
2702 int i;
2703
2704 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002705 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002706 if (!adapter->tx_scrq[i])
2707 continue;
2708
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002709 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2710 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002711 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 free_irq(adapter->tx_scrq[i]->irq,
2713 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002714 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002715 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002716 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002717
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002718 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2719 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002720 }
2721
Nathan Fontenot9501df32017-03-15 23:38:07 -04002722 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002724 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 }
2726
2727 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002728 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002729 if (!adapter->rx_scrq[i])
2730 continue;
2731
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002732 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2733 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002734 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002735 free_irq(adapter->rx_scrq[i]->irq,
2736 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002737 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002738 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002739 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002740
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002741 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2742 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002743 }
2744
Nathan Fontenot9501df32017-03-15 23:38:07 -04002745 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002746 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002747 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002748 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002749}
2750
2751static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2752 struct ibmvnic_sub_crq_queue *scrq)
2753{
2754 struct device *dev = &adapter->vdev->dev;
2755 unsigned long rc;
2756
2757 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2758 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2759 if (rc)
2760 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2761 scrq->hw_irq, rc);
2762 return rc;
2763}
2764
2765static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2766 struct ibmvnic_sub_crq_queue *scrq)
2767{
2768 struct device *dev = &adapter->vdev->dev;
2769 unsigned long rc;
2770
2771 if (scrq->hw_irq > 0x100000000ULL) {
2772 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2773 return 1;
2774 }
2775
Nathan Fontenot73f9d362018-05-22 11:21:10 -05002776 if (adapter->resetting &&
2777 adapter->reset_reason == VNIC_RESET_MOBILITY) {
2778 u64 val = (0xff000000) | scrq->hw_irq;
2779
2780 rc = plpar_hcall_norets(H_EOI, val);
2781 if (rc)
2782 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2783 val, rc);
2784 }
Thomas Falconf23e0642018-04-15 18:53:36 -05002785
Thomas Falcon032c5e82015-12-21 11:26:06 -06002786 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2787 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2788 if (rc)
2789 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2790 scrq->hw_irq, rc);
2791 return rc;
2792}
2793
2794static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2795 struct ibmvnic_sub_crq_queue *scrq)
2796{
2797 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05002798 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799 struct ibmvnic_tx_buff *txbuff;
2800 union sub_crq *next;
2801 int index;
2802 int i, j;
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002803 u8 *first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002804
2805restart_loop:
2806 while (pending_scrq(adapter, scrq)) {
2807 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002808 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809
2810 next = ibmvnic_next_scrq(adapter, scrq);
2811 for (i = 0; i < next->tx_comp.num_comps; i++) {
2812 if (next->tx_comp.rcs[i]) {
2813 dev_err(dev, "tx error %x\n",
2814 next->tx_comp.rcs[i]);
2815 continue;
2816 }
2817 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05002818 if (index & IBMVNIC_TSO_POOL_MASK) {
2819 tx_pool = &adapter->tso_pool[pool];
2820 index &= ~IBMVNIC_TSO_POOL_MASK;
2821 } else {
2822 tx_pool = &adapter->tx_pool[pool];
2823 }
2824
2825 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06002826
2827 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2828 if (!txbuff->data_dma[j])
2829 continue;
2830
2831 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002832 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002833 /* if sub_crq was sent indirectly */
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002834 first = &txbuff->indir_arr[0].generic.first;
2835 if (*first == IBMVNIC_CRQ_CMD) {
Thomas Falconad7775d2016-04-01 17:20:34 -05002836 dma_unmap_single(dev, txbuff->indir_dma,
2837 sizeof(txbuff->indir_arr),
2838 DMA_TO_DEVICE);
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002839 *first = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05002840 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002842 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002843 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002844 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002845 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002846
Thomas Falconffc385b2018-02-18 10:08:41 -06002847 num_entries += txbuff->num_entries;
2848
Thomas Falcon06b3e352018-03-16 20:00:28 -05002849 tx_pool->free_map[tx_pool->producer_index] = index;
2850 tx_pool->producer_index =
2851 (tx_pool->producer_index + 1) %
2852 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853 }
2854 /* remove tx_comp scrq*/
2855 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002856
Thomas Falconffc385b2018-02-18 10:08:41 -06002857 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002858 (adapter->req_tx_entries_per_subcrq / 2) &&
2859 __netif_subqueue_stopped(adapter->netdev,
2860 scrq->pool_index)) {
2861 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002862 netdev_dbg(adapter->netdev, "Started queue %d\n",
2863 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002864 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002865 }
2866
2867 enable_scrq_irq(adapter, scrq);
2868
2869 if (pending_scrq(adapter, scrq)) {
2870 disable_scrq_irq(adapter, scrq);
2871 goto restart_loop;
2872 }
2873
2874 return 0;
2875}
2876
2877static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2878{
2879 struct ibmvnic_sub_crq_queue *scrq = instance;
2880 struct ibmvnic_adapter *adapter = scrq->adapter;
2881
2882 disable_scrq_irq(adapter, scrq);
2883 ibmvnic_complete_tx(adapter, scrq);
2884
2885 return IRQ_HANDLED;
2886}
2887
2888static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2889{
2890 struct ibmvnic_sub_crq_queue *scrq = instance;
2891 struct ibmvnic_adapter *adapter = scrq->adapter;
2892
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002893 /* When booting a kdump kernel we can hit pending interrupts
2894 * prior to completing driver initialization.
2895 */
2896 if (unlikely(adapter->state != VNIC_OPEN))
2897 return IRQ_NONE;
2898
John Allen3d52b592017-08-02 16:44:14 -05002899 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2900
Thomas Falcon032c5e82015-12-21 11:26:06 -06002901 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2902 disable_scrq_irq(adapter, scrq);
2903 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2904 }
2905
2906 return IRQ_HANDLED;
2907}
2908
Thomas Falconea22d512016-07-06 15:35:17 -05002909static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2910{
2911 struct device *dev = &adapter->vdev->dev;
2912 struct ibmvnic_sub_crq_queue *scrq;
2913 int i = 0, j = 0;
2914 int rc = 0;
2915
2916 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002917 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2918 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002919 scrq = adapter->tx_scrq[i];
2920 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2921
Michael Ellerman99c17902016-09-10 19:59:05 +10002922 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002923 rc = -EINVAL;
2924 dev_err(dev, "Error mapping irq\n");
2925 goto req_tx_irq_failed;
2926 }
2927
2928 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2929 0, "ibmvnic_tx", scrq);
2930
2931 if (rc) {
2932 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2933 scrq->irq, rc);
2934 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002935 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002936 }
2937 }
2938
2939 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002940 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2941 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002942 scrq = adapter->rx_scrq[i];
2943 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002944 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002945 rc = -EINVAL;
2946 dev_err(dev, "Error mapping irq\n");
2947 goto req_rx_irq_failed;
2948 }
2949 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2950 0, "ibmvnic_rx", scrq);
2951 if (rc) {
2952 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2953 scrq->irq, rc);
2954 irq_dispose_mapping(scrq->irq);
2955 goto req_rx_irq_failed;
2956 }
2957 }
2958 return rc;
2959
2960req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002961 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002962 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2963 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002964 }
Thomas Falconea22d512016-07-06 15:35:17 -05002965 i = adapter->req_tx_queues;
2966req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002967 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002968 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2969 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002970 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002971 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002972 return rc;
2973}
2974
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002975static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976{
2977 struct device *dev = &adapter->vdev->dev;
2978 struct ibmvnic_sub_crq_queue **allqueues;
2979 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002980 int total_queues;
2981 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002982 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002983
Thomas Falcon032c5e82015-12-21 11:26:06 -06002984 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2985
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002986 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002987 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002988 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002989
2990 for (i = 0; i < total_queues; i++) {
2991 allqueues[i] = init_sub_crq_queue(adapter);
2992 if (!allqueues[i]) {
2993 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2994 break;
2995 }
2996 registered_queues++;
2997 }
2998
2999 /* Make sure we were able to register the minimum number of queues */
3000 if (registered_queues <
3001 adapter->min_tx_queues + adapter->min_rx_queues) {
3002 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3003 goto tx_failed;
3004 }
3005
3006 /* Distribute the failed allocated queues*/
3007 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3008 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3009 switch (i % 3) {
3010 case 0:
3011 if (adapter->req_rx_queues > adapter->min_rx_queues)
3012 adapter->req_rx_queues--;
3013 else
3014 more++;
3015 break;
3016 case 1:
3017 if (adapter->req_tx_queues > adapter->min_tx_queues)
3018 adapter->req_tx_queues--;
3019 else
3020 more++;
3021 break;
3022 }
3023 }
3024
3025 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003026 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003027 if (!adapter->tx_scrq)
3028 goto tx_failed;
3029
3030 for (i = 0; i < adapter->req_tx_queues; i++) {
3031 adapter->tx_scrq[i] = allqueues[i];
3032 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003033 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003034 }
3035
3036 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003037 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003038 if (!adapter->rx_scrq)
3039 goto rx_failed;
3040
3041 for (i = 0; i < adapter->req_rx_queues; i++) {
3042 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3043 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003044 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 }
3046
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003047 kfree(allqueues);
3048 return 0;
3049
3050rx_failed:
3051 kfree(adapter->tx_scrq);
3052 adapter->tx_scrq = NULL;
3053tx_failed:
3054 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003055 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003056 kfree(allqueues);
3057 return -1;
3058}
3059
3060static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3061{
3062 struct device *dev = &adapter->vdev->dev;
3063 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003064 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003065
3066 if (!retry) {
3067 /* Sub-CRQ entries are 32 byte long */
3068 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3069
3070 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3071 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3072 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3073 return;
3074 }
3075
John Allenc26eba02017-10-26 16:23:25 -05003076 if (adapter->desired.mtu)
3077 adapter->req_mtu = adapter->desired.mtu;
3078 else
3079 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003080
John Allenc26eba02017-10-26 16:23:25 -05003081 if (!adapter->desired.tx_entries)
3082 adapter->desired.tx_entries =
3083 adapter->max_tx_entries_per_subcrq;
3084 if (!adapter->desired.rx_entries)
3085 adapter->desired.rx_entries =
3086 adapter->max_rx_add_entries_per_subcrq;
3087
3088 max_entries = IBMVNIC_MAX_LTB_SIZE /
3089 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3090
3091 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3092 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3093 adapter->desired.tx_entries = max_entries;
3094 }
3095
3096 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3097 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3098 adapter->desired.rx_entries = max_entries;
3099 }
3100
3101 if (adapter->desired.tx_entries)
3102 adapter->req_tx_entries_per_subcrq =
3103 adapter->desired.tx_entries;
3104 else
3105 adapter->req_tx_entries_per_subcrq =
3106 adapter->max_tx_entries_per_subcrq;
3107
3108 if (adapter->desired.rx_entries)
3109 adapter->req_rx_add_entries_per_subcrq =
3110 adapter->desired.rx_entries;
3111 else
3112 adapter->req_rx_add_entries_per_subcrq =
3113 adapter->max_rx_add_entries_per_subcrq;
3114
3115 if (adapter->desired.tx_queues)
3116 adapter->req_tx_queues =
3117 adapter->desired.tx_queues;
3118 else
3119 adapter->req_tx_queues =
3120 adapter->opt_tx_comp_sub_queues;
3121
3122 if (adapter->desired.rx_queues)
3123 adapter->req_rx_queues =
3124 adapter->desired.rx_queues;
3125 else
3126 adapter->req_rx_queues =
3127 adapter->opt_rx_comp_queues;
3128
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003129 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003130 }
3131
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132 memset(&crq, 0, sizeof(crq));
3133 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3134 crq.request_capability.cmd = REQUEST_CAPABILITY;
3135
3136 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003137 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003138 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139 ibmvnic_send_crq(adapter, &crq);
3140
3141 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003142 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003143 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003144 ibmvnic_send_crq(adapter, &crq);
3145
3146 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003147 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003148 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003149 ibmvnic_send_crq(adapter, &crq);
3150
3151 crq.request_capability.capability =
3152 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3153 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003154 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003155 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003156 ibmvnic_send_crq(adapter, &crq);
3157
3158 crq.request_capability.capability =
3159 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3160 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003161 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003162 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003163 ibmvnic_send_crq(adapter, &crq);
3164
3165 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003166 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06003167 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168 ibmvnic_send_crq(adapter, &crq);
3169
3170 if (adapter->netdev->flags & IFF_PROMISC) {
3171 if (adapter->promisc_supported) {
3172 crq.request_capability.capability =
3173 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003174 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003175 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003176 ibmvnic_send_crq(adapter, &crq);
3177 }
3178 } else {
3179 crq.request_capability.capability =
3180 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003181 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003182 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003183 ibmvnic_send_crq(adapter, &crq);
3184 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003185}
3186
3187static int pending_scrq(struct ibmvnic_adapter *adapter,
3188 struct ibmvnic_sub_crq_queue *scrq)
3189{
3190 union sub_crq *entry = &scrq->msgs[scrq->cur];
3191
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003192 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193 return 1;
3194 else
3195 return 0;
3196}
3197
3198static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3199 struct ibmvnic_sub_crq_queue *scrq)
3200{
3201 union sub_crq *entry;
3202 unsigned long flags;
3203
3204 spin_lock_irqsave(&scrq->lock, flags);
3205 entry = &scrq->msgs[scrq->cur];
3206 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3207 if (++scrq->cur == scrq->size)
3208 scrq->cur = 0;
3209 } else {
3210 entry = NULL;
3211 }
3212 spin_unlock_irqrestore(&scrq->lock, flags);
3213
3214 return entry;
3215}
3216
3217static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3218{
3219 struct ibmvnic_crq_queue *queue = &adapter->crq;
3220 union ibmvnic_crq *crq;
3221
3222 crq = &queue->msgs[queue->cur];
3223 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3224 if (++queue->cur == queue->size)
3225 queue->cur = 0;
3226 } else {
3227 crq = NULL;
3228 }
3229
3230 return crq;
3231}
3232
Thomas Falcon2d14d372018-07-13 12:03:32 -05003233static void print_subcrq_error(struct device *dev, int rc, const char *func)
3234{
3235 switch (rc) {
3236 case H_PARAMETER:
3237 dev_warn_ratelimited(dev,
3238 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3239 func, rc);
3240 break;
3241 case H_CLOSED:
3242 dev_warn_ratelimited(dev,
3243 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3244 func, rc);
3245 break;
3246 default:
3247 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3248 break;
3249 }
3250}
3251
Thomas Falcon032c5e82015-12-21 11:26:06 -06003252static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3253 union sub_crq *sub_crq)
3254{
3255 unsigned int ua = adapter->vdev->unit_address;
3256 struct device *dev = &adapter->vdev->dev;
3257 u64 *u64_crq = (u64 *)sub_crq;
3258 int rc;
3259
3260 netdev_dbg(adapter->netdev,
3261 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3262 (unsigned long int)cpu_to_be64(remote_handle),
3263 (unsigned long int)cpu_to_be64(u64_crq[0]),
3264 (unsigned long int)cpu_to_be64(u64_crq[1]),
3265 (unsigned long int)cpu_to_be64(u64_crq[2]),
3266 (unsigned long int)cpu_to_be64(u64_crq[3]));
3267
3268 /* Make sure the hypervisor sees the complete request */
3269 mb();
3270
3271 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3272 cpu_to_be64(remote_handle),
3273 cpu_to_be64(u64_crq[0]),
3274 cpu_to_be64(u64_crq[1]),
3275 cpu_to_be64(u64_crq[2]),
3276 cpu_to_be64(u64_crq[3]));
3277
Thomas Falcon2d14d372018-07-13 12:03:32 -05003278 if (rc)
3279 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003280
3281 return rc;
3282}
3283
Thomas Falconad7775d2016-04-01 17:20:34 -05003284static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3285 u64 remote_handle, u64 ioba, u64 num_entries)
3286{
3287 unsigned int ua = adapter->vdev->unit_address;
3288 struct device *dev = &adapter->vdev->dev;
3289 int rc;
3290
3291 /* Make sure the hypervisor sees the complete request */
3292 mb();
3293 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3294 cpu_to_be64(remote_handle),
3295 ioba, num_entries);
3296
Thomas Falcon2d14d372018-07-13 12:03:32 -05003297 if (rc)
3298 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003299
3300 return rc;
3301}
3302
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3304 union ibmvnic_crq *crq)
3305{
3306 unsigned int ua = adapter->vdev->unit_address;
3307 struct device *dev = &adapter->vdev->dev;
3308 u64 *u64_crq = (u64 *)crq;
3309 int rc;
3310
3311 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3312 (unsigned long int)cpu_to_be64(u64_crq[0]),
3313 (unsigned long int)cpu_to_be64(u64_crq[1]));
3314
Thomas Falcon51536982018-05-23 13:37:56 -05003315 if (!adapter->crq.active &&
3316 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3317 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3318 return -EINVAL;
3319 }
3320
Thomas Falcon032c5e82015-12-21 11:26:06 -06003321 /* Make sure the hypervisor sees the complete request */
3322 mb();
3323
3324 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3325 cpu_to_be64(u64_crq[0]),
3326 cpu_to_be64(u64_crq[1]));
3327
3328 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003329 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003330 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003331 if (adapter->resetting)
3332 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3333 }
3334
Thomas Falcon032c5e82015-12-21 11:26:06 -06003335 dev_warn(dev, "Send error (rc=%d)\n", rc);
3336 }
3337
3338 return rc;
3339}
3340
3341static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3342{
3343 union ibmvnic_crq crq;
3344
3345 memset(&crq, 0, sizeof(crq));
3346 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3347 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3348 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3349
3350 return ibmvnic_send_crq(adapter, &crq);
3351}
3352
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353static int send_version_xchg(struct ibmvnic_adapter *adapter)
3354{
3355 union ibmvnic_crq crq;
3356
3357 memset(&crq, 0, sizeof(crq));
3358 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3359 crq.version_exchange.cmd = VERSION_EXCHANGE;
3360 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3361
3362 return ibmvnic_send_crq(adapter, &crq);
3363}
3364
Nathan Fontenot37798d02017-11-08 11:23:56 -06003365struct vnic_login_client_data {
3366 u8 type;
3367 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003368 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003369} __packed;
3370
3371static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3372{
3373 int len;
3374
3375 /* Calculate the amount of buffer space needed for the
3376 * vnic client data in the login buffer. There are four entries,
3377 * OS name, LPAR name, device name, and a null last entry.
3378 */
3379 len = 4 * sizeof(struct vnic_login_client_data);
3380 len += 6; /* "Linux" plus NULL */
3381 len += strlen(utsname()->nodename) + 1;
3382 len += strlen(adapter->netdev->name) + 1;
3383
3384 return len;
3385}
3386
3387static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3388 struct vnic_login_client_data *vlcd)
3389{
3390 const char *os_name = "Linux";
3391 int len;
3392
3393 /* Type 1 - LPAR OS */
3394 vlcd->type = 1;
3395 len = strlen(os_name) + 1;
3396 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003397 strncpy(vlcd->name, os_name, len);
3398 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003399
3400 /* Type 2 - LPAR name */
3401 vlcd->type = 2;
3402 len = strlen(utsname()->nodename) + 1;
3403 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003404 strncpy(vlcd->name, utsname()->nodename, len);
3405 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003406
3407 /* Type 3 - device name */
3408 vlcd->type = 3;
3409 len = strlen(adapter->netdev->name) + 1;
3410 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003411 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003412}
3413
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003414static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003415{
3416 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3417 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003418 struct device *dev = &adapter->vdev->dev;
3419 dma_addr_t rsp_buffer_token;
3420 dma_addr_t buffer_token;
3421 size_t rsp_buffer_size;
3422 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003423 size_t buffer_size;
3424 __be64 *tx_list_p;
3425 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003426 int client_data_len;
3427 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003428 int i;
3429
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003430 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3431 netdev_err(adapter->netdev,
3432 "RX or TX queues are not allocated, device login failed\n");
3433 return -1;
3434 }
3435
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003436 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003437 client_data_len = vnic_client_data_len(adapter);
3438
Thomas Falcon032c5e82015-12-21 11:26:06 -06003439 buffer_size =
3440 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003441 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3442 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003443
Nathan Fontenot37798d02017-11-08 11:23:56 -06003444 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003445 if (!login_buffer)
3446 goto buf_alloc_failed;
3447
3448 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3449 DMA_TO_DEVICE);
3450 if (dma_mapping_error(dev, buffer_token)) {
3451 dev_err(dev, "Couldn't map login buffer\n");
3452 goto buf_map_failed;
3453 }
3454
John Allen498cd8e2016-04-06 11:49:55 -05003455 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3456 sizeof(u64) * adapter->req_tx_queues +
3457 sizeof(u64) * adapter->req_rx_queues +
3458 sizeof(u64) * adapter->req_rx_queues +
3459 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003460
3461 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3462 if (!login_rsp_buffer)
3463 goto buf_rsp_alloc_failed;
3464
3465 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3466 rsp_buffer_size, DMA_FROM_DEVICE);
3467 if (dma_mapping_error(dev, rsp_buffer_token)) {
3468 dev_err(dev, "Couldn't map login rsp buffer\n");
3469 goto buf_rsp_map_failed;
3470 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003471
Thomas Falcon032c5e82015-12-21 11:26:06 -06003472 adapter->login_buf = login_buffer;
3473 adapter->login_buf_token = buffer_token;
3474 adapter->login_buf_sz = buffer_size;
3475 adapter->login_rsp_buf = login_rsp_buffer;
3476 adapter->login_rsp_buf_token = rsp_buffer_token;
3477 adapter->login_rsp_buf_sz = rsp_buffer_size;
3478
3479 login_buffer->len = cpu_to_be32(buffer_size);
3480 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3481 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3482 login_buffer->off_txcomp_subcrqs =
3483 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3484 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3485 login_buffer->off_rxcomp_subcrqs =
3486 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3487 sizeof(u64) * adapter->req_tx_queues);
3488 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3489 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3490
3491 tx_list_p = (__be64 *)((char *)login_buffer +
3492 sizeof(struct ibmvnic_login_buffer));
3493 rx_list_p = (__be64 *)((char *)login_buffer +
3494 sizeof(struct ibmvnic_login_buffer) +
3495 sizeof(u64) * adapter->req_tx_queues);
3496
3497 for (i = 0; i < adapter->req_tx_queues; i++) {
3498 if (adapter->tx_scrq[i]) {
3499 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3500 crq_num);
3501 }
3502 }
3503
3504 for (i = 0; i < adapter->req_rx_queues; i++) {
3505 if (adapter->rx_scrq[i]) {
3506 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3507 crq_num);
3508 }
3509 }
3510
Nathan Fontenot37798d02017-11-08 11:23:56 -06003511 /* Insert vNIC login client data */
3512 vlcd = (struct vnic_login_client_data *)
3513 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3514 login_buffer->client_data_offset =
3515 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3516 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3517
3518 vnic_add_client_data(adapter, vlcd);
3519
Thomas Falcon032c5e82015-12-21 11:26:06 -06003520 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3521 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3522 netdev_dbg(adapter->netdev, "%016lx\n",
3523 ((unsigned long int *)(adapter->login_buf))[i]);
3524 }
3525
3526 memset(&crq, 0, sizeof(crq));
3527 crq.login.first = IBMVNIC_CRQ_CMD;
3528 crq.login.cmd = LOGIN;
3529 crq.login.ioba = cpu_to_be32(buffer_token);
3530 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003531 ibmvnic_send_crq(adapter, &crq);
3532
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003533 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003534
Thomas Falcon032c5e82015-12-21 11:26:06 -06003535buf_rsp_map_failed:
3536 kfree(login_rsp_buffer);
3537buf_rsp_alloc_failed:
3538 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3539buf_map_failed:
3540 kfree(login_buffer);
3541buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003542 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003543}
3544
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003545static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3546 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003547{
3548 union ibmvnic_crq crq;
3549
3550 memset(&crq, 0, sizeof(crq));
3551 crq.request_map.first = IBMVNIC_CRQ_CMD;
3552 crq.request_map.cmd = REQUEST_MAP;
3553 crq.request_map.map_id = map_id;
3554 crq.request_map.ioba = cpu_to_be32(addr);
3555 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003556 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003557}
3558
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003559static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003560{
3561 union ibmvnic_crq crq;
3562
3563 memset(&crq, 0, sizeof(crq));
3564 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3565 crq.request_unmap.cmd = REQUEST_UNMAP;
3566 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003567 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003568}
3569
3570static void send_map_query(struct ibmvnic_adapter *adapter)
3571{
3572 union ibmvnic_crq crq;
3573
3574 memset(&crq, 0, sizeof(crq));
3575 crq.query_map.first = IBMVNIC_CRQ_CMD;
3576 crq.query_map.cmd = QUERY_MAP;
3577 ibmvnic_send_crq(adapter, &crq);
3578}
3579
3580/* Send a series of CRQs requesting various capabilities of the VNIC server */
3581static void send_cap_queries(struct ibmvnic_adapter *adapter)
3582{
3583 union ibmvnic_crq crq;
3584
Thomas Falcon901e0402017-02-15 12:17:59 -06003585 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003586 memset(&crq, 0, sizeof(crq));
3587 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3588 crq.query_capability.cmd = QUERY_CAPABILITY;
3589
3590 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003591 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592 ibmvnic_send_crq(adapter, &crq);
3593
3594 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003595 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003596 ibmvnic_send_crq(adapter, &crq);
3597
3598 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003599 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003600 ibmvnic_send_crq(adapter, &crq);
3601
3602 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003603 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003604 ibmvnic_send_crq(adapter, &crq);
3605
3606 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003607 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 ibmvnic_send_crq(adapter, &crq);
3609
3610 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003611 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003612 ibmvnic_send_crq(adapter, &crq);
3613
3614 crq.query_capability.capability =
3615 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003616 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003617 ibmvnic_send_crq(adapter, &crq);
3618
3619 crq.query_capability.capability =
3620 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003621 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003622 ibmvnic_send_crq(adapter, &crq);
3623
3624 crq.query_capability.capability =
3625 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003626 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003627 ibmvnic_send_crq(adapter, &crq);
3628
3629 crq.query_capability.capability =
3630 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003631 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003632 ibmvnic_send_crq(adapter, &crq);
3633
3634 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003635 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636 ibmvnic_send_crq(adapter, &crq);
3637
3638 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003639 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003640 ibmvnic_send_crq(adapter, &crq);
3641
3642 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003643 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003644 ibmvnic_send_crq(adapter, &crq);
3645
3646 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003647 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003648 ibmvnic_send_crq(adapter, &crq);
3649
3650 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003651 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003652 ibmvnic_send_crq(adapter, &crq);
3653
3654 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003655 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003656 ibmvnic_send_crq(adapter, &crq);
3657
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003658 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3659 atomic_inc(&adapter->running_cap_crqs);
3660 ibmvnic_send_crq(adapter, &crq);
3661
Thomas Falcon032c5e82015-12-21 11:26:06 -06003662 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003663 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003664 ibmvnic_send_crq(adapter, &crq);
3665
3666 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003667 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003668 ibmvnic_send_crq(adapter, &crq);
3669
3670 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003671 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003672 ibmvnic_send_crq(adapter, &crq);
3673
3674 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003675 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003676 ibmvnic_send_crq(adapter, &crq);
3677
3678 crq.query_capability.capability =
3679 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003680 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003681 ibmvnic_send_crq(adapter, &crq);
3682
3683 crq.query_capability.capability =
3684 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003685 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003686 ibmvnic_send_crq(adapter, &crq);
3687
3688 crq.query_capability.capability =
3689 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003690 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003691 ibmvnic_send_crq(adapter, &crq);
3692
3693 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003694 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003695 ibmvnic_send_crq(adapter, &crq);
3696}
3697
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003698static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3699 struct ibmvnic_adapter *adapter)
3700{
3701 struct device *dev = &adapter->vdev->dev;
3702
3703 if (crq->get_vpd_size_rsp.rc.code) {
3704 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3705 crq->get_vpd_size_rsp.rc.code);
3706 complete(&adapter->fw_done);
3707 return;
3708 }
3709
3710 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3711 complete(&adapter->fw_done);
3712}
3713
3714static void handle_vpd_rsp(union ibmvnic_crq *crq,
3715 struct ibmvnic_adapter *adapter)
3716{
3717 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003718 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003719 u8 fw_level_len = 0;
3720
3721 memset(adapter->fw_version, 0, 32);
3722
3723 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3724 DMA_FROM_DEVICE);
3725
3726 if (crq->get_vpd_rsp.rc.code) {
3727 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3728 crq->get_vpd_rsp.rc.code);
3729 goto complete;
3730 }
3731
3732 /* get the position of the firmware version info
3733 * located after the ASCII 'RM' substring in the buffer
3734 */
3735 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3736 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003737 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 -02003738 goto complete;
3739 }
3740
3741 /* get length of firmware level ASCII substring */
3742 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3743 fw_level_len = *(substr + 2);
3744 } else {
3745 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3746 goto complete;
3747 }
3748
3749 /* copy firmware version string from vpd into adapter */
3750 if ((substr + 3 + fw_level_len) <
3751 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003752 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003753 } else {
3754 dev_info(dev, "FW substr extrapolated VPD buff\n");
3755 }
3756
3757complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003758 if (adapter->fw_version[0] == '\0')
3759 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003760 complete(&adapter->fw_done);
3761}
3762
Thomas Falcon032c5e82015-12-21 11:26:06 -06003763static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3764{
3765 struct device *dev = &adapter->vdev->dev;
3766 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3767 union ibmvnic_crq crq;
3768 int i;
3769
3770 dma_unmap_single(dev, adapter->ip_offload_tok,
3771 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3772
3773 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3774 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3775 netdev_dbg(adapter->netdev, "%016lx\n",
3776 ((unsigned long int *)(buf))[i]);
3777
3778 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3779 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3780 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3781 buf->tcp_ipv4_chksum);
3782 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3783 buf->tcp_ipv6_chksum);
3784 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3785 buf->udp_ipv4_chksum);
3786 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3787 buf->udp_ipv6_chksum);
3788 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3789 buf->large_tx_ipv4);
3790 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3791 buf->large_tx_ipv6);
3792 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3793 buf->large_rx_ipv4);
3794 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3795 buf->large_rx_ipv6);
3796 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3797 buf->max_ipv4_header_size);
3798 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3799 buf->max_ipv6_header_size);
3800 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3801 buf->max_tcp_header_size);
3802 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3803 buf->max_udp_header_size);
3804 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3805 buf->max_large_tx_size);
3806 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3807 buf->max_large_rx_size);
3808 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3809 buf->ipv6_extension_header);
3810 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3811 buf->tcp_pseudosum_req);
3812 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3813 buf->num_ipv6_ext_headers);
3814 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3815 buf->off_ipv6_ext_headers);
3816
3817 adapter->ip_offload_ctrl_tok =
3818 dma_map_single(dev, &adapter->ip_offload_ctrl,
3819 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3820
3821 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3822 dev_err(dev, "Couldn't map ip offload control buffer\n");
3823 return;
3824 }
3825
Thomas Falconf6897942018-01-18 19:05:01 -06003826 adapter->ip_offload_ctrl.len =
3827 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003828 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003829 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3830 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003831 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3832 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3833 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3834 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003835 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3836 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003837
Thomas Falconfdb06102017-10-17 12:36:55 -05003838 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003839 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3840 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3841
Thomas Falcon15482052017-10-17 12:36:54 -05003842 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843
3844 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3845 adapter->netdev->features |= NETIF_F_IP_CSUM;
3846
3847 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3848 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3849
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003850 if ((adapter->netdev->features &
3851 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3852 adapter->netdev->features |= NETIF_F_RXCSUM;
3853
Thomas Falconfdb06102017-10-17 12:36:55 -05003854 if (buf->large_tx_ipv4)
3855 adapter->netdev->features |= NETIF_F_TSO;
3856 if (buf->large_tx_ipv6)
3857 adapter->netdev->features |= NETIF_F_TSO6;
3858
Thomas Falconaa0bf852017-10-17 12:36:56 -05003859 adapter->netdev->hw_features |= adapter->netdev->features;
3860
Thomas Falcon032c5e82015-12-21 11:26:06 -06003861 memset(&crq, 0, sizeof(crq));
3862 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3863 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3864 crq.control_ip_offload.len =
3865 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3866 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3867 ibmvnic_send_crq(adapter, &crq);
3868}
3869
Thomas Falconc9008d32018-08-06 21:39:59 -05003870static const char *ibmvnic_fw_err_cause(u16 cause)
3871{
3872 switch (cause) {
3873 case ADAPTER_PROBLEM:
3874 return "adapter problem";
3875 case BUS_PROBLEM:
3876 return "bus problem";
3877 case FW_PROBLEM:
3878 return "firmware problem";
3879 case DD_PROBLEM:
3880 return "device driver problem";
3881 case EEH_RECOVERY:
3882 return "EEH recovery";
3883 case FW_UPDATED:
3884 return "firmware updated";
3885 case LOW_MEMORY:
3886 return "low Memory";
3887 default:
3888 return "unknown";
3889 }
3890}
3891
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003892static void handle_error_indication(union ibmvnic_crq *crq,
3893 struct ibmvnic_adapter *adapter)
3894{
3895 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05003896 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003897
Thomas Falconc9008d32018-08-06 21:39:59 -05003898 cause = be16_to_cpu(crq->error_indication.error_cause);
3899
3900 dev_warn_ratelimited(dev,
3901 "Firmware reports %serror, cause: %s. Starting recovery...\n",
3902 crq->error_indication.flags
3903 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3904 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003905
Nathan Fontenoted651a12017-05-03 14:04:38 -04003906 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3907 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003908 else
3909 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003910}
3911
Thomas Falconf8136142018-01-29 13:45:05 -06003912static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3913 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003914{
3915 struct net_device *netdev = adapter->netdev;
3916 struct device *dev = &adapter->vdev->dev;
3917 long rc;
3918
3919 rc = crq->change_mac_addr_rsp.rc.code;
3920 if (rc) {
3921 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003922 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003923 }
3924 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3925 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003926out:
3927 complete(&adapter->fw_done);
3928 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929}
3930
3931static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3932 struct ibmvnic_adapter *adapter)
3933{
3934 struct device *dev = &adapter->vdev->dev;
3935 u64 *req_value;
3936 char *name;
3937
Thomas Falcon901e0402017-02-15 12:17:59 -06003938 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3940 case REQ_TX_QUEUES:
3941 req_value = &adapter->req_tx_queues;
3942 name = "tx";
3943 break;
3944 case REQ_RX_QUEUES:
3945 req_value = &adapter->req_rx_queues;
3946 name = "rx";
3947 break;
3948 case REQ_RX_ADD_QUEUES:
3949 req_value = &adapter->req_rx_add_queues;
3950 name = "rx_add";
3951 break;
3952 case REQ_TX_ENTRIES_PER_SUBCRQ:
3953 req_value = &adapter->req_tx_entries_per_subcrq;
3954 name = "tx_entries_per_subcrq";
3955 break;
3956 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3957 req_value = &adapter->req_rx_add_entries_per_subcrq;
3958 name = "rx_add_entries_per_subcrq";
3959 break;
3960 case REQ_MTU:
3961 req_value = &adapter->req_mtu;
3962 name = "mtu";
3963 break;
3964 case PROMISC_REQUESTED:
3965 req_value = &adapter->promisc;
3966 name = "promisc";
3967 break;
3968 default:
3969 dev_err(dev, "Got invalid cap request rsp %d\n",
3970 crq->request_capability.capability);
3971 return;
3972 }
3973
3974 switch (crq->request_capability_rsp.rc.code) {
3975 case SUCCESS:
3976 break;
3977 case PARTIALSUCCESS:
3978 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3979 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003980 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981 number), name);
John Allene7913802018-01-18 16:27:12 -06003982
3983 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3984 REQ_MTU) {
3985 pr_err("mtu of %llu is not supported. Reverting.\n",
3986 *req_value);
3987 *req_value = adapter->fallback.mtu;
3988 } else {
3989 *req_value =
3990 be64_to_cpu(crq->request_capability_rsp.number);
3991 }
3992
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003993 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003994 return;
3995 default:
3996 dev_err(dev, "Error %d in request cap rsp\n",
3997 crq->request_capability_rsp.rc.code);
3998 return;
3999 }
4000
4001 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06004002 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003 union ibmvnic_crq newcrq;
4004 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4005 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4006 &adapter->ip_offload_buf;
4007
Thomas Falcon249168a2017-02-15 12:18:00 -06004008 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4010 buf_sz,
4011 DMA_FROM_DEVICE);
4012
4013 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4014 if (!firmware_has_feature(FW_FEATURE_CMO))
4015 dev_err(dev, "Couldn't map offload buffer\n");
4016 return;
4017 }
4018
4019 memset(&newcrq, 0, sizeof(newcrq));
4020 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4021 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4022 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4023 newcrq.query_ip_offload.ioba =
4024 cpu_to_be32(adapter->ip_offload_tok);
4025
4026 ibmvnic_send_crq(adapter, &newcrq);
4027 }
4028}
4029
4030static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4031 struct ibmvnic_adapter *adapter)
4032{
4033 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004034 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004035 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4036 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004037 int i;
4038
4039 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004040 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004041 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004042 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004043
John Allen498cd8e2016-04-06 11:49:55 -05004044 /* If the number of queues requested can't be allocated by the
4045 * server, the login response will return with code 1. We will need
4046 * to resend the login buffer with fewer queues requested.
4047 */
4048 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004049 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004050 complete(&adapter->init_done);
4051 return 0;
4052 }
4053
John Allenc26eba02017-10-26 16:23:25 -05004054 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4055
Thomas Falcon032c5e82015-12-21 11:26:06 -06004056 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4057 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4058 netdev_dbg(adapter->netdev, "%016lx\n",
4059 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4060 }
4061
4062 /* Sanity checks */
4063 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4064 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4065 adapter->req_rx_add_queues !=
4066 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4067 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4068 ibmvnic_remove(adapter->vdev);
4069 return -EIO;
4070 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06004071 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072 complete(&adapter->init_done);
4073
Thomas Falcon032c5e82015-12-21 11:26:06 -06004074 return 0;
4075}
4076
Thomas Falcon032c5e82015-12-21 11:26:06 -06004077static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4078 struct ibmvnic_adapter *adapter)
4079{
4080 struct device *dev = &adapter->vdev->dev;
4081 long rc;
4082
4083 rc = crq->request_unmap_rsp.rc.code;
4084 if (rc)
4085 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4086}
4087
4088static void handle_query_map_rsp(union ibmvnic_crq *crq,
4089 struct ibmvnic_adapter *adapter)
4090{
4091 struct net_device *netdev = adapter->netdev;
4092 struct device *dev = &adapter->vdev->dev;
4093 long rc;
4094
4095 rc = crq->query_map_rsp.rc.code;
4096 if (rc) {
4097 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4098 return;
4099 }
4100 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4101 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4102 crq->query_map_rsp.free_pages);
4103}
4104
4105static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4106 struct ibmvnic_adapter *adapter)
4107{
4108 struct net_device *netdev = adapter->netdev;
4109 struct device *dev = &adapter->vdev->dev;
4110 long rc;
4111
Thomas Falcon901e0402017-02-15 12:17:59 -06004112 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004113 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004114 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004115 rc = crq->query_capability.rc.code;
4116 if (rc) {
4117 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4118 goto out;
4119 }
4120
4121 switch (be16_to_cpu(crq->query_capability.capability)) {
4122 case MIN_TX_QUEUES:
4123 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004124 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004125 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4126 adapter->min_tx_queues);
4127 break;
4128 case MIN_RX_QUEUES:
4129 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004130 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004131 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4132 adapter->min_rx_queues);
4133 break;
4134 case MIN_RX_ADD_QUEUES:
4135 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004136 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004137 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4138 adapter->min_rx_add_queues);
4139 break;
4140 case MAX_TX_QUEUES:
4141 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004142 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004143 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4144 adapter->max_tx_queues);
4145 break;
4146 case MAX_RX_QUEUES:
4147 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004148 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4150 adapter->max_rx_queues);
4151 break;
4152 case MAX_RX_ADD_QUEUES:
4153 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004154 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004155 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4156 adapter->max_rx_add_queues);
4157 break;
4158 case MIN_TX_ENTRIES_PER_SUBCRQ:
4159 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004160 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004161 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4162 adapter->min_tx_entries_per_subcrq);
4163 break;
4164 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4165 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004166 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004167 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4168 adapter->min_rx_add_entries_per_subcrq);
4169 break;
4170 case MAX_TX_ENTRIES_PER_SUBCRQ:
4171 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004172 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004173 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4174 adapter->max_tx_entries_per_subcrq);
4175 break;
4176 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4177 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004178 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4180 adapter->max_rx_add_entries_per_subcrq);
4181 break;
4182 case TCP_IP_OFFLOAD:
4183 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004184 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004185 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4186 adapter->tcp_ip_offload);
4187 break;
4188 case PROMISC_SUPPORTED:
4189 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004190 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004191 netdev_dbg(netdev, "promisc_supported = %lld\n",
4192 adapter->promisc_supported);
4193 break;
4194 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004195 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004196 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004197 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4198 break;
4199 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004200 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004201 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4203 break;
4204 case MAX_MULTICAST_FILTERS:
4205 adapter->max_multicast_filters =
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, "max_multicast_filters = %lld\n",
4208 adapter->max_multicast_filters);
4209 break;
4210 case VLAN_HEADER_INSERTION:
4211 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004212 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004213 if (adapter->vlan_header_insertion)
4214 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4215 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4216 adapter->vlan_header_insertion);
4217 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004218 case RX_VLAN_HEADER_INSERTION:
4219 adapter->rx_vlan_header_insertion =
4220 be64_to_cpu(crq->query_capability.number);
4221 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4222 adapter->rx_vlan_header_insertion);
4223 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004224 case MAX_TX_SG_ENTRIES:
4225 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004226 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004227 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4228 adapter->max_tx_sg_entries);
4229 break;
4230 case RX_SG_SUPPORTED:
4231 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004232 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004233 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4234 adapter->rx_sg_supported);
4235 break;
4236 case OPT_TX_COMP_SUB_QUEUES:
4237 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004238 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004239 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4240 adapter->opt_tx_comp_sub_queues);
4241 break;
4242 case OPT_RX_COMP_QUEUES:
4243 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004244 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004245 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4246 adapter->opt_rx_comp_queues);
4247 break;
4248 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4249 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004250 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004251 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4252 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4253 break;
4254 case OPT_TX_ENTRIES_PER_SUBCRQ:
4255 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004256 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004257 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4258 adapter->opt_tx_entries_per_subcrq);
4259 break;
4260 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4261 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004262 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004263 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4264 adapter->opt_rxba_entries_per_subcrq);
4265 break;
4266 case TX_RX_DESC_REQ:
4267 adapter->tx_rx_desc_req = crq->query_capability.number;
4268 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4269 adapter->tx_rx_desc_req);
4270 break;
4271
4272 default:
4273 netdev_err(netdev, "Got invalid cap rsp %d\n",
4274 crq->query_capability.capability);
4275 }
4276
4277out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004278 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4279 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004280 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004281 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004282}
4283
Thomas Falcon032c5e82015-12-21 11:26:06 -06004284static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4285 struct ibmvnic_adapter *adapter)
4286{
4287 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4288 struct net_device *netdev = adapter->netdev;
4289 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004290 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004291 long rc;
4292
4293 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004294 (unsigned long int)cpu_to_be64(u64_crq[0]),
4295 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004296 switch (gen_crq->first) {
4297 case IBMVNIC_CRQ_INIT_RSP:
4298 switch (gen_crq->cmd) {
4299 case IBMVNIC_CRQ_INIT:
4300 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004301 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004302 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004303 if (!completion_done(&adapter->init_done)) {
4304 complete(&adapter->init_done);
4305 adapter->init_done_rc = -EIO;
4306 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004307 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004308 break;
4309 case IBMVNIC_CRQ_INIT_COMPLETE:
4310 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004311 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312 send_version_xchg(adapter);
4313 break;
4314 default:
4315 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4316 }
4317 return;
4318 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004319 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004320 adapter->crq.active = false;
Thomas Falcon2770a792018-05-23 13:38:02 -05004321 if (adapter->resetting)
4322 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004323 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004324 dev_info(dev, "Migrated, re-enabling adapter\n");
4325 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004326 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4327 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004328 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004329 } else {
4330 /* The adapter lost the connection */
4331 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4332 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004333 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004334 }
4335 return;
4336 case IBMVNIC_CRQ_CMD_RSP:
4337 break;
4338 default:
4339 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4340 gen_crq->first);
4341 return;
4342 }
4343
4344 switch (gen_crq->cmd) {
4345 case VERSION_EXCHANGE_RSP:
4346 rc = crq->version_exchange_rsp.rc.code;
4347 if (rc) {
4348 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4349 break;
4350 }
4351 dev_info(dev, "Partner protocol version is %d\n",
4352 crq->version_exchange_rsp.version);
4353 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4354 ibmvnic_version)
4355 ibmvnic_version =
4356 be16_to_cpu(crq->version_exchange_rsp.version);
4357 send_cap_queries(adapter);
4358 break;
4359 case QUERY_CAPABILITY_RSP:
4360 handle_query_cap_rsp(crq, adapter);
4361 break;
4362 case QUERY_MAP_RSP:
4363 handle_query_map_rsp(crq, adapter);
4364 break;
4365 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004366 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4367 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004368 break;
4369 case REQUEST_UNMAP_RSP:
4370 handle_request_unmap_rsp(crq, adapter);
4371 break;
4372 case REQUEST_CAPABILITY_RSP:
4373 handle_request_cap_rsp(crq, adapter);
4374 break;
4375 case LOGIN_RSP:
4376 netdev_dbg(netdev, "Got Login Response\n");
4377 handle_login_rsp(crq, adapter);
4378 break;
4379 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004380 netdev_dbg(netdev,
4381 "Got Logical Link State Response, state: %d rc: %d\n",
4382 crq->logical_link_state_rsp.link_state,
4383 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004384 adapter->logical_link_state =
4385 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004386 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4387 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004388 break;
4389 case LINK_STATE_INDICATION:
4390 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4391 adapter->phys_link_state =
4392 crq->link_state_indication.phys_link_state;
4393 adapter->logical_link_state =
4394 crq->link_state_indication.logical_link_state;
4395 break;
4396 case CHANGE_MAC_ADDR_RSP:
4397 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004398 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004399 break;
4400 case ERROR_INDICATION:
4401 netdev_dbg(netdev, "Got Error Indication\n");
4402 handle_error_indication(crq, adapter);
4403 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004404 case REQUEST_STATISTICS_RSP:
4405 netdev_dbg(netdev, "Got Statistics Response\n");
4406 complete(&adapter->stats_done);
4407 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004408 case QUERY_IP_OFFLOAD_RSP:
4409 netdev_dbg(netdev, "Got Query IP offload Response\n");
4410 handle_query_ip_offload_rsp(adapter);
4411 break;
4412 case MULTICAST_CTRL_RSP:
4413 netdev_dbg(netdev, "Got multicast control Response\n");
4414 break;
4415 case CONTROL_IP_OFFLOAD_RSP:
4416 netdev_dbg(netdev, "Got Control IP offload Response\n");
4417 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4418 sizeof(adapter->ip_offload_ctrl),
4419 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004420 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004421 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004422 case COLLECT_FW_TRACE_RSP:
4423 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4424 complete(&adapter->fw_done);
4425 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004426 case GET_VPD_SIZE_RSP:
4427 handle_vpd_size_rsp(crq, adapter);
4428 break;
4429 case GET_VPD_RSP:
4430 handle_vpd_rsp(crq, adapter);
4431 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004432 default:
4433 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4434 gen_crq->cmd);
4435 }
4436}
4437
4438static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4439{
4440 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004441
Thomas Falcon6c267b32017-02-15 12:17:58 -06004442 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004443 return IRQ_HANDLED;
4444}
4445
4446static void ibmvnic_tasklet(void *data)
4447{
4448 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004449 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450 union ibmvnic_crq *crq;
4451 unsigned long flags;
4452 bool done = false;
4453
4454 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004455 while (!done) {
4456 /* Pull all the valid messages off the CRQ */
4457 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4458 ibmvnic_handle_crq(crq, adapter);
4459 crq->generic.first = 0;
4460 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004461
4462 /* remain in tasklet until all
4463 * capabilities responses are received
4464 */
4465 if (!adapter->wait_capability)
4466 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004467 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004468 /* if capabilities CRQ's were sent in this tasklet, the following
4469 * tasklet must wait until all responses are received
4470 */
4471 if (atomic_read(&adapter->running_cap_crqs) != 0)
4472 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004473 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004474}
4475
4476static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4477{
4478 struct vio_dev *vdev = adapter->vdev;
4479 int rc;
4480
4481 do {
4482 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4483 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4484
4485 if (rc)
4486 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4487
4488 return rc;
4489}
4490
4491static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4492{
4493 struct ibmvnic_crq_queue *crq = &adapter->crq;
4494 struct device *dev = &adapter->vdev->dev;
4495 struct vio_dev *vdev = adapter->vdev;
4496 int rc;
4497
4498 /* Close the CRQ */
4499 do {
4500 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4501 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4502
4503 /* Clean out the queue */
4504 memset(crq->msgs, 0, PAGE_SIZE);
4505 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004506 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004507
4508 /* And re-open it again */
4509 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4510 crq->msg_token, PAGE_SIZE);
4511
4512 if (rc == H_CLOSED)
4513 /* Adapter is good, but other end is not ready */
4514 dev_warn(dev, "Partner adapter not ready\n");
4515 else if (rc != 0)
4516 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4517
4518 return rc;
4519}
4520
Nathan Fontenotf9928872017-03-30 02:48:54 -04004521static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004522{
4523 struct ibmvnic_crq_queue *crq = &adapter->crq;
4524 struct vio_dev *vdev = adapter->vdev;
4525 long rc;
4526
Nathan Fontenotf9928872017-03-30 02:48:54 -04004527 if (!crq->msgs)
4528 return;
4529
Thomas Falcon032c5e82015-12-21 11:26:06 -06004530 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4531 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004532 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004533 do {
4534 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4535 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4536
4537 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4538 DMA_BIDIRECTIONAL);
4539 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004540 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004541 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004542}
4543
Nathan Fontenotf9928872017-03-30 02:48:54 -04004544static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004545{
4546 struct ibmvnic_crq_queue *crq = &adapter->crq;
4547 struct device *dev = &adapter->vdev->dev;
4548 struct vio_dev *vdev = adapter->vdev;
4549 int rc, retrc = -ENOMEM;
4550
Nathan Fontenotf9928872017-03-30 02:48:54 -04004551 if (crq->msgs)
4552 return 0;
4553
Thomas Falcon032c5e82015-12-21 11:26:06 -06004554 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4555 /* Should we allocate more than one page? */
4556
4557 if (!crq->msgs)
4558 return -ENOMEM;
4559
4560 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4561 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4562 DMA_BIDIRECTIONAL);
4563 if (dma_mapping_error(dev, crq->msg_token))
4564 goto map_failed;
4565
4566 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4567 crq->msg_token, PAGE_SIZE);
4568
4569 if (rc == H_RESOURCE)
4570 /* maybe kexecing and resource is busy. try a reset */
4571 rc = ibmvnic_reset_crq(adapter);
4572 retrc = rc;
4573
4574 if (rc == H_CLOSED) {
4575 dev_warn(dev, "Partner adapter not ready\n");
4576 } else if (rc) {
4577 dev_warn(dev, "Error %d opening adapter\n", rc);
4578 goto reg_crq_failed;
4579 }
4580
4581 retrc = 0;
4582
Thomas Falcon6c267b32017-02-15 12:17:58 -06004583 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4584 (unsigned long)adapter);
4585
Thomas Falcon032c5e82015-12-21 11:26:06 -06004586 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4587 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4588 adapter);
4589 if (rc) {
4590 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4591 vdev->irq, rc);
4592 goto req_irq_failed;
4593 }
4594
4595 rc = vio_enable_interrupts(vdev);
4596 if (rc) {
4597 dev_err(dev, "Error %d enabling interrupts\n", rc);
4598 goto req_irq_failed;
4599 }
4600
4601 crq->cur = 0;
4602 spin_lock_init(&crq->lock);
4603
4604 return retrc;
4605
4606req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004607 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004608 do {
4609 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4610 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4611reg_crq_failed:
4612 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4613map_failed:
4614 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004615 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004616 return retrc;
4617}
4618
Thomas Falcon8a348452018-05-23 13:38:00 -05004619static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
John Allenf6ef6402017-03-17 17:13:42 -05004620{
4621 struct device *dev = &adapter->vdev->dev;
4622 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004623 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004624 int rc;
4625
John Allen017892c12017-05-26 10:30:19 -04004626 adapter->from_passive_init = false;
4627
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004628 old_num_rx_queues = adapter->req_rx_queues;
4629 old_num_tx_queues = adapter->req_tx_queues;
4630
John Allenf6ef6402017-03-17 17:13:42 -05004631 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004632 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004633 ibmvnic_send_crq_init(adapter);
4634 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4635 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004636 return -1;
4637 }
4638
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004639 if (adapter->init_done_rc) {
4640 release_crq_queue(adapter);
4641 return adapter->init_done_rc;
4642 }
4643
John Allen017892c12017-05-26 10:30:19 -04004644 if (adapter->from_passive_init) {
4645 adapter->state = VNIC_OPEN;
4646 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004647 return -1;
4648 }
4649
Nathan Fontenot30f79622018-04-06 18:37:06 -05004650 if (adapter->resetting && !adapter->wait_for_reset &&
4651 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004652 if (adapter->req_rx_queues != old_num_rx_queues ||
4653 adapter->req_tx_queues != old_num_tx_queues) {
4654 release_sub_crqs(adapter, 0);
4655 rc = init_sub_crqs(adapter);
4656 } else {
4657 rc = reset_sub_crq_queues(adapter);
4658 }
4659 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004660 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004661 }
4662
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004663 if (rc) {
4664 dev_err(dev, "Initialization of sub crqs failed\n");
4665 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004666 return rc;
4667 }
4668
4669 rc = init_sub_crq_irqs(adapter);
4670 if (rc) {
4671 dev_err(dev, "Failed to initialize sub crq irqs\n");
4672 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004673 }
4674
4675 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004676}
4677
Thomas Falcon8a348452018-05-23 13:38:00 -05004678static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4679{
4680 struct device *dev = &adapter->vdev->dev;
4681 unsigned long timeout = msecs_to_jiffies(30000);
4682 int rc;
4683
4684 adapter->from_passive_init = false;
4685
4686 init_completion(&adapter->init_done);
4687 adapter->init_done_rc = 0;
4688 ibmvnic_send_crq_init(adapter);
4689 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4690 dev_err(dev, "Initialization sequence timed out\n");
4691 return -1;
4692 }
4693
4694 if (adapter->init_done_rc) {
4695 release_crq_queue(adapter);
4696 return adapter->init_done_rc;
4697 }
4698
4699 if (adapter->from_passive_init) {
4700 adapter->state = VNIC_OPEN;
4701 adapter->from_passive_init = false;
4702 return -1;
4703 }
4704
4705 rc = init_sub_crqs(adapter);
4706 if (rc) {
4707 dev_err(dev, "Initialization of sub crqs failed\n");
4708 release_crq_queue(adapter);
4709 return rc;
4710 }
4711
4712 rc = init_sub_crq_irqs(adapter);
4713 if (rc) {
4714 dev_err(dev, "Failed to initialize sub crq irqs\n");
4715 release_crq_queue(adapter);
4716 }
4717
4718 return rc;
4719}
4720
Thomas Falcon40c9db82017-06-12 12:35:04 -05004721static struct device_attribute dev_attr_failover;
4722
Thomas Falcon032c5e82015-12-21 11:26:06 -06004723static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4724{
4725 struct ibmvnic_adapter *adapter;
4726 struct net_device *netdev;
4727 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004728 int rc;
4729
4730 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4731 dev->unit_address);
4732
4733 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4734 VETH_MAC_ADDR, NULL);
4735 if (!mac_addr_p) {
4736 dev_err(&dev->dev,
4737 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4738 __FILE__, __LINE__);
4739 return 0;
4740 }
4741
4742 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004743 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004744 if (!netdev)
4745 return -ENOMEM;
4746
4747 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004748 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004749 dev_set_drvdata(&dev->dev, netdev);
4750 adapter->vdev = dev;
4751 adapter->netdev = netdev;
4752
4753 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4754 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4755 netdev->irq = dev->irq;
4756 netdev->netdev_ops = &ibmvnic_netdev_ops;
4757 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4758 SET_NETDEV_DEV(netdev, &dev->dev);
4759
4760 spin_lock_init(&adapter->stats_lock);
4761
Nathan Fontenoted651a12017-05-03 14:04:38 -04004762 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4763 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06004764 spin_lock_init(&adapter->rwi_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004765 adapter->resetting = false;
4766
John Allenc26eba02017-10-26 16:23:25 -05004767 adapter->mac_change_pending = false;
4768
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004769 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05004770 rc = init_crq_queue(adapter);
4771 if (rc) {
4772 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4773 rc);
4774 goto ibmvnic_init_fail;
4775 }
4776
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004777 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004778 if (rc && rc != EAGAIN)
4779 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004780 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004781
Thomas Falcon07184212018-05-16 15:49:05 -05004782 rc = init_stats_buffers(adapter);
4783 if (rc)
4784 goto ibmvnic_init_fail;
4785
4786 rc = init_stats_token(adapter);
4787 if (rc)
4788 goto ibmvnic_stats_fail;
4789
Thomas Falconf39f0d12017-02-14 10:22:59 -06004790 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004791 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4792 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004793
Thomas Falcon40c9db82017-06-12 12:35:04 -05004794 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004795 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05004796 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004797
Mick Tarsele876a8a2017-09-28 13:53:18 -07004798 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004799 rc = register_netdev(netdev);
4800 if (rc) {
4801 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004802 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004803 }
4804 dev_info(&dev->dev, "ibmvnic registered\n");
4805
Nathan Fontenot90c80142017-05-03 14:04:32 -04004806 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004807
4808 adapter->wait_for_reset = false;
4809
Thomas Falcon032c5e82015-12-21 11:26:06 -06004810 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004811
4812ibmvnic_register_fail:
4813 device_remove_file(&dev->dev, &dev_attr_failover);
4814
Thomas Falcon07184212018-05-16 15:49:05 -05004815ibmvnic_dev_file_err:
4816 release_stats_token(adapter);
4817
4818ibmvnic_stats_fail:
4819 release_stats_buffers(adapter);
4820
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004821ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004822 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004823 release_crq_queue(adapter);
4824 free_netdev(netdev);
4825
4826 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004827}
4828
4829static int ibmvnic_remove(struct vio_dev *dev)
4830{
4831 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004832 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004833
Nathan Fontenot90c80142017-05-03 14:04:32 -04004834 adapter->state = VNIC_REMOVING;
Juliet Kima5681e22018-11-19 15:59:22 -06004835 rtnl_lock();
4836 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004837
4838 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004839 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004840 release_crq_queue(adapter);
4841
Thomas Falcon53cc7722018-02-26 18:10:56 -06004842 release_stats_token(adapter);
4843 release_stats_buffers(adapter);
4844
Nathan Fontenot90c80142017-05-03 14:04:32 -04004845 adapter->state = VNIC_REMOVED;
4846
Juliet Kima5681e22018-11-19 15:59:22 -06004847 rtnl_unlock();
Thomas Falcon40c9db82017-06-12 12:35:04 -05004848 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004849 free_netdev(netdev);
4850 dev_set_drvdata(&dev->dev, NULL);
4851
4852 return 0;
4853}
4854
Thomas Falcon40c9db82017-06-12 12:35:04 -05004855static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4856 const char *buf, size_t count)
4857{
4858 struct net_device *netdev = dev_get_drvdata(dev);
4859 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4860 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4861 __be64 session_token;
4862 long rc;
4863
4864 if (!sysfs_streq(buf, "1"))
4865 return -EINVAL;
4866
4867 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4868 H_GET_SESSION_TOKEN, 0, 0, 0);
4869 if (rc) {
4870 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4871 rc);
4872 return -EINVAL;
4873 }
4874
4875 session_token = (__be64)retbuf[0];
4876 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4877 be64_to_cpu(session_token));
4878 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4879 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4880 if (rc) {
4881 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4882 rc);
4883 return -EINVAL;
4884 }
4885
4886 return count;
4887}
4888
Joe Perches6cbaefb2017-12-19 10:15:09 -08004889static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004890
Thomas Falcon032c5e82015-12-21 11:26:06 -06004891static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4892{
4893 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4894 struct ibmvnic_adapter *adapter;
4895 struct iommu_table *tbl;
4896 unsigned long ret = 0;
4897 int i;
4898
4899 tbl = get_iommu_table_base(&vdev->dev);
4900
4901 /* netdev inits at probe time along with the structures we need below*/
4902 if (!netdev)
4903 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4904
4905 adapter = netdev_priv(netdev);
4906
4907 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004908 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4909
4910 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4911 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4912
4913 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4914 i++)
4915 ret += adapter->rx_pool[i].size *
4916 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4917
4918 return ret;
4919}
4920
4921static int ibmvnic_resume(struct device *dev)
4922{
4923 struct net_device *netdev = dev_get_drvdata(dev);
4924 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004925
John Allencb89ba22017-06-19 11:27:53 -05004926 if (adapter->state != VNIC_OPEN)
4927 return 0;
4928
John Allena2488782017-07-24 13:26:06 -05004929 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004930
4931 return 0;
4932}
4933
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304934static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004935 {"network", "IBM,vnic"},
4936 {"", "" }
4937};
4938MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4939
4940static const struct dev_pm_ops ibmvnic_pm_ops = {
4941 .resume = ibmvnic_resume
4942};
4943
4944static struct vio_driver ibmvnic_driver = {
4945 .id_table = ibmvnic_device_table,
4946 .probe = ibmvnic_probe,
4947 .remove = ibmvnic_remove,
4948 .get_desired_dma = ibmvnic_get_desired_dma,
4949 .name = ibmvnic_driver_name,
4950 .pm = &ibmvnic_pm_ops,
4951};
4952
4953/* module functions */
4954static int __init ibmvnic_module_init(void)
4955{
4956 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4957 IBMVNIC_DRIVER_VERSION);
4958
4959 return vio_register_driver(&ibmvnic_driver);
4960}
4961
4962static void __exit ibmvnic_module_exit(void)
4963{
4964 vio_unregister_driver(&ibmvnic_driver);
4965}
4966
4967module_init(ibmvnic_module_init);
4968module_exit(ibmvnic_module_exit);