blob: 25b8e04ef11a78178030e954e51110580c73a92c [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);
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -0300123static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600124
125struct ibmvnic_stat {
126 char name[ETH_GSTRING_LEN];
127 int offset;
128};
129
130#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
131 offsetof(struct ibmvnic_statistics, stat))
132#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
133
134static const struct ibmvnic_stat ibmvnic_stats[] = {
135 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
136 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
137 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
138 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
139 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
140 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
141 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
142 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
143 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
144 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
145 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
146 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
147 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
148 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
149 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
150 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
151 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
152 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
153 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
154 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
155 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
156 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
157};
158
159static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
160 unsigned long length, unsigned long *number,
161 unsigned long *irq)
162{
163 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
164 long rc;
165
166 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
167 *number = retbuf[0];
168 *irq = retbuf[1];
169
170 return rc;
171}
172
Thomas Falcon032c5e82015-12-21 11:26:06 -0600173static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
174 struct ibmvnic_long_term_buff *ltb, int size)
175{
176 struct device *dev = &adapter->vdev->dev;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500177 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600178
179 ltb->size = size;
180 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
181 GFP_KERNEL);
182
183 if (!ltb->buff) {
184 dev_err(dev, "Couldn't alloc long term buffer\n");
185 return -ENOMEM;
186 }
187 ltb->map_id = adapter->map_id;
188 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500189
190 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500191 rc = send_request_map(adapter, ltb->addr,
192 ltb->size, ltb->map_id);
193 if (rc) {
194 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
195 return rc;
196 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600197 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500198
199 if (adapter->fw_done_rc) {
200 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
201 adapter->fw_done_rc);
Thomas Falcon4cf2ddf32018-05-16 15:49:03 -0500202 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500203 return -1;
204 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600205 return 0;
206}
207
208static void free_long_term_buff(struct ibmvnic_adapter *adapter,
209 struct ibmvnic_long_term_buff *ltb)
210{
211 struct device *dev = &adapter->vdev->dev;
212
Nathan Fontenotc657e322017-03-30 02:49:06 -0400213 if (!ltb->buff)
214 return;
215
Nathan Fontenoted651a12017-05-03 14:04:38 -0400216 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
217 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500218 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400219 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600220}
221
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500222static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
223 struct ibmvnic_long_term_buff *ltb)
224{
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500225 int rc;
226
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500227 memset(ltb->buff, 0, ltb->size);
228
229 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500230 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
231 if (rc)
232 return rc;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500233 wait_for_completion(&adapter->fw_done);
234
235 if (adapter->fw_done_rc) {
236 dev_info(&adapter->vdev->dev,
237 "Reset failed, attempting to free and reallocate buffer\n");
238 free_long_term_buff(adapter, ltb);
239 return alloc_long_term_buff(adapter, ltb, ltb->size);
240 }
241 return 0;
242}
243
Thomas Falconf185a492017-05-26 10:30:48 -0400244static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
245{
246 int i;
247
248 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
249 i++)
250 adapter->rx_pool[i].active = 0;
251}
252
Thomas Falcon032c5e82015-12-21 11:26:06 -0600253static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
254 struct ibmvnic_rx_pool *pool)
255{
256 int count = pool->size - atomic_read(&pool->available);
257 struct device *dev = &adapter->vdev->dev;
258 int buffers_added = 0;
259 unsigned long lpar_rc;
260 union sub_crq sub_crq;
261 struct sk_buff *skb;
262 unsigned int offset;
263 dma_addr_t dma_addr;
264 unsigned char *dst;
265 u64 *handle_array;
266 int shift = 0;
267 int index;
268 int i;
269
Thomas Falconf185a492017-05-26 10:30:48 -0400270 if (!pool->active)
271 return;
272
Thomas Falcon032c5e82015-12-21 11:26:06 -0600273 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
274 be32_to_cpu(adapter->login_rsp_buf->
275 off_rxadd_subcrqs));
276
277 for (i = 0; i < count; ++i) {
278 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
279 if (!skb) {
280 dev_err(dev, "Couldn't replenish rx buff\n");
281 adapter->replenish_no_mem++;
282 break;
283 }
284
285 index = pool->free_map[pool->next_free];
286
287 if (pool->rx_buff[index].skb)
288 dev_err(dev, "Inconsistent free_map!\n");
289
290 /* Copy the skb to the long term mapped DMA buffer */
291 offset = index * pool->buff_size;
292 dst = pool->long_term_buff.buff + offset;
293 memset(dst, 0, pool->buff_size);
294 dma_addr = pool->long_term_buff.addr + offset;
295 pool->rx_buff[index].data = dst;
296
297 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
298 pool->rx_buff[index].dma = dma_addr;
299 pool->rx_buff[index].skb = skb;
300 pool->rx_buff[index].pool_index = pool->index;
301 pool->rx_buff[index].size = pool->buff_size;
302
303 memset(&sub_crq, 0, sizeof(sub_crq));
304 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
305 sub_crq.rx_add.correlator =
306 cpu_to_be64((u64)&pool->rx_buff[index]);
307 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
308 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
309
310 /* The length field of the sCRQ is defined to be 24 bits so the
311 * buffer size needs to be left shifted by a byte before it is
312 * converted to big endian to prevent the last byte from being
313 * truncated.
314 */
315#ifdef __LITTLE_ENDIAN__
316 shift = 8;
317#endif
318 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
319
320 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
321 &sub_crq);
322 if (lpar_rc != H_SUCCESS)
323 goto failure;
324
325 buffers_added++;
326 adapter->replenish_add_buff_success++;
327 pool->next_free = (pool->next_free + 1) % pool->size;
328 }
329 atomic_add(buffers_added, &pool->available);
330 return;
331
332failure:
Thomas Falcon2d14d372018-07-13 12:03:32 -0500333 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
334 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -0600335 pool->free_map[pool->next_free] = index;
336 pool->rx_buff[index].skb = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600337
338 dev_kfree_skb_any(skb);
339 adapter->replenish_add_buff_failure++;
340 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400341
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500342 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
Thomas Falconf185a492017-05-26 10:30:48 -0400343 /* Disable buffer pool replenishment and report carrier off if
Thomas Falcon5a18e1e2018-04-06 18:37:05 -0500344 * queue is closed or pending failover.
345 * Firmware guarantees that a signal will be sent to the
346 * driver, triggering a reset.
Thomas Falconf185a492017-05-26 10:30:48 -0400347 */
348 deactivate_rx_pools(adapter);
349 netif_carrier_off(adapter->netdev);
350 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600351}
352
353static void replenish_pools(struct ibmvnic_adapter *adapter)
354{
355 int i;
356
Thomas Falcon032c5e82015-12-21 11:26:06 -0600357 adapter->replenish_task_cycles++;
358 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
359 i++) {
360 if (adapter->rx_pool[i].active)
361 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
362 }
363}
364
John Allen3d52b592017-08-02 16:44:14 -0500365static void release_stats_buffers(struct ibmvnic_adapter *adapter)
366{
367 kfree(adapter->tx_stats_buffers);
368 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600369 adapter->tx_stats_buffers = NULL;
370 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500371}
372
373static int init_stats_buffers(struct ibmvnic_adapter *adapter)
374{
375 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600376 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500377 sizeof(struct ibmvnic_tx_queue_stats),
378 GFP_KERNEL);
379 if (!adapter->tx_stats_buffers)
380 return -ENOMEM;
381
382 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600383 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500384 sizeof(struct ibmvnic_rx_queue_stats),
385 GFP_KERNEL);
386 if (!adapter->rx_stats_buffers)
387 return -ENOMEM;
388
389 return 0;
390}
391
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400392static void release_stats_token(struct ibmvnic_adapter *adapter)
393{
394 struct device *dev = &adapter->vdev->dev;
395
396 if (!adapter->stats_token)
397 return;
398
399 dma_unmap_single(dev, adapter->stats_token,
400 sizeof(struct ibmvnic_statistics),
401 DMA_FROM_DEVICE);
402 adapter->stats_token = 0;
403}
404
405static int init_stats_token(struct ibmvnic_adapter *adapter)
406{
407 struct device *dev = &adapter->vdev->dev;
408 dma_addr_t stok;
409
410 stok = dma_map_single(dev, &adapter->stats,
411 sizeof(struct ibmvnic_statistics),
412 DMA_FROM_DEVICE);
413 if (dma_mapping_error(dev, stok)) {
414 dev_err(dev, "Couldn't map stats buffer\n");
415 return -1;
416 }
417
418 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500419 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400420 return 0;
421}
422
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400423static int reset_rx_pools(struct ibmvnic_adapter *adapter)
424{
425 struct ibmvnic_rx_pool *rx_pool;
426 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500427 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600428 u64 *size_array;
429
430 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
431 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400432
433 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
434 for (i = 0; i < rx_scrqs; i++) {
435 rx_pool = &adapter->rx_pool[i];
436
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500437 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
438
John Allen896d8692018-01-18 16:26:31 -0600439 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
440 free_long_term_buff(adapter, &rx_pool->long_term_buff);
441 rx_pool->buff_size = be64_to_cpu(size_array[i]);
442 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
443 rx_pool->size *
444 rx_pool->buff_size);
445 } else {
446 rc = reset_long_term_buff(adapter,
447 &rx_pool->long_term_buff);
448 }
449
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500450 if (rc)
451 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400452
453 for (j = 0; j < rx_pool->size; j++)
454 rx_pool->free_map[j] = j;
455
456 memset(rx_pool->rx_buff, 0,
457 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
458
459 atomic_set(&rx_pool->available, 0);
460 rx_pool->next_alloc = 0;
461 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500462 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400463 }
464
465 return 0;
466}
467
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400468static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600469{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400470 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400471 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600472
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400473 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600474 return;
475
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600476 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400477 rx_pool = &adapter->rx_pool[i];
478
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500479 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
480
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400481 kfree(rx_pool->free_map);
482 free_long_term_buff(adapter, &rx_pool->long_term_buff);
483
484 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400485 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400486
487 for (j = 0; j < rx_pool->size; j++) {
488 if (rx_pool->rx_buff[j].skb) {
Thomas Falconb7cdec32018-11-21 11:17:58 -0600489 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
490 rx_pool->rx_buff[j].skb = NULL;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400491 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600492 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400493
494 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600495 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400496
497 kfree(adapter->rx_pool);
498 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600499 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400500}
501
502static int init_rx_pools(struct net_device *netdev)
503{
504 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
505 struct device *dev = &adapter->vdev->dev;
506 struct ibmvnic_rx_pool *rx_pool;
507 int rxadd_subcrqs;
508 u64 *size_array;
509 int i, j;
510
511 rxadd_subcrqs =
512 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
513 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
514 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
515
516 adapter->rx_pool = kcalloc(rxadd_subcrqs,
517 sizeof(struct ibmvnic_rx_pool),
518 GFP_KERNEL);
519 if (!adapter->rx_pool) {
520 dev_err(dev, "Failed to allocate rx pools\n");
521 return -1;
522 }
523
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600524 adapter->num_active_rx_pools = rxadd_subcrqs;
525
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400526 for (i = 0; i < rxadd_subcrqs; i++) {
527 rx_pool = &adapter->rx_pool[i];
528
529 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500530 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400531 i, adapter->req_rx_add_entries_per_subcrq,
532 be64_to_cpu(size_array[i]));
533
534 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
535 rx_pool->index = i;
536 rx_pool->buff_size = be64_to_cpu(size_array[i]);
537 rx_pool->active = 1;
538
539 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
540 GFP_KERNEL);
541 if (!rx_pool->free_map) {
542 release_rx_pools(adapter);
543 return -1;
544 }
545
546 rx_pool->rx_buff = kcalloc(rx_pool->size,
547 sizeof(struct ibmvnic_rx_buff),
548 GFP_KERNEL);
549 if (!rx_pool->rx_buff) {
550 dev_err(dev, "Couldn't alloc rx buffers\n");
551 release_rx_pools(adapter);
552 return -1;
553 }
554
555 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
556 rx_pool->size * rx_pool->buff_size)) {
557 release_rx_pools(adapter);
558 return -1;
559 }
560
561 for (j = 0; j < rx_pool->size; ++j)
562 rx_pool->free_map[j] = j;
563
564 atomic_set(&rx_pool->available, 0);
565 rx_pool->next_alloc = 0;
566 rx_pool->next_free = 0;
567 }
568
569 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600570}
571
Thomas Falcone26dc252018-03-16 20:00:25 -0500572static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
573 struct ibmvnic_tx_pool *tx_pool)
574{
575 int rc, i;
576
577 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
578 if (rc)
579 return rc;
580
581 memset(tx_pool->tx_buff, 0,
582 tx_pool->num_buffers *
583 sizeof(struct ibmvnic_tx_buff));
584
585 for (i = 0; i < tx_pool->num_buffers; i++)
586 tx_pool->free_map[i] = i;
587
588 tx_pool->consumer_index = 0;
589 tx_pool->producer_index = 0;
590
591 return 0;
592}
593
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400594static int reset_tx_pools(struct ibmvnic_adapter *adapter)
595{
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400596 int tx_scrqs;
Thomas Falcone26dc252018-03-16 20:00:25 -0500597 int i, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400598
599 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
600 for (i = 0; i < tx_scrqs; i++) {
Thomas Falcone26dc252018-03-16 20:00:25 -0500601 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500602 if (rc)
603 return rc;
Thomas Falcone26dc252018-03-16 20:00:25 -0500604 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
Thomas Falconfdb06102017-10-17 12:36:55 -0500605 if (rc)
606 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400607 }
608
609 return 0;
610}
611
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200612static void release_vpd_data(struct ibmvnic_adapter *adapter)
613{
614 if (!adapter->vpd)
615 return;
616
617 kfree(adapter->vpd->buff);
618 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600619
620 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200621}
622
Thomas Falconfb794212018-03-16 20:00:26 -0500623static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
624 struct ibmvnic_tx_pool *tx_pool)
625{
626 kfree(tx_pool->tx_buff);
627 kfree(tx_pool->free_map);
628 free_long_term_buff(adapter, &tx_pool->long_term_buff);
629}
630
Nathan Fontenotc657e322017-03-30 02:49:06 -0400631static void release_tx_pools(struct ibmvnic_adapter *adapter)
632{
John Allen896d8692018-01-18 16:26:31 -0600633 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400634
635 if (!adapter->tx_pool)
636 return;
637
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600638 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Thomas Falconfb794212018-03-16 20:00:26 -0500639 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
640 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400641 }
642
643 kfree(adapter->tx_pool);
644 adapter->tx_pool = NULL;
Thomas Falconfb794212018-03-16 20:00:26 -0500645 kfree(adapter->tso_pool);
646 adapter->tso_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600647 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400648}
649
Thomas Falcon32053062018-03-16 20:00:27 -0500650static int init_one_tx_pool(struct net_device *netdev,
651 struct ibmvnic_tx_pool *tx_pool,
652 int num_entries, int buf_size)
653{
654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
655 int i;
656
657 tx_pool->tx_buff = kcalloc(num_entries,
658 sizeof(struct ibmvnic_tx_buff),
659 GFP_KERNEL);
660 if (!tx_pool->tx_buff)
661 return -1;
662
663 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
664 num_entries * buf_size))
665 return -1;
666
667 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
668 if (!tx_pool->free_map)
669 return -1;
670
671 for (i = 0; i < num_entries; i++)
672 tx_pool->free_map[i] = i;
673
674 tx_pool->consumer_index = 0;
675 tx_pool->producer_index = 0;
676 tx_pool->num_buffers = num_entries;
677 tx_pool->buf_size = buf_size;
678
679 return 0;
680}
681
Nathan Fontenotc657e322017-03-30 02:49:06 -0400682static int init_tx_pools(struct net_device *netdev)
683{
684 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400685 int tx_subcrqs;
Thomas Falcon32053062018-03-16 20:00:27 -0500686 int i, rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400687
688 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
689 adapter->tx_pool = kcalloc(tx_subcrqs,
690 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
691 if (!adapter->tx_pool)
692 return -1;
693
Thomas Falcon32053062018-03-16 20:00:27 -0500694 adapter->tso_pool = kcalloc(tx_subcrqs,
695 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
696 if (!adapter->tso_pool)
697 return -1;
698
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600699 adapter->num_active_tx_pools = tx_subcrqs;
700
Nathan Fontenotc657e322017-03-30 02:49:06 -0400701 for (i = 0; i < tx_subcrqs; i++) {
Thomas Falcon32053062018-03-16 20:00:27 -0500702 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
703 adapter->req_tx_entries_per_subcrq,
704 adapter->req_mtu + VLAN_HLEN);
705 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400706 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500707 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400708 }
709
Thomas Falcon32053062018-03-16 20:00:27 -0500710 init_one_tx_pool(netdev, &adapter->tso_pool[i],
711 IBMVNIC_TSO_BUFS,
712 IBMVNIC_TSO_BUF_SZ);
713 if (rc) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400714 release_tx_pools(adapter);
Thomas Falcon32053062018-03-16 20:00:27 -0500715 return rc;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400716 }
Nathan Fontenotc657e322017-03-30 02:49:06 -0400717 }
718
719 return 0;
720}
721
John Allend944c3d62017-05-26 10:30:13 -0400722static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
723{
724 int i;
725
726 if (adapter->napi_enabled)
727 return;
728
729 for (i = 0; i < adapter->req_rx_queues; i++)
730 napi_enable(&adapter->napi[i]);
731
732 adapter->napi_enabled = true;
733}
734
735static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
736{
737 int i;
738
739 if (!adapter->napi_enabled)
740 return;
741
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500742 for (i = 0; i < adapter->req_rx_queues; i++) {
743 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400744 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500745 }
John Allend944c3d62017-05-26 10:30:13 -0400746
747 adapter->napi_enabled = false;
748}
749
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600750static int init_napi(struct ibmvnic_adapter *adapter)
751{
752 int i;
753
754 adapter->napi = kcalloc(adapter->req_rx_queues,
755 sizeof(struct napi_struct), GFP_KERNEL);
756 if (!adapter->napi)
757 return -ENOMEM;
758
759 for (i = 0; i < adapter->req_rx_queues; i++) {
760 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
761 netif_napi_add(adapter->netdev, &adapter->napi[i],
762 ibmvnic_poll, NAPI_POLL_WEIGHT);
763 }
764
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600765 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600766 return 0;
767}
768
769static void release_napi(struct ibmvnic_adapter *adapter)
770{
771 int i;
772
773 if (!adapter->napi)
774 return;
775
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600776 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Wen Yang390de192018-12-11 12:20:46 +0800777 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
778 netif_napi_del(&adapter->napi[i]);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600779 }
780
781 kfree(adapter->napi);
782 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600783 adapter->num_active_rx_napi = 0;
Thomas Falconc3f22412018-05-23 13:37:55 -0500784 adapter->napi_enabled = false;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600785}
786
John Allena57a5d22017-03-17 17:13:41 -0500787static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600788{
789 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500790 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500791 int retry_count = 0;
Thomas Falconeb110412018-05-24 14:37:53 -0500792 bool retry;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500793 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600794
John Allenbd0b6722017-03-17 17:13:40 -0500795 do {
Thomas Falconeb110412018-05-24 14:37:53 -0500796 retry = false;
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500797 if (retry_count > IBMVNIC_MAX_QUEUES) {
798 netdev_warn(netdev, "Login attempts exceeded\n");
799 return -1;
800 }
801
802 adapter->init_done_rc = 0;
803 reinit_completion(&adapter->init_done);
804 rc = send_login(adapter);
805 if (rc) {
806 netdev_warn(netdev, "Unable to login\n");
807 return rc;
808 }
809
810 if (!wait_for_completion_timeout(&adapter->init_done,
811 timeout)) {
812 netdev_warn(netdev, "Login timed out\n");
813 return -1;
814 }
815
816 if (adapter->init_done_rc == PARTIALSUCCESS) {
817 retry_count++;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600818 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500819
Thomas Falconeb110412018-05-24 14:37:53 -0500820 retry = true;
821 netdev_dbg(netdev,
822 "Received partial success, retrying...\n");
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500823 adapter->init_done_rc = 0;
John Allenbd0b6722017-03-17 17:13:40 -0500824 reinit_completion(&adapter->init_done);
825 send_cap_queries(adapter);
826 if (!wait_for_completion_timeout(&adapter->init_done,
827 timeout)) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500828 netdev_warn(netdev,
829 "Capabilities query timed out\n");
John Allenbd0b6722017-03-17 17:13:40 -0500830 return -1;
831 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500832
Thomas Falcon4d96f122017-08-01 15:04:36 -0500833 rc = init_sub_crqs(adapter);
834 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500835 netdev_warn(netdev,
836 "SCRQ initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500837 return -1;
838 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500839
Thomas Falcon4d96f122017-08-01 15:04:36 -0500840 rc = init_sub_crq_irqs(adapter);
841 if (rc) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500842 netdev_warn(netdev,
843 "SCRQ irq initialization failed\n");
Thomas Falcon4d96f122017-08-01 15:04:36 -0500844 return -1;
845 }
Nathan Fontenot64d92aa2018-04-11 10:09:32 -0500846 } else if (adapter->init_done_rc) {
847 netdev_warn(netdev, "Adapter login failed\n");
John Allenbd0b6722017-03-17 17:13:40 -0500848 return -1;
849 }
Thomas Falconeb110412018-05-24 14:37:53 -0500850 } while (retry);
John Allenbd0b6722017-03-17 17:13:40 -0500851
Thomas Falcon3d166132018-01-10 19:39:52 -0600852 /* handle pending MAC address changes after successful login */
853 if (adapter->mac_change_pending) {
854 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
855 adapter->mac_change_pending = false;
856 }
857
John Allena57a5d22017-03-17 17:13:41 -0500858 return 0;
859}
860
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600861static void release_login_buffer(struct ibmvnic_adapter *adapter)
862{
863 kfree(adapter->login_buf);
864 adapter->login_buf = NULL;
865}
866
867static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
868{
869 kfree(adapter->login_rsp_buf);
870 adapter->login_rsp_buf = NULL;
871}
872
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400873static void release_resources(struct ibmvnic_adapter *adapter)
874{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200875 release_vpd_data(adapter);
876
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400877 release_tx_pools(adapter);
878 release_rx_pools(adapter);
879
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600880 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600881 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400882}
883
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400884static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
885{
886 struct net_device *netdev = adapter->netdev;
887 unsigned long timeout = msecs_to_jiffies(30000);
888 union ibmvnic_crq crq;
889 bool resend;
890 int rc;
891
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500892 netdev_dbg(netdev, "setting link state %d\n", link_state);
893
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400894 memset(&crq, 0, sizeof(crq));
895 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
896 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
897 crq.logical_link_state.link_state = link_state;
898
899 do {
900 resend = false;
901
902 reinit_completion(&adapter->init_done);
903 rc = ibmvnic_send_crq(adapter, &crq);
904 if (rc) {
905 netdev_err(netdev, "Failed to set link state\n");
906 return rc;
907 }
908
909 if (!wait_for_completion_timeout(&adapter->init_done,
910 timeout)) {
911 netdev_err(netdev, "timeout setting link state\n");
912 return -1;
913 }
914
915 if (adapter->init_done_rc == 1) {
916 /* Partuial success, delay and re-send */
917 mdelay(1000);
918 resend = true;
Thomas Falconab5ec332018-05-23 13:37:59 -0500919 } else if (adapter->init_done_rc) {
920 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
921 adapter->init_done_rc);
922 return adapter->init_done_rc;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400923 }
924 } while (resend);
925
926 return 0;
927}
928
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400929static int set_real_num_queues(struct net_device *netdev)
930{
931 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
932 int rc;
933
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500934 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
935 adapter->req_tx_queues, adapter->req_rx_queues);
936
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400937 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
938 if (rc) {
939 netdev_err(netdev, "failed to set the number of tx queues\n");
940 return rc;
941 }
942
943 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
944 if (rc)
945 netdev_err(netdev, "failed to set the number of rx queues\n");
946
947 return rc;
948}
949
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200950static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
951{
952 struct device *dev = &adapter->vdev->dev;
953 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200954 int len = 0;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500955 int rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200956
957 if (adapter->vpd->buff)
958 len = adapter->vpd->len;
959
John Allen69d08dc2018-01-18 16:27:58 -0600960 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200961 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
962 crq.get_vpd_size.cmd = GET_VPD_SIZE;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500963 rc = ibmvnic_send_crq(adapter, &crq);
964 if (rc)
965 return rc;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200966 wait_for_completion(&adapter->fw_done);
967
968 if (!adapter->vpd->len)
969 return -ENODATA;
970
971 if (!adapter->vpd->buff)
972 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
973 else if (adapter->vpd->len != len)
974 adapter->vpd->buff =
975 krealloc(adapter->vpd->buff,
976 adapter->vpd->len, GFP_KERNEL);
977
978 if (!adapter->vpd->buff) {
979 dev_err(dev, "Could allocate VPD buffer\n");
980 return -ENOMEM;
981 }
982
983 adapter->vpd->dma_addr =
984 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
985 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200986 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200987 dev_err(dev, "Could not map VPD buffer\n");
988 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600989 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200990 return -ENOMEM;
991 }
992
993 reinit_completion(&adapter->fw_done);
994 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
995 crq.get_vpd.cmd = GET_VPD;
996 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
997 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -0500998 rc = ibmvnic_send_crq(adapter, &crq);
999 if (rc) {
1000 kfree(adapter->vpd->buff);
1001 adapter->vpd->buff = NULL;
1002 return rc;
1003 }
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001004 wait_for_completion(&adapter->fw_done);
1005
1006 return 0;
1007}
1008
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001009static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -05001010{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001011 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001012 int rc;
John Allena57a5d22017-03-17 17:13:41 -05001013
Thomas Falcon7f3c6e62017-04-21 15:38:40 -04001014 rc = set_real_num_queues(netdev);
1015 if (rc)
1016 return rc;
John Allenbd0b6722017-03-17 17:13:40 -05001017
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001018 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1019 if (!adapter->vpd)
1020 return -ENOMEM;
1021
John Allen69d08dc2018-01-18 16:27:58 -06001022 /* Vital Product Data (VPD) */
1023 rc = ibmvnic_get_vpd(adapter);
1024 if (rc) {
1025 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1026 return rc;
1027 }
1028
Thomas Falcon032c5e82015-12-21 11:26:06 -06001029 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001030
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001031 rc = init_napi(adapter);
1032 if (rc)
1033 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001034
Thomas Falcon032c5e82015-12-21 11:26:06 -06001035 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -04001036
1037 rc = init_rx_pools(netdev);
1038 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001039 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001040
Nathan Fontenotc657e322017-03-30 02:49:06 -04001041 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001042 return rc;
1043}
1044
Nathan Fontenoted651a12017-05-03 14:04:38 -04001045static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001046{
1047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001048 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001049 int i, rc;
1050
Nathan Fontenot90c80142017-05-03 14:04:32 -04001051 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001052 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001053 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001054
Thomas Falcon032c5e82015-12-21 11:26:06 -06001055 /* We're ready to receive frames, enable the sub-crq interrupts and
1056 * set the logical link state to up
1057 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001058 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001059 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001060 if (prev_state == VNIC_CLOSED)
1061 enable_irq(adapter->rx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001062 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001063 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001064
Nathan Fontenoted651a12017-05-03 14:04:38 -04001065 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001066 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001067 if (prev_state == VNIC_CLOSED)
1068 enable_irq(adapter->tx_scrq[i]->irq);
Thomas Falconf23e0642018-04-15 18:53:36 -05001069 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001070 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001071
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001072 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001073 if (rc) {
1074 for (i = 0; i < adapter->req_rx_queues; i++)
1075 napi_disable(&adapter->napi[i]);
1076 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001077 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001078 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001079
Nathan Fontenoted651a12017-05-03 14:04:38 -04001080 netif_tx_start_all_queues(netdev);
1081
1082 if (prev_state == VNIC_CLOSED) {
1083 for (i = 0; i < adapter->req_rx_queues; i++)
1084 napi_schedule(&adapter->napi[i]);
1085 }
1086
1087 adapter->state = VNIC_OPEN;
1088 return rc;
1089}
1090
1091static int ibmvnic_open(struct net_device *netdev)
1092{
1093 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001094 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001095
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001096 /* If device failover is pending, just set device state and return.
1097 * Device operation will be handled by reset routine.
1098 */
1099 if (adapter->failover_pending) {
1100 adapter->state = VNIC_OPEN;
1101 return 0;
1102 }
1103
Nathan Fontenoted651a12017-05-03 14:04:38 -04001104 if (adapter->state != VNIC_CLOSED) {
1105 rc = ibmvnic_login(netdev);
Juliet Kima5681e22018-11-19 15:59:22 -06001106 if (rc)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001107 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001108
1109 rc = init_resources(adapter);
1110 if (rc) {
1111 netdev_err(netdev, "failed to initialize resources\n");
1112 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001113 return rc;
1114 }
1115 }
1116
1117 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001118 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001119
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001120 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001121}
1122
Thomas Falcond0869c02018-02-13 18:23:43 -06001123static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1124{
1125 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001126 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001127 u64 rx_entries;
1128 int rx_scrqs;
1129 int i, j;
1130
1131 if (!adapter->rx_pool)
1132 return;
1133
Thomas Falcon660e3092018-04-20 14:25:32 -05001134 rx_scrqs = adapter->num_active_rx_pools;
Thomas Falcond0869c02018-02-13 18:23:43 -06001135 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1136
1137 /* Free any remaining skbs in the rx buffer pools */
1138 for (i = 0; i < rx_scrqs; i++) {
1139 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001140 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001141 continue;
1142
1143 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1144 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001145 rx_buff = &rx_pool->rx_buff[j];
1146 if (rx_buff && rx_buff->skb) {
1147 dev_kfree_skb_any(rx_buff->skb);
1148 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001149 }
1150 }
1151 }
1152}
1153
Thomas Falcone9e1e972018-03-16 20:00:30 -05001154static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1155 struct ibmvnic_tx_pool *tx_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001156{
Thomas Falcon637f81d2018-02-26 18:10:57 -06001157 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001158 u64 tx_entries;
Thomas Falcone9e1e972018-03-16 20:00:30 -05001159 int i;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001160
Dan Carpenter050e85c2018-03-23 14:36:15 +03001161 if (!tx_pool || !tx_pool->tx_buff)
Thomas Falcone9e1e972018-03-16 20:00:30 -05001162 return;
1163
1164 tx_entries = tx_pool->num_buffers;
1165
1166 for (i = 0; i < tx_entries; i++) {
1167 tx_buff = &tx_pool->tx_buff[i];
1168 if (tx_buff && tx_buff->skb) {
1169 dev_kfree_skb_any(tx_buff->skb);
1170 tx_buff->skb = NULL;
1171 }
1172 }
1173}
1174
1175static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1176{
1177 int tx_scrqs;
1178 int i;
1179
1180 if (!adapter->tx_pool || !adapter->tso_pool)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001181 return;
1182
Thomas Falcon660e3092018-04-20 14:25:32 -05001183 tx_scrqs = adapter->num_active_tx_pools;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001184
1185 /* Free any remaining skbs in the tx buffer pools */
1186 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001187 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Thomas Falcone9e1e972018-03-16 20:00:30 -05001188 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1189 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001190 }
1191}
1192
John Allen6095e592018-03-30 13:44:21 -05001193static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
John Allenea5509f2017-03-17 17:13:43 -05001194{
John Allen6095e592018-03-30 13:44:21 -05001195 struct net_device *netdev = adapter->netdev;
John Allenea5509f2017-03-17 17:13:43 -05001196 int i;
1197
Nathan Fontenot46293b92017-05-03 14:05:02 -04001198 if (adapter->tx_scrq) {
1199 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001200 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001201 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001202 "Disabling tx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001203 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001204 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001205 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001206 }
1207
Nathan Fontenot46293b92017-05-03 14:05:02 -04001208 if (adapter->rx_scrq) {
1209 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001210 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001211 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001212 "Disabling rx_scrq[%d] irq\n", i);
Thomas Falconf23e0642018-04-15 18:53:36 -05001213 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001214 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001215 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001216 }
1217 }
John Allen6095e592018-03-30 13:44:21 -05001218}
1219
1220static void ibmvnic_cleanup(struct net_device *netdev)
1221{
1222 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1223
1224 /* ensure that transmissions are stopped if called by do_reset */
1225 if (adapter->resetting)
1226 netif_tx_disable(netdev);
1227 else
1228 netif_tx_stop_all_queues(netdev);
1229
1230 ibmvnic_napi_disable(adapter);
1231 ibmvnic_disable_irqs(adapter);
1232
Thomas Falcond0869c02018-02-13 18:23:43 -06001233 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001234 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001235}
1236
1237static int __ibmvnic_close(struct net_device *netdev)
1238{
1239 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1240 int rc = 0;
1241
1242 adapter->state = VNIC_CLOSING;
1243 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1244 if (rc)
1245 return rc;
Nathan Fontenot90c80142017-05-03 14:04:32 -04001246 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001247 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001248}
1249
Nathan Fontenoted651a12017-05-03 14:04:38 -04001250static int ibmvnic_close(struct net_device *netdev)
1251{
1252 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1253 int rc;
1254
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001255 /* If device failover is pending, just set device state and return.
1256 * Device operation will be handled by reset routine.
1257 */
1258 if (adapter->failover_pending) {
1259 adapter->state = VNIC_CLOSED;
1260 return 0;
1261 }
1262
Nathan Fontenoted651a12017-05-03 14:04:38 -04001263 rc = __ibmvnic_close(netdev);
Nathan Fontenot30f79622018-04-06 18:37:06 -05001264 ibmvnic_cleanup(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001265
1266 return rc;
1267}
1268
Thomas Falconad7775d2016-04-01 17:20:34 -05001269/**
1270 * build_hdr_data - creates L2/L3/L4 header data buffer
1271 * @hdr_field - bitfield determining needed headers
1272 * @skb - socket buffer
1273 * @hdr_len - array of header lengths
1274 * @tot_len - total length of data
1275 *
1276 * Reads hdr_field to determine which headers are needed by firmware.
1277 * Builds a buffer containing these headers. Saves individual header
1278 * lengths and total buffer length to be used to build descriptors.
1279 */
1280static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1281 int *hdr_len, u8 *hdr_data)
1282{
1283 int len = 0;
1284 u8 *hdr;
1285
Thomas Falconda75e3b2018-03-12 11:51:02 -05001286 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1287 hdr_len[0] = sizeof(struct vlan_ethhdr);
1288 else
1289 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001290
1291 if (skb->protocol == htons(ETH_P_IP)) {
1292 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1293 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1294 hdr_len[2] = tcp_hdrlen(skb);
1295 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1296 hdr_len[2] = sizeof(struct udphdr);
1297 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1298 hdr_len[1] = sizeof(struct ipv6hdr);
1299 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1300 hdr_len[2] = tcp_hdrlen(skb);
1301 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1302 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001303 } else if (skb->protocol == htons(ETH_P_ARP)) {
1304 hdr_len[1] = arp_hdr_len(skb->dev);
1305 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001306 }
1307
1308 memset(hdr_data, 0, 120);
1309 if ((hdr_field >> 6) & 1) {
1310 hdr = skb_mac_header(skb);
1311 memcpy(hdr_data, hdr, hdr_len[0]);
1312 len += hdr_len[0];
1313 }
1314
1315 if ((hdr_field >> 5) & 1) {
1316 hdr = skb_network_header(skb);
1317 memcpy(hdr_data + len, hdr, hdr_len[1]);
1318 len += hdr_len[1];
1319 }
1320
1321 if ((hdr_field >> 4) & 1) {
1322 hdr = skb_transport_header(skb);
1323 memcpy(hdr_data + len, hdr, hdr_len[2]);
1324 len += hdr_len[2];
1325 }
1326 return len;
1327}
1328
1329/**
1330 * create_hdr_descs - create header and header extension descriptors
1331 * @hdr_field - bitfield determining needed headers
1332 * @data - buffer containing header data
1333 * @len - length of data buffer
1334 * @hdr_len - array of individual header lengths
1335 * @scrq_arr - descriptor array
1336 *
1337 * Creates header and, if needed, header extension descriptors and
1338 * places them in a descriptor array, scrq_arr
1339 */
1340
Thomas Falcon2de09682017-10-16 10:02:11 -05001341static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1342 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001343{
1344 union sub_crq hdr_desc;
1345 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001346 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001347 u8 *data, *cur;
1348 int tmp;
1349
1350 while (tmp_len > 0) {
1351 cur = hdr_data + len - tmp_len;
1352
1353 memset(&hdr_desc, 0, sizeof(hdr_desc));
1354 if (cur != hdr_data) {
1355 data = hdr_desc.hdr_ext.data;
1356 tmp = tmp_len > 29 ? 29 : tmp_len;
1357 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1358 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1359 hdr_desc.hdr_ext.len = tmp;
1360 } else {
1361 data = hdr_desc.hdr.data;
1362 tmp = tmp_len > 24 ? 24 : tmp_len;
1363 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1364 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1365 hdr_desc.hdr.len = tmp;
1366 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1367 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1368 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1369 hdr_desc.hdr.flag = hdr_field << 1;
1370 }
1371 memcpy(data, cur, tmp);
1372 tmp_len -= tmp;
1373 *scrq_arr = hdr_desc;
1374 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001375 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001376 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001377
1378 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001379}
1380
1381/**
1382 * build_hdr_descs_arr - build a header descriptor array
1383 * @skb - socket buffer
1384 * @num_entries - number of descriptors to be sent
1385 * @subcrq - first TX descriptor
1386 * @hdr_field - bit field determining which headers will be sent
1387 *
1388 * This function will build a TX descriptor array with applicable
1389 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1390 */
1391
1392static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1393 int *num_entries, u8 hdr_field)
1394{
1395 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001396 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001397 u8 *hdr_data = txbuff->hdr_data;
1398
1399 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1400 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001401 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001402 txbuff->indir_arr + 1);
1403}
1404
Thomas Falcon1f247a62018-03-12 11:51:04 -05001405static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1406 struct net_device *netdev)
1407{
1408 /* For some backing devices, mishandling of small packets
1409 * can result in a loss of connection or TX stall. Device
1410 * architects recommend that no packet should be smaller
1411 * than the minimum MTU value provided to the driver, so
1412 * pad any packets to that length
1413 */
1414 if (skb->len < netdev->min_mtu)
1415 return skb_put_padto(skb, netdev->min_mtu);
Thomas Falcon7083a452018-03-12 21:05:26 -05001416
1417 return 0;
Thomas Falcon1f247a62018-03-12 11:51:04 -05001418}
1419
YueHaibing94b2bb22018-09-18 14:35:47 +08001420static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001421{
1422 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1423 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001424 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001425 struct device *dev = &adapter->vdev->dev;
1426 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001427 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001428 struct ibmvnic_tx_pool *tx_pool;
1429 unsigned int tx_send_failed = 0;
1430 unsigned int tx_map_failed = 0;
1431 unsigned int tx_dropped = 0;
1432 unsigned int tx_packets = 0;
1433 unsigned int tx_bytes = 0;
1434 dma_addr_t data_dma_addr;
1435 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001436 unsigned long lpar_rc;
1437 union sub_crq tx_crq;
1438 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001439 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001440 unsigned char *dst;
1441 u64 *handle_array;
1442 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001443 u8 proto = 0;
YueHaibing94b2bb22018-09-18 14:35:47 +08001444 netdev_tx_t ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001445
Nathan Fontenoted651a12017-05-03 14:04:38 -04001446 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001447 if (!netif_subqueue_stopped(netdev, skb))
1448 netif_stop_subqueue(netdev, queue_num);
1449 dev_kfree_skb_any(skb);
1450
Thomas Falcon032c5e82015-12-21 11:26:06 -06001451 tx_send_failed++;
1452 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001453 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001454 goto out;
1455 }
1456
Thomas Falcon7083a452018-03-12 21:05:26 -05001457 if (ibmvnic_xmit_workarounds(skb, netdev)) {
Thomas Falcon1f247a62018-03-12 11:51:04 -05001458 tx_dropped++;
1459 tx_send_failed++;
1460 ret = NETDEV_TX_OK;
1461 goto out;
1462 }
Thomas Falcon06b3e352018-03-16 20:00:28 -05001463 if (skb_is_gso(skb))
1464 tx_pool = &adapter->tso_pool[queue_num];
1465 else
1466 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon1f247a62018-03-12 11:51:04 -05001467
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001468 tx_scrq = adapter->tx_scrq[queue_num];
1469 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1470 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1471 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1472
Thomas Falcon032c5e82015-12-21 11:26:06 -06001473 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001474
Thomas Falcon86b61a52018-03-16 20:00:29 -05001475 if (index == IBMVNIC_INVALID_MAP) {
1476 dev_kfree_skb_any(skb);
1477 tx_send_failed++;
1478 tx_dropped++;
1479 ret = NETDEV_TX_OK;
1480 goto out;
1481 }
1482
1483 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1484
Thomas Falcon06b3e352018-03-16 20:00:28 -05001485 offset = index * tx_pool->buf_size;
1486 dst = tx_pool->long_term_buff.buff + offset;
1487 memset(dst, 0, tx_pool->buf_size);
1488 data_dma_addr = tx_pool->long_term_buff.addr + offset;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001489
Thomas Falcon15482052017-10-17 12:36:54 -05001490 if (skb_shinfo(skb)->nr_frags) {
1491 int cur, i;
1492
1493 /* Copy the head */
1494 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1495 cur = skb_headlen(skb);
1496
1497 /* Copy the frags */
1498 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1499 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1500
1501 memcpy(dst + cur,
1502 page_address(skb_frag_page(frag)) +
1503 frag->page_offset, skb_frag_size(frag));
1504 cur += skb_frag_size(frag);
1505 }
1506 } else {
1507 skb_copy_from_linear_data(skb, dst, skb->len);
1508 }
1509
Thomas Falcon032c5e82015-12-21 11:26:06 -06001510 tx_pool->consumer_index =
Thomas Falcon06b3e352018-03-16 20:00:28 -05001511 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001512
1513 tx_buff = &tx_pool->tx_buff[index];
1514 tx_buff->skb = skb;
1515 tx_buff->data_dma[0] = data_dma_addr;
1516 tx_buff->data_len[0] = skb->len;
1517 tx_buff->index = index;
1518 tx_buff->pool_index = queue_num;
1519 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001520
1521 memset(&tx_crq, 0, sizeof(tx_crq));
1522 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1523 tx_crq.v1.type = IBMVNIC_TX_DESC;
1524 tx_crq.v1.n_crq_elem = 1;
1525 tx_crq.v1.n_sge = 1;
1526 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
Thomas Falcon06b3e352018-03-16 20:00:28 -05001527
Thomas Falconfdb06102017-10-17 12:36:55 -05001528 if (skb_is_gso(skb))
Thomas Falcon06b3e352018-03-16 20:00:28 -05001529 tx_crq.v1.correlator =
1530 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
Thomas Falconfdb06102017-10-17 12:36:55 -05001531 else
Thomas Falcon06b3e352018-03-16 20:00:28 -05001532 tx_crq.v1.correlator = cpu_to_be32(index);
1533 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001534 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1535 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1536
Michał Mirosławe84b4792018-11-07 17:50:52 +01001537 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001538 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1539 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1540 }
1541
1542 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001543 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1544 proto = ip_hdr(skb)->protocol;
1545 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1546 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1547 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001548 }
1549
Thomas Falcona0dca102018-01-18 19:29:48 -06001550 if (proto == IPPROTO_TCP)
1551 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1552 else if (proto == IPPROTO_UDP)
1553 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1554
Thomas Falconad7775d2016-04-01 17:20:34 -05001555 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001556 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001557 hdrs += 2;
1558 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001559 if (skb_is_gso(skb)) {
1560 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1561 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1562 hdrs += 2;
1563 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001564 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001565 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001566 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1567 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001568 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001569 tx_buff->indir_arr[0] = tx_crq;
1570 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1571 sizeof(tx_buff->indir_arr),
1572 DMA_TO_DEVICE);
1573 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001574 dev_kfree_skb_any(skb);
1575 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001576 if (!firmware_has_feature(FW_FEATURE_CMO))
1577 dev_err(dev, "tx: unable to map descriptor array\n");
1578 tx_map_failed++;
1579 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001580 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001581 goto tx_err_out;
Thomas Falconad7775d2016-04-01 17:20:34 -05001582 }
John Allen498cd8e2016-04-06 11:49:55 -05001583 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001584 (u64)tx_buff->indir_dma,
1585 (u64)num_entries);
1586 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001587 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001588 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1589 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001590 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001591 if (lpar_rc != H_SUCCESS) {
Thomas Falcon2d14d372018-07-13 12:03:32 -05001592 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1593 dev_err_ratelimited(dev, "tx: send failed\n");
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001594 dev_kfree_skb_any(skb);
1595 tx_buff->skb = NULL;
1596
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05001597 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1598 /* Disable TX and report carrier off if queue is closed
1599 * or pending failover.
Thomas Falconb8c80b82017-05-26 10:30:42 -04001600 * Firmware guarantees that a signal will be sent to the
1601 * driver, triggering a reset or some other action.
1602 */
1603 netif_tx_stop_all_queues(netdev);
1604 netif_carrier_off(netdev);
1605 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001606
Thomas Falcon032c5e82015-12-21 11:26:06 -06001607 tx_send_failed++;
1608 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001609 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001610 goto tx_err_out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001611 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001612
Thomas Falconffc385b2018-02-18 10:08:41 -06001613 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001614 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001615 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001616 netif_stop_subqueue(netdev, queue_num);
1617 }
1618
Thomas Falcon032c5e82015-12-21 11:26:06 -06001619 tx_packets++;
1620 tx_bytes += skb->len;
1621 txq->trans_start = jiffies;
1622 ret = NETDEV_TX_OK;
Thomas Falcon86b61a52018-03-16 20:00:29 -05001623 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001624
Thomas Falcon86b61a52018-03-16 20:00:29 -05001625tx_err_out:
1626 /* roll back consumer index and map array*/
1627 if (tx_pool->consumer_index == 0)
1628 tx_pool->consumer_index =
1629 tx_pool->num_buffers - 1;
1630 else
1631 tx_pool->consumer_index--;
1632 tx_pool->free_map[tx_pool->consumer_index] = index;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001633out:
1634 netdev->stats.tx_dropped += tx_dropped;
1635 netdev->stats.tx_bytes += tx_bytes;
1636 netdev->stats.tx_packets += tx_packets;
1637 adapter->tx_send_failed += tx_send_failed;
1638 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001639 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1640 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1641 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001642
1643 return ret;
1644}
1645
1646static void ibmvnic_set_multi(struct net_device *netdev)
1647{
1648 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1649 struct netdev_hw_addr *ha;
1650 union ibmvnic_crq crq;
1651
1652 memset(&crq, 0, sizeof(crq));
1653 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1654 crq.request_capability.cmd = REQUEST_CAPABILITY;
1655
1656 if (netdev->flags & IFF_PROMISC) {
1657 if (!adapter->promisc_supported)
1658 return;
1659 } else {
1660 if (netdev->flags & IFF_ALLMULTI) {
1661 /* Accept all multicast */
1662 memset(&crq, 0, sizeof(crq));
1663 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1664 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1665 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1666 ibmvnic_send_crq(adapter, &crq);
1667 } else if (netdev_mc_empty(netdev)) {
1668 /* Reject all multicast */
1669 memset(&crq, 0, sizeof(crq));
1670 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1671 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1672 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1673 ibmvnic_send_crq(adapter, &crq);
1674 } else {
1675 /* Accept one or more multicast(s) */
1676 netdev_for_each_mc_addr(ha, netdev) {
1677 memset(&crq, 0, sizeof(crq));
1678 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1679 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1680 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1681 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1682 ha->addr);
1683 ibmvnic_send_crq(adapter, &crq);
1684 }
1685 }
1686 }
1687}
1688
John Allenc26eba02017-10-26 16:23:25 -05001689static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001690{
1691 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1692 struct sockaddr *addr = p;
1693 union ibmvnic_crq crq;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001694 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001695
1696 if (!is_valid_ether_addr(addr->sa_data))
1697 return -EADDRNOTAVAIL;
1698
1699 memset(&crq, 0, sizeof(crq));
1700 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1701 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1702 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001703
1704 init_completion(&adapter->fw_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05001705 rc = ibmvnic_send_crq(adapter, &crq);
1706 if (rc)
1707 return rc;
Thomas Falconf8136142018-01-29 13:45:05 -06001708 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001709 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001710 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711}
1712
John Allenc26eba02017-10-26 16:23:25 -05001713static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1714{
1715 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1716 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001717 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001718
Thomas Falcon3d166132018-01-10 19:39:52 -06001719 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001720 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1721 adapter->mac_change_pending = true;
1722 return 0;
1723 }
1724
Thomas Falconf8136142018-01-29 13:45:05 -06001725 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001726
Thomas Falconf8136142018-01-29 13:45:05 -06001727 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001728}
1729
Nathan Fontenoted651a12017-05-03 14:04:38 -04001730/**
1731 * do_reset returns zero if we are able to keep processing reset events, or
1732 * non-zero if we hit a fatal error and must halt.
1733 */
1734static int do_reset(struct ibmvnic_adapter *adapter,
1735 struct ibmvnic_rwi *rwi, u32 reset_state)
1736{
John Allen896d8692018-01-18 16:26:31 -06001737 u64 old_num_rx_queues, old_num_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001738 u64 old_num_rx_slots, old_num_tx_slots;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001739 struct net_device *netdev = adapter->netdev;
1740 int i, rc;
1741
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001742 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1743 rwi->reset_reason);
1744
Nathan Fontenoted651a12017-05-03 14:04:38 -04001745 netif_carrier_off(netdev);
1746 adapter->reset_reason = rwi->reset_reason;
1747
John Allen896d8692018-01-18 16:26:31 -06001748 old_num_rx_queues = adapter->req_rx_queues;
1749 old_num_tx_queues = adapter->req_tx_queues;
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001750 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1751 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
John Allen896d8692018-01-18 16:26:31 -06001752
Nathan Fontenot30f79622018-04-06 18:37:06 -05001753 ibmvnic_cleanup(netdev);
1754
1755 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1756 adapter->reset_reason != VNIC_RESET_FAILOVER) {
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001757 rc = __ibmvnic_close(netdev);
1758 if (rc)
1759 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001760 }
1761
John Allenc26eba02017-10-26 16:23:25 -05001762 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1763 adapter->wait_for_reset) {
1764 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001765 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001766 release_crq_queue(adapter);
1767 }
1768
John Allen8cb31cf2017-05-26 10:30:37 -04001769 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1770 /* remove the closed state so when we call open it appears
1771 * we are coming from the probed state.
1772 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001773 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001774
Nathan Fontenot30f79622018-04-06 18:37:06 -05001775 if (adapter->wait_for_reset) {
1776 rc = init_crq_queue(adapter);
1777 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1778 rc = ibmvnic_reenable_crq_queue(adapter);
1779 release_sub_crqs(adapter, 1);
1780 } else {
1781 rc = ibmvnic_reset_crq(adapter);
1782 if (!rc)
1783 rc = vio_enable_interrupts(adapter->vdev);
1784 }
1785
1786 if (rc) {
1787 netdev_err(adapter->netdev,
1788 "Couldn't initialize crq. rc=%d\n", rc);
1789 return rc;
1790 }
1791
Thomas Falcon8a348452018-05-23 13:38:00 -05001792 rc = ibmvnic_reset_init(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001793 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001794 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001795
1796 /* If the adapter was in PROBE state prior to the reset,
1797 * exit here.
1798 */
1799 if (reset_state == VNIC_PROBED)
1800 return 0;
1801
1802 rc = ibmvnic_login(netdev);
1803 if (rc) {
John Allen3578a7e2018-07-16 10:29:30 -05001804 adapter->state = reset_state;
1805 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001806 }
1807
John Allenc26eba02017-10-26 16:23:25 -05001808 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1809 adapter->wait_for_reset) {
1810 rc = init_resources(adapter);
1811 if (rc)
1812 return rc;
John Allen896d8692018-01-18 16:26:31 -06001813 } else if (adapter->req_rx_queues != old_num_rx_queues ||
Thomas Falcon5bf032e2018-11-21 11:17:59 -06001814 adapter->req_tx_queues != old_num_tx_queues ||
1815 adapter->req_rx_add_entries_per_subcrq !=
1816 old_num_rx_slots ||
1817 adapter->req_tx_entries_per_subcrq !=
1818 old_num_tx_slots) {
John Allen896d8692018-01-18 16:26:31 -06001819 release_rx_pools(adapter);
1820 release_tx_pools(adapter);
Juliet Kima5681e22018-11-19 15:59:22 -06001821 release_napi(adapter);
1822 release_vpd_data(adapter);
1823
1824 rc = init_resources(adapter);
Thomas Falconf611a5b2018-08-30 13:19:53 -05001825 if (rc)
1826 return rc;
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001827
John Allenc26eba02017-10-26 16:23:25 -05001828 } else {
1829 rc = reset_tx_pools(adapter);
1830 if (rc)
1831 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001832
John Allenc26eba02017-10-26 16:23:25 -05001833 rc = reset_rx_pools(adapter);
1834 if (rc)
1835 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001836 }
Thomas Falcon134bbe72018-05-16 15:49:04 -05001837 ibmvnic_disable_irqs(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001838 }
John Allene676d812018-03-14 10:41:29 -05001839 adapter->state = VNIC_CLOSED;
1840
1841 if (reset_state == VNIC_CLOSED)
1842 return 0;
1843
Nathan Fontenoted651a12017-05-03 14:04:38 -04001844 rc = __ibmvnic_open(netdev);
1845 if (rc) {
1846 if (list_empty(&adapter->rwi_list))
1847 adapter->state = VNIC_CLOSED;
1848 else
1849 adapter->state = reset_state;
1850
1851 return 0;
1852 }
1853
Nathan Fontenoted651a12017-05-03 14:04:38 -04001854 /* kick napi */
1855 for (i = 0; i < adapter->req_rx_queues; i++)
1856 napi_schedule(&adapter->napi[i]);
1857
Nathan Fontenotebc701b72018-04-11 10:09:38 -05001858 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1859 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
Thomas Falcon986103e2018-11-30 10:59:08 -06001860 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001861
Thomas Falconcc85c022018-02-13 15:32:50 -06001862 netif_carrier_on(netdev);
1863
Nathan Fontenoted651a12017-05-03 14:04:38 -04001864 return 0;
1865}
1866
Thomas Falcon2770a792018-05-23 13:38:02 -05001867static int do_hard_reset(struct ibmvnic_adapter *adapter,
1868 struct ibmvnic_rwi *rwi, u32 reset_state)
1869{
1870 struct net_device *netdev = adapter->netdev;
1871 int rc;
1872
1873 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
1874 rwi->reset_reason);
1875
1876 netif_carrier_off(netdev);
1877 adapter->reset_reason = rwi->reset_reason;
1878
1879 ibmvnic_cleanup(netdev);
1880 release_resources(adapter);
1881 release_sub_crqs(adapter, 0);
1882 release_crq_queue(adapter);
1883
1884 /* remove the closed state so when we call open it appears
1885 * we are coming from the probed state.
1886 */
1887 adapter->state = VNIC_PROBED;
1888
1889 rc = init_crq_queue(adapter);
1890 if (rc) {
1891 netdev_err(adapter->netdev,
1892 "Couldn't initialize crq. rc=%d\n", rc);
1893 return rc;
1894 }
1895
1896 rc = ibmvnic_init(adapter);
1897 if (rc)
1898 return rc;
1899
1900 /* If the adapter was in PROBE state prior to the reset,
1901 * exit here.
1902 */
1903 if (reset_state == VNIC_PROBED)
1904 return 0;
1905
1906 rc = ibmvnic_login(netdev);
1907 if (rc) {
1908 adapter->state = VNIC_PROBED;
1909 return 0;
1910 }
Juliet Kima5681e22018-11-19 15:59:22 -06001911
1912 rc = init_resources(adapter);
Thomas Falcon2770a792018-05-23 13:38:02 -05001913 if (rc)
1914 return rc;
1915
1916 ibmvnic_disable_irqs(adapter);
1917 adapter->state = VNIC_CLOSED;
1918
1919 if (reset_state == VNIC_CLOSED)
1920 return 0;
1921
1922 rc = __ibmvnic_open(netdev);
1923 if (rc) {
1924 if (list_empty(&adapter->rwi_list))
1925 adapter->state = VNIC_CLOSED;
1926 else
1927 adapter->state = reset_state;
1928
1929 return 0;
1930 }
1931
1932 netif_carrier_on(netdev);
1933
1934 return 0;
1935}
1936
Nathan Fontenoted651a12017-05-03 14:04:38 -04001937static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1938{
1939 struct ibmvnic_rwi *rwi;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001940 unsigned long flags;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001941
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001942 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001943
1944 if (!list_empty(&adapter->rwi_list)) {
1945 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1946 list);
1947 list_del(&rwi->list);
1948 } else {
1949 rwi = NULL;
1950 }
1951
Thomas Falcon6c5c7482018-12-10 15:22:22 -06001952 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001953 return rwi;
1954}
1955
1956static void free_all_rwi(struct ibmvnic_adapter *adapter)
1957{
1958 struct ibmvnic_rwi *rwi;
1959
1960 rwi = get_next_rwi(adapter);
1961 while (rwi) {
1962 kfree(rwi);
1963 rwi = get_next_rwi(adapter);
1964 }
1965}
1966
1967static void __ibmvnic_reset(struct work_struct *work)
1968{
1969 struct ibmvnic_rwi *rwi;
1970 struct ibmvnic_adapter *adapter;
1971 struct net_device *netdev;
Juliet Kima5681e22018-11-19 15:59:22 -06001972 bool we_lock_rtnl = false;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001973 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001974 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001975
1976 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1977 netdev = adapter->netdev;
1978
Juliet Kima5681e22018-11-19 15:59:22 -06001979 /* netif_set_real_num_xx_queues needs to take rtnl lock here
1980 * unless wait_for_reset is set, in which case the rtnl lock
1981 * has already been taken before initializing the reset
1982 */
1983 if (!adapter->wait_for_reset) {
1984 rtnl_lock();
1985 we_lock_rtnl = true;
1986 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001987 reset_state = adapter->state;
1988
1989 rwi = get_next_rwi(adapter);
1990 while (rwi) {
Thomas Falcon2770a792018-05-23 13:38:02 -05001991 if (adapter->force_reset_recovery) {
1992 adapter->force_reset_recovery = false;
1993 rc = do_hard_reset(adapter, rwi, reset_state);
1994 } else {
1995 rc = do_reset(adapter, rwi, reset_state);
1996 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001997 kfree(rwi);
Thomas Falcon2770a792018-05-23 13:38:02 -05001998 if (rc && rc != IBMVNIC_INIT_FAILED &&
1999 !adapter->force_reset_recovery)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002000 break;
2001
2002 rwi = get_next_rwi(adapter);
2003 }
2004
John Allenc26eba02017-10-26 16:23:25 -05002005 if (adapter->wait_for_reset) {
2006 adapter->wait_for_reset = false;
2007 adapter->reset_done_rc = rc;
2008 complete(&adapter->reset_done);
2009 }
2010
Nathan Fontenoted651a12017-05-03 14:04:38 -04002011 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002012 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04002013 free_all_rwi(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002014 }
2015
2016 adapter->resetting = false;
Juliet Kima5681e22018-11-19 15:59:22 -06002017 if (we_lock_rtnl)
2018 rtnl_unlock();
Nathan Fontenoted651a12017-05-03 14:04:38 -04002019}
2020
Thomas Falconaf894d22018-04-06 18:37:04 -05002021static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2022 enum ibmvnic_reset_reason reason)
Nathan Fontenoted651a12017-05-03 14:04:38 -04002023{
Thomas Falcon2770a792018-05-23 13:38:02 -05002024 struct list_head *entry, *tmp_entry;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002025 struct ibmvnic_rwi *rwi, *tmp;
2026 struct net_device *netdev = adapter->netdev;
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002027 unsigned long flags;
Thomas Falconaf894d22018-04-06 18:37:04 -05002028 int ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002029
2030 if (adapter->state == VNIC_REMOVING ||
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002031 adapter->state == VNIC_REMOVED ||
2032 adapter->failover_pending) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002033 ret = EBUSY;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05002034 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002035 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002036 }
2037
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002038 if (adapter->state == VNIC_PROBING) {
2039 netdev_warn(netdev, "Adapter reset during probe\n");
Thomas Falconaf894d22018-04-06 18:37:04 -05002040 ret = adapter->init_done_rc = EAGAIN;
2041 goto err;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04002042 }
2043
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002044 spin_lock_irqsave(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002045
2046 list_for_each(entry, &adapter->rwi_list) {
2047 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2048 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002049 netdev_dbg(netdev, "Skipping matching reset\n");
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002050 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falconaf894d22018-04-06 18:37:04 -05002051 ret = EBUSY;
2052 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002053 }
2054 }
2055
Thomas Falcon1d1bbc32018-12-10 15:22:23 -06002056 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002057 if (!rwi) {
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002058 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002059 ibmvnic_close(netdev);
Thomas Falconaf894d22018-04-06 18:37:04 -05002060 ret = ENOMEM;
2061 goto err;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002062 }
Thomas Falcon2770a792018-05-23 13:38:02 -05002063 /* if we just received a transport event,
2064 * flush reset queue and process this reset
2065 */
2066 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2067 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2068 list_del(entry);
2069 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04002070 rwi->reset_reason = reason;
2071 list_add_tail(&rwi->list, &adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06002072 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
Thomas Falcon06e43d72018-05-23 13:38:01 -05002073 adapter->resetting = true;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002074 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002075 schedule_work(&adapter->ibmvnic_reset);
Thomas Falconaf894d22018-04-06 18:37:04 -05002076
2077 return 0;
2078err:
2079 if (adapter->wait_for_reset)
2080 adapter->wait_for_reset = false;
2081 return -ret;
Nathan Fontenoted651a12017-05-03 14:04:38 -04002082}
2083
Thomas Falcon032c5e82015-12-21 11:26:06 -06002084static void ibmvnic_tx_timeout(struct net_device *dev)
2085{
2086 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002087
Nathan Fontenoted651a12017-05-03 14:04:38 -04002088 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002089}
2090
2091static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2092 struct ibmvnic_rx_buff *rx_buff)
2093{
2094 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2095
2096 rx_buff->skb = NULL;
2097
2098 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2099 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2100
2101 atomic_dec(&pool->available);
2102}
2103
2104static int ibmvnic_poll(struct napi_struct *napi, int budget)
2105{
2106 struct net_device *netdev = napi->dev;
2107 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2108 int scrq_num = (int)(napi - adapter->napi);
2109 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04002110
Thomas Falcon032c5e82015-12-21 11:26:06 -06002111restart_poll:
2112 while (frames_processed < budget) {
2113 struct sk_buff *skb;
2114 struct ibmvnic_rx_buff *rx_buff;
2115 union sub_crq *next;
2116 u32 length;
2117 u16 offset;
2118 u8 flags = 0;
2119
John Allen34686562018-02-06 16:21:49 -06002120 if (unlikely(adapter->resetting &&
2121 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05002122 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2123 napi_complete_done(napi, frames_processed);
2124 return frames_processed;
2125 }
2126
Thomas Falcon032c5e82015-12-21 11:26:06 -06002127 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2128 break;
2129 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2130 rx_buff =
2131 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2132 rx_comp.correlator);
2133 /* do error checking */
2134 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05002135 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2136 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002137 /* free the entry */
2138 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06002139 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002140 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04002141 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06002142 } else if (!rx_buff->skb) {
2143 /* free the entry */
2144 next->rx_comp.first = 0;
2145 remove_buff_from_pool(adapter, rx_buff);
2146 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002147 }
2148
2149 length = be32_to_cpu(next->rx_comp.len);
2150 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2151 flags = next->rx_comp.flags;
2152 skb = rx_buff->skb;
2153 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2154 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002155
2156 /* VLAN Header has been stripped by the system firmware and
2157 * needs to be inserted by the driver
2158 */
2159 if (adapter->rx_vlan_header_insertion &&
2160 (flags & IBMVNIC_VLAN_STRIPPED))
2161 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2162 ntohs(next->rx_comp.vlan_tci));
2163
Thomas Falcon032c5e82015-12-21 11:26:06 -06002164 /* free the entry */
2165 next->rx_comp.first = 0;
2166 remove_buff_from_pool(adapter, rx_buff);
2167
2168 skb_put(skb, length);
2169 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04002170 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002171
2172 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2173 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2174 skb->ip_summed = CHECKSUM_UNNECESSARY;
2175 }
2176
2177 length = skb->len;
2178 napi_gro_receive(napi, skb); /* send it up */
2179 netdev->stats.rx_packets++;
2180 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05002181 adapter->rx_stats_buffers[scrq_num].packets++;
2182 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002183 frames_processed++;
2184 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04002185
2186 if (adapter->state != VNIC_CLOSING)
2187 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188
2189 if (frames_processed < budget) {
2190 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08002191 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002192 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2193 napi_reschedule(napi)) {
2194 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2195 goto restart_poll;
2196 }
2197 }
2198 return frames_processed;
2199}
2200
John Allenc26eba02017-10-26 16:23:25 -05002201static int wait_for_reset(struct ibmvnic_adapter *adapter)
2202{
Thomas Falconaf894d22018-04-06 18:37:04 -05002203 int rc, ret;
2204
John Allenc26eba02017-10-26 16:23:25 -05002205 adapter->fallback.mtu = adapter->req_mtu;
2206 adapter->fallback.rx_queues = adapter->req_rx_queues;
2207 adapter->fallback.tx_queues = adapter->req_tx_queues;
2208 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2209 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2210
2211 init_completion(&adapter->reset_done);
John Allenc26eba02017-10-26 16:23:25 -05002212 adapter->wait_for_reset = true;
Thomas Falconaf894d22018-04-06 18:37:04 -05002213 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2214 if (rc)
2215 return rc;
John Allenc26eba02017-10-26 16:23:25 -05002216 wait_for_completion(&adapter->reset_done);
2217
Thomas Falconaf894d22018-04-06 18:37:04 -05002218 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002219 if (adapter->reset_done_rc) {
Thomas Falconaf894d22018-04-06 18:37:04 -05002220 ret = -EIO;
John Allenc26eba02017-10-26 16:23:25 -05002221 adapter->desired.mtu = adapter->fallback.mtu;
2222 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2223 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2224 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2225 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2226
2227 init_completion(&adapter->reset_done);
Thomas Falconaf894d22018-04-06 18:37:04 -05002228 adapter->wait_for_reset = true;
2229 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2230 if (rc)
2231 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002232 wait_for_completion(&adapter->reset_done);
2233 }
2234 adapter->wait_for_reset = false;
2235
Thomas Falconaf894d22018-04-06 18:37:04 -05002236 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002237}
2238
John Allen3a807b72017-06-06 16:55:52 -05002239static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2240{
John Allenc26eba02017-10-26 16:23:25 -05002241 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2242
2243 adapter->desired.mtu = new_mtu + ETH_HLEN;
2244
2245 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002246}
2247
Thomas Falconf10b09e2018-03-12 11:51:05 -05002248static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2249 struct net_device *dev,
2250 netdev_features_t features)
2251{
2252 /* Some backing hardware adapters can not
2253 * handle packets with a MSS less than 224
2254 * or with only one segment.
2255 */
2256 if (skb_is_gso(skb)) {
2257 if (skb_shinfo(skb)->gso_size < 224 ||
2258 skb_shinfo(skb)->gso_segs == 1)
2259 features &= ~NETIF_F_GSO_MASK;
2260 }
2261
2262 return features;
2263}
2264
Thomas Falcon032c5e82015-12-21 11:26:06 -06002265static const struct net_device_ops ibmvnic_netdev_ops = {
2266 .ndo_open = ibmvnic_open,
2267 .ndo_stop = ibmvnic_close,
2268 .ndo_start_xmit = ibmvnic_xmit,
2269 .ndo_set_rx_mode = ibmvnic_set_multi,
2270 .ndo_set_mac_address = ibmvnic_set_mac,
2271 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002272 .ndo_tx_timeout = ibmvnic_tx_timeout,
John Allen3a807b72017-06-06 16:55:52 -05002273 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002274 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002275};
2276
2277/* ethtool functions */
2278
Philippe Reynes8a433792017-01-07 22:37:29 +01002279static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2280 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002281{
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002282 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2283 int rc;
Philippe Reynes8a433792017-01-07 22:37:29 +01002284
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03002285 rc = send_query_phys_parms(adapter);
2286 if (rc) {
2287 adapter->speed = SPEED_UNKNOWN;
2288 adapter->duplex = DUPLEX_UNKNOWN;
2289 }
2290 cmd->base.speed = adapter->speed;
2291 cmd->base.duplex = adapter->duplex;
Philippe Reynes8a433792017-01-07 22:37:29 +01002292 cmd->base.port = PORT_FIBRE;
2293 cmd->base.phy_address = 0;
2294 cmd->base.autoneg = AUTONEG_ENABLE;
2295
Thomas Falcon032c5e82015-12-21 11:26:06 -06002296 return 0;
2297}
2298
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002299static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002300 struct ethtool_drvinfo *info)
2301{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002302 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2303
Thomas Falcon032c5e82015-12-21 11:26:06 -06002304 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2305 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002306 strlcpy(info->fw_version, adapter->fw_version,
2307 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002308}
2309
2310static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2311{
2312 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2313
2314 return adapter->msg_enable;
2315}
2316
2317static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2318{
2319 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2320
2321 adapter->msg_enable = data;
2322}
2323
2324static u32 ibmvnic_get_link(struct net_device *netdev)
2325{
2326 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2327
2328 /* Don't need to send a query because we request a logical link up at
2329 * init and then we wait for link state indications
2330 */
2331 return adapter->logical_link_state;
2332}
2333
2334static void ibmvnic_get_ringparam(struct net_device *netdev,
2335 struct ethtool_ringparam *ring)
2336{
John Allenbc131b32017-08-02 16:46:30 -05002337 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2338
Thomas Falcon723ad912018-09-28 18:38:26 -05002339 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2340 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2341 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2342 } else {
2343 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2344 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2345 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002346 ring->rx_mini_max_pending = 0;
2347 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002348 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2349 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002350 ring->rx_mini_pending = 0;
2351 ring->rx_jumbo_pending = 0;
2352}
2353
John Allenc26eba02017-10-26 16:23:25 -05002354static int ibmvnic_set_ringparam(struct net_device *netdev,
2355 struct ethtool_ringparam *ring)
2356{
2357 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002358 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002359
Thomas Falcon723ad912018-09-28 18:38:26 -05002360 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002361 adapter->desired.rx_entries = ring->rx_pending;
2362 adapter->desired.tx_entries = ring->tx_pending;
2363
Thomas Falcon723ad912018-09-28 18:38:26 -05002364 ret = wait_for_reset(adapter);
2365
2366 if (!ret &&
2367 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2368 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2369 netdev_info(netdev,
2370 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2371 ring->rx_pending, ring->tx_pending,
2372 adapter->req_rx_add_entries_per_subcrq,
2373 adapter->req_tx_entries_per_subcrq);
2374 return ret;
John Allenc26eba02017-10-26 16:23:25 -05002375}
2376
John Allenc2dbeb62017-08-02 16:47:17 -05002377static void ibmvnic_get_channels(struct net_device *netdev,
2378 struct ethtool_channels *channels)
2379{
2380 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2381
Thomas Falcon723ad912018-09-28 18:38:26 -05002382 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2383 channels->max_rx = adapter->max_rx_queues;
2384 channels->max_tx = adapter->max_tx_queues;
2385 } else {
2386 channels->max_rx = IBMVNIC_MAX_QUEUES;
2387 channels->max_tx = IBMVNIC_MAX_QUEUES;
2388 }
2389
John Allenc2dbeb62017-08-02 16:47:17 -05002390 channels->max_other = 0;
2391 channels->max_combined = 0;
2392 channels->rx_count = adapter->req_rx_queues;
2393 channels->tx_count = adapter->req_tx_queues;
2394 channels->other_count = 0;
2395 channels->combined_count = 0;
2396}
2397
John Allenc26eba02017-10-26 16:23:25 -05002398static int ibmvnic_set_channels(struct net_device *netdev,
2399 struct ethtool_channels *channels)
2400{
2401 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon723ad912018-09-28 18:38:26 -05002402 int ret;
John Allenc26eba02017-10-26 16:23:25 -05002403
Thomas Falcon723ad912018-09-28 18:38:26 -05002404 ret = 0;
John Allenc26eba02017-10-26 16:23:25 -05002405 adapter->desired.rx_queues = channels->rx_count;
2406 adapter->desired.tx_queues = channels->tx_count;
2407
Thomas Falcon723ad912018-09-28 18:38:26 -05002408 ret = wait_for_reset(adapter);
2409
2410 if (!ret &&
2411 (adapter->req_rx_queues != channels->rx_count ||
2412 adapter->req_tx_queues != channels->tx_count))
2413 netdev_info(netdev,
2414 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2415 channels->rx_count, channels->tx_count,
2416 adapter->req_rx_queues, adapter->req_tx_queues);
2417 return ret;
2418
John Allenc26eba02017-10-26 16:23:25 -05002419}
2420
Thomas Falcon032c5e82015-12-21 11:26:06 -06002421static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2422{
John Allen3d52b592017-08-02 16:44:14 -05002423 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002424 int i;
2425
Thomas Falcon723ad912018-09-28 18:38:26 -05002426 switch (stringset) {
2427 case ETH_SS_STATS:
2428 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2429 i++, data += ETH_GSTRING_LEN)
2430 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2431
2432 for (i = 0; i < adapter->req_tx_queues; i++) {
2433 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2434 data += ETH_GSTRING_LEN;
2435
2436 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2437 data += ETH_GSTRING_LEN;
2438
2439 snprintf(data, ETH_GSTRING_LEN,
2440 "tx%d_dropped_packets", i);
2441 data += ETH_GSTRING_LEN;
2442 }
2443
2444 for (i = 0; i < adapter->req_rx_queues; i++) {
2445 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2446 data += ETH_GSTRING_LEN;
2447
2448 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2449 data += ETH_GSTRING_LEN;
2450
2451 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2452 data += ETH_GSTRING_LEN;
2453 }
2454 break;
2455
2456 case ETH_SS_PRIV_FLAGS:
2457 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2458 strcpy(data + i * ETH_GSTRING_LEN,
2459 ibmvnic_priv_flags[i]);
2460 break;
2461 default:
Thomas Falcon032c5e82015-12-21 11:26:06 -06002462 return;
John Allen3d52b592017-08-02 16:44:14 -05002463 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464}
2465
2466static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2467{
John Allen3d52b592017-08-02 16:44:14 -05002468 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2469
Thomas Falcon032c5e82015-12-21 11:26:06 -06002470 switch (sset) {
2471 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002472 return ARRAY_SIZE(ibmvnic_stats) +
2473 adapter->req_tx_queues * NUM_TX_STATS +
2474 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon723ad912018-09-28 18:38:26 -05002475 case ETH_SS_PRIV_FLAGS:
2476 return ARRAY_SIZE(ibmvnic_priv_flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002477 default:
2478 return -EOPNOTSUPP;
2479 }
2480}
2481
2482static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2483 struct ethtool_stats *stats, u64 *data)
2484{
2485 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2486 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002487 int i, j;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002488 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002489
2490 memset(&crq, 0, sizeof(crq));
2491 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2492 crq.request_statistics.cmd = REQUEST_STATISTICS;
2493 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2494 crq.request_statistics.len =
2495 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002496
2497 /* Wait for data to be written */
2498 init_completion(&adapter->stats_done);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05002499 rc = ibmvnic_send_crq(adapter, &crq);
2500 if (rc)
2501 return;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002502 wait_for_completion(&adapter->stats_done);
2503
2504 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002505 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2506 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002507
2508 for (j = 0; j < adapter->req_tx_queues; j++) {
2509 data[i] = adapter->tx_stats_buffers[j].packets;
2510 i++;
2511 data[i] = adapter->tx_stats_buffers[j].bytes;
2512 i++;
2513 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2514 i++;
2515 }
2516
2517 for (j = 0; j < adapter->req_rx_queues; j++) {
2518 data[i] = adapter->rx_stats_buffers[j].packets;
2519 i++;
2520 data[i] = adapter->rx_stats_buffers[j].bytes;
2521 i++;
2522 data[i] = adapter->rx_stats_buffers[j].interrupts;
2523 i++;
2524 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002525}
2526
Thomas Falcon723ad912018-09-28 18:38:26 -05002527static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2528{
2529 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2530
2531 return adapter->priv_flags;
2532}
2533
2534static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2535{
2536 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2537 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2538
2539 if (which_maxes)
2540 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2541 else
2542 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2543
2544 return 0;
2545}
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002547 .get_drvinfo = ibmvnic_get_drvinfo,
2548 .get_msglevel = ibmvnic_get_msglevel,
2549 .set_msglevel = ibmvnic_set_msglevel,
2550 .get_link = ibmvnic_get_link,
2551 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002552 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002553 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002554 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002555 .get_strings = ibmvnic_get_strings,
2556 .get_sset_count = ibmvnic_get_sset_count,
2557 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002558 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon723ad912018-09-28 18:38:26 -05002559 .get_priv_flags = ibmvnic_get_priv_flags,
2560 .set_priv_flags = ibmvnic_set_priv_flags,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002561};
2562
2563/* Routines for managing CRQs/sCRQs */
2564
Nathan Fontenot57a49432017-05-26 10:31:12 -04002565static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2566 struct ibmvnic_sub_crq_queue *scrq)
2567{
2568 int rc;
2569
2570 if (scrq->irq) {
2571 free_irq(scrq->irq, scrq);
2572 irq_dispose_mapping(scrq->irq);
2573 scrq->irq = 0;
2574 }
2575
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002576 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Thomas Falcon41f71462018-04-06 18:37:03 -05002577 atomic_set(&scrq->used, 0);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002578 scrq->cur = 0;
2579
2580 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2581 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2582 return rc;
2583}
2584
2585static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2586{
2587 int i, rc;
2588
2589 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002590 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002591 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2592 if (rc)
2593 return rc;
2594 }
2595
2596 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002597 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002598 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2599 if (rc)
2600 return rc;
2601 }
2602
Nathan Fontenot57a49432017-05-26 10:31:12 -04002603 return rc;
2604}
2605
Thomas Falcon032c5e82015-12-21 11:26:06 -06002606static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002607 struct ibmvnic_sub_crq_queue *scrq,
2608 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002609{
2610 struct device *dev = &adapter->vdev->dev;
2611 long rc;
2612
2613 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2614
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002615 if (do_h_free) {
2616 /* Close the sub-crqs */
2617 do {
2618 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2619 adapter->vdev->unit_address,
2620 scrq->crq_num);
2621 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002622
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002623 if (rc) {
2624 netdev_err(adapter->netdev,
2625 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2626 scrq->crq_num, rc);
2627 }
Thomas Falconffa73852017-04-19 13:44:29 -04002628 }
2629
Thomas Falcon032c5e82015-12-21 11:26:06 -06002630 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2631 DMA_BIDIRECTIONAL);
2632 free_pages((unsigned long)scrq->msgs, 2);
2633 kfree(scrq);
2634}
2635
2636static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2637 *adapter)
2638{
2639 struct device *dev = &adapter->vdev->dev;
2640 struct ibmvnic_sub_crq_queue *scrq;
2641 int rc;
2642
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002643 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002644 if (!scrq)
2645 return NULL;
2646
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002647 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002648 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002649 if (!scrq->msgs) {
2650 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2651 goto zero_page_failed;
2652 }
2653
2654 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2655 DMA_BIDIRECTIONAL);
2656 if (dma_mapping_error(dev, scrq->msg_token)) {
2657 dev_warn(dev, "Couldn't map crq queue messages page\n");
2658 goto map_failed;
2659 }
2660
2661 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2662 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2663
2664 if (rc == H_RESOURCE)
2665 rc = ibmvnic_reset_crq(adapter);
2666
2667 if (rc == H_CLOSED) {
2668 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2669 } else if (rc) {
2670 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2671 goto reg_failed;
2672 }
2673
Thomas Falcon032c5e82015-12-21 11:26:06 -06002674 scrq->adapter = adapter;
2675 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002676 spin_lock_init(&scrq->lock);
2677
2678 netdev_dbg(adapter->netdev,
2679 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2680 scrq->crq_num, scrq->hw_irq, scrq->irq);
2681
2682 return scrq;
2683
Thomas Falcon032c5e82015-12-21 11:26:06 -06002684reg_failed:
2685 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2686 DMA_BIDIRECTIONAL);
2687map_failed:
2688 free_pages((unsigned long)scrq->msgs, 2);
2689zero_page_failed:
2690 kfree(scrq);
2691
2692 return NULL;
2693}
2694
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002695static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002696{
2697 int i;
2698
2699 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002700 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002701 if (!adapter->tx_scrq[i])
2702 continue;
2703
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002704 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2705 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002706 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 free_irq(adapter->tx_scrq[i]->irq,
2708 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002709 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002710 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002711 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002712
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002713 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2714 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002715 }
2716
Nathan Fontenot9501df32017-03-15 23:38:07 -04002717 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002719 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720 }
2721
2722 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002723 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002724 if (!adapter->rx_scrq[i])
2725 continue;
2726
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002727 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2728 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002729 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 free_irq(adapter->rx_scrq[i]->irq,
2731 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002732 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002733 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002734 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002735
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002736 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2737 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002738 }
2739
Nathan Fontenot9501df32017-03-15 23:38:07 -04002740 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002742 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002743 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744}
2745
2746static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2747 struct ibmvnic_sub_crq_queue *scrq)
2748{
2749 struct device *dev = &adapter->vdev->dev;
2750 unsigned long rc;
2751
2752 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2753 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2754 if (rc)
2755 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2756 scrq->hw_irq, rc);
2757 return rc;
2758}
2759
2760static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2761 struct ibmvnic_sub_crq_queue *scrq)
2762{
2763 struct device *dev = &adapter->vdev->dev;
2764 unsigned long rc;
2765
2766 if (scrq->hw_irq > 0x100000000ULL) {
2767 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2768 return 1;
2769 }
2770
Nathan Fontenot73f9d362018-05-22 11:21:10 -05002771 if (adapter->resetting &&
2772 adapter->reset_reason == VNIC_RESET_MOBILITY) {
2773 u64 val = (0xff000000) | scrq->hw_irq;
2774
2775 rc = plpar_hcall_norets(H_EOI, val);
2776 if (rc)
2777 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2778 val, rc);
2779 }
Thomas Falconf23e0642018-04-15 18:53:36 -05002780
Thomas Falcon032c5e82015-12-21 11:26:06 -06002781 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2782 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2783 if (rc)
2784 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2785 scrq->hw_irq, rc);
2786 return rc;
2787}
2788
2789static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2790 struct ibmvnic_sub_crq_queue *scrq)
2791{
2792 struct device *dev = &adapter->vdev->dev;
Thomas Falcon06b3e352018-03-16 20:00:28 -05002793 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794 struct ibmvnic_tx_buff *txbuff;
2795 union sub_crq *next;
2796 int index;
2797 int i, j;
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002798 u8 *first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799
2800restart_loop:
2801 while (pending_scrq(adapter, scrq)) {
2802 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002803 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002804
2805 next = ibmvnic_next_scrq(adapter, scrq);
2806 for (i = 0; i < next->tx_comp.num_comps; i++) {
2807 if (next->tx_comp.rcs[i]) {
2808 dev_err(dev, "tx error %x\n",
2809 next->tx_comp.rcs[i]);
2810 continue;
2811 }
2812 index = be32_to_cpu(next->tx_comp.correlators[i]);
Thomas Falcon06b3e352018-03-16 20:00:28 -05002813 if (index & IBMVNIC_TSO_POOL_MASK) {
2814 tx_pool = &adapter->tso_pool[pool];
2815 index &= ~IBMVNIC_TSO_POOL_MASK;
2816 } else {
2817 tx_pool = &adapter->tx_pool[pool];
2818 }
2819
2820 txbuff = &tx_pool->tx_buff[index];
Thomas Falcon032c5e82015-12-21 11:26:06 -06002821
2822 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2823 if (!txbuff->data_dma[j])
2824 continue;
2825
2826 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002827 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002828 /* if sub_crq was sent indirectly */
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002829 first = &txbuff->indir_arr[0].generic.first;
2830 if (*first == IBMVNIC_CRQ_CMD) {
Thomas Falconad7775d2016-04-01 17:20:34 -05002831 dma_unmap_single(dev, txbuff->indir_dma,
2832 sizeof(txbuff->indir_arr),
2833 DMA_TO_DEVICE);
Thomas Falcon37e40fa2018-04-06 18:37:02 -05002834 *first = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05002835 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002836
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002837 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002838 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002839 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002840 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841
Thomas Falconffc385b2018-02-18 10:08:41 -06002842 num_entries += txbuff->num_entries;
2843
Thomas Falcon06b3e352018-03-16 20:00:28 -05002844 tx_pool->free_map[tx_pool->producer_index] = index;
2845 tx_pool->producer_index =
2846 (tx_pool->producer_index + 1) %
2847 tx_pool->num_buffers;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002848 }
2849 /* remove tx_comp scrq*/
2850 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002851
Thomas Falconffc385b2018-02-18 10:08:41 -06002852 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002853 (adapter->req_tx_entries_per_subcrq / 2) &&
2854 __netif_subqueue_stopped(adapter->netdev,
2855 scrq->pool_index)) {
2856 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002857 netdev_dbg(adapter->netdev, "Started queue %d\n",
2858 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002859 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002860 }
2861
2862 enable_scrq_irq(adapter, scrq);
2863
2864 if (pending_scrq(adapter, scrq)) {
2865 disable_scrq_irq(adapter, scrq);
2866 goto restart_loop;
2867 }
2868
2869 return 0;
2870}
2871
2872static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2873{
2874 struct ibmvnic_sub_crq_queue *scrq = instance;
2875 struct ibmvnic_adapter *adapter = scrq->adapter;
2876
2877 disable_scrq_irq(adapter, scrq);
2878 ibmvnic_complete_tx(adapter, scrq);
2879
2880 return IRQ_HANDLED;
2881}
2882
2883static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2884{
2885 struct ibmvnic_sub_crq_queue *scrq = instance;
2886 struct ibmvnic_adapter *adapter = scrq->adapter;
2887
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002888 /* When booting a kdump kernel we can hit pending interrupts
2889 * prior to completing driver initialization.
2890 */
2891 if (unlikely(adapter->state != VNIC_OPEN))
2892 return IRQ_NONE;
2893
John Allen3d52b592017-08-02 16:44:14 -05002894 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2895
Thomas Falcon032c5e82015-12-21 11:26:06 -06002896 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2897 disable_scrq_irq(adapter, scrq);
2898 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2899 }
2900
2901 return IRQ_HANDLED;
2902}
2903
Thomas Falconea22d512016-07-06 15:35:17 -05002904static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2905{
2906 struct device *dev = &adapter->vdev->dev;
2907 struct ibmvnic_sub_crq_queue *scrq;
2908 int i = 0, j = 0;
2909 int rc = 0;
2910
2911 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002912 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2913 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002914 scrq = adapter->tx_scrq[i];
2915 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2916
Michael Ellerman99c17902016-09-10 19:59:05 +10002917 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002918 rc = -EINVAL;
2919 dev_err(dev, "Error mapping irq\n");
2920 goto req_tx_irq_failed;
2921 }
2922
2923 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2924 0, "ibmvnic_tx", scrq);
2925
2926 if (rc) {
2927 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2928 scrq->irq, rc);
2929 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002930 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002931 }
2932 }
2933
2934 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002935 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2936 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002937 scrq = adapter->rx_scrq[i];
2938 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002939 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002940 rc = -EINVAL;
2941 dev_err(dev, "Error mapping irq\n");
2942 goto req_rx_irq_failed;
2943 }
2944 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2945 0, "ibmvnic_rx", scrq);
2946 if (rc) {
2947 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2948 scrq->irq, rc);
2949 irq_dispose_mapping(scrq->irq);
2950 goto req_rx_irq_failed;
2951 }
2952 }
2953 return rc;
2954
2955req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002956 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002957 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2958 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002959 }
Thomas Falconea22d512016-07-06 15:35:17 -05002960 i = adapter->req_tx_queues;
2961req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002962 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002963 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2964 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002965 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002966 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002967 return rc;
2968}
2969
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002970static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971{
2972 struct device *dev = &adapter->vdev->dev;
2973 struct ibmvnic_sub_crq_queue **allqueues;
2974 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975 int total_queues;
2976 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002977 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002978
Thomas Falcon032c5e82015-12-21 11:26:06 -06002979 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2980
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002981 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002983 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002984
2985 for (i = 0; i < total_queues; i++) {
2986 allqueues[i] = init_sub_crq_queue(adapter);
2987 if (!allqueues[i]) {
2988 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2989 break;
2990 }
2991 registered_queues++;
2992 }
2993
2994 /* Make sure we were able to register the minimum number of queues */
2995 if (registered_queues <
2996 adapter->min_tx_queues + adapter->min_rx_queues) {
2997 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2998 goto tx_failed;
2999 }
3000
3001 /* Distribute the failed allocated queues*/
3002 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3003 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3004 switch (i % 3) {
3005 case 0:
3006 if (adapter->req_rx_queues > adapter->min_rx_queues)
3007 adapter->req_rx_queues--;
3008 else
3009 more++;
3010 break;
3011 case 1:
3012 if (adapter->req_tx_queues > adapter->min_tx_queues)
3013 adapter->req_tx_queues--;
3014 else
3015 more++;
3016 break;
3017 }
3018 }
3019
3020 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003021 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003022 if (!adapter->tx_scrq)
3023 goto tx_failed;
3024
3025 for (i = 0; i < adapter->req_tx_queues; i++) {
3026 adapter->tx_scrq[i] = allqueues[i];
3027 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003028 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003029 }
3030
3031 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003032 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003033 if (!adapter->rx_scrq)
3034 goto rx_failed;
3035
3036 for (i = 0; i < adapter->req_rx_queues; i++) {
3037 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3038 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06003039 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040 }
3041
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003042 kfree(allqueues);
3043 return 0;
3044
3045rx_failed:
3046 kfree(adapter->tx_scrq);
3047 adapter->tx_scrq = NULL;
3048tx_failed:
3049 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06003050 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003051 kfree(allqueues);
3052 return -1;
3053}
3054
3055static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3056{
3057 struct device *dev = &adapter->vdev->dev;
3058 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05003059 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003060
3061 if (!retry) {
3062 /* Sub-CRQ entries are 32 byte long */
3063 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3064
3065 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3066 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3067 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3068 return;
3069 }
3070
John Allenc26eba02017-10-26 16:23:25 -05003071 if (adapter->desired.mtu)
3072 adapter->req_mtu = adapter->desired.mtu;
3073 else
3074 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003075
John Allenc26eba02017-10-26 16:23:25 -05003076 if (!adapter->desired.tx_entries)
3077 adapter->desired.tx_entries =
3078 adapter->max_tx_entries_per_subcrq;
3079 if (!adapter->desired.rx_entries)
3080 adapter->desired.rx_entries =
3081 adapter->max_rx_add_entries_per_subcrq;
3082
3083 max_entries = IBMVNIC_MAX_LTB_SIZE /
3084 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3085
3086 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3087 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3088 adapter->desired.tx_entries = max_entries;
3089 }
3090
3091 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3092 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3093 adapter->desired.rx_entries = max_entries;
3094 }
3095
3096 if (adapter->desired.tx_entries)
3097 adapter->req_tx_entries_per_subcrq =
3098 adapter->desired.tx_entries;
3099 else
3100 adapter->req_tx_entries_per_subcrq =
3101 adapter->max_tx_entries_per_subcrq;
3102
3103 if (adapter->desired.rx_entries)
3104 adapter->req_rx_add_entries_per_subcrq =
3105 adapter->desired.rx_entries;
3106 else
3107 adapter->req_rx_add_entries_per_subcrq =
3108 adapter->max_rx_add_entries_per_subcrq;
3109
3110 if (adapter->desired.tx_queues)
3111 adapter->req_tx_queues =
3112 adapter->desired.tx_queues;
3113 else
3114 adapter->req_tx_queues =
3115 adapter->opt_tx_comp_sub_queues;
3116
3117 if (adapter->desired.rx_queues)
3118 adapter->req_rx_queues =
3119 adapter->desired.rx_queues;
3120 else
3121 adapter->req_rx_queues =
3122 adapter->opt_rx_comp_queues;
3123
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003124 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003125 }
3126
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 memset(&crq, 0, sizeof(crq));
3128 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3129 crq.request_capability.cmd = REQUEST_CAPABILITY;
3130
3131 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003132 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06003133 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003134 ibmvnic_send_crq(adapter, &crq);
3135
3136 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003137 crq.request_capability.number = cpu_to_be64(adapter->req_rx_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_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06003142 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_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 =
3147 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3148 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003149 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003150 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003151 ibmvnic_send_crq(adapter, &crq);
3152
3153 crq.request_capability.capability =
3154 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3155 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06003156 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06003157 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003158 ibmvnic_send_crq(adapter, &crq);
3159
3160 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06003161 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
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 if (adapter->netdev->flags & IFF_PROMISC) {
3166 if (adapter->promisc_supported) {
3167 crq.request_capability.capability =
3168 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003169 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06003170 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171 ibmvnic_send_crq(adapter, &crq);
3172 }
3173 } else {
3174 crq.request_capability.capability =
3175 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06003176 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06003177 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003178 ibmvnic_send_crq(adapter, &crq);
3179 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003180}
3181
3182static int pending_scrq(struct ibmvnic_adapter *adapter,
3183 struct ibmvnic_sub_crq_queue *scrq)
3184{
3185 union sub_crq *entry = &scrq->msgs[scrq->cur];
3186
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05003187 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003188 return 1;
3189 else
3190 return 0;
3191}
3192
3193static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3194 struct ibmvnic_sub_crq_queue *scrq)
3195{
3196 union sub_crq *entry;
3197 unsigned long flags;
3198
3199 spin_lock_irqsave(&scrq->lock, flags);
3200 entry = &scrq->msgs[scrq->cur];
3201 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3202 if (++scrq->cur == scrq->size)
3203 scrq->cur = 0;
3204 } else {
3205 entry = NULL;
3206 }
3207 spin_unlock_irqrestore(&scrq->lock, flags);
3208
3209 return entry;
3210}
3211
3212static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3213{
3214 struct ibmvnic_crq_queue *queue = &adapter->crq;
3215 union ibmvnic_crq *crq;
3216
3217 crq = &queue->msgs[queue->cur];
3218 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3219 if (++queue->cur == queue->size)
3220 queue->cur = 0;
3221 } else {
3222 crq = NULL;
3223 }
3224
3225 return crq;
3226}
3227
Thomas Falcon2d14d372018-07-13 12:03:32 -05003228static void print_subcrq_error(struct device *dev, int rc, const char *func)
3229{
3230 switch (rc) {
3231 case H_PARAMETER:
3232 dev_warn_ratelimited(dev,
3233 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3234 func, rc);
3235 break;
3236 case H_CLOSED:
3237 dev_warn_ratelimited(dev,
3238 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3239 func, rc);
3240 break;
3241 default:
3242 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3243 break;
3244 }
3245}
3246
Thomas Falcon032c5e82015-12-21 11:26:06 -06003247static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3248 union sub_crq *sub_crq)
3249{
3250 unsigned int ua = adapter->vdev->unit_address;
3251 struct device *dev = &adapter->vdev->dev;
3252 u64 *u64_crq = (u64 *)sub_crq;
3253 int rc;
3254
3255 netdev_dbg(adapter->netdev,
3256 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3257 (unsigned long int)cpu_to_be64(remote_handle),
3258 (unsigned long int)cpu_to_be64(u64_crq[0]),
3259 (unsigned long int)cpu_to_be64(u64_crq[1]),
3260 (unsigned long int)cpu_to_be64(u64_crq[2]),
3261 (unsigned long int)cpu_to_be64(u64_crq[3]));
3262
3263 /* Make sure the hypervisor sees the complete request */
3264 mb();
3265
3266 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3267 cpu_to_be64(remote_handle),
3268 cpu_to_be64(u64_crq[0]),
3269 cpu_to_be64(u64_crq[1]),
3270 cpu_to_be64(u64_crq[2]),
3271 cpu_to_be64(u64_crq[3]));
3272
Thomas Falcon2d14d372018-07-13 12:03:32 -05003273 if (rc)
3274 print_subcrq_error(dev, rc, __func__);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003275
3276 return rc;
3277}
3278
Thomas Falconad7775d2016-04-01 17:20:34 -05003279static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3280 u64 remote_handle, u64 ioba, u64 num_entries)
3281{
3282 unsigned int ua = adapter->vdev->unit_address;
3283 struct device *dev = &adapter->vdev->dev;
3284 int rc;
3285
3286 /* Make sure the hypervisor sees the complete request */
3287 mb();
3288 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3289 cpu_to_be64(remote_handle),
3290 ioba, num_entries);
3291
Thomas Falcon2d14d372018-07-13 12:03:32 -05003292 if (rc)
3293 print_subcrq_error(dev, rc, __func__);
Thomas Falconad7775d2016-04-01 17:20:34 -05003294
3295 return rc;
3296}
3297
Thomas Falcon032c5e82015-12-21 11:26:06 -06003298static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3299 union ibmvnic_crq *crq)
3300{
3301 unsigned int ua = adapter->vdev->unit_address;
3302 struct device *dev = &adapter->vdev->dev;
3303 u64 *u64_crq = (u64 *)crq;
3304 int rc;
3305
3306 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3307 (unsigned long int)cpu_to_be64(u64_crq[0]),
3308 (unsigned long int)cpu_to_be64(u64_crq[1]));
3309
Thomas Falcon51536982018-05-23 13:37:56 -05003310 if (!adapter->crq.active &&
3311 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3312 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3313 return -EINVAL;
3314 }
3315
Thomas Falcon032c5e82015-12-21 11:26:06 -06003316 /* Make sure the hypervisor sees the complete request */
3317 mb();
3318
3319 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3320 cpu_to_be64(u64_crq[0]),
3321 cpu_to_be64(u64_crq[1]));
3322
3323 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003324 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003325 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003326 if (adapter->resetting)
3327 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3328 }
3329
Thomas Falcon032c5e82015-12-21 11:26:06 -06003330 dev_warn(dev, "Send error (rc=%d)\n", rc);
3331 }
3332
3333 return rc;
3334}
3335
3336static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3337{
3338 union ibmvnic_crq crq;
3339
3340 memset(&crq, 0, sizeof(crq));
3341 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3342 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3343 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3344
3345 return ibmvnic_send_crq(adapter, &crq);
3346}
3347
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348static int send_version_xchg(struct ibmvnic_adapter *adapter)
3349{
3350 union ibmvnic_crq crq;
3351
3352 memset(&crq, 0, sizeof(crq));
3353 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3354 crq.version_exchange.cmd = VERSION_EXCHANGE;
3355 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3356
3357 return ibmvnic_send_crq(adapter, &crq);
3358}
3359
Nathan Fontenot37798d02017-11-08 11:23:56 -06003360struct vnic_login_client_data {
3361 u8 type;
3362 __be16 len;
Kees Cook08ea5562018-04-10 15:26:43 -07003363 char name[];
Nathan Fontenot37798d02017-11-08 11:23:56 -06003364} __packed;
3365
3366static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3367{
3368 int len;
3369
3370 /* Calculate the amount of buffer space needed for the
3371 * vnic client data in the login buffer. There are four entries,
3372 * OS name, LPAR name, device name, and a null last entry.
3373 */
3374 len = 4 * sizeof(struct vnic_login_client_data);
3375 len += 6; /* "Linux" plus NULL */
3376 len += strlen(utsname()->nodename) + 1;
3377 len += strlen(adapter->netdev->name) + 1;
3378
3379 return len;
3380}
3381
3382static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3383 struct vnic_login_client_data *vlcd)
3384{
3385 const char *os_name = "Linux";
3386 int len;
3387
3388 /* Type 1 - LPAR OS */
3389 vlcd->type = 1;
3390 len = strlen(os_name) + 1;
3391 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003392 strncpy(vlcd->name, os_name, len);
3393 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003394
3395 /* Type 2 - LPAR name */
3396 vlcd->type = 2;
3397 len = strlen(utsname()->nodename) + 1;
3398 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003399 strncpy(vlcd->name, utsname()->nodename, len);
3400 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003401
3402 /* Type 3 - device name */
3403 vlcd->type = 3;
3404 len = strlen(adapter->netdev->name) + 1;
3405 vlcd->len = cpu_to_be16(len);
Kees Cook08ea5562018-04-10 15:26:43 -07003406 strncpy(vlcd->name, adapter->netdev->name, len);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003407}
3408
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003409static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003410{
3411 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3412 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003413 struct device *dev = &adapter->vdev->dev;
3414 dma_addr_t rsp_buffer_token;
3415 dma_addr_t buffer_token;
3416 size_t rsp_buffer_size;
3417 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003418 size_t buffer_size;
3419 __be64 *tx_list_p;
3420 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003421 int client_data_len;
3422 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003423 int i;
3424
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003425 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3426 netdev_err(adapter->netdev,
3427 "RX or TX queues are not allocated, device login failed\n");
3428 return -1;
3429 }
3430
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003431 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003432 client_data_len = vnic_client_data_len(adapter);
3433
Thomas Falcon032c5e82015-12-21 11:26:06 -06003434 buffer_size =
3435 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003436 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3437 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003438
Nathan Fontenot37798d02017-11-08 11:23:56 -06003439 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003440 if (!login_buffer)
3441 goto buf_alloc_failed;
3442
3443 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3444 DMA_TO_DEVICE);
3445 if (dma_mapping_error(dev, buffer_token)) {
3446 dev_err(dev, "Couldn't map login buffer\n");
3447 goto buf_map_failed;
3448 }
3449
John Allen498cd8e2016-04-06 11:49:55 -05003450 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3451 sizeof(u64) * adapter->req_tx_queues +
3452 sizeof(u64) * adapter->req_rx_queues +
3453 sizeof(u64) * adapter->req_rx_queues +
3454 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003455
3456 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3457 if (!login_rsp_buffer)
3458 goto buf_rsp_alloc_failed;
3459
3460 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3461 rsp_buffer_size, DMA_FROM_DEVICE);
3462 if (dma_mapping_error(dev, rsp_buffer_token)) {
3463 dev_err(dev, "Couldn't map login rsp buffer\n");
3464 goto buf_rsp_map_failed;
3465 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003466
Thomas Falcon032c5e82015-12-21 11:26:06 -06003467 adapter->login_buf = login_buffer;
3468 adapter->login_buf_token = buffer_token;
3469 adapter->login_buf_sz = buffer_size;
3470 adapter->login_rsp_buf = login_rsp_buffer;
3471 adapter->login_rsp_buf_token = rsp_buffer_token;
3472 adapter->login_rsp_buf_sz = rsp_buffer_size;
3473
3474 login_buffer->len = cpu_to_be32(buffer_size);
3475 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3476 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3477 login_buffer->off_txcomp_subcrqs =
3478 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3479 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3480 login_buffer->off_rxcomp_subcrqs =
3481 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3482 sizeof(u64) * adapter->req_tx_queues);
3483 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3484 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3485
3486 tx_list_p = (__be64 *)((char *)login_buffer +
3487 sizeof(struct ibmvnic_login_buffer));
3488 rx_list_p = (__be64 *)((char *)login_buffer +
3489 sizeof(struct ibmvnic_login_buffer) +
3490 sizeof(u64) * adapter->req_tx_queues);
3491
3492 for (i = 0; i < adapter->req_tx_queues; i++) {
3493 if (adapter->tx_scrq[i]) {
3494 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3495 crq_num);
3496 }
3497 }
3498
3499 for (i = 0; i < adapter->req_rx_queues; i++) {
3500 if (adapter->rx_scrq[i]) {
3501 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3502 crq_num);
3503 }
3504 }
3505
Nathan Fontenot37798d02017-11-08 11:23:56 -06003506 /* Insert vNIC login client data */
3507 vlcd = (struct vnic_login_client_data *)
3508 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3509 login_buffer->client_data_offset =
3510 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3511 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3512
3513 vnic_add_client_data(adapter, vlcd);
3514
Thomas Falcon032c5e82015-12-21 11:26:06 -06003515 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3516 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3517 netdev_dbg(adapter->netdev, "%016lx\n",
3518 ((unsigned long int *)(adapter->login_buf))[i]);
3519 }
3520
3521 memset(&crq, 0, sizeof(crq));
3522 crq.login.first = IBMVNIC_CRQ_CMD;
3523 crq.login.cmd = LOGIN;
3524 crq.login.ioba = cpu_to_be32(buffer_token);
3525 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003526 ibmvnic_send_crq(adapter, &crq);
3527
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003528 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003529
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530buf_rsp_map_failed:
3531 kfree(login_rsp_buffer);
3532buf_rsp_alloc_failed:
3533 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3534buf_map_failed:
3535 kfree(login_buffer);
3536buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003537 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003538}
3539
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003540static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3541 u32 len, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003542{
3543 union ibmvnic_crq crq;
3544
3545 memset(&crq, 0, sizeof(crq));
3546 crq.request_map.first = IBMVNIC_CRQ_CMD;
3547 crq.request_map.cmd = REQUEST_MAP;
3548 crq.request_map.map_id = map_id;
3549 crq.request_map.ioba = cpu_to_be32(addr);
3550 crq.request_map.len = cpu_to_be32(len);
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003551 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003552}
3553
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003554static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003555{
3556 union ibmvnic_crq crq;
3557
3558 memset(&crq, 0, sizeof(crq));
3559 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3560 crq.request_unmap.cmd = REQUEST_UNMAP;
3561 crq.request_unmap.map_id = map_id;
Thomas Falcon9c4eaab2018-05-23 13:37:57 -05003562 return ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003563}
3564
3565static void send_map_query(struct ibmvnic_adapter *adapter)
3566{
3567 union ibmvnic_crq crq;
3568
3569 memset(&crq, 0, sizeof(crq));
3570 crq.query_map.first = IBMVNIC_CRQ_CMD;
3571 crq.query_map.cmd = QUERY_MAP;
3572 ibmvnic_send_crq(adapter, &crq);
3573}
3574
3575/* Send a series of CRQs requesting various capabilities of the VNIC server */
3576static void send_cap_queries(struct ibmvnic_adapter *adapter)
3577{
3578 union ibmvnic_crq crq;
3579
Thomas Falcon901e0402017-02-15 12:17:59 -06003580 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003581 memset(&crq, 0, sizeof(crq));
3582 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3583 crq.query_capability.cmd = QUERY_CAPABILITY;
3584
3585 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003586 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003587 ibmvnic_send_crq(adapter, &crq);
3588
3589 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003590 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 ibmvnic_send_crq(adapter, &crq);
3592
3593 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003594 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595 ibmvnic_send_crq(adapter, &crq);
3596
3597 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003598 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599 ibmvnic_send_crq(adapter, &crq);
3600
3601 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003602 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003603 ibmvnic_send_crq(adapter, &crq);
3604
3605 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003606 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003607 ibmvnic_send_crq(adapter, &crq);
3608
3609 crq.query_capability.capability =
3610 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
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_RX_ADD_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(MAX_TX_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_RX_ADD_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 = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003630 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003631 ibmvnic_send_crq(adapter, &crq);
3632
3633 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003634 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003635 ibmvnic_send_crq(adapter, &crq);
3636
3637 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003638 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003639 ibmvnic_send_crq(adapter, &crq);
3640
3641 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003642 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003643 ibmvnic_send_crq(adapter, &crq);
3644
3645 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003646 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003647 ibmvnic_send_crq(adapter, &crq);
3648
3649 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003650 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003651 ibmvnic_send_crq(adapter, &crq);
3652
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003653 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3654 atomic_inc(&adapter->running_cap_crqs);
3655 ibmvnic_send_crq(adapter, &crq);
3656
Thomas Falcon032c5e82015-12-21 11:26:06 -06003657 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003658 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003659 ibmvnic_send_crq(adapter, &crq);
3660
3661 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003662 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003663 ibmvnic_send_crq(adapter, &crq);
3664
3665 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003666 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003667 ibmvnic_send_crq(adapter, &crq);
3668
3669 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003670 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671 ibmvnic_send_crq(adapter, &crq);
3672
3673 crq.query_capability.capability =
3674 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
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_TX_ENTRIES_PER_SUBCRQ);
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_RXBA_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 = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003689 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003690 ibmvnic_send_crq(adapter, &crq);
3691}
3692
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003693static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3694 struct ibmvnic_adapter *adapter)
3695{
3696 struct device *dev = &adapter->vdev->dev;
3697
3698 if (crq->get_vpd_size_rsp.rc.code) {
3699 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3700 crq->get_vpd_size_rsp.rc.code);
3701 complete(&adapter->fw_done);
3702 return;
3703 }
3704
3705 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3706 complete(&adapter->fw_done);
3707}
3708
3709static void handle_vpd_rsp(union ibmvnic_crq *crq,
3710 struct ibmvnic_adapter *adapter)
3711{
3712 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003713 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003714 u8 fw_level_len = 0;
3715
3716 memset(adapter->fw_version, 0, 32);
3717
3718 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3719 DMA_FROM_DEVICE);
3720
3721 if (crq->get_vpd_rsp.rc.code) {
3722 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3723 crq->get_vpd_rsp.rc.code);
3724 goto complete;
3725 }
3726
3727 /* get the position of the firmware version info
3728 * located after the ASCII 'RM' substring in the buffer
3729 */
3730 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3731 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003732 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 -02003733 goto complete;
3734 }
3735
3736 /* get length of firmware level ASCII substring */
3737 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3738 fw_level_len = *(substr + 2);
3739 } else {
3740 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3741 goto complete;
3742 }
3743
3744 /* copy firmware version string from vpd into adapter */
3745 if ((substr + 3 + fw_level_len) <
3746 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003747 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003748 } else {
3749 dev_info(dev, "FW substr extrapolated VPD buff\n");
3750 }
3751
3752complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003753 if (adapter->fw_version[0] == '\0')
3754 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003755 complete(&adapter->fw_done);
3756}
3757
Thomas Falcon032c5e82015-12-21 11:26:06 -06003758static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3759{
3760 struct device *dev = &adapter->vdev->dev;
3761 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3762 union ibmvnic_crq crq;
3763 int i;
3764
3765 dma_unmap_single(dev, adapter->ip_offload_tok,
3766 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3767
3768 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3769 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3770 netdev_dbg(adapter->netdev, "%016lx\n",
3771 ((unsigned long int *)(buf))[i]);
3772
3773 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3774 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3775 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3776 buf->tcp_ipv4_chksum);
3777 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3778 buf->tcp_ipv6_chksum);
3779 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3780 buf->udp_ipv4_chksum);
3781 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3782 buf->udp_ipv6_chksum);
3783 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3784 buf->large_tx_ipv4);
3785 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3786 buf->large_tx_ipv6);
3787 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3788 buf->large_rx_ipv4);
3789 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3790 buf->large_rx_ipv6);
3791 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3792 buf->max_ipv4_header_size);
3793 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3794 buf->max_ipv6_header_size);
3795 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3796 buf->max_tcp_header_size);
3797 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3798 buf->max_udp_header_size);
3799 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3800 buf->max_large_tx_size);
3801 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3802 buf->max_large_rx_size);
3803 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3804 buf->ipv6_extension_header);
3805 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3806 buf->tcp_pseudosum_req);
3807 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3808 buf->num_ipv6_ext_headers);
3809 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3810 buf->off_ipv6_ext_headers);
3811
3812 adapter->ip_offload_ctrl_tok =
3813 dma_map_single(dev, &adapter->ip_offload_ctrl,
3814 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3815
3816 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3817 dev_err(dev, "Couldn't map ip offload control buffer\n");
3818 return;
3819 }
3820
Thomas Falconf6897942018-01-18 19:05:01 -06003821 adapter->ip_offload_ctrl.len =
3822 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003823 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003824 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3825 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003826 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3827 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3828 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3829 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003830 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3831 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003832
Thomas Falconfdb06102017-10-17 12:36:55 -05003833 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003834 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3835 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3836
Thomas Falcon15482052017-10-17 12:36:54 -05003837 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003838
3839 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3840 adapter->netdev->features |= NETIF_F_IP_CSUM;
3841
3842 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3843 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3844
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003845 if ((adapter->netdev->features &
3846 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3847 adapter->netdev->features |= NETIF_F_RXCSUM;
3848
Thomas Falconfdb06102017-10-17 12:36:55 -05003849 if (buf->large_tx_ipv4)
3850 adapter->netdev->features |= NETIF_F_TSO;
3851 if (buf->large_tx_ipv6)
3852 adapter->netdev->features |= NETIF_F_TSO6;
3853
Thomas Falconaa0bf852017-10-17 12:36:56 -05003854 adapter->netdev->hw_features |= adapter->netdev->features;
3855
Thomas Falcon032c5e82015-12-21 11:26:06 -06003856 memset(&crq, 0, sizeof(crq));
3857 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3858 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3859 crq.control_ip_offload.len =
3860 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3861 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3862 ibmvnic_send_crq(adapter, &crq);
3863}
3864
Thomas Falconc9008d32018-08-06 21:39:59 -05003865static const char *ibmvnic_fw_err_cause(u16 cause)
3866{
3867 switch (cause) {
3868 case ADAPTER_PROBLEM:
3869 return "adapter problem";
3870 case BUS_PROBLEM:
3871 return "bus problem";
3872 case FW_PROBLEM:
3873 return "firmware problem";
3874 case DD_PROBLEM:
3875 return "device driver problem";
3876 case EEH_RECOVERY:
3877 return "EEH recovery";
3878 case FW_UPDATED:
3879 return "firmware updated";
3880 case LOW_MEMORY:
3881 return "low Memory";
3882 default:
3883 return "unknown";
3884 }
3885}
3886
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003887static void handle_error_indication(union ibmvnic_crq *crq,
3888 struct ibmvnic_adapter *adapter)
3889{
3890 struct device *dev = &adapter->vdev->dev;
Thomas Falconc9008d32018-08-06 21:39:59 -05003891 u16 cause;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003892
Thomas Falconc9008d32018-08-06 21:39:59 -05003893 cause = be16_to_cpu(crq->error_indication.error_cause);
3894
3895 dev_warn_ratelimited(dev,
3896 "Firmware reports %serror, cause: %s. Starting recovery...\n",
3897 crq->error_indication.flags
3898 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3899 ibmvnic_fw_err_cause(cause));
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003900
Nathan Fontenoted651a12017-05-03 14:04:38 -04003901 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3902 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003903 else
3904 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003905}
3906
Thomas Falconf8136142018-01-29 13:45:05 -06003907static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3908 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909{
3910 struct net_device *netdev = adapter->netdev;
3911 struct device *dev = &adapter->vdev->dev;
3912 long rc;
3913
3914 rc = crq->change_mac_addr_rsp.rc.code;
3915 if (rc) {
3916 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003917 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003918 }
3919 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3920 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003921out:
3922 complete(&adapter->fw_done);
3923 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924}
3925
3926static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3927 struct ibmvnic_adapter *adapter)
3928{
3929 struct device *dev = &adapter->vdev->dev;
3930 u64 *req_value;
3931 char *name;
3932
Thomas Falcon901e0402017-02-15 12:17:59 -06003933 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003934 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3935 case REQ_TX_QUEUES:
3936 req_value = &adapter->req_tx_queues;
3937 name = "tx";
3938 break;
3939 case REQ_RX_QUEUES:
3940 req_value = &adapter->req_rx_queues;
3941 name = "rx";
3942 break;
3943 case REQ_RX_ADD_QUEUES:
3944 req_value = &adapter->req_rx_add_queues;
3945 name = "rx_add";
3946 break;
3947 case REQ_TX_ENTRIES_PER_SUBCRQ:
3948 req_value = &adapter->req_tx_entries_per_subcrq;
3949 name = "tx_entries_per_subcrq";
3950 break;
3951 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3952 req_value = &adapter->req_rx_add_entries_per_subcrq;
3953 name = "rx_add_entries_per_subcrq";
3954 break;
3955 case REQ_MTU:
3956 req_value = &adapter->req_mtu;
3957 name = "mtu";
3958 break;
3959 case PROMISC_REQUESTED:
3960 req_value = &adapter->promisc;
3961 name = "promisc";
3962 break;
3963 default:
3964 dev_err(dev, "Got invalid cap request rsp %d\n",
3965 crq->request_capability.capability);
3966 return;
3967 }
3968
3969 switch (crq->request_capability_rsp.rc.code) {
3970 case SUCCESS:
3971 break;
3972 case PARTIALSUCCESS:
3973 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3974 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003975 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003976 number), name);
John Allene7913802018-01-18 16:27:12 -06003977
3978 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3979 REQ_MTU) {
3980 pr_err("mtu of %llu is not supported. Reverting.\n",
3981 *req_value);
3982 *req_value = adapter->fallback.mtu;
3983 } else {
3984 *req_value =
3985 be64_to_cpu(crq->request_capability_rsp.number);
3986 }
3987
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003988 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003989 return;
3990 default:
3991 dev_err(dev, "Error %d in request cap rsp\n",
3992 crq->request_capability_rsp.rc.code);
3993 return;
3994 }
3995
3996 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003997 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003998 union ibmvnic_crq newcrq;
3999 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4000 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4001 &adapter->ip_offload_buf;
4002
Thomas Falcon249168a2017-02-15 12:18:00 -06004003 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004004 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4005 buf_sz,
4006 DMA_FROM_DEVICE);
4007
4008 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4009 if (!firmware_has_feature(FW_FEATURE_CMO))
4010 dev_err(dev, "Couldn't map offload buffer\n");
4011 return;
4012 }
4013
4014 memset(&newcrq, 0, sizeof(newcrq));
4015 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4016 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4017 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4018 newcrq.query_ip_offload.ioba =
4019 cpu_to_be32(adapter->ip_offload_tok);
4020
4021 ibmvnic_send_crq(adapter, &newcrq);
4022 }
4023}
4024
4025static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4026 struct ibmvnic_adapter *adapter)
4027{
4028 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05004029 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004030 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4031 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004032 int i;
4033
4034 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004035 DMA_TO_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004036 dma_unmap_single(dev, adapter->login_rsp_buf_token,
Thomas Falcon37e40fa2018-04-06 18:37:02 -05004037 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004038
John Allen498cd8e2016-04-06 11:49:55 -05004039 /* If the number of queues requested can't be allocated by the
4040 * server, the login response will return with code 1. We will need
4041 * to resend the login buffer with fewer queues requested.
4042 */
4043 if (login_rsp_crq->generic.rc.code) {
Nathan Fontenot64d92aa2018-04-11 10:09:32 -05004044 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
John Allen498cd8e2016-04-06 11:49:55 -05004045 complete(&adapter->init_done);
4046 return 0;
4047 }
4048
John Allenc26eba02017-10-26 16:23:25 -05004049 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4050
Thomas Falcon032c5e82015-12-21 11:26:06 -06004051 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4052 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4053 netdev_dbg(adapter->netdev, "%016lx\n",
4054 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4055 }
4056
4057 /* Sanity checks */
4058 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4059 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4060 adapter->req_rx_add_queues !=
4061 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4062 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4063 ibmvnic_remove(adapter->vdev);
4064 return -EIO;
4065 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06004066 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004067 complete(&adapter->init_done);
4068
Thomas Falcon032c5e82015-12-21 11:26:06 -06004069 return 0;
4070}
4071
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4073 struct ibmvnic_adapter *adapter)
4074{
4075 struct device *dev = &adapter->vdev->dev;
4076 long rc;
4077
4078 rc = crq->request_unmap_rsp.rc.code;
4079 if (rc)
4080 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4081}
4082
4083static void handle_query_map_rsp(union ibmvnic_crq *crq,
4084 struct ibmvnic_adapter *adapter)
4085{
4086 struct net_device *netdev = adapter->netdev;
4087 struct device *dev = &adapter->vdev->dev;
4088 long rc;
4089
4090 rc = crq->query_map_rsp.rc.code;
4091 if (rc) {
4092 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4093 return;
4094 }
4095 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4096 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4097 crq->query_map_rsp.free_pages);
4098}
4099
4100static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4101 struct ibmvnic_adapter *adapter)
4102{
4103 struct net_device *netdev = adapter->netdev;
4104 struct device *dev = &adapter->vdev->dev;
4105 long rc;
4106
Thomas Falcon901e0402017-02-15 12:17:59 -06004107 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004108 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06004109 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004110 rc = crq->query_capability.rc.code;
4111 if (rc) {
4112 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4113 goto out;
4114 }
4115
4116 switch (be16_to_cpu(crq->query_capability.capability)) {
4117 case MIN_TX_QUEUES:
4118 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004119 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004120 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4121 adapter->min_tx_queues);
4122 break;
4123 case MIN_RX_QUEUES:
4124 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004125 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004126 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4127 adapter->min_rx_queues);
4128 break;
4129 case MIN_RX_ADD_QUEUES:
4130 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004131 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004132 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4133 adapter->min_rx_add_queues);
4134 break;
4135 case MAX_TX_QUEUES:
4136 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004137 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004138 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4139 adapter->max_tx_queues);
4140 break;
4141 case MAX_RX_QUEUES:
4142 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004143 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004144 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4145 adapter->max_rx_queues);
4146 break;
4147 case MAX_RX_ADD_QUEUES:
4148 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004149 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004150 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4151 adapter->max_rx_add_queues);
4152 break;
4153 case MIN_TX_ENTRIES_PER_SUBCRQ:
4154 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004155 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004156 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4157 adapter->min_tx_entries_per_subcrq);
4158 break;
4159 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4160 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004161 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004162 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4163 adapter->min_rx_add_entries_per_subcrq);
4164 break;
4165 case MAX_TX_ENTRIES_PER_SUBCRQ:
4166 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004167 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4169 adapter->max_tx_entries_per_subcrq);
4170 break;
4171 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4172 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004173 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004174 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4175 adapter->max_rx_add_entries_per_subcrq);
4176 break;
4177 case TCP_IP_OFFLOAD:
4178 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06004179 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004180 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4181 adapter->tcp_ip_offload);
4182 break;
4183 case PROMISC_SUPPORTED:
4184 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004185 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004186 netdev_dbg(netdev, "promisc_supported = %lld\n",
4187 adapter->promisc_supported);
4188 break;
4189 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004190 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004191 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004192 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4193 break;
4194 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06004195 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06004196 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004197 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4198 break;
4199 case MAX_MULTICAST_FILTERS:
4200 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004201 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4203 adapter->max_multicast_filters);
4204 break;
4205 case VLAN_HEADER_INSERTION:
4206 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004207 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004208 if (adapter->vlan_header_insertion)
4209 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4210 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4211 adapter->vlan_header_insertion);
4212 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004213 case RX_VLAN_HEADER_INSERTION:
4214 adapter->rx_vlan_header_insertion =
4215 be64_to_cpu(crq->query_capability.number);
4216 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4217 adapter->rx_vlan_header_insertion);
4218 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004219 case MAX_TX_SG_ENTRIES:
4220 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004221 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004222 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4223 adapter->max_tx_sg_entries);
4224 break;
4225 case RX_SG_SUPPORTED:
4226 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004227 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004228 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4229 adapter->rx_sg_supported);
4230 break;
4231 case OPT_TX_COMP_SUB_QUEUES:
4232 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004233 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004234 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4235 adapter->opt_tx_comp_sub_queues);
4236 break;
4237 case OPT_RX_COMP_QUEUES:
4238 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004239 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004240 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4241 adapter->opt_rx_comp_queues);
4242 break;
4243 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4244 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004245 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004246 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4247 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4248 break;
4249 case OPT_TX_ENTRIES_PER_SUBCRQ:
4250 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004251 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004252 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4253 adapter->opt_tx_entries_per_subcrq);
4254 break;
4255 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4256 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004257 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004258 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4259 adapter->opt_rxba_entries_per_subcrq);
4260 break;
4261 case TX_RX_DESC_REQ:
4262 adapter->tx_rx_desc_req = crq->query_capability.number;
4263 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4264 adapter->tx_rx_desc_req);
4265 break;
4266
4267 default:
4268 netdev_err(netdev, "Got invalid cap rsp %d\n",
4269 crq->query_capability.capability);
4270 }
4271
4272out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004273 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4274 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004275 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004276 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004277}
4278
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004279static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4280{
4281 union ibmvnic_crq crq;
4282 int rc;
4283
4284 memset(&crq, 0, sizeof(crq));
4285 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4286 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
4287 init_completion(&adapter->fw_done);
4288 rc = ibmvnic_send_crq(adapter, &crq);
4289 if (rc)
4290 return rc;
4291 wait_for_completion(&adapter->fw_done);
4292 return adapter->fw_done_rc ? -EIO : 0;
4293}
4294
4295static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4296 struct ibmvnic_adapter *adapter)
4297{
4298 struct net_device *netdev = adapter->netdev;
4299 int rc;
4300
4301 rc = crq->query_phys_parms_rsp.rc.code;
4302 if (rc) {
4303 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4304 return rc;
4305 }
4306 switch (cpu_to_be32(crq->query_phys_parms_rsp.speed)) {
4307 case IBMVNIC_10MBPS:
4308 adapter->speed = SPEED_10;
4309 break;
4310 case IBMVNIC_100MBPS:
4311 adapter->speed = SPEED_100;
4312 break;
4313 case IBMVNIC_1GBPS:
4314 adapter->speed = SPEED_1000;
4315 break;
4316 case IBMVNIC_10GBP:
4317 adapter->speed = SPEED_10000;
4318 break;
4319 case IBMVNIC_25GBPS:
4320 adapter->speed = SPEED_25000;
4321 break;
4322 case IBMVNIC_40GBPS:
4323 adapter->speed = SPEED_40000;
4324 break;
4325 case IBMVNIC_50GBPS:
4326 adapter->speed = SPEED_50000;
4327 break;
4328 case IBMVNIC_100GBPS:
4329 adapter->speed = SPEED_100000;
4330 break;
4331 default:
4332 netdev_warn(netdev, "Unknown speed 0x%08x\n",
4333 cpu_to_be32(crq->query_phys_parms_rsp.speed));
4334 adapter->speed = SPEED_UNKNOWN;
4335 }
4336 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4337 adapter->duplex = DUPLEX_FULL;
4338 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4339 adapter->duplex = DUPLEX_HALF;
4340 else
4341 adapter->duplex = DUPLEX_UNKNOWN;
4342
4343 return rc;
4344}
4345
Thomas Falcon032c5e82015-12-21 11:26:06 -06004346static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4347 struct ibmvnic_adapter *adapter)
4348{
4349 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4350 struct net_device *netdev = adapter->netdev;
4351 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004352 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004353 long rc;
4354
4355 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004356 (unsigned long int)cpu_to_be64(u64_crq[0]),
4357 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004358 switch (gen_crq->first) {
4359 case IBMVNIC_CRQ_INIT_RSP:
4360 switch (gen_crq->cmd) {
4361 case IBMVNIC_CRQ_INIT:
4362 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004363 adapter->from_passive_init = true;
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004364 adapter->failover_pending = false;
Thomas Falcon17c87052018-05-23 13:37:58 -05004365 if (!completion_done(&adapter->init_done)) {
4366 complete(&adapter->init_done);
4367 adapter->init_done_rc = -EIO;
4368 }
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004369 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004370 break;
4371 case IBMVNIC_CRQ_INIT_COMPLETE:
4372 dev_info(dev, "Partner initialization complete\n");
Thomas Falcon51536982018-05-23 13:37:56 -05004373 adapter->crq.active = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004374 send_version_xchg(adapter);
4375 break;
4376 default:
4377 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4378 }
4379 return;
4380 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004381 netif_carrier_off(netdev);
Thomas Falcon51536982018-05-23 13:37:56 -05004382 adapter->crq.active = false;
Thomas Falcon2770a792018-05-23 13:38:02 -05004383 if (adapter->resetting)
4384 adapter->force_reset_recovery = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004385 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004386 dev_info(dev, "Migrated, re-enabling adapter\n");
4387 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004388 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4389 dev_info(dev, "Backing device failover detected\n");
Thomas Falcon5a18e1e2018-04-06 18:37:05 -05004390 adapter->failover_pending = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004391 } else {
4392 /* The adapter lost the connection */
4393 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4394 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004395 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004396 }
4397 return;
4398 case IBMVNIC_CRQ_CMD_RSP:
4399 break;
4400 default:
4401 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4402 gen_crq->first);
4403 return;
4404 }
4405
4406 switch (gen_crq->cmd) {
4407 case VERSION_EXCHANGE_RSP:
4408 rc = crq->version_exchange_rsp.rc.code;
4409 if (rc) {
4410 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4411 break;
4412 }
4413 dev_info(dev, "Partner protocol version is %d\n",
4414 crq->version_exchange_rsp.version);
4415 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4416 ibmvnic_version)
4417 ibmvnic_version =
4418 be16_to_cpu(crq->version_exchange_rsp.version);
4419 send_cap_queries(adapter);
4420 break;
4421 case QUERY_CAPABILITY_RSP:
4422 handle_query_cap_rsp(crq, adapter);
4423 break;
4424 case QUERY_MAP_RSP:
4425 handle_query_map_rsp(crq, adapter);
4426 break;
4427 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004428 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4429 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004430 break;
4431 case REQUEST_UNMAP_RSP:
4432 handle_request_unmap_rsp(crq, adapter);
4433 break;
4434 case REQUEST_CAPABILITY_RSP:
4435 handle_request_cap_rsp(crq, adapter);
4436 break;
4437 case LOGIN_RSP:
4438 netdev_dbg(netdev, "Got Login Response\n");
4439 handle_login_rsp(crq, adapter);
4440 break;
4441 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004442 netdev_dbg(netdev,
4443 "Got Logical Link State Response, state: %d rc: %d\n",
4444 crq->logical_link_state_rsp.link_state,
4445 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004446 adapter->logical_link_state =
4447 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004448 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4449 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450 break;
4451 case LINK_STATE_INDICATION:
4452 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4453 adapter->phys_link_state =
4454 crq->link_state_indication.phys_link_state;
4455 adapter->logical_link_state =
4456 crq->link_state_indication.logical_link_state;
4457 break;
4458 case CHANGE_MAC_ADDR_RSP:
4459 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004460 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004461 break;
4462 case ERROR_INDICATION:
4463 netdev_dbg(netdev, "Got Error Indication\n");
4464 handle_error_indication(crq, adapter);
4465 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004466 case REQUEST_STATISTICS_RSP:
4467 netdev_dbg(netdev, "Got Statistics Response\n");
4468 complete(&adapter->stats_done);
4469 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004470 case QUERY_IP_OFFLOAD_RSP:
4471 netdev_dbg(netdev, "Got Query IP offload Response\n");
4472 handle_query_ip_offload_rsp(adapter);
4473 break;
4474 case MULTICAST_CTRL_RSP:
4475 netdev_dbg(netdev, "Got multicast control Response\n");
4476 break;
4477 case CONTROL_IP_OFFLOAD_RSP:
4478 netdev_dbg(netdev, "Got Control IP offload Response\n");
4479 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4480 sizeof(adapter->ip_offload_ctrl),
4481 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004482 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004483 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004484 case COLLECT_FW_TRACE_RSP:
4485 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4486 complete(&adapter->fw_done);
4487 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004488 case GET_VPD_SIZE_RSP:
4489 handle_vpd_size_rsp(crq, adapter);
4490 break;
4491 case GET_VPD_RSP:
4492 handle_vpd_rsp(crq, adapter);
4493 break;
Murilo Fossa Vicentinif8d6ae02019-03-19 10:28:51 -03004494 case QUERY_PHYS_PARMS_RSP:
4495 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4496 complete(&adapter->fw_done);
4497 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004498 default:
4499 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4500 gen_crq->cmd);
4501 }
4502}
4503
4504static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4505{
4506 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004507
Thomas Falcon6c267b32017-02-15 12:17:58 -06004508 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004509 return IRQ_HANDLED;
4510}
4511
4512static void ibmvnic_tasklet(void *data)
4513{
4514 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004515 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 union ibmvnic_crq *crq;
4517 unsigned long flags;
4518 bool done = false;
4519
4520 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004521 while (!done) {
4522 /* Pull all the valid messages off the CRQ */
4523 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4524 ibmvnic_handle_crq(crq, adapter);
4525 crq->generic.first = 0;
4526 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004527
4528 /* remain in tasklet until all
4529 * capabilities responses are received
4530 */
4531 if (!adapter->wait_capability)
4532 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004533 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004534 /* if capabilities CRQ's were sent in this tasklet, the following
4535 * tasklet must wait until all responses are received
4536 */
4537 if (atomic_read(&adapter->running_cap_crqs) != 0)
4538 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004539 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004540}
4541
4542static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4543{
4544 struct vio_dev *vdev = adapter->vdev;
4545 int rc;
4546
4547 do {
4548 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4549 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4550
4551 if (rc)
4552 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4553
4554 return rc;
4555}
4556
4557static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4558{
4559 struct ibmvnic_crq_queue *crq = &adapter->crq;
4560 struct device *dev = &adapter->vdev->dev;
4561 struct vio_dev *vdev = adapter->vdev;
4562 int rc;
4563
4564 /* Close the CRQ */
4565 do {
4566 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4567 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4568
4569 /* Clean out the queue */
4570 memset(crq->msgs, 0, PAGE_SIZE);
4571 crq->cur = 0;
Thomas Falcon51536982018-05-23 13:37:56 -05004572 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004573
4574 /* And re-open it again */
4575 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4576 crq->msg_token, PAGE_SIZE);
4577
4578 if (rc == H_CLOSED)
4579 /* Adapter is good, but other end is not ready */
4580 dev_warn(dev, "Partner adapter not ready\n");
4581 else if (rc != 0)
4582 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4583
4584 return rc;
4585}
4586
Nathan Fontenotf9928872017-03-30 02:48:54 -04004587static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004588{
4589 struct ibmvnic_crq_queue *crq = &adapter->crq;
4590 struct vio_dev *vdev = adapter->vdev;
4591 long rc;
4592
Nathan Fontenotf9928872017-03-30 02:48:54 -04004593 if (!crq->msgs)
4594 return;
4595
Thomas Falcon032c5e82015-12-21 11:26:06 -06004596 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4597 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004598 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004599 do {
4600 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4601 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4602
4603 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4604 DMA_BIDIRECTIONAL);
4605 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004606 crq->msgs = NULL;
Thomas Falcon51536982018-05-23 13:37:56 -05004607 crq->active = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004608}
4609
Nathan Fontenotf9928872017-03-30 02:48:54 -04004610static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004611{
4612 struct ibmvnic_crq_queue *crq = &adapter->crq;
4613 struct device *dev = &adapter->vdev->dev;
4614 struct vio_dev *vdev = adapter->vdev;
4615 int rc, retrc = -ENOMEM;
4616
Nathan Fontenotf9928872017-03-30 02:48:54 -04004617 if (crq->msgs)
4618 return 0;
4619
Thomas Falcon032c5e82015-12-21 11:26:06 -06004620 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4621 /* Should we allocate more than one page? */
4622
4623 if (!crq->msgs)
4624 return -ENOMEM;
4625
4626 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4627 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4628 DMA_BIDIRECTIONAL);
4629 if (dma_mapping_error(dev, crq->msg_token))
4630 goto map_failed;
4631
4632 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4633 crq->msg_token, PAGE_SIZE);
4634
4635 if (rc == H_RESOURCE)
4636 /* maybe kexecing and resource is busy. try a reset */
4637 rc = ibmvnic_reset_crq(adapter);
4638 retrc = rc;
4639
4640 if (rc == H_CLOSED) {
4641 dev_warn(dev, "Partner adapter not ready\n");
4642 } else if (rc) {
4643 dev_warn(dev, "Error %d opening adapter\n", rc);
4644 goto reg_crq_failed;
4645 }
4646
4647 retrc = 0;
4648
Thomas Falcon6c267b32017-02-15 12:17:58 -06004649 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4650 (unsigned long)adapter);
4651
Thomas Falcon032c5e82015-12-21 11:26:06 -06004652 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4653 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4654 adapter);
4655 if (rc) {
4656 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4657 vdev->irq, rc);
4658 goto req_irq_failed;
4659 }
4660
4661 rc = vio_enable_interrupts(vdev);
4662 if (rc) {
4663 dev_err(dev, "Error %d enabling interrupts\n", rc);
4664 goto req_irq_failed;
4665 }
4666
4667 crq->cur = 0;
4668 spin_lock_init(&crq->lock);
4669
4670 return retrc;
4671
4672req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004673 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004674 do {
4675 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4676 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4677reg_crq_failed:
4678 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4679map_failed:
4680 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004681 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004682 return retrc;
4683}
4684
Thomas Falcon8a348452018-05-23 13:38:00 -05004685static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
John Allenf6ef6402017-03-17 17:13:42 -05004686{
4687 struct device *dev = &adapter->vdev->dev;
4688 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004689 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004690 int rc;
4691
John Allen017892c12017-05-26 10:30:19 -04004692 adapter->from_passive_init = false;
4693
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004694 old_num_rx_queues = adapter->req_rx_queues;
4695 old_num_tx_queues = adapter->req_tx_queues;
4696
John Allenf6ef6402017-03-17 17:13:42 -05004697 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004698 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004699 ibmvnic_send_crq_init(adapter);
4700 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4701 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004702 return -1;
4703 }
4704
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004705 if (adapter->init_done_rc) {
4706 release_crq_queue(adapter);
4707 return adapter->init_done_rc;
4708 }
4709
John Allen017892c12017-05-26 10:30:19 -04004710 if (adapter->from_passive_init) {
4711 adapter->state = VNIC_OPEN;
4712 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004713 return -1;
4714 }
4715
Nathan Fontenot30f79622018-04-06 18:37:06 -05004716 if (adapter->resetting && !adapter->wait_for_reset &&
4717 adapter->reset_reason != VNIC_RESET_MOBILITY) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004718 if (adapter->req_rx_queues != old_num_rx_queues ||
4719 adapter->req_tx_queues != old_num_tx_queues) {
4720 release_sub_crqs(adapter, 0);
4721 rc = init_sub_crqs(adapter);
4722 } else {
4723 rc = reset_sub_crq_queues(adapter);
4724 }
4725 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004726 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004727 }
4728
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004729 if (rc) {
4730 dev_err(dev, "Initialization of sub crqs failed\n");
4731 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004732 return rc;
4733 }
4734
4735 rc = init_sub_crq_irqs(adapter);
4736 if (rc) {
4737 dev_err(dev, "Failed to initialize sub crq irqs\n");
4738 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004739 }
4740
4741 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004742}
4743
Thomas Falcon8a348452018-05-23 13:38:00 -05004744static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4745{
4746 struct device *dev = &adapter->vdev->dev;
4747 unsigned long timeout = msecs_to_jiffies(30000);
4748 int rc;
4749
4750 adapter->from_passive_init = false;
4751
4752 init_completion(&adapter->init_done);
4753 adapter->init_done_rc = 0;
4754 ibmvnic_send_crq_init(adapter);
4755 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4756 dev_err(dev, "Initialization sequence timed out\n");
4757 return -1;
4758 }
4759
4760 if (adapter->init_done_rc) {
4761 release_crq_queue(adapter);
4762 return adapter->init_done_rc;
4763 }
4764
4765 if (adapter->from_passive_init) {
4766 adapter->state = VNIC_OPEN;
4767 adapter->from_passive_init = false;
4768 return -1;
4769 }
4770
4771 rc = init_sub_crqs(adapter);
4772 if (rc) {
4773 dev_err(dev, "Initialization of sub crqs failed\n");
4774 release_crq_queue(adapter);
4775 return rc;
4776 }
4777
4778 rc = init_sub_crq_irqs(adapter);
4779 if (rc) {
4780 dev_err(dev, "Failed to initialize sub crq irqs\n");
4781 release_crq_queue(adapter);
4782 }
4783
4784 return rc;
4785}
4786
Thomas Falcon40c9db82017-06-12 12:35:04 -05004787static struct device_attribute dev_attr_failover;
4788
Thomas Falcon032c5e82015-12-21 11:26:06 -06004789static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4790{
4791 struct ibmvnic_adapter *adapter;
4792 struct net_device *netdev;
4793 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004794 int rc;
4795
4796 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4797 dev->unit_address);
4798
4799 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4800 VETH_MAC_ADDR, NULL);
4801 if (!mac_addr_p) {
4802 dev_err(&dev->dev,
4803 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4804 __FILE__, __LINE__);
4805 return 0;
4806 }
4807
4808 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004809 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004810 if (!netdev)
4811 return -ENOMEM;
4812
4813 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004814 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004815 dev_set_drvdata(&dev->dev, netdev);
4816 adapter->vdev = dev;
4817 adapter->netdev = netdev;
4818
4819 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4820 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4821 netdev->irq = dev->irq;
4822 netdev->netdev_ops = &ibmvnic_netdev_ops;
4823 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4824 SET_NETDEV_DEV(netdev, &dev->dev);
4825
4826 spin_lock_init(&adapter->stats_lock);
4827
Nathan Fontenoted651a12017-05-03 14:04:38 -04004828 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4829 INIT_LIST_HEAD(&adapter->rwi_list);
Thomas Falcon6c5c7482018-12-10 15:22:22 -06004830 spin_lock_init(&adapter->rwi_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004831 adapter->resetting = false;
4832
John Allenc26eba02017-10-26 16:23:25 -05004833 adapter->mac_change_pending = false;
4834
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004835 do {
Nathan Fontenot30f79622018-04-06 18:37:06 -05004836 rc = init_crq_queue(adapter);
4837 if (rc) {
4838 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4839 rc);
4840 goto ibmvnic_init_fail;
4841 }
4842
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004843 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004844 if (rc && rc != EAGAIN)
4845 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004846 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004847
Thomas Falcon07184212018-05-16 15:49:05 -05004848 rc = init_stats_buffers(adapter);
4849 if (rc)
4850 goto ibmvnic_init_fail;
4851
4852 rc = init_stats_token(adapter);
4853 if (rc)
4854 goto ibmvnic_stats_fail;
4855
Thomas Falconf39f0d12017-02-14 10:22:59 -06004856 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004857 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4858 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004859
Thomas Falcon40c9db82017-06-12 12:35:04 -05004860 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004861 if (rc)
Thomas Falcon07184212018-05-16 15:49:05 -05004862 goto ibmvnic_dev_file_err;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004863
Mick Tarsele876a8a2017-09-28 13:53:18 -07004864 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004865 rc = register_netdev(netdev);
4866 if (rc) {
4867 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004868 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004869 }
4870 dev_info(&dev->dev, "ibmvnic registered\n");
4871
Nathan Fontenot90c80142017-05-03 14:04:32 -04004872 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004873
4874 adapter->wait_for_reset = false;
4875
Thomas Falcon032c5e82015-12-21 11:26:06 -06004876 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004877
4878ibmvnic_register_fail:
4879 device_remove_file(&dev->dev, &dev_attr_failover);
4880
Thomas Falcon07184212018-05-16 15:49:05 -05004881ibmvnic_dev_file_err:
4882 release_stats_token(adapter);
4883
4884ibmvnic_stats_fail:
4885 release_stats_buffers(adapter);
4886
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004887ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004888 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004889 release_crq_queue(adapter);
4890 free_netdev(netdev);
4891
4892 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004893}
4894
4895static int ibmvnic_remove(struct vio_dev *dev)
4896{
4897 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004898 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004899
Nathan Fontenot90c80142017-05-03 14:04:32 -04004900 adapter->state = VNIC_REMOVING;
Juliet Kima5681e22018-11-19 15:59:22 -06004901 rtnl_lock();
4902 unregister_netdevice(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004903
4904 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004905 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004906 release_crq_queue(adapter);
4907
Thomas Falcon53cc7722018-02-26 18:10:56 -06004908 release_stats_token(adapter);
4909 release_stats_buffers(adapter);
4910
Nathan Fontenot90c80142017-05-03 14:04:32 -04004911 adapter->state = VNIC_REMOVED;
4912
Juliet Kima5681e22018-11-19 15:59:22 -06004913 rtnl_unlock();
Thomas Falcon40c9db82017-06-12 12:35:04 -05004914 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004915 free_netdev(netdev);
4916 dev_set_drvdata(&dev->dev, NULL);
4917
4918 return 0;
4919}
4920
Thomas Falcon40c9db82017-06-12 12:35:04 -05004921static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4922 const char *buf, size_t count)
4923{
4924 struct net_device *netdev = dev_get_drvdata(dev);
4925 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4926 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4927 __be64 session_token;
4928 long rc;
4929
4930 if (!sysfs_streq(buf, "1"))
4931 return -EINVAL;
4932
4933 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4934 H_GET_SESSION_TOKEN, 0, 0, 0);
4935 if (rc) {
4936 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4937 rc);
4938 return -EINVAL;
4939 }
4940
4941 session_token = (__be64)retbuf[0];
4942 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4943 be64_to_cpu(session_token));
4944 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4945 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4946 if (rc) {
4947 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4948 rc);
4949 return -EINVAL;
4950 }
4951
4952 return count;
4953}
4954
Joe Perches6cbaefb2017-12-19 10:15:09 -08004955static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004956
Thomas Falcon032c5e82015-12-21 11:26:06 -06004957static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4958{
4959 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4960 struct ibmvnic_adapter *adapter;
4961 struct iommu_table *tbl;
4962 unsigned long ret = 0;
4963 int i;
4964
4965 tbl = get_iommu_table_base(&vdev->dev);
4966
4967 /* netdev inits at probe time along with the structures we need below*/
4968 if (!netdev)
4969 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4970
4971 adapter = netdev_priv(netdev);
4972
4973 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004974 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4975
4976 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4977 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4978
4979 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4980 i++)
4981 ret += adapter->rx_pool[i].size *
4982 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4983
4984 return ret;
4985}
4986
4987static int ibmvnic_resume(struct device *dev)
4988{
4989 struct net_device *netdev = dev_get_drvdata(dev);
4990 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004991
John Allencb89ba22017-06-19 11:27:53 -05004992 if (adapter->state != VNIC_OPEN)
4993 return 0;
4994
John Allena2488782017-07-24 13:26:06 -05004995 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004996
4997 return 0;
4998}
4999
Arvind Yadav8c37bc62017-08-17 18:52:54 +05305000static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06005001 {"network", "IBM,vnic"},
5002 {"", "" }
5003};
5004MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5005
5006static const struct dev_pm_ops ibmvnic_pm_ops = {
5007 .resume = ibmvnic_resume
5008};
5009
5010static struct vio_driver ibmvnic_driver = {
5011 .id_table = ibmvnic_device_table,
5012 .probe = ibmvnic_probe,
5013 .remove = ibmvnic_remove,
5014 .get_desired_dma = ibmvnic_get_desired_dma,
5015 .name = ibmvnic_driver_name,
5016 .pm = &ibmvnic_pm_ops,
5017};
5018
5019/* module functions */
5020static int __init ibmvnic_module_init(void)
5021{
5022 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5023 IBMVNIC_DRIVER_VERSION);
5024
5025 return vio_register_driver(&ibmvnic_driver);
5026}
5027
5028static void __exit ibmvnic_module_exit(void)
5029{
5030 vio_unregister_driver(&ibmvnic_driver);
5031}
5032
5033module_init(ibmvnic_module_init);
5034module_exit(ibmvnic_module_exit);