blob: 7ed87fbb156b66d17ee9243a1e502a0a2c6e4be6 [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);
112static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113static void 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 *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400119static void release_crq_queue(struct ibmvnic_adapter *);
John Allenc26eba02017-10-26 16:23:25 -0500120static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600121
122struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
124 int offset;
125};
126
127#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
130
131static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
154};
155
156static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
158 unsigned long *irq)
159{
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
161 long rc;
162
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
164 *number = retbuf[0];
165 *irq = retbuf[1];
166
167 return rc;
168}
169
Thomas Falcon032c5e82015-12-21 11:26:06 -0600170static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
172{
173 struct device *dev = &adapter->vdev->dev;
174
175 ltb->size = size;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
177 GFP_KERNEL);
178
179 if (!ltb->buff) {
180 dev_err(dev, "Couldn't alloc long term buffer\n");
181 return -ENOMEM;
182 }
183 ltb->map_id = adapter->map_id;
184 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500185
186 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600189 wait_for_completion(&adapter->fw_done);
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500190
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
194 return -1;
195 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600196 return 0;
197}
198
199static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
201{
202 struct device *dev = &adapter->vdev->dev;
203
Nathan Fontenotc657e322017-03-30 02:49:06 -0400204 if (!ltb->buff)
205 return;
206
Nathan Fontenoted651a12017-05-03 14:04:38 -0400207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
Thomas Falcondfad09a2016-08-18 11:37:51 -0500209 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600211}
212
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500213static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
215{
216 memset(ltb->buff, 0, ltb->size);
217
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
221
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
227 }
228 return 0;
229}
230
Thomas Falconf185a492017-05-26 10:30:48 -0400231static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
232{
233 int i;
234
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
236 i++)
237 adapter->rx_pool[i].active = 0;
238}
239
Thomas Falcon032c5e82015-12-21 11:26:06 -0600240static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
242{
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
248 struct sk_buff *skb;
249 unsigned int offset;
250 dma_addr_t dma_addr;
251 unsigned char *dst;
252 u64 *handle_array;
253 int shift = 0;
254 int index;
255 int i;
256
Thomas Falconf185a492017-05-26 10:30:48 -0400257 if (!pool->active)
258 return;
259
Thomas Falcon032c5e82015-12-21 11:26:06 -0600260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
262 off_rxadd_subcrqs));
263
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
266 if (!skb) {
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
269 break;
270 }
271
272 index = pool->free_map[pool->next_free];
273
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
276
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
283
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
289
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
296
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
300 * truncated.
301 */
302#ifdef __LITTLE_ENDIAN__
303 shift = 8;
304#endif
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
306
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
308 &sub_crq);
309 if (lpar_rc != H_SUCCESS)
310 goto failure;
311
312 buffers_added++;
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
315 }
316 atomic_add(buffers_added, &pool->available);
317 return;
318
319failure:
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
325 DMA_FROM_DEVICE);
326
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400330
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
335 */
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
338 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600339}
340
341static void replenish_pools(struct ibmvnic_adapter *adapter)
342{
343 int i;
344
Thomas Falcon032c5e82015-12-21 11:26:06 -0600345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
347 i++) {
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
350 }
351}
352
John Allen3d52b592017-08-02 16:44:14 -0500353static void release_stats_buffers(struct ibmvnic_adapter *adapter)
354{
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
John Allen3d52b592017-08-02 16:44:14 -0500359}
360
361static int init_stats_buffers(struct ibmvnic_adapter *adapter)
362{
363 adapter->tx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600364 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500365 sizeof(struct ibmvnic_tx_queue_stats),
366 GFP_KERNEL);
367 if (!adapter->tx_stats_buffers)
368 return -ENOMEM;
369
370 adapter->rx_stats_buffers =
Nathan Fontenotabcae542018-02-19 13:30:47 -0600371 kcalloc(IBMVNIC_MAX_QUEUES,
John Allen3d52b592017-08-02 16:44:14 -0500372 sizeof(struct ibmvnic_rx_queue_stats),
373 GFP_KERNEL);
374 if (!adapter->rx_stats_buffers)
375 return -ENOMEM;
376
377 return 0;
378}
379
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400380static void release_stats_token(struct ibmvnic_adapter *adapter)
381{
382 struct device *dev = &adapter->vdev->dev;
383
384 if (!adapter->stats_token)
385 return;
386
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
389 DMA_FROM_DEVICE);
390 adapter->stats_token = 0;
391}
392
393static int init_stats_token(struct ibmvnic_adapter *adapter)
394{
395 struct device *dev = &adapter->vdev->dev;
396 dma_addr_t stok;
397
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
400 DMA_FROM_DEVICE);
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
403 return -1;
404 }
405
406 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400408 return 0;
409}
410
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400411static int reset_rx_pools(struct ibmvnic_adapter *adapter)
412{
413 struct ibmvnic_rx_pool *rx_pool;
414 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500415 int i, j, rc;
John Allen896d8692018-01-18 16:26:31 -0600416 u64 *size_array;
417
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400420
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
424
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
426
John Allen896d8692018-01-18 16:26:31 -0600427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
431 rx_pool->size *
432 rx_pool->buff_size);
433 } else {
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
436 }
437
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500438 if (rc)
439 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400440
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
443
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
446
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500450 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400451 }
452
453 return 0;
454}
455
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400456static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600457{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400458 struct ibmvnic_rx_pool *rx_pool;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400459 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600460
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400461 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600462 return;
463
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400465 rx_pool = &adapter->rx_pool[i];
466
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
468
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
471
472 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400473 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400474
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
479 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600480 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400481
482 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600483 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400484
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600487 adapter->num_active_rx_pools = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400488}
489
490static int init_rx_pools(struct net_device *netdev)
491{
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
495 int rxadd_subcrqs;
496 u64 *size_array;
497 int i, j;
498
499 rxadd_subcrqs =
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
503
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
506 GFP_KERNEL);
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
509 return -1;
510 }
511
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600512 adapter->num_active_rx_pools = rxadd_subcrqs;
513
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
516
517 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
521
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
523 rx_pool->index = i;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
525 rx_pool->active = 1;
526
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
528 GFP_KERNEL);
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
531 return -1;
532 }
533
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
536 GFP_KERNEL);
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
540 return -1;
541 }
542
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
546 return -1;
547 }
548
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
551
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
555 }
556
557 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600558}
559
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400560static int reset_tx_pools(struct ibmvnic_adapter *adapter)
561{
562 struct ibmvnic_tx_pool *tx_pool;
563 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500564 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400565
566 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
567 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500568 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
569
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400570 tx_pool = &adapter->tx_pool[i];
571
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500572 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
573 if (rc)
574 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400575
Thomas Falconfdb06102017-10-17 12:36:55 -0500576 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
577 if (rc)
578 return rc;
579
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400580 memset(tx_pool->tx_buff, 0,
581 adapter->req_tx_entries_per_subcrq *
582 sizeof(struct ibmvnic_tx_buff));
583
584 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
585 tx_pool->free_map[j] = j;
586
587 tx_pool->consumer_index = 0;
588 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500589 tx_pool->tso_index = 0;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400590 }
591
592 return 0;
593}
594
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200595static void release_vpd_data(struct ibmvnic_adapter *adapter)
596{
597 if (!adapter->vpd)
598 return;
599
600 kfree(adapter->vpd->buff);
601 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600602
603 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200604}
605
Nathan Fontenotc657e322017-03-30 02:49:06 -0400606static void release_tx_pools(struct ibmvnic_adapter *adapter)
607{
608 struct ibmvnic_tx_pool *tx_pool;
John Allen896d8692018-01-18 16:26:31 -0600609 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400610
611 if (!adapter->tx_pool)
612 return;
613
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600614 for (i = 0; i < adapter->num_active_tx_pools; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500615 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400616 tx_pool = &adapter->tx_pool[i];
617 kfree(tx_pool->tx_buff);
618 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500619 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400620 kfree(tx_pool->free_map);
621 }
622
623 kfree(adapter->tx_pool);
624 adapter->tx_pool = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600625 adapter->num_active_tx_pools = 0;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400626}
627
628static int init_tx_pools(struct net_device *netdev)
629{
630 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
631 struct device *dev = &adapter->vdev->dev;
632 struct ibmvnic_tx_pool *tx_pool;
633 int tx_subcrqs;
634 int i, j;
635
636 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
637 adapter->tx_pool = kcalloc(tx_subcrqs,
638 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
639 if (!adapter->tx_pool)
640 return -1;
641
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600642 adapter->num_active_tx_pools = tx_subcrqs;
643
Nathan Fontenotc657e322017-03-30 02:49:06 -0400644 for (i = 0; i < tx_subcrqs; i++) {
645 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500646
647 netdev_dbg(adapter->netdev,
648 "Initializing tx_pool[%d], %lld buffs\n",
649 i, adapter->req_tx_entries_per_subcrq);
650
Nathan Fontenotc657e322017-03-30 02:49:06 -0400651 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
652 sizeof(struct ibmvnic_tx_buff),
653 GFP_KERNEL);
654 if (!tx_pool->tx_buff) {
655 dev_err(dev, "tx pool buffer allocation failed\n");
656 release_tx_pools(adapter);
657 return -1;
658 }
659
660 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
661 adapter->req_tx_entries_per_subcrq *
Thomas Falcon8dff66c2018-03-12 11:51:03 -0500662 (adapter->req_mtu + VLAN_HLEN))) {
Nathan Fontenotc657e322017-03-30 02:49:06 -0400663 release_tx_pools(adapter);
664 return -1;
665 }
666
Thomas Falconfdb06102017-10-17 12:36:55 -0500667 /* alloc TSO ltb */
668 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
669 IBMVNIC_TSO_BUFS *
670 IBMVNIC_TSO_BUF_SZ)) {
671 release_tx_pools(adapter);
672 return -1;
673 }
674
675 tx_pool->tso_index = 0;
676
Nathan Fontenotc657e322017-03-30 02:49:06 -0400677 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
678 sizeof(int), GFP_KERNEL);
679 if (!tx_pool->free_map) {
680 release_tx_pools(adapter);
681 return -1;
682 }
683
684 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
685 tx_pool->free_map[j] = j;
686
687 tx_pool->consumer_index = 0;
688 tx_pool->producer_index = 0;
689 }
690
691 return 0;
692}
693
Nathan Fontenot661a2622017-04-19 13:44:58 -0400694static void release_error_buffers(struct ibmvnic_adapter *adapter)
695{
696 struct device *dev = &adapter->vdev->dev;
697 struct ibmvnic_error_buff *error_buff, *tmp;
698 unsigned long flags;
699
700 spin_lock_irqsave(&adapter->error_list_lock, flags);
701 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
702 list_del(&error_buff->list);
703 dma_unmap_single(dev, error_buff->dma, error_buff->len,
704 DMA_FROM_DEVICE);
705 kfree(error_buff->buff);
706 kfree(error_buff);
707 }
708 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
709}
710
John Allend944c3d62017-05-26 10:30:13 -0400711static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
712{
713 int i;
714
715 if (adapter->napi_enabled)
716 return;
717
718 for (i = 0; i < adapter->req_rx_queues; i++)
719 napi_enable(&adapter->napi[i]);
720
721 adapter->napi_enabled = true;
722}
723
724static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
725{
726 int i;
727
728 if (!adapter->napi_enabled)
729 return;
730
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500731 for (i = 0; i < adapter->req_rx_queues; i++) {
732 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400733 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500734 }
John Allend944c3d62017-05-26 10:30:13 -0400735
736 adapter->napi_enabled = false;
737}
738
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600739static int init_napi(struct ibmvnic_adapter *adapter)
740{
741 int i;
742
743 adapter->napi = kcalloc(adapter->req_rx_queues,
744 sizeof(struct napi_struct), GFP_KERNEL);
745 if (!adapter->napi)
746 return -ENOMEM;
747
748 for (i = 0; i < adapter->req_rx_queues; i++) {
749 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
750 netif_napi_add(adapter->netdev, &adapter->napi[i],
751 ibmvnic_poll, NAPI_POLL_WEIGHT);
752 }
753
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600754 adapter->num_active_rx_napi = adapter->req_rx_queues;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600755 return 0;
756}
757
758static void release_napi(struct ibmvnic_adapter *adapter)
759{
760 int i;
761
762 if (!adapter->napi)
763 return;
764
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600765 for (i = 0; i < adapter->num_active_rx_napi; i++) {
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600766 if (&adapter->napi[i]) {
767 netdev_dbg(adapter->netdev,
768 "Releasing napi[%d]\n", i);
769 netif_napi_del(&adapter->napi[i]);
770 }
771 }
772
773 kfree(adapter->napi);
774 adapter->napi = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -0600775 adapter->num_active_rx_napi = 0;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600776}
777
John Allena57a5d22017-03-17 17:13:41 -0500778static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600779{
780 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500781 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600782 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500783 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600784
John Allenbd0b6722017-03-17 17:13:40 -0500785 do {
786 if (adapter->renegotiate) {
787 adapter->renegotiate = false;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600788 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500789
790 reinit_completion(&adapter->init_done);
791 send_cap_queries(adapter);
792 if (!wait_for_completion_timeout(&adapter->init_done,
793 timeout)) {
794 dev_err(dev, "Capabilities query timeout\n");
795 return -1;
796 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500797 rc = init_sub_crqs(adapter);
798 if (rc) {
799 dev_err(dev,
800 "Initialization of SCRQ's failed\n");
801 return -1;
802 }
803 rc = init_sub_crq_irqs(adapter);
804 if (rc) {
805 dev_err(dev,
806 "Initialization of SCRQ's irqs failed\n");
807 return -1;
808 }
John Allenbd0b6722017-03-17 17:13:40 -0500809 }
810
811 reinit_completion(&adapter->init_done);
Thomas Falcon20a8ab72018-02-26 18:10:59 -0600812 rc = send_login(adapter);
813 if (rc) {
814 dev_err(dev, "Unable to attempt device login\n");
815 return rc;
816 } else if (!wait_for_completion_timeout(&adapter->init_done,
John Allenbd0b6722017-03-17 17:13:40 -0500817 timeout)) {
818 dev_err(dev, "Login timeout\n");
819 return -1;
820 }
821 } while (adapter->renegotiate);
822
Thomas Falcon3d166132018-01-10 19:39:52 -0600823 /* handle pending MAC address changes after successful login */
824 if (adapter->mac_change_pending) {
825 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
826 adapter->mac_change_pending = false;
827 }
828
John Allena57a5d22017-03-17 17:13:41 -0500829 return 0;
830}
831
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600832static void release_login_buffer(struct ibmvnic_adapter *adapter)
833{
834 kfree(adapter->login_buf);
835 adapter->login_buf = NULL;
836}
837
838static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
839{
840 kfree(adapter->login_rsp_buf);
841 adapter->login_rsp_buf = NULL;
842}
843
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400844static void release_resources(struct ibmvnic_adapter *adapter)
845{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200846 release_vpd_data(adapter);
847
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400848 release_tx_pools(adapter);
849 release_rx_pools(adapter);
850
Nathan Fontenot661a2622017-04-19 13:44:58 -0400851 release_error_buffers(adapter);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600852 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600853 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400854}
855
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400856static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
857{
858 struct net_device *netdev = adapter->netdev;
859 unsigned long timeout = msecs_to_jiffies(30000);
860 union ibmvnic_crq crq;
861 bool resend;
862 int rc;
863
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500864 netdev_dbg(netdev, "setting link state %d\n", link_state);
865
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400866 memset(&crq, 0, sizeof(crq));
867 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
868 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
869 crq.logical_link_state.link_state = link_state;
870
871 do {
872 resend = false;
873
874 reinit_completion(&adapter->init_done);
875 rc = ibmvnic_send_crq(adapter, &crq);
876 if (rc) {
877 netdev_err(netdev, "Failed to set link state\n");
878 return rc;
879 }
880
881 if (!wait_for_completion_timeout(&adapter->init_done,
882 timeout)) {
883 netdev_err(netdev, "timeout setting link state\n");
884 return -1;
885 }
886
887 if (adapter->init_done_rc == 1) {
888 /* Partuial success, delay and re-send */
889 mdelay(1000);
890 resend = true;
891 }
892 } while (resend);
893
894 return 0;
895}
896
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400897static int set_real_num_queues(struct net_device *netdev)
898{
899 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
900 int rc;
901
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500902 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
903 adapter->req_tx_queues, adapter->req_rx_queues);
904
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400905 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
906 if (rc) {
907 netdev_err(netdev, "failed to set the number of tx queues\n");
908 return rc;
909 }
910
911 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
912 if (rc)
913 netdev_err(netdev, "failed to set the number of rx queues\n");
914
915 return rc;
916}
917
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200918static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
919{
920 struct device *dev = &adapter->vdev->dev;
921 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200922 int len = 0;
923
924 if (adapter->vpd->buff)
925 len = adapter->vpd->len;
926
John Allen69d08dc2018-01-18 16:27:58 -0600927 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200928 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
929 crq.get_vpd_size.cmd = GET_VPD_SIZE;
930 ibmvnic_send_crq(adapter, &crq);
931 wait_for_completion(&adapter->fw_done);
932
933 if (!adapter->vpd->len)
934 return -ENODATA;
935
936 if (!adapter->vpd->buff)
937 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
938 else if (adapter->vpd->len != len)
939 adapter->vpd->buff =
940 krealloc(adapter->vpd->buff,
941 adapter->vpd->len, GFP_KERNEL);
942
943 if (!adapter->vpd->buff) {
944 dev_err(dev, "Could allocate VPD buffer\n");
945 return -ENOMEM;
946 }
947
948 adapter->vpd->dma_addr =
949 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
950 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200951 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200952 dev_err(dev, "Could not map VPD buffer\n");
953 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600954 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200955 return -ENOMEM;
956 }
957
958 reinit_completion(&adapter->fw_done);
959 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
960 crq.get_vpd.cmd = GET_VPD;
961 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
962 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
963 ibmvnic_send_crq(adapter, &crq);
964 wait_for_completion(&adapter->fw_done);
965
966 return 0;
967}
968
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400969static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500970{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400971 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600972 int rc;
John Allena57a5d22017-03-17 17:13:41 -0500973
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400974 rc = set_real_num_queues(netdev);
975 if (rc)
976 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500977
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200978 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
979 if (!adapter->vpd)
980 return -ENOMEM;
981
John Allen69d08dc2018-01-18 16:27:58 -0600982 /* Vital Product Data (VPD) */
983 rc = ibmvnic_get_vpd(adapter);
984 if (rc) {
985 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
986 return rc;
987 }
988
Thomas Falcon032c5e82015-12-21 11:26:06 -0600989 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400990
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600991 rc = init_napi(adapter);
992 if (rc)
993 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600994
Thomas Falcon032c5e82015-12-21 11:26:06 -0600995 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400996
997 rc = init_rx_pools(netdev);
998 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400999 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001000
Nathan Fontenotc657e322017-03-30 02:49:06 -04001001 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001002 return rc;
1003}
1004
Nathan Fontenoted651a12017-05-03 14:04:38 -04001005static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001006{
1007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001008 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001009 int i, rc;
1010
Nathan Fontenot90c80142017-05-03 14:04:32 -04001011 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001012 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001013 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001014
Thomas Falcon032c5e82015-12-21 11:26:06 -06001015 /* We're ready to receive frames, enable the sub-crq interrupts and
1016 * set the logical link state to up
1017 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001018 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001019 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001020 if (prev_state == VNIC_CLOSED)
1021 enable_irq(adapter->rx_scrq[i]->irq);
1022 else
1023 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1024 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001025
Nathan Fontenoted651a12017-05-03 14:04:38 -04001026 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001027 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001028 if (prev_state == VNIC_CLOSED)
1029 enable_irq(adapter->tx_scrq[i]->irq);
1030 else
1031 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1032 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001033
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001034 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001035 if (rc) {
1036 for (i = 0; i < adapter->req_rx_queues; i++)
1037 napi_disable(&adapter->napi[i]);
1038 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001039 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001040 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001041
Nathan Fontenoted651a12017-05-03 14:04:38 -04001042 netif_tx_start_all_queues(netdev);
1043
1044 if (prev_state == VNIC_CLOSED) {
1045 for (i = 0; i < adapter->req_rx_queues; i++)
1046 napi_schedule(&adapter->napi[i]);
1047 }
1048
1049 adapter->state = VNIC_OPEN;
1050 return rc;
1051}
1052
1053static int ibmvnic_open(struct net_device *netdev)
1054{
1055 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001056 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001057
1058 mutex_lock(&adapter->reset_lock);
1059
1060 if (adapter->state != VNIC_CLOSED) {
1061 rc = ibmvnic_login(netdev);
1062 if (rc) {
1063 mutex_unlock(&adapter->reset_lock);
1064 return rc;
1065 }
1066
1067 rc = init_resources(adapter);
1068 if (rc) {
1069 netdev_err(netdev, "failed to initialize resources\n");
1070 release_resources(adapter);
1071 mutex_unlock(&adapter->reset_lock);
1072 return rc;
1073 }
1074 }
1075
1076 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001077 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001078
Nathan Fontenoted651a12017-05-03 14:04:38 -04001079 mutex_unlock(&adapter->reset_lock);
1080
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001081 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001082}
1083
Thomas Falcond0869c02018-02-13 18:23:43 -06001084static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1085{
1086 struct ibmvnic_rx_pool *rx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001087 struct ibmvnic_rx_buff *rx_buff;
Thomas Falcond0869c02018-02-13 18:23:43 -06001088 u64 rx_entries;
1089 int rx_scrqs;
1090 int i, j;
1091
1092 if (!adapter->rx_pool)
1093 return;
1094
1095 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1096 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1097
1098 /* Free any remaining skbs in the rx buffer pools */
1099 for (i = 0; i < rx_scrqs; i++) {
1100 rx_pool = &adapter->rx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001101 if (!rx_pool || !rx_pool->rx_buff)
Thomas Falcond0869c02018-02-13 18:23:43 -06001102 continue;
1103
1104 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1105 for (j = 0; j < rx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001106 rx_buff = &rx_pool->rx_buff[j];
1107 if (rx_buff && rx_buff->skb) {
1108 dev_kfree_skb_any(rx_buff->skb);
1109 rx_buff->skb = NULL;
Thomas Falcond0869c02018-02-13 18:23:43 -06001110 }
1111 }
1112 }
1113}
1114
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001115static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1116{
1117 struct ibmvnic_tx_pool *tx_pool;
Thomas Falcon637f81d2018-02-26 18:10:57 -06001118 struct ibmvnic_tx_buff *tx_buff;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001119 u64 tx_entries;
1120 int tx_scrqs;
1121 int i, j;
1122
1123 if (!adapter->tx_pool)
1124 return;
1125
1126 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1127 tx_entries = adapter->req_tx_entries_per_subcrq;
1128
1129 /* Free any remaining skbs in the tx buffer pools */
1130 for (i = 0; i < tx_scrqs; i++) {
1131 tx_pool = &adapter->tx_pool[i];
Thomas Falcon637f81d2018-02-26 18:10:57 -06001132 if (!tx_pool && !tx_pool->tx_buff)
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001133 continue;
1134
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001135 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001136 for (j = 0; j < tx_entries; j++) {
Thomas Falcon637f81d2018-02-26 18:10:57 -06001137 tx_buff = &tx_pool->tx_buff[j];
1138 if (tx_buff && tx_buff->skb) {
1139 dev_kfree_skb_any(tx_buff->skb);
1140 tx_buff->skb = NULL;
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001141 }
1142 }
1143 }
1144}
1145
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001146static void ibmvnic_cleanup(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001147{
1148 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenea5509f2017-03-17 17:13:43 -05001149 int i;
1150
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001151 /* ensure that transmissions are stopped if called by do_reset */
1152 if (adapter->resetting)
1153 netif_tx_disable(netdev);
1154 else
1155 netif_tx_stop_all_queues(netdev);
1156
John Allend944c3d62017-05-26 10:30:13 -04001157 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001158
1159 if (adapter->tx_scrq) {
1160 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001161 if (adapter->tx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001162 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001163 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001164 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001165 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001166 }
1167
Nathan Fontenot46293b92017-05-03 14:05:02 -04001168 if (adapter->rx_scrq) {
1169 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001170 if (adapter->rx_scrq[i]->irq) {
Thomas Falconf8738662018-03-07 17:51:45 -06001171 netdev_dbg(netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001172 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001173 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001174 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001175 }
1176 }
Thomas Falcond0869c02018-02-13 18:23:43 -06001177 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001178 clean_tx_pools(adapter);
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001179}
1180
1181static int __ibmvnic_close(struct net_device *netdev)
1182{
1183 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1184 int rc = 0;
1185
1186 adapter->state = VNIC_CLOSING;
1187 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1188 if (rc)
1189 return rc;
1190 ibmvnic_cleanup(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001191 adapter->state = VNIC_CLOSED;
Thomas Falcon01d9bd72018-03-07 17:51:46 -06001192 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001193}
1194
Nathan Fontenoted651a12017-05-03 14:04:38 -04001195static int ibmvnic_close(struct net_device *netdev)
1196{
1197 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1198 int rc;
1199
1200 mutex_lock(&adapter->reset_lock);
1201 rc = __ibmvnic_close(netdev);
1202 mutex_unlock(&adapter->reset_lock);
1203
1204 return rc;
1205}
1206
Thomas Falconad7775d2016-04-01 17:20:34 -05001207/**
1208 * build_hdr_data - creates L2/L3/L4 header data buffer
1209 * @hdr_field - bitfield determining needed headers
1210 * @skb - socket buffer
1211 * @hdr_len - array of header lengths
1212 * @tot_len - total length of data
1213 *
1214 * Reads hdr_field to determine which headers are needed by firmware.
1215 * Builds a buffer containing these headers. Saves individual header
1216 * lengths and total buffer length to be used to build descriptors.
1217 */
1218static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1219 int *hdr_len, u8 *hdr_data)
1220{
1221 int len = 0;
1222 u8 *hdr;
1223
Thomas Falconda75e3b2018-03-12 11:51:02 -05001224 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1225 hdr_len[0] = sizeof(struct vlan_ethhdr);
1226 else
1227 hdr_len[0] = sizeof(struct ethhdr);
Thomas Falconad7775d2016-04-01 17:20:34 -05001228
1229 if (skb->protocol == htons(ETH_P_IP)) {
1230 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1231 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1232 hdr_len[2] = tcp_hdrlen(skb);
1233 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1234 hdr_len[2] = sizeof(struct udphdr);
1235 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1236 hdr_len[1] = sizeof(struct ipv6hdr);
1237 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1238 hdr_len[2] = tcp_hdrlen(skb);
1239 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1240 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001241 } else if (skb->protocol == htons(ETH_P_ARP)) {
1242 hdr_len[1] = arp_hdr_len(skb->dev);
1243 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001244 }
1245
1246 memset(hdr_data, 0, 120);
1247 if ((hdr_field >> 6) & 1) {
1248 hdr = skb_mac_header(skb);
1249 memcpy(hdr_data, hdr, hdr_len[0]);
1250 len += hdr_len[0];
1251 }
1252
1253 if ((hdr_field >> 5) & 1) {
1254 hdr = skb_network_header(skb);
1255 memcpy(hdr_data + len, hdr, hdr_len[1]);
1256 len += hdr_len[1];
1257 }
1258
1259 if ((hdr_field >> 4) & 1) {
1260 hdr = skb_transport_header(skb);
1261 memcpy(hdr_data + len, hdr, hdr_len[2]);
1262 len += hdr_len[2];
1263 }
1264 return len;
1265}
1266
1267/**
1268 * create_hdr_descs - create header and header extension descriptors
1269 * @hdr_field - bitfield determining needed headers
1270 * @data - buffer containing header data
1271 * @len - length of data buffer
1272 * @hdr_len - array of individual header lengths
1273 * @scrq_arr - descriptor array
1274 *
1275 * Creates header and, if needed, header extension descriptors and
1276 * places them in a descriptor array, scrq_arr
1277 */
1278
Thomas Falcon2de09682017-10-16 10:02:11 -05001279static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1280 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001281{
1282 union sub_crq hdr_desc;
1283 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001284 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001285 u8 *data, *cur;
1286 int tmp;
1287
1288 while (tmp_len > 0) {
1289 cur = hdr_data + len - tmp_len;
1290
1291 memset(&hdr_desc, 0, sizeof(hdr_desc));
1292 if (cur != hdr_data) {
1293 data = hdr_desc.hdr_ext.data;
1294 tmp = tmp_len > 29 ? 29 : tmp_len;
1295 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1296 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1297 hdr_desc.hdr_ext.len = tmp;
1298 } else {
1299 data = hdr_desc.hdr.data;
1300 tmp = tmp_len > 24 ? 24 : tmp_len;
1301 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1302 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1303 hdr_desc.hdr.len = tmp;
1304 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1305 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1306 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1307 hdr_desc.hdr.flag = hdr_field << 1;
1308 }
1309 memcpy(data, cur, tmp);
1310 tmp_len -= tmp;
1311 *scrq_arr = hdr_desc;
1312 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001313 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001314 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001315
1316 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001317}
1318
1319/**
1320 * build_hdr_descs_arr - build a header descriptor array
1321 * @skb - socket buffer
1322 * @num_entries - number of descriptors to be sent
1323 * @subcrq - first TX descriptor
1324 * @hdr_field - bit field determining which headers will be sent
1325 *
1326 * This function will build a TX descriptor array with applicable
1327 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1328 */
1329
1330static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1331 int *num_entries, u8 hdr_field)
1332{
1333 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001334 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001335 u8 *hdr_data = txbuff->hdr_data;
1336
1337 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1338 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001339 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001340 txbuff->indir_arr + 1);
1341}
1342
Thomas Falcon1f247a62018-03-12 11:51:04 -05001343static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1344 struct net_device *netdev)
1345{
1346 /* For some backing devices, mishandling of small packets
1347 * can result in a loss of connection or TX stall. Device
1348 * architects recommend that no packet should be smaller
1349 * than the minimum MTU value provided to the driver, so
1350 * pad any packets to that length
1351 */
1352 if (skb->len < netdev->min_mtu)
1353 return skb_put_padto(skb, netdev->min_mtu);
1354}
1355
Thomas Falcon032c5e82015-12-21 11:26:06 -06001356static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1357{
1358 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1359 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001360 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001361 struct device *dev = &adapter->vdev->dev;
1362 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001363 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001364 struct ibmvnic_tx_pool *tx_pool;
1365 unsigned int tx_send_failed = 0;
1366 unsigned int tx_map_failed = 0;
1367 unsigned int tx_dropped = 0;
1368 unsigned int tx_packets = 0;
1369 unsigned int tx_bytes = 0;
1370 dma_addr_t data_dma_addr;
1371 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001372 unsigned long lpar_rc;
1373 union sub_crq tx_crq;
1374 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001375 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001376 unsigned char *dst;
1377 u64 *handle_array;
1378 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001379 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001380 int ret = 0;
1381
Nathan Fontenoted651a12017-05-03 14:04:38 -04001382 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001383 if (!netif_subqueue_stopped(netdev, skb))
1384 netif_stop_subqueue(netdev, queue_num);
1385 dev_kfree_skb_any(skb);
1386
Thomas Falcon032c5e82015-12-21 11:26:06 -06001387 tx_send_failed++;
1388 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001389 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001390 goto out;
1391 }
1392
Thomas Falcon1f247a62018-03-12 11:51:04 -05001393 if (ibmvnic_xmit_workarounds(skb, adapter)) {
1394 tx_dropped++;
1395 tx_send_failed++;
1396 ret = NETDEV_TX_OK;
1397 goto out;
1398 }
1399
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001400 tx_pool = &adapter->tx_pool[queue_num];
1401 tx_scrq = adapter->tx_scrq[queue_num];
1402 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1403 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1404 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1405
Thomas Falcon032c5e82015-12-21 11:26:06 -06001406 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001407
1408 if (skb_is_gso(skb)) {
1409 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1410 dst = tx_pool->tso_ltb.buff + offset;
1411 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1412 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1413 tx_pool->tso_index++;
1414 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1415 tx_pool->tso_index = 0;
1416 } else {
Thomas Falcon8dff66c2018-03-12 11:51:03 -05001417 offset = index * (adapter->req_mtu + VLAN_HLEN);
Thomas Falconfdb06102017-10-17 12:36:55 -05001418 dst = tx_pool->long_term_buff.buff + offset;
Thomas Falcon8dff66c2018-03-12 11:51:03 -05001419 memset(dst, 0, adapter->req_mtu + VLAN_HLEN);
Thomas Falconfdb06102017-10-17 12:36:55 -05001420 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1421 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001422
Thomas Falcon15482052017-10-17 12:36:54 -05001423 if (skb_shinfo(skb)->nr_frags) {
1424 int cur, i;
1425
1426 /* Copy the head */
1427 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1428 cur = skb_headlen(skb);
1429
1430 /* Copy the frags */
1431 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1432 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1433
1434 memcpy(dst + cur,
1435 page_address(skb_frag_page(frag)) +
1436 frag->page_offset, skb_frag_size(frag));
1437 cur += skb_frag_size(frag);
1438 }
1439 } else {
1440 skb_copy_from_linear_data(skb, dst, skb->len);
1441 }
1442
Thomas Falcon032c5e82015-12-21 11:26:06 -06001443 tx_pool->consumer_index =
1444 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001445 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001446
1447 tx_buff = &tx_pool->tx_buff[index];
1448 tx_buff->skb = skb;
1449 tx_buff->data_dma[0] = data_dma_addr;
1450 tx_buff->data_len[0] = skb->len;
1451 tx_buff->index = index;
1452 tx_buff->pool_index = queue_num;
1453 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001454
1455 memset(&tx_crq, 0, sizeof(tx_crq));
1456 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1457 tx_crq.v1.type = IBMVNIC_TX_DESC;
1458 tx_crq.v1.n_crq_elem = 1;
1459 tx_crq.v1.n_sge = 1;
1460 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1461 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001462 if (skb_is_gso(skb))
1463 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1464 else
1465 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001466 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1467 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1468
1469 if (adapter->vlan_header_insertion) {
1470 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1471 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1472 }
1473
1474 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001475 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1476 proto = ip_hdr(skb)->protocol;
1477 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1478 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1479 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001480 }
1481
Thomas Falcona0dca102018-01-18 19:29:48 -06001482 if (proto == IPPROTO_TCP)
1483 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1484 else if (proto == IPPROTO_UDP)
1485 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1486
Thomas Falconad7775d2016-04-01 17:20:34 -05001487 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001488 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001489 hdrs += 2;
1490 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001491 if (skb_is_gso(skb)) {
1492 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1493 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1494 hdrs += 2;
1495 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001496 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001497 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001498 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1499 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001500 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001501 tx_buff->indir_arr[0] = tx_crq;
1502 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1503 sizeof(tx_buff->indir_arr),
1504 DMA_TO_DEVICE);
1505 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001506 dev_kfree_skb_any(skb);
1507 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001508 if (!firmware_has_feature(FW_FEATURE_CMO))
1509 dev_err(dev, "tx: unable to map descriptor array\n");
1510 tx_map_failed++;
1511 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001512 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001513 goto out;
1514 }
John Allen498cd8e2016-04-06 11:49:55 -05001515 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001516 (u64)tx_buff->indir_dma,
1517 (u64)num_entries);
1518 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001519 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001520 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1521 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001522 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001523 if (lpar_rc != H_SUCCESS) {
1524 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1525
1526 if (tx_pool->consumer_index == 0)
1527 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001528 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001529 else
1530 tx_pool->consumer_index--;
1531
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001532 dev_kfree_skb_any(skb);
1533 tx_buff->skb = NULL;
1534
Thomas Falconb8c80b82017-05-26 10:30:42 -04001535 if (lpar_rc == H_CLOSED) {
1536 /* Disable TX and report carrier off if queue is closed.
1537 * Firmware guarantees that a signal will be sent to the
1538 * driver, triggering a reset or some other action.
1539 */
1540 netif_tx_stop_all_queues(netdev);
1541 netif_carrier_off(netdev);
1542 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001543
Thomas Falcon032c5e82015-12-21 11:26:06 -06001544 tx_send_failed++;
1545 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001546 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001547 goto out;
1548 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001549
Thomas Falconffc385b2018-02-18 10:08:41 -06001550 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001551 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001552 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001553 netif_stop_subqueue(netdev, queue_num);
1554 }
1555
Thomas Falcon032c5e82015-12-21 11:26:06 -06001556 tx_packets++;
1557 tx_bytes += skb->len;
1558 txq->trans_start = jiffies;
1559 ret = NETDEV_TX_OK;
1560
1561out:
1562 netdev->stats.tx_dropped += tx_dropped;
1563 netdev->stats.tx_bytes += tx_bytes;
1564 netdev->stats.tx_packets += tx_packets;
1565 adapter->tx_send_failed += tx_send_failed;
1566 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001567 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1568 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1569 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001570
1571 return ret;
1572}
1573
1574static void ibmvnic_set_multi(struct net_device *netdev)
1575{
1576 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1577 struct netdev_hw_addr *ha;
1578 union ibmvnic_crq crq;
1579
1580 memset(&crq, 0, sizeof(crq));
1581 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1582 crq.request_capability.cmd = REQUEST_CAPABILITY;
1583
1584 if (netdev->flags & IFF_PROMISC) {
1585 if (!adapter->promisc_supported)
1586 return;
1587 } else {
1588 if (netdev->flags & IFF_ALLMULTI) {
1589 /* Accept all multicast */
1590 memset(&crq, 0, sizeof(crq));
1591 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1592 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1593 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1594 ibmvnic_send_crq(adapter, &crq);
1595 } else if (netdev_mc_empty(netdev)) {
1596 /* Reject all multicast */
1597 memset(&crq, 0, sizeof(crq));
1598 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1599 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1600 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1601 ibmvnic_send_crq(adapter, &crq);
1602 } else {
1603 /* Accept one or more multicast(s) */
1604 netdev_for_each_mc_addr(ha, netdev) {
1605 memset(&crq, 0, sizeof(crq));
1606 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1607 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1608 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1609 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1610 ha->addr);
1611 ibmvnic_send_crq(adapter, &crq);
1612 }
1613 }
1614 }
1615}
1616
John Allenc26eba02017-10-26 16:23:25 -05001617static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001618{
1619 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1620 struct sockaddr *addr = p;
1621 union ibmvnic_crq crq;
1622
1623 if (!is_valid_ether_addr(addr->sa_data))
1624 return -EADDRNOTAVAIL;
1625
1626 memset(&crq, 0, sizeof(crq));
1627 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1628 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1629 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001630
1631 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001632 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001633 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001634 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001635 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001636}
1637
John Allenc26eba02017-10-26 16:23:25 -05001638static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1639{
1640 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1641 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001642 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001643
Thomas Falcon3d166132018-01-10 19:39:52 -06001644 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001645 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1646 adapter->mac_change_pending = true;
1647 return 0;
1648 }
1649
Thomas Falconf8136142018-01-29 13:45:05 -06001650 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001651
Thomas Falconf8136142018-01-29 13:45:05 -06001652 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001653}
1654
Nathan Fontenoted651a12017-05-03 14:04:38 -04001655/**
1656 * do_reset returns zero if we are able to keep processing reset events, or
1657 * non-zero if we hit a fatal error and must halt.
1658 */
1659static int do_reset(struct ibmvnic_adapter *adapter,
1660 struct ibmvnic_rwi *rwi, u32 reset_state)
1661{
John Allen896d8692018-01-18 16:26:31 -06001662 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001663 struct net_device *netdev = adapter->netdev;
1664 int i, rc;
1665
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001666 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1667 rwi->reset_reason);
1668
Nathan Fontenoted651a12017-05-03 14:04:38 -04001669 netif_carrier_off(netdev);
1670 adapter->reset_reason = rwi->reset_reason;
1671
John Allen896d8692018-01-18 16:26:31 -06001672 old_num_rx_queues = adapter->req_rx_queues;
1673 old_num_tx_queues = adapter->req_tx_queues;
1674
Nathan Fontenoted651a12017-05-03 14:04:38 -04001675 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1676 rc = ibmvnic_reenable_crq_queue(adapter);
1677 if (rc)
1678 return 0;
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001679 ibmvnic_cleanup(netdev);
1680 } else if (rwi->reset_reason == VNIC_RESET_FAILOVER) {
1681 ibmvnic_cleanup(netdev);
1682 } else {
1683 rc = __ibmvnic_close(netdev);
1684 if (rc)
1685 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001686 }
1687
John Allenc26eba02017-10-26 16:23:25 -05001688 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1689 adapter->wait_for_reset) {
1690 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001691 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001692 release_crq_queue(adapter);
1693 }
1694
John Allen8cb31cf2017-05-26 10:30:37 -04001695 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1696 /* remove the closed state so when we call open it appears
1697 * we are coming from the probed state.
1698 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001699 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001700
John Allen8cb31cf2017-05-26 10:30:37 -04001701 rc = ibmvnic_init(adapter);
1702 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001703 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001704
1705 /* If the adapter was in PROBE state prior to the reset,
1706 * exit here.
1707 */
1708 if (reset_state == VNIC_PROBED)
1709 return 0;
1710
1711 rc = ibmvnic_login(netdev);
1712 if (rc) {
1713 adapter->state = VNIC_PROBED;
1714 return 0;
1715 }
1716
John Allenc26eba02017-10-26 16:23:25 -05001717 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1718 adapter->wait_for_reset) {
1719 rc = init_resources(adapter);
1720 if (rc)
1721 return rc;
John Allen896d8692018-01-18 16:26:31 -06001722 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1723 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001724 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001725 release_rx_pools(adapter);
1726 release_tx_pools(adapter);
1727 init_rx_pools(netdev);
1728 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001729
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001730 release_napi(adapter);
1731 init_napi(adapter);
John Allenc26eba02017-10-26 16:23:25 -05001732 } else {
1733 rc = reset_tx_pools(adapter);
1734 if (rc)
1735 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001736
John Allenc26eba02017-10-26 16:23:25 -05001737 rc = reset_rx_pools(adapter);
1738 if (rc)
1739 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001740
John Allenc26eba02017-10-26 16:23:25 -05001741 if (reset_state == VNIC_CLOSED)
1742 return 0;
1743 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001744 }
1745
Nathan Fontenoted651a12017-05-03 14:04:38 -04001746 rc = __ibmvnic_open(netdev);
1747 if (rc) {
1748 if (list_empty(&adapter->rwi_list))
1749 adapter->state = VNIC_CLOSED;
1750 else
1751 adapter->state = reset_state;
1752
1753 return 0;
1754 }
1755
Nathan Fontenoted651a12017-05-03 14:04:38 -04001756 /* kick napi */
1757 for (i = 0; i < adapter->req_rx_queues; i++)
1758 napi_schedule(&adapter->napi[i]);
1759
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001760 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1761 netdev_notify_peers(netdev);
1762
Thomas Falconcc85c022018-02-13 15:32:50 -06001763 netif_carrier_on(netdev);
1764
Nathan Fontenoted651a12017-05-03 14:04:38 -04001765 return 0;
1766}
1767
1768static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1769{
1770 struct ibmvnic_rwi *rwi;
1771
1772 mutex_lock(&adapter->rwi_lock);
1773
1774 if (!list_empty(&adapter->rwi_list)) {
1775 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1776 list);
1777 list_del(&rwi->list);
1778 } else {
1779 rwi = NULL;
1780 }
1781
1782 mutex_unlock(&adapter->rwi_lock);
1783 return rwi;
1784}
1785
1786static void free_all_rwi(struct ibmvnic_adapter *adapter)
1787{
1788 struct ibmvnic_rwi *rwi;
1789
1790 rwi = get_next_rwi(adapter);
1791 while (rwi) {
1792 kfree(rwi);
1793 rwi = get_next_rwi(adapter);
1794 }
1795}
1796
1797static void __ibmvnic_reset(struct work_struct *work)
1798{
1799 struct ibmvnic_rwi *rwi;
1800 struct ibmvnic_adapter *adapter;
1801 struct net_device *netdev;
1802 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001803 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001804
1805 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1806 netdev = adapter->netdev;
1807
1808 mutex_lock(&adapter->reset_lock);
1809 adapter->resetting = true;
1810 reset_state = adapter->state;
1811
1812 rwi = get_next_rwi(adapter);
1813 while (rwi) {
1814 rc = do_reset(adapter, rwi, reset_state);
1815 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001816 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001817 break;
1818
1819 rwi = get_next_rwi(adapter);
1820 }
1821
John Allenc26eba02017-10-26 16:23:25 -05001822 if (adapter->wait_for_reset) {
1823 adapter->wait_for_reset = false;
1824 adapter->reset_done_rc = rc;
1825 complete(&adapter->reset_done);
1826 }
1827
Nathan Fontenoted651a12017-05-03 14:04:38 -04001828 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001829 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001830 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001831 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001832 return;
1833 }
1834
1835 adapter->resetting = false;
1836 mutex_unlock(&adapter->reset_lock);
1837}
1838
1839static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1840 enum ibmvnic_reset_reason reason)
1841{
1842 struct ibmvnic_rwi *rwi, *tmp;
1843 struct net_device *netdev = adapter->netdev;
1844 struct list_head *entry;
1845
1846 if (adapter->state == VNIC_REMOVING ||
1847 adapter->state == VNIC_REMOVED) {
1848 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1849 return;
1850 }
1851
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001852 if (adapter->state == VNIC_PROBING) {
1853 netdev_warn(netdev, "Adapter reset during probe\n");
1854 adapter->init_done_rc = EAGAIN;
1855 return;
1856 }
1857
Nathan Fontenoted651a12017-05-03 14:04:38 -04001858 mutex_lock(&adapter->rwi_lock);
1859
1860 list_for_each(entry, &adapter->rwi_list) {
1861 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1862 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001863 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001864 mutex_unlock(&adapter->rwi_lock);
1865 return;
1866 }
1867 }
1868
1869 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1870 if (!rwi) {
1871 mutex_unlock(&adapter->rwi_lock);
1872 ibmvnic_close(netdev);
1873 return;
1874 }
1875
1876 rwi->reset_reason = reason;
1877 list_add_tail(&rwi->list, &adapter->rwi_list);
1878 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001879
1880 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001881 schedule_work(&adapter->ibmvnic_reset);
1882}
1883
Thomas Falcon032c5e82015-12-21 11:26:06 -06001884static void ibmvnic_tx_timeout(struct net_device *dev)
1885{
1886 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001887
Nathan Fontenoted651a12017-05-03 14:04:38 -04001888 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001889}
1890
1891static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1892 struct ibmvnic_rx_buff *rx_buff)
1893{
1894 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1895
1896 rx_buff->skb = NULL;
1897
1898 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1899 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1900
1901 atomic_dec(&pool->available);
1902}
1903
1904static int ibmvnic_poll(struct napi_struct *napi, int budget)
1905{
1906 struct net_device *netdev = napi->dev;
1907 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1908 int scrq_num = (int)(napi - adapter->napi);
1909 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001910
Thomas Falcon032c5e82015-12-21 11:26:06 -06001911restart_poll:
1912 while (frames_processed < budget) {
1913 struct sk_buff *skb;
1914 struct ibmvnic_rx_buff *rx_buff;
1915 union sub_crq *next;
1916 u32 length;
1917 u16 offset;
1918 u8 flags = 0;
1919
John Allen34686562018-02-06 16:21:49 -06001920 if (unlikely(adapter->resetting &&
1921 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001922 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1923 napi_complete_done(napi, frames_processed);
1924 return frames_processed;
1925 }
1926
Thomas Falcon032c5e82015-12-21 11:26:06 -06001927 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1928 break;
1929 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1930 rx_buff =
1931 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1932 rx_comp.correlator);
1933 /* do error checking */
1934 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001935 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1936 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001937 /* free the entry */
1938 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001939 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001940 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001941 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06001942 } else if (!rx_buff->skb) {
1943 /* free the entry */
1944 next->rx_comp.first = 0;
1945 remove_buff_from_pool(adapter, rx_buff);
1946 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001947 }
1948
1949 length = be32_to_cpu(next->rx_comp.len);
1950 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1951 flags = next->rx_comp.flags;
1952 skb = rx_buff->skb;
1953 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1954 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001955
1956 /* VLAN Header has been stripped by the system firmware and
1957 * needs to be inserted by the driver
1958 */
1959 if (adapter->rx_vlan_header_insertion &&
1960 (flags & IBMVNIC_VLAN_STRIPPED))
1961 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1962 ntohs(next->rx_comp.vlan_tci));
1963
Thomas Falcon032c5e82015-12-21 11:26:06 -06001964 /* free the entry */
1965 next->rx_comp.first = 0;
1966 remove_buff_from_pool(adapter, rx_buff);
1967
1968 skb_put(skb, length);
1969 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001970 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001971
1972 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1973 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1974 skb->ip_summed = CHECKSUM_UNNECESSARY;
1975 }
1976
1977 length = skb->len;
1978 napi_gro_receive(napi, skb); /* send it up */
1979 netdev->stats.rx_packets++;
1980 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001981 adapter->rx_stats_buffers[scrq_num].packets++;
1982 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001983 frames_processed++;
1984 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001985
1986 if (adapter->state != VNIC_CLOSING)
1987 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001988
1989 if (frames_processed < budget) {
1990 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001991 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001992 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1993 napi_reschedule(napi)) {
1994 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1995 goto restart_poll;
1996 }
1997 }
1998 return frames_processed;
1999}
2000
2001#ifdef CONFIG_NET_POLL_CONTROLLER
2002static void ibmvnic_netpoll_controller(struct net_device *dev)
2003{
2004 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2005 int i;
2006
2007 replenish_pools(netdev_priv(dev));
2008 for (i = 0; i < adapter->req_rx_queues; i++)
2009 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2010 adapter->rx_scrq[i]);
2011}
2012#endif
2013
John Allenc26eba02017-10-26 16:23:25 -05002014static int wait_for_reset(struct ibmvnic_adapter *adapter)
2015{
2016 adapter->fallback.mtu = adapter->req_mtu;
2017 adapter->fallback.rx_queues = adapter->req_rx_queues;
2018 adapter->fallback.tx_queues = adapter->req_tx_queues;
2019 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2020 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2021
2022 init_completion(&adapter->reset_done);
2023 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2024 adapter->wait_for_reset = true;
2025 wait_for_completion(&adapter->reset_done);
2026
2027 if (adapter->reset_done_rc) {
2028 adapter->desired.mtu = adapter->fallback.mtu;
2029 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2030 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2031 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2032 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2033
2034 init_completion(&adapter->reset_done);
2035 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2036 wait_for_completion(&adapter->reset_done);
2037 }
2038 adapter->wait_for_reset = false;
2039
2040 return adapter->reset_done_rc;
2041}
2042
John Allen3a807b72017-06-06 16:55:52 -05002043static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2044{
John Allenc26eba02017-10-26 16:23:25 -05002045 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2046
2047 adapter->desired.mtu = new_mtu + ETH_HLEN;
2048
2049 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002050}
2051
Thomas Falcon032c5e82015-12-21 11:26:06 -06002052static const struct net_device_ops ibmvnic_netdev_ops = {
2053 .ndo_open = ibmvnic_open,
2054 .ndo_stop = ibmvnic_close,
2055 .ndo_start_xmit = ibmvnic_xmit,
2056 .ndo_set_rx_mode = ibmvnic_set_multi,
2057 .ndo_set_mac_address = ibmvnic_set_mac,
2058 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002059 .ndo_tx_timeout = ibmvnic_tx_timeout,
2060#ifdef CONFIG_NET_POLL_CONTROLLER
2061 .ndo_poll_controller = ibmvnic_netpoll_controller,
2062#endif
John Allen3a807b72017-06-06 16:55:52 -05002063 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002064};
2065
2066/* ethtool functions */
2067
Philippe Reynes8a433792017-01-07 22:37:29 +01002068static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2069 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002070{
Philippe Reynes8a433792017-01-07 22:37:29 +01002071 u32 supported, advertising;
2072
2073 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002074 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002075 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002076 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002077 cmd->base.speed = SPEED_1000;
2078 cmd->base.duplex = DUPLEX_FULL;
2079 cmd->base.port = PORT_FIBRE;
2080 cmd->base.phy_address = 0;
2081 cmd->base.autoneg = AUTONEG_ENABLE;
2082
2083 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2084 supported);
2085 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2086 advertising);
2087
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088 return 0;
2089}
2090
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002091static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002092 struct ethtool_drvinfo *info)
2093{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002094 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2095
Thomas Falcon032c5e82015-12-21 11:26:06 -06002096 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2097 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002098 strlcpy(info->fw_version, adapter->fw_version,
2099 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002100}
2101
2102static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2103{
2104 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2105
2106 return adapter->msg_enable;
2107}
2108
2109static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2110{
2111 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2112
2113 adapter->msg_enable = data;
2114}
2115
2116static u32 ibmvnic_get_link(struct net_device *netdev)
2117{
2118 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2119
2120 /* Don't need to send a query because we request a logical link up at
2121 * init and then we wait for link state indications
2122 */
2123 return adapter->logical_link_state;
2124}
2125
2126static void ibmvnic_get_ringparam(struct net_device *netdev,
2127 struct ethtool_ringparam *ring)
2128{
John Allenbc131b32017-08-02 16:46:30 -05002129 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2130
2131 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2132 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133 ring->rx_mini_max_pending = 0;
2134 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002135 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2136 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002137 ring->rx_mini_pending = 0;
2138 ring->rx_jumbo_pending = 0;
2139}
2140
John Allenc26eba02017-10-26 16:23:25 -05002141static int ibmvnic_set_ringparam(struct net_device *netdev,
2142 struct ethtool_ringparam *ring)
2143{
2144 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2145
2146 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2147 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2148 netdev_err(netdev, "Invalid request.\n");
2149 netdev_err(netdev, "Max tx buffers = %llu\n",
2150 adapter->max_rx_add_entries_per_subcrq);
2151 netdev_err(netdev, "Max rx buffers = %llu\n",
2152 adapter->max_tx_entries_per_subcrq);
2153 return -EINVAL;
2154 }
2155
2156 adapter->desired.rx_entries = ring->rx_pending;
2157 adapter->desired.tx_entries = ring->tx_pending;
2158
2159 return wait_for_reset(adapter);
2160}
2161
John Allenc2dbeb62017-08-02 16:47:17 -05002162static void ibmvnic_get_channels(struct net_device *netdev,
2163 struct ethtool_channels *channels)
2164{
2165 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2166
2167 channels->max_rx = adapter->max_rx_queues;
2168 channels->max_tx = adapter->max_tx_queues;
2169 channels->max_other = 0;
2170 channels->max_combined = 0;
2171 channels->rx_count = adapter->req_rx_queues;
2172 channels->tx_count = adapter->req_tx_queues;
2173 channels->other_count = 0;
2174 channels->combined_count = 0;
2175}
2176
John Allenc26eba02017-10-26 16:23:25 -05002177static int ibmvnic_set_channels(struct net_device *netdev,
2178 struct ethtool_channels *channels)
2179{
2180 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2181
2182 adapter->desired.rx_queues = channels->rx_count;
2183 adapter->desired.tx_queues = channels->tx_count;
2184
2185 return wait_for_reset(adapter);
2186}
2187
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2189{
John Allen3d52b592017-08-02 16:44:14 -05002190 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002191 int i;
2192
2193 if (stringset != ETH_SS_STATS)
2194 return;
2195
2196 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2197 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002198
2199 for (i = 0; i < adapter->req_tx_queues; i++) {
2200 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2201 data += ETH_GSTRING_LEN;
2202
2203 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2204 data += ETH_GSTRING_LEN;
2205
2206 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2207 data += ETH_GSTRING_LEN;
2208 }
2209
2210 for (i = 0; i < adapter->req_rx_queues; i++) {
2211 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2212 data += ETH_GSTRING_LEN;
2213
2214 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2215 data += ETH_GSTRING_LEN;
2216
2217 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2218 data += ETH_GSTRING_LEN;
2219 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002220}
2221
2222static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2223{
John Allen3d52b592017-08-02 16:44:14 -05002224 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2225
Thomas Falcon032c5e82015-12-21 11:26:06 -06002226 switch (sset) {
2227 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002228 return ARRAY_SIZE(ibmvnic_stats) +
2229 adapter->req_tx_queues * NUM_TX_STATS +
2230 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002231 default:
2232 return -EOPNOTSUPP;
2233 }
2234}
2235
2236static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2237 struct ethtool_stats *stats, u64 *data)
2238{
2239 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2240 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002241 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002242
2243 memset(&crq, 0, sizeof(crq));
2244 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2245 crq.request_statistics.cmd = REQUEST_STATISTICS;
2246 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2247 crq.request_statistics.len =
2248 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002249
2250 /* Wait for data to be written */
2251 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002252 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002253 wait_for_completion(&adapter->stats_done);
2254
2255 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002256 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2257 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002258
2259 for (j = 0; j < adapter->req_tx_queues; j++) {
2260 data[i] = adapter->tx_stats_buffers[j].packets;
2261 i++;
2262 data[i] = adapter->tx_stats_buffers[j].bytes;
2263 i++;
2264 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2265 i++;
2266 }
2267
2268 for (j = 0; j < adapter->req_rx_queues; j++) {
2269 data[i] = adapter->rx_stats_buffers[j].packets;
2270 i++;
2271 data[i] = adapter->rx_stats_buffers[j].bytes;
2272 i++;
2273 data[i] = adapter->rx_stats_buffers[j].interrupts;
2274 i++;
2275 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002276}
2277
2278static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002279 .get_drvinfo = ibmvnic_get_drvinfo,
2280 .get_msglevel = ibmvnic_get_msglevel,
2281 .set_msglevel = ibmvnic_set_msglevel,
2282 .get_link = ibmvnic_get_link,
2283 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002284 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002285 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002286 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002287 .get_strings = ibmvnic_get_strings,
2288 .get_sset_count = ibmvnic_get_sset_count,
2289 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002290 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002291};
2292
2293/* Routines for managing CRQs/sCRQs */
2294
Nathan Fontenot57a49432017-05-26 10:31:12 -04002295static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2296 struct ibmvnic_sub_crq_queue *scrq)
2297{
2298 int rc;
2299
2300 if (scrq->irq) {
2301 free_irq(scrq->irq, scrq);
2302 irq_dispose_mapping(scrq->irq);
2303 scrq->irq = 0;
2304 }
2305
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002306 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002307 scrq->cur = 0;
2308
2309 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2310 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2311 return rc;
2312}
2313
2314static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2315{
2316 int i, rc;
2317
2318 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002319 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002320 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2321 if (rc)
2322 return rc;
2323 }
2324
2325 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002326 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002327 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2328 if (rc)
2329 return rc;
2330 }
2331
Nathan Fontenot57a49432017-05-26 10:31:12 -04002332 return rc;
2333}
2334
Thomas Falcon032c5e82015-12-21 11:26:06 -06002335static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002336 struct ibmvnic_sub_crq_queue *scrq,
2337 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338{
2339 struct device *dev = &adapter->vdev->dev;
2340 long rc;
2341
2342 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2343
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002344 if (do_h_free) {
2345 /* Close the sub-crqs */
2346 do {
2347 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2348 adapter->vdev->unit_address,
2349 scrq->crq_num);
2350 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002351
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002352 if (rc) {
2353 netdev_err(adapter->netdev,
2354 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2355 scrq->crq_num, rc);
2356 }
Thomas Falconffa73852017-04-19 13:44:29 -04002357 }
2358
Thomas Falcon032c5e82015-12-21 11:26:06 -06002359 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2360 DMA_BIDIRECTIONAL);
2361 free_pages((unsigned long)scrq->msgs, 2);
2362 kfree(scrq);
2363}
2364
2365static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2366 *adapter)
2367{
2368 struct device *dev = &adapter->vdev->dev;
2369 struct ibmvnic_sub_crq_queue *scrq;
2370 int rc;
2371
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002372 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002373 if (!scrq)
2374 return NULL;
2375
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002376 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002377 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002378 if (!scrq->msgs) {
2379 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2380 goto zero_page_failed;
2381 }
2382
2383 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2384 DMA_BIDIRECTIONAL);
2385 if (dma_mapping_error(dev, scrq->msg_token)) {
2386 dev_warn(dev, "Couldn't map crq queue messages page\n");
2387 goto map_failed;
2388 }
2389
2390 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2391 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2392
2393 if (rc == H_RESOURCE)
2394 rc = ibmvnic_reset_crq(adapter);
2395
2396 if (rc == H_CLOSED) {
2397 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2398 } else if (rc) {
2399 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2400 goto reg_failed;
2401 }
2402
Thomas Falcon032c5e82015-12-21 11:26:06 -06002403 scrq->adapter = adapter;
2404 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002405 spin_lock_init(&scrq->lock);
2406
2407 netdev_dbg(adapter->netdev,
2408 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2409 scrq->crq_num, scrq->hw_irq, scrq->irq);
2410
2411 return scrq;
2412
Thomas Falcon032c5e82015-12-21 11:26:06 -06002413reg_failed:
2414 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2415 DMA_BIDIRECTIONAL);
2416map_failed:
2417 free_pages((unsigned long)scrq->msgs, 2);
2418zero_page_failed:
2419 kfree(scrq);
2420
2421 return NULL;
2422}
2423
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002424static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002425{
2426 int i;
2427
2428 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002429 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002430 if (!adapter->tx_scrq[i])
2431 continue;
2432
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002433 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2434 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002435 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002436 free_irq(adapter->tx_scrq[i]->irq,
2437 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002438 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002439 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002440 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002441
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002442 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2443 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002444 }
2445
Nathan Fontenot9501df32017-03-15 23:38:07 -04002446 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002447 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002448 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002449 }
2450
2451 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002452 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002453 if (!adapter->rx_scrq[i])
2454 continue;
2455
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002456 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2457 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002458 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002459 free_irq(adapter->rx_scrq[i]->irq,
2460 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002461 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002462 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002463 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002464
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002465 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2466 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002467 }
2468
Nathan Fontenot9501df32017-03-15 23:38:07 -04002469 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002470 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002471 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002472 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002473}
2474
2475static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2476 struct ibmvnic_sub_crq_queue *scrq)
2477{
2478 struct device *dev = &adapter->vdev->dev;
2479 unsigned long rc;
2480
2481 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2482 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2483 if (rc)
2484 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2485 scrq->hw_irq, rc);
2486 return rc;
2487}
2488
2489static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2490 struct ibmvnic_sub_crq_queue *scrq)
2491{
2492 struct device *dev = &adapter->vdev->dev;
2493 unsigned long rc;
2494
2495 if (scrq->hw_irq > 0x100000000ULL) {
2496 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2497 return 1;
2498 }
2499
2500 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2501 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2502 if (rc)
2503 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2504 scrq->hw_irq, rc);
2505 return rc;
2506}
2507
2508static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2509 struct ibmvnic_sub_crq_queue *scrq)
2510{
2511 struct device *dev = &adapter->vdev->dev;
2512 struct ibmvnic_tx_buff *txbuff;
2513 union sub_crq *next;
2514 int index;
2515 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002516 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002517
2518restart_loop:
2519 while (pending_scrq(adapter, scrq)) {
2520 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002521 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002522
2523 next = ibmvnic_next_scrq(adapter, scrq);
2524 for (i = 0; i < next->tx_comp.num_comps; i++) {
2525 if (next->tx_comp.rcs[i]) {
2526 dev_err(dev, "tx error %x\n",
2527 next->tx_comp.rcs[i]);
2528 continue;
2529 }
2530 index = be32_to_cpu(next->tx_comp.correlators[i]);
2531 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2532
2533 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2534 if (!txbuff->data_dma[j])
2535 continue;
2536
2537 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002538 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002539 /* if sub_crq was sent indirectly */
2540 first = txbuff->indir_arr[0].generic.first;
2541 if (first == IBMVNIC_CRQ_CMD) {
2542 dma_unmap_single(dev, txbuff->indir_dma,
2543 sizeof(txbuff->indir_arr),
2544 DMA_TO_DEVICE);
2545 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002547 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002548 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002549 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002550 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002551
Thomas Falconffc385b2018-02-18 10:08:41 -06002552 num_entries += txbuff->num_entries;
2553
Thomas Falcon032c5e82015-12-21 11:26:06 -06002554 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2555 producer_index] = index;
2556 adapter->tx_pool[pool].producer_index =
2557 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002558 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002559 }
2560 /* remove tx_comp scrq*/
2561 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002562
Thomas Falconffc385b2018-02-18 10:08:41 -06002563 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002564 (adapter->req_tx_entries_per_subcrq / 2) &&
2565 __netif_subqueue_stopped(adapter->netdev,
2566 scrq->pool_index)) {
2567 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002568 netdev_dbg(adapter->netdev, "Started queue %d\n",
2569 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002570 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002571 }
2572
2573 enable_scrq_irq(adapter, scrq);
2574
2575 if (pending_scrq(adapter, scrq)) {
2576 disable_scrq_irq(adapter, scrq);
2577 goto restart_loop;
2578 }
2579
2580 return 0;
2581}
2582
2583static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2584{
2585 struct ibmvnic_sub_crq_queue *scrq = instance;
2586 struct ibmvnic_adapter *adapter = scrq->adapter;
2587
2588 disable_scrq_irq(adapter, scrq);
2589 ibmvnic_complete_tx(adapter, scrq);
2590
2591 return IRQ_HANDLED;
2592}
2593
2594static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2595{
2596 struct ibmvnic_sub_crq_queue *scrq = instance;
2597 struct ibmvnic_adapter *adapter = scrq->adapter;
2598
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002599 /* When booting a kdump kernel we can hit pending interrupts
2600 * prior to completing driver initialization.
2601 */
2602 if (unlikely(adapter->state != VNIC_OPEN))
2603 return IRQ_NONE;
2604
John Allen3d52b592017-08-02 16:44:14 -05002605 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2606
Thomas Falcon032c5e82015-12-21 11:26:06 -06002607 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2608 disable_scrq_irq(adapter, scrq);
2609 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2610 }
2611
2612 return IRQ_HANDLED;
2613}
2614
Thomas Falconea22d512016-07-06 15:35:17 -05002615static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2616{
2617 struct device *dev = &adapter->vdev->dev;
2618 struct ibmvnic_sub_crq_queue *scrq;
2619 int i = 0, j = 0;
2620 int rc = 0;
2621
2622 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002623 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2624 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002625 scrq = adapter->tx_scrq[i];
2626 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2627
Michael Ellerman99c17902016-09-10 19:59:05 +10002628 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002629 rc = -EINVAL;
2630 dev_err(dev, "Error mapping irq\n");
2631 goto req_tx_irq_failed;
2632 }
2633
2634 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2635 0, "ibmvnic_tx", scrq);
2636
2637 if (rc) {
2638 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2639 scrq->irq, rc);
2640 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002641 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002642 }
2643 }
2644
2645 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002646 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2647 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002648 scrq = adapter->rx_scrq[i];
2649 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002650 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002651 rc = -EINVAL;
2652 dev_err(dev, "Error mapping irq\n");
2653 goto req_rx_irq_failed;
2654 }
2655 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2656 0, "ibmvnic_rx", scrq);
2657 if (rc) {
2658 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2659 scrq->irq, rc);
2660 irq_dispose_mapping(scrq->irq);
2661 goto req_rx_irq_failed;
2662 }
2663 }
2664 return rc;
2665
2666req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002667 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002668 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2669 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002670 }
Thomas Falconea22d512016-07-06 15:35:17 -05002671 i = adapter->req_tx_queues;
2672req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002673 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002674 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2675 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002676 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002677 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002678 return rc;
2679}
2680
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002681static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002682{
2683 struct device *dev = &adapter->vdev->dev;
2684 struct ibmvnic_sub_crq_queue **allqueues;
2685 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002686 int total_queues;
2687 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002688 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002689
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2691
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002692 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002693 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002694 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695
2696 for (i = 0; i < total_queues; i++) {
2697 allqueues[i] = init_sub_crq_queue(adapter);
2698 if (!allqueues[i]) {
2699 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2700 break;
2701 }
2702 registered_queues++;
2703 }
2704
2705 /* Make sure we were able to register the minimum number of queues */
2706 if (registered_queues <
2707 adapter->min_tx_queues + adapter->min_rx_queues) {
2708 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2709 goto tx_failed;
2710 }
2711
2712 /* Distribute the failed allocated queues*/
2713 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2714 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2715 switch (i % 3) {
2716 case 0:
2717 if (adapter->req_rx_queues > adapter->min_rx_queues)
2718 adapter->req_rx_queues--;
2719 else
2720 more++;
2721 break;
2722 case 1:
2723 if (adapter->req_tx_queues > adapter->min_tx_queues)
2724 adapter->req_tx_queues--;
2725 else
2726 more++;
2727 break;
2728 }
2729 }
2730
2731 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002732 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002733 if (!adapter->tx_scrq)
2734 goto tx_failed;
2735
2736 for (i = 0; i < adapter->req_tx_queues; i++) {
2737 adapter->tx_scrq[i] = allqueues[i];
2738 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002739 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740 }
2741
2742 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002743 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744 if (!adapter->rx_scrq)
2745 goto rx_failed;
2746
2747 for (i = 0; i < adapter->req_rx_queues; i++) {
2748 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2749 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002750 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 }
2752
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002753 kfree(allqueues);
2754 return 0;
2755
2756rx_failed:
2757 kfree(adapter->tx_scrq);
2758 adapter->tx_scrq = NULL;
2759tx_failed:
2760 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002761 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002762 kfree(allqueues);
2763 return -1;
2764}
2765
2766static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2767{
2768 struct device *dev = &adapter->vdev->dev;
2769 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002770 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002771
2772 if (!retry) {
2773 /* Sub-CRQ entries are 32 byte long */
2774 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2775
2776 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2777 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2778 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2779 return;
2780 }
2781
John Allenc26eba02017-10-26 16:23:25 -05002782 if (adapter->desired.mtu)
2783 adapter->req_mtu = adapter->desired.mtu;
2784 else
2785 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002786
John Allenc26eba02017-10-26 16:23:25 -05002787 if (!adapter->desired.tx_entries)
2788 adapter->desired.tx_entries =
2789 adapter->max_tx_entries_per_subcrq;
2790 if (!adapter->desired.rx_entries)
2791 adapter->desired.rx_entries =
2792 adapter->max_rx_add_entries_per_subcrq;
2793
2794 max_entries = IBMVNIC_MAX_LTB_SIZE /
2795 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2796
2797 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2798 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2799 adapter->desired.tx_entries = max_entries;
2800 }
2801
2802 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2803 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2804 adapter->desired.rx_entries = max_entries;
2805 }
2806
2807 if (adapter->desired.tx_entries)
2808 adapter->req_tx_entries_per_subcrq =
2809 adapter->desired.tx_entries;
2810 else
2811 adapter->req_tx_entries_per_subcrq =
2812 adapter->max_tx_entries_per_subcrq;
2813
2814 if (adapter->desired.rx_entries)
2815 adapter->req_rx_add_entries_per_subcrq =
2816 adapter->desired.rx_entries;
2817 else
2818 adapter->req_rx_add_entries_per_subcrq =
2819 adapter->max_rx_add_entries_per_subcrq;
2820
2821 if (adapter->desired.tx_queues)
2822 adapter->req_tx_queues =
2823 adapter->desired.tx_queues;
2824 else
2825 adapter->req_tx_queues =
2826 adapter->opt_tx_comp_sub_queues;
2827
2828 if (adapter->desired.rx_queues)
2829 adapter->req_rx_queues =
2830 adapter->desired.rx_queues;
2831 else
2832 adapter->req_rx_queues =
2833 adapter->opt_rx_comp_queues;
2834
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002835 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002836 }
2837
Thomas Falcon032c5e82015-12-21 11:26:06 -06002838 memset(&crq, 0, sizeof(crq));
2839 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2840 crq.request_capability.cmd = REQUEST_CAPABILITY;
2841
2842 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002843 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002844 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845 ibmvnic_send_crq(adapter, &crq);
2846
2847 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002848 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002849 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002850 ibmvnic_send_crq(adapter, &crq);
2851
2852 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002853 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002854 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002855 ibmvnic_send_crq(adapter, &crq);
2856
2857 crq.request_capability.capability =
2858 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2859 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002860 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002861 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002862 ibmvnic_send_crq(adapter, &crq);
2863
2864 crq.request_capability.capability =
2865 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2866 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002867 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002868 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869 ibmvnic_send_crq(adapter, &crq);
2870
2871 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002872 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002873 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002874 ibmvnic_send_crq(adapter, &crq);
2875
2876 if (adapter->netdev->flags & IFF_PROMISC) {
2877 if (adapter->promisc_supported) {
2878 crq.request_capability.capability =
2879 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002880 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002881 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002882 ibmvnic_send_crq(adapter, &crq);
2883 }
2884 } else {
2885 crq.request_capability.capability =
2886 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002887 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002888 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002889 ibmvnic_send_crq(adapter, &crq);
2890 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002891}
2892
2893static int pending_scrq(struct ibmvnic_adapter *adapter,
2894 struct ibmvnic_sub_crq_queue *scrq)
2895{
2896 union sub_crq *entry = &scrq->msgs[scrq->cur];
2897
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002898 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002899 return 1;
2900 else
2901 return 0;
2902}
2903
2904static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2905 struct ibmvnic_sub_crq_queue *scrq)
2906{
2907 union sub_crq *entry;
2908 unsigned long flags;
2909
2910 spin_lock_irqsave(&scrq->lock, flags);
2911 entry = &scrq->msgs[scrq->cur];
2912 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2913 if (++scrq->cur == scrq->size)
2914 scrq->cur = 0;
2915 } else {
2916 entry = NULL;
2917 }
2918 spin_unlock_irqrestore(&scrq->lock, flags);
2919
2920 return entry;
2921}
2922
2923static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2924{
2925 struct ibmvnic_crq_queue *queue = &adapter->crq;
2926 union ibmvnic_crq *crq;
2927
2928 crq = &queue->msgs[queue->cur];
2929 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2930 if (++queue->cur == queue->size)
2931 queue->cur = 0;
2932 } else {
2933 crq = NULL;
2934 }
2935
2936 return crq;
2937}
2938
2939static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2940 union sub_crq *sub_crq)
2941{
2942 unsigned int ua = adapter->vdev->unit_address;
2943 struct device *dev = &adapter->vdev->dev;
2944 u64 *u64_crq = (u64 *)sub_crq;
2945 int rc;
2946
2947 netdev_dbg(adapter->netdev,
2948 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2949 (unsigned long int)cpu_to_be64(remote_handle),
2950 (unsigned long int)cpu_to_be64(u64_crq[0]),
2951 (unsigned long int)cpu_to_be64(u64_crq[1]),
2952 (unsigned long int)cpu_to_be64(u64_crq[2]),
2953 (unsigned long int)cpu_to_be64(u64_crq[3]));
2954
2955 /* Make sure the hypervisor sees the complete request */
2956 mb();
2957
2958 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2959 cpu_to_be64(remote_handle),
2960 cpu_to_be64(u64_crq[0]),
2961 cpu_to_be64(u64_crq[1]),
2962 cpu_to_be64(u64_crq[2]),
2963 cpu_to_be64(u64_crq[3]));
2964
2965 if (rc) {
2966 if (rc == H_CLOSED)
2967 dev_warn(dev, "CRQ Queue closed\n");
2968 dev_err(dev, "Send error (rc=%d)\n", rc);
2969 }
2970
2971 return rc;
2972}
2973
Thomas Falconad7775d2016-04-01 17:20:34 -05002974static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2975 u64 remote_handle, u64 ioba, u64 num_entries)
2976{
2977 unsigned int ua = adapter->vdev->unit_address;
2978 struct device *dev = &adapter->vdev->dev;
2979 int rc;
2980
2981 /* Make sure the hypervisor sees the complete request */
2982 mb();
2983 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2984 cpu_to_be64(remote_handle),
2985 ioba, num_entries);
2986
2987 if (rc) {
2988 if (rc == H_CLOSED)
2989 dev_warn(dev, "CRQ Queue closed\n");
2990 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2991 }
2992
2993 return rc;
2994}
2995
Thomas Falcon032c5e82015-12-21 11:26:06 -06002996static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2997 union ibmvnic_crq *crq)
2998{
2999 unsigned int ua = adapter->vdev->unit_address;
3000 struct device *dev = &adapter->vdev->dev;
3001 u64 *u64_crq = (u64 *)crq;
3002 int rc;
3003
3004 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3005 (unsigned long int)cpu_to_be64(u64_crq[0]),
3006 (unsigned long int)cpu_to_be64(u64_crq[1]));
3007
3008 /* Make sure the hypervisor sees the complete request */
3009 mb();
3010
3011 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3012 cpu_to_be64(u64_crq[0]),
3013 cpu_to_be64(u64_crq[1]));
3014
3015 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003016 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003017 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003018 if (adapter->resetting)
3019 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3020 }
3021
Thomas Falcon032c5e82015-12-21 11:26:06 -06003022 dev_warn(dev, "Send error (rc=%d)\n", rc);
3023 }
3024
3025 return rc;
3026}
3027
3028static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3029{
3030 union ibmvnic_crq crq;
3031
3032 memset(&crq, 0, sizeof(crq));
3033 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3034 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3035 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3036
3037 return ibmvnic_send_crq(adapter, &crq);
3038}
3039
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040static int send_version_xchg(struct ibmvnic_adapter *adapter)
3041{
3042 union ibmvnic_crq crq;
3043
3044 memset(&crq, 0, sizeof(crq));
3045 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3046 crq.version_exchange.cmd = VERSION_EXCHANGE;
3047 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3048
3049 return ibmvnic_send_crq(adapter, &crq);
3050}
3051
Nathan Fontenot37798d02017-11-08 11:23:56 -06003052struct vnic_login_client_data {
3053 u8 type;
3054 __be16 len;
3055 char name;
3056} __packed;
3057
3058static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3059{
3060 int len;
3061
3062 /* Calculate the amount of buffer space needed for the
3063 * vnic client data in the login buffer. There are four entries,
3064 * OS name, LPAR name, device name, and a null last entry.
3065 */
3066 len = 4 * sizeof(struct vnic_login_client_data);
3067 len += 6; /* "Linux" plus NULL */
3068 len += strlen(utsname()->nodename) + 1;
3069 len += strlen(adapter->netdev->name) + 1;
3070
3071 return len;
3072}
3073
3074static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3075 struct vnic_login_client_data *vlcd)
3076{
3077 const char *os_name = "Linux";
3078 int len;
3079
3080 /* Type 1 - LPAR OS */
3081 vlcd->type = 1;
3082 len = strlen(os_name) + 1;
3083 vlcd->len = cpu_to_be16(len);
3084 strncpy(&vlcd->name, os_name, len);
3085 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3086
3087 /* Type 2 - LPAR name */
3088 vlcd->type = 2;
3089 len = strlen(utsname()->nodename) + 1;
3090 vlcd->len = cpu_to_be16(len);
3091 strncpy(&vlcd->name, utsname()->nodename, len);
3092 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3093
3094 /* Type 3 - device name */
3095 vlcd->type = 3;
3096 len = strlen(adapter->netdev->name) + 1;
3097 vlcd->len = cpu_to_be16(len);
3098 strncpy(&vlcd->name, adapter->netdev->name, len);
3099}
3100
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003101static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003102{
3103 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3104 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003105 struct device *dev = &adapter->vdev->dev;
3106 dma_addr_t rsp_buffer_token;
3107 dma_addr_t buffer_token;
3108 size_t rsp_buffer_size;
3109 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003110 size_t buffer_size;
3111 __be64 *tx_list_p;
3112 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003113 int client_data_len;
3114 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003115 int i;
3116
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003117 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3118 netdev_err(adapter->netdev,
3119 "RX or TX queues are not allocated, device login failed\n");
3120 return -1;
3121 }
3122
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003123 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003124 client_data_len = vnic_client_data_len(adapter);
3125
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126 buffer_size =
3127 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003128 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3129 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003130
Nathan Fontenot37798d02017-11-08 11:23:56 -06003131 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132 if (!login_buffer)
3133 goto buf_alloc_failed;
3134
3135 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3136 DMA_TO_DEVICE);
3137 if (dma_mapping_error(dev, buffer_token)) {
3138 dev_err(dev, "Couldn't map login buffer\n");
3139 goto buf_map_failed;
3140 }
3141
John Allen498cd8e2016-04-06 11:49:55 -05003142 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3143 sizeof(u64) * adapter->req_tx_queues +
3144 sizeof(u64) * adapter->req_rx_queues +
3145 sizeof(u64) * adapter->req_rx_queues +
3146 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147
3148 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3149 if (!login_rsp_buffer)
3150 goto buf_rsp_alloc_failed;
3151
3152 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3153 rsp_buffer_size, DMA_FROM_DEVICE);
3154 if (dma_mapping_error(dev, rsp_buffer_token)) {
3155 dev_err(dev, "Couldn't map login rsp buffer\n");
3156 goto buf_rsp_map_failed;
3157 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003158
Thomas Falcon032c5e82015-12-21 11:26:06 -06003159 adapter->login_buf = login_buffer;
3160 adapter->login_buf_token = buffer_token;
3161 adapter->login_buf_sz = buffer_size;
3162 adapter->login_rsp_buf = login_rsp_buffer;
3163 adapter->login_rsp_buf_token = rsp_buffer_token;
3164 adapter->login_rsp_buf_sz = rsp_buffer_size;
3165
3166 login_buffer->len = cpu_to_be32(buffer_size);
3167 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3168 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3169 login_buffer->off_txcomp_subcrqs =
3170 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3171 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3172 login_buffer->off_rxcomp_subcrqs =
3173 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3174 sizeof(u64) * adapter->req_tx_queues);
3175 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3176 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3177
3178 tx_list_p = (__be64 *)((char *)login_buffer +
3179 sizeof(struct ibmvnic_login_buffer));
3180 rx_list_p = (__be64 *)((char *)login_buffer +
3181 sizeof(struct ibmvnic_login_buffer) +
3182 sizeof(u64) * adapter->req_tx_queues);
3183
3184 for (i = 0; i < adapter->req_tx_queues; i++) {
3185 if (adapter->tx_scrq[i]) {
3186 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3187 crq_num);
3188 }
3189 }
3190
3191 for (i = 0; i < adapter->req_rx_queues; i++) {
3192 if (adapter->rx_scrq[i]) {
3193 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3194 crq_num);
3195 }
3196 }
3197
Nathan Fontenot37798d02017-11-08 11:23:56 -06003198 /* Insert vNIC login client data */
3199 vlcd = (struct vnic_login_client_data *)
3200 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3201 login_buffer->client_data_offset =
3202 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3203 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3204
3205 vnic_add_client_data(adapter, vlcd);
3206
Thomas Falcon032c5e82015-12-21 11:26:06 -06003207 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3208 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3209 netdev_dbg(adapter->netdev, "%016lx\n",
3210 ((unsigned long int *)(adapter->login_buf))[i]);
3211 }
3212
3213 memset(&crq, 0, sizeof(crq));
3214 crq.login.first = IBMVNIC_CRQ_CMD;
3215 crq.login.cmd = LOGIN;
3216 crq.login.ioba = cpu_to_be32(buffer_token);
3217 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003218 ibmvnic_send_crq(adapter, &crq);
3219
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003220 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003221
Thomas Falcon032c5e82015-12-21 11:26:06 -06003222buf_rsp_map_failed:
3223 kfree(login_rsp_buffer);
3224buf_rsp_alloc_failed:
3225 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3226buf_map_failed:
3227 kfree(login_buffer);
3228buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003229 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003230}
3231
3232static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3233 u32 len, u8 map_id)
3234{
3235 union ibmvnic_crq crq;
3236
3237 memset(&crq, 0, sizeof(crq));
3238 crq.request_map.first = IBMVNIC_CRQ_CMD;
3239 crq.request_map.cmd = REQUEST_MAP;
3240 crq.request_map.map_id = map_id;
3241 crq.request_map.ioba = cpu_to_be32(addr);
3242 crq.request_map.len = cpu_to_be32(len);
3243 ibmvnic_send_crq(adapter, &crq);
3244}
3245
3246static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3247{
3248 union ibmvnic_crq crq;
3249
3250 memset(&crq, 0, sizeof(crq));
3251 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3252 crq.request_unmap.cmd = REQUEST_UNMAP;
3253 crq.request_unmap.map_id = map_id;
3254 ibmvnic_send_crq(adapter, &crq);
3255}
3256
3257static void send_map_query(struct ibmvnic_adapter *adapter)
3258{
3259 union ibmvnic_crq crq;
3260
3261 memset(&crq, 0, sizeof(crq));
3262 crq.query_map.first = IBMVNIC_CRQ_CMD;
3263 crq.query_map.cmd = QUERY_MAP;
3264 ibmvnic_send_crq(adapter, &crq);
3265}
3266
3267/* Send a series of CRQs requesting various capabilities of the VNIC server */
3268static void send_cap_queries(struct ibmvnic_adapter *adapter)
3269{
3270 union ibmvnic_crq crq;
3271
Thomas Falcon901e0402017-02-15 12:17:59 -06003272 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003273 memset(&crq, 0, sizeof(crq));
3274 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3275 crq.query_capability.cmd = QUERY_CAPABILITY;
3276
3277 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003278 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003279 ibmvnic_send_crq(adapter, &crq);
3280
3281 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003282 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003283 ibmvnic_send_crq(adapter, &crq);
3284
3285 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003286 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003287 ibmvnic_send_crq(adapter, &crq);
3288
3289 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003290 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 ibmvnic_send_crq(adapter, &crq);
3292
3293 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003294 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003295 ibmvnic_send_crq(adapter, &crq);
3296
3297 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003298 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299 ibmvnic_send_crq(adapter, &crq);
3300
3301 crq.query_capability.capability =
3302 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003303 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304 ibmvnic_send_crq(adapter, &crq);
3305
3306 crq.query_capability.capability =
3307 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003308 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003309 ibmvnic_send_crq(adapter, &crq);
3310
3311 crq.query_capability.capability =
3312 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003313 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003314 ibmvnic_send_crq(adapter, &crq);
3315
3316 crq.query_capability.capability =
3317 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003318 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 ibmvnic_send_crq(adapter, &crq);
3320
3321 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003322 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323 ibmvnic_send_crq(adapter, &crq);
3324
3325 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003326 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003327 ibmvnic_send_crq(adapter, &crq);
3328
3329 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003330 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003331 ibmvnic_send_crq(adapter, &crq);
3332
3333 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003334 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003335 ibmvnic_send_crq(adapter, &crq);
3336
3337 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003338 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003339 ibmvnic_send_crq(adapter, &crq);
3340
3341 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003342 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343 ibmvnic_send_crq(adapter, &crq);
3344
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003345 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3346 atomic_inc(&adapter->running_cap_crqs);
3347 ibmvnic_send_crq(adapter, &crq);
3348
Thomas Falcon032c5e82015-12-21 11:26:06 -06003349 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003350 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351 ibmvnic_send_crq(adapter, &crq);
3352
3353 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003354 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003355 ibmvnic_send_crq(adapter, &crq);
3356
3357 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003358 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003359 ibmvnic_send_crq(adapter, &crq);
3360
3361 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003362 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 ibmvnic_send_crq(adapter, &crq);
3364
3365 crq.query_capability.capability =
3366 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003367 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003368 ibmvnic_send_crq(adapter, &crq);
3369
3370 crq.query_capability.capability =
3371 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003372 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373 ibmvnic_send_crq(adapter, &crq);
3374
3375 crq.query_capability.capability =
3376 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003377 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003378 ibmvnic_send_crq(adapter, &crq);
3379
3380 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003381 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003382 ibmvnic_send_crq(adapter, &crq);
3383}
3384
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003385static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3386 struct ibmvnic_adapter *adapter)
3387{
3388 struct device *dev = &adapter->vdev->dev;
3389
3390 if (crq->get_vpd_size_rsp.rc.code) {
3391 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3392 crq->get_vpd_size_rsp.rc.code);
3393 complete(&adapter->fw_done);
3394 return;
3395 }
3396
3397 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3398 complete(&adapter->fw_done);
3399}
3400
3401static void handle_vpd_rsp(union ibmvnic_crq *crq,
3402 struct ibmvnic_adapter *adapter)
3403{
3404 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003405 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003406 u8 fw_level_len = 0;
3407
3408 memset(adapter->fw_version, 0, 32);
3409
3410 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3411 DMA_FROM_DEVICE);
3412
3413 if (crq->get_vpd_rsp.rc.code) {
3414 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3415 crq->get_vpd_rsp.rc.code);
3416 goto complete;
3417 }
3418
3419 /* get the position of the firmware version info
3420 * located after the ASCII 'RM' substring in the buffer
3421 */
3422 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3423 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003424 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 -02003425 goto complete;
3426 }
3427
3428 /* get length of firmware level ASCII substring */
3429 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3430 fw_level_len = *(substr + 2);
3431 } else {
3432 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3433 goto complete;
3434 }
3435
3436 /* copy firmware version string from vpd into adapter */
3437 if ((substr + 3 + fw_level_len) <
3438 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003439 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003440 } else {
3441 dev_info(dev, "FW substr extrapolated VPD buff\n");
3442 }
3443
3444complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003445 if (adapter->fw_version[0] == '\0')
3446 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003447 complete(&adapter->fw_done);
3448}
3449
Thomas Falcon032c5e82015-12-21 11:26:06 -06003450static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3451{
3452 struct device *dev = &adapter->vdev->dev;
3453 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3454 union ibmvnic_crq crq;
3455 int i;
3456
3457 dma_unmap_single(dev, adapter->ip_offload_tok,
3458 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3459
3460 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3461 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3462 netdev_dbg(adapter->netdev, "%016lx\n",
3463 ((unsigned long int *)(buf))[i]);
3464
3465 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3466 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3467 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3468 buf->tcp_ipv4_chksum);
3469 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3470 buf->tcp_ipv6_chksum);
3471 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3472 buf->udp_ipv4_chksum);
3473 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3474 buf->udp_ipv6_chksum);
3475 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3476 buf->large_tx_ipv4);
3477 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3478 buf->large_tx_ipv6);
3479 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3480 buf->large_rx_ipv4);
3481 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3482 buf->large_rx_ipv6);
3483 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3484 buf->max_ipv4_header_size);
3485 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3486 buf->max_ipv6_header_size);
3487 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3488 buf->max_tcp_header_size);
3489 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3490 buf->max_udp_header_size);
3491 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3492 buf->max_large_tx_size);
3493 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3494 buf->max_large_rx_size);
3495 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3496 buf->ipv6_extension_header);
3497 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3498 buf->tcp_pseudosum_req);
3499 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3500 buf->num_ipv6_ext_headers);
3501 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3502 buf->off_ipv6_ext_headers);
3503
3504 adapter->ip_offload_ctrl_tok =
3505 dma_map_single(dev, &adapter->ip_offload_ctrl,
3506 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3507
3508 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3509 dev_err(dev, "Couldn't map ip offload control buffer\n");
3510 return;
3511 }
3512
Thomas Falconf6897942018-01-18 19:05:01 -06003513 adapter->ip_offload_ctrl.len =
3514 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003515 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003516 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3517 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003518 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3519 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3520 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3521 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003522 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3523 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003524
Thomas Falconfdb06102017-10-17 12:36:55 -05003525 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003526 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3527 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3528
Thomas Falcon15482052017-10-17 12:36:54 -05003529 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530
3531 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3532 adapter->netdev->features |= NETIF_F_IP_CSUM;
3533
3534 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3535 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3536
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003537 if ((adapter->netdev->features &
3538 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3539 adapter->netdev->features |= NETIF_F_RXCSUM;
3540
Thomas Falconfdb06102017-10-17 12:36:55 -05003541 if (buf->large_tx_ipv4)
3542 adapter->netdev->features |= NETIF_F_TSO;
3543 if (buf->large_tx_ipv6)
3544 adapter->netdev->features |= NETIF_F_TSO6;
3545
Thomas Falconaa0bf852017-10-17 12:36:56 -05003546 adapter->netdev->hw_features |= adapter->netdev->features;
3547
Thomas Falcon032c5e82015-12-21 11:26:06 -06003548 memset(&crq, 0, sizeof(crq));
3549 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3550 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3551 crq.control_ip_offload.len =
3552 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3553 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3554 ibmvnic_send_crq(adapter, &crq);
3555}
3556
3557static void handle_error_info_rsp(union ibmvnic_crq *crq,
3558 struct ibmvnic_adapter *adapter)
3559{
3560 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003561 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003562 unsigned long flags;
3563 bool found = false;
3564 int i;
3565
3566 if (!crq->request_error_rsp.rc.code) {
3567 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3568 crq->request_error_rsp.rc.code);
3569 return;
3570 }
3571
3572 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003573 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003574 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3575 found = true;
3576 list_del(&error_buff->list);
3577 break;
3578 }
3579 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3580
3581 if (!found) {
3582 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003583 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003584 return;
3585 }
3586
3587 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003588 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003589
3590 for (i = 0; i < error_buff->len; i++) {
3591 pr_cont("%02x", (int)error_buff->buff[i]);
3592 if (i % 8 == 7)
3593 pr_cont(" ");
3594 }
3595 pr_cont("\n");
3596
3597 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3598 DMA_FROM_DEVICE);
3599 kfree(error_buff->buff);
3600 kfree(error_buff);
3601}
3602
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003603static void request_error_information(struct ibmvnic_adapter *adapter,
3604 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003605{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003606 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003607 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003609 unsigned long timeout = msecs_to_jiffies(30000);
3610 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003611 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003612 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613
3614 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3615 if (!error_buff)
3616 return;
3617
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003618 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003619 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3620 if (!error_buff->buff) {
3621 kfree(error_buff);
3622 return;
3623 }
3624
3625 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3626 DMA_FROM_DEVICE);
3627 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003628 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003629 kfree(error_buff->buff);
3630 kfree(error_buff);
3631 return;
3632 }
3633
Thomas Falcon032c5e82015-12-21 11:26:06 -06003634 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003635 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636
3637 spin_lock_irqsave(&adapter->error_list_lock, flags);
3638 list_add_tail(&error_buff->list, &adapter->errors);
3639 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3640
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003641 memset(&crq, 0, sizeof(crq));
3642 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3643 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3644 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3645 crq.request_error_info.len = cpu_to_be32(detail_len);
3646 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3647
3648 rc = ibmvnic_send_crq(adapter, &crq);
3649 if (rc) {
3650 netdev_err(netdev, "failed to request error information\n");
3651 goto err_info_fail;
3652 }
3653
3654 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3655 netdev_err(netdev, "timeout waiting for error information\n");
3656 goto err_info_fail;
3657 }
3658
3659 return;
3660
3661err_info_fail:
3662 spin_lock_irqsave(&adapter->error_list_lock, flags);
3663 list_del(&error_buff->list);
3664 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3665
3666 kfree(error_buff->buff);
3667 kfree(error_buff);
3668}
3669
3670static void handle_error_indication(union ibmvnic_crq *crq,
3671 struct ibmvnic_adapter *adapter)
3672{
3673 struct device *dev = &adapter->vdev->dev;
3674
3675 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3676 crq->error_indication.flags
3677 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3678 be32_to_cpu(crq->error_indication.error_id),
3679 be16_to_cpu(crq->error_indication.error_cause));
3680
3681 if (be32_to_cpu(crq->error_indication.error_id))
3682 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003683
3684 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3685 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003686 else
3687 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003688}
3689
Thomas Falconf8136142018-01-29 13:45:05 -06003690static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3691 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003692{
3693 struct net_device *netdev = adapter->netdev;
3694 struct device *dev = &adapter->vdev->dev;
3695 long rc;
3696
3697 rc = crq->change_mac_addr_rsp.rc.code;
3698 if (rc) {
3699 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003700 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003701 }
3702 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3703 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003704out:
3705 complete(&adapter->fw_done);
3706 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003707}
3708
3709static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3710 struct ibmvnic_adapter *adapter)
3711{
3712 struct device *dev = &adapter->vdev->dev;
3713 u64 *req_value;
3714 char *name;
3715
Thomas Falcon901e0402017-02-15 12:17:59 -06003716 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003717 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3718 case REQ_TX_QUEUES:
3719 req_value = &adapter->req_tx_queues;
3720 name = "tx";
3721 break;
3722 case REQ_RX_QUEUES:
3723 req_value = &adapter->req_rx_queues;
3724 name = "rx";
3725 break;
3726 case REQ_RX_ADD_QUEUES:
3727 req_value = &adapter->req_rx_add_queues;
3728 name = "rx_add";
3729 break;
3730 case REQ_TX_ENTRIES_PER_SUBCRQ:
3731 req_value = &adapter->req_tx_entries_per_subcrq;
3732 name = "tx_entries_per_subcrq";
3733 break;
3734 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3735 req_value = &adapter->req_rx_add_entries_per_subcrq;
3736 name = "rx_add_entries_per_subcrq";
3737 break;
3738 case REQ_MTU:
3739 req_value = &adapter->req_mtu;
3740 name = "mtu";
3741 break;
3742 case PROMISC_REQUESTED:
3743 req_value = &adapter->promisc;
3744 name = "promisc";
3745 break;
3746 default:
3747 dev_err(dev, "Got invalid cap request rsp %d\n",
3748 crq->request_capability.capability);
3749 return;
3750 }
3751
3752 switch (crq->request_capability_rsp.rc.code) {
3753 case SUCCESS:
3754 break;
3755 case PARTIALSUCCESS:
3756 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3757 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003758 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003759 number), name);
John Allene7913802018-01-18 16:27:12 -06003760
3761 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3762 REQ_MTU) {
3763 pr_err("mtu of %llu is not supported. Reverting.\n",
3764 *req_value);
3765 *req_value = adapter->fallback.mtu;
3766 } else {
3767 *req_value =
3768 be64_to_cpu(crq->request_capability_rsp.number);
3769 }
3770
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003771 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003772 return;
3773 default:
3774 dev_err(dev, "Error %d in request cap rsp\n",
3775 crq->request_capability_rsp.rc.code);
3776 return;
3777 }
3778
3779 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003780 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003781 union ibmvnic_crq newcrq;
3782 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3783 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3784 &adapter->ip_offload_buf;
3785
Thomas Falcon249168a2017-02-15 12:18:00 -06003786 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003787 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3788 buf_sz,
3789 DMA_FROM_DEVICE);
3790
3791 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3792 if (!firmware_has_feature(FW_FEATURE_CMO))
3793 dev_err(dev, "Couldn't map offload buffer\n");
3794 return;
3795 }
3796
3797 memset(&newcrq, 0, sizeof(newcrq));
3798 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3799 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3800 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3801 newcrq.query_ip_offload.ioba =
3802 cpu_to_be32(adapter->ip_offload_tok);
3803
3804 ibmvnic_send_crq(adapter, &newcrq);
3805 }
3806}
3807
3808static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3809 struct ibmvnic_adapter *adapter)
3810{
3811 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003812 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3814 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003815 int i;
3816
3817 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3818 DMA_BIDIRECTIONAL);
3819 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3820 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3821
John Allen498cd8e2016-04-06 11:49:55 -05003822 /* If the number of queues requested can't be allocated by the
3823 * server, the login response will return with code 1. We will need
3824 * to resend the login buffer with fewer queues requested.
3825 */
3826 if (login_rsp_crq->generic.rc.code) {
3827 adapter->renegotiate = true;
3828 complete(&adapter->init_done);
3829 return 0;
3830 }
3831
John Allenc26eba02017-10-26 16:23:25 -05003832 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3833
Thomas Falcon032c5e82015-12-21 11:26:06 -06003834 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3835 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3836 netdev_dbg(adapter->netdev, "%016lx\n",
3837 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3838 }
3839
3840 /* Sanity checks */
3841 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3842 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3843 adapter->req_rx_add_queues !=
3844 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3845 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3846 ibmvnic_remove(adapter->vdev);
3847 return -EIO;
3848 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003849 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003850 complete(&adapter->init_done);
3851
Thomas Falcon032c5e82015-12-21 11:26:06 -06003852 return 0;
3853}
3854
Thomas Falcon032c5e82015-12-21 11:26:06 -06003855static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3856 struct ibmvnic_adapter *adapter)
3857{
3858 struct device *dev = &adapter->vdev->dev;
3859 long rc;
3860
3861 rc = crq->request_unmap_rsp.rc.code;
3862 if (rc)
3863 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3864}
3865
3866static void handle_query_map_rsp(union ibmvnic_crq *crq,
3867 struct ibmvnic_adapter *adapter)
3868{
3869 struct net_device *netdev = adapter->netdev;
3870 struct device *dev = &adapter->vdev->dev;
3871 long rc;
3872
3873 rc = crq->query_map_rsp.rc.code;
3874 if (rc) {
3875 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3876 return;
3877 }
3878 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3879 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3880 crq->query_map_rsp.free_pages);
3881}
3882
3883static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3884 struct ibmvnic_adapter *adapter)
3885{
3886 struct net_device *netdev = adapter->netdev;
3887 struct device *dev = &adapter->vdev->dev;
3888 long rc;
3889
Thomas Falcon901e0402017-02-15 12:17:59 -06003890 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003891 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003892 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003893 rc = crq->query_capability.rc.code;
3894 if (rc) {
3895 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3896 goto out;
3897 }
3898
3899 switch (be16_to_cpu(crq->query_capability.capability)) {
3900 case MIN_TX_QUEUES:
3901 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003902 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003903 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3904 adapter->min_tx_queues);
3905 break;
3906 case MIN_RX_QUEUES:
3907 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003908 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3910 adapter->min_rx_queues);
3911 break;
3912 case MIN_RX_ADD_QUEUES:
3913 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003914 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003915 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3916 adapter->min_rx_add_queues);
3917 break;
3918 case MAX_TX_QUEUES:
3919 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003920 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003921 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3922 adapter->max_tx_queues);
3923 break;
3924 case MAX_RX_QUEUES:
3925 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003926 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003927 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3928 adapter->max_rx_queues);
3929 break;
3930 case MAX_RX_ADD_QUEUES:
3931 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003932 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003933 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3934 adapter->max_rx_add_queues);
3935 break;
3936 case MIN_TX_ENTRIES_PER_SUBCRQ:
3937 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003938 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3940 adapter->min_tx_entries_per_subcrq);
3941 break;
3942 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3943 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003944 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003945 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3946 adapter->min_rx_add_entries_per_subcrq);
3947 break;
3948 case MAX_TX_ENTRIES_PER_SUBCRQ:
3949 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003950 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003951 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3952 adapter->max_tx_entries_per_subcrq);
3953 break;
3954 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3955 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003956 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003957 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3958 adapter->max_rx_add_entries_per_subcrq);
3959 break;
3960 case TCP_IP_OFFLOAD:
3961 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003962 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003963 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3964 adapter->tcp_ip_offload);
3965 break;
3966 case PROMISC_SUPPORTED:
3967 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003968 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003969 netdev_dbg(netdev, "promisc_supported = %lld\n",
3970 adapter->promisc_supported);
3971 break;
3972 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003973 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003974 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003975 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3976 break;
3977 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003978 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003979 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003980 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3981 break;
3982 case MAX_MULTICAST_FILTERS:
3983 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003984 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3986 adapter->max_multicast_filters);
3987 break;
3988 case VLAN_HEADER_INSERTION:
3989 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003990 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003991 if (adapter->vlan_header_insertion)
3992 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3993 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3994 adapter->vlan_header_insertion);
3995 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003996 case RX_VLAN_HEADER_INSERTION:
3997 adapter->rx_vlan_header_insertion =
3998 be64_to_cpu(crq->query_capability.number);
3999 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4000 adapter->rx_vlan_header_insertion);
4001 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004002 case MAX_TX_SG_ENTRIES:
4003 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06004004 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004005 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4006 adapter->max_tx_sg_entries);
4007 break;
4008 case RX_SG_SUPPORTED:
4009 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06004010 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004011 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4012 adapter->rx_sg_supported);
4013 break;
4014 case OPT_TX_COMP_SUB_QUEUES:
4015 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004016 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4018 adapter->opt_tx_comp_sub_queues);
4019 break;
4020 case OPT_RX_COMP_QUEUES:
4021 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004022 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004023 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4024 adapter->opt_rx_comp_queues);
4025 break;
4026 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4027 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004028 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004029 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4030 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4031 break;
4032 case OPT_TX_ENTRIES_PER_SUBCRQ:
4033 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004034 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004035 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4036 adapter->opt_tx_entries_per_subcrq);
4037 break;
4038 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4039 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004040 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004041 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4042 adapter->opt_rxba_entries_per_subcrq);
4043 break;
4044 case TX_RX_DESC_REQ:
4045 adapter->tx_rx_desc_req = crq->query_capability.number;
4046 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4047 adapter->tx_rx_desc_req);
4048 break;
4049
4050 default:
4051 netdev_err(netdev, "Got invalid cap rsp %d\n",
4052 crq->query_capability.capability);
4053 }
4054
4055out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004056 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4057 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004058 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004059 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004060}
4061
Thomas Falcon032c5e82015-12-21 11:26:06 -06004062static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4063 struct ibmvnic_adapter *adapter)
4064{
4065 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4066 struct net_device *netdev = adapter->netdev;
4067 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004068 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004069 long rc;
4070
4071 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004072 (unsigned long int)cpu_to_be64(u64_crq[0]),
4073 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004074 switch (gen_crq->first) {
4075 case IBMVNIC_CRQ_INIT_RSP:
4076 switch (gen_crq->cmd) {
4077 case IBMVNIC_CRQ_INIT:
4078 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004079 adapter->from_passive_init = true;
4080 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004081 break;
4082 case IBMVNIC_CRQ_INIT_COMPLETE:
4083 dev_info(dev, "Partner initialization complete\n");
4084 send_version_xchg(adapter);
4085 break;
4086 default:
4087 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4088 }
4089 return;
4090 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004091 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004092 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004093 dev_info(dev, "Migrated, re-enabling adapter\n");
4094 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004095 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4096 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004097 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004098 } else {
4099 /* The adapter lost the connection */
4100 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4101 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004102 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004103 }
4104 return;
4105 case IBMVNIC_CRQ_CMD_RSP:
4106 break;
4107 default:
4108 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4109 gen_crq->first);
4110 return;
4111 }
4112
4113 switch (gen_crq->cmd) {
4114 case VERSION_EXCHANGE_RSP:
4115 rc = crq->version_exchange_rsp.rc.code;
4116 if (rc) {
4117 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4118 break;
4119 }
4120 dev_info(dev, "Partner protocol version is %d\n",
4121 crq->version_exchange_rsp.version);
4122 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4123 ibmvnic_version)
4124 ibmvnic_version =
4125 be16_to_cpu(crq->version_exchange_rsp.version);
4126 send_cap_queries(adapter);
4127 break;
4128 case QUERY_CAPABILITY_RSP:
4129 handle_query_cap_rsp(crq, adapter);
4130 break;
4131 case QUERY_MAP_RSP:
4132 handle_query_map_rsp(crq, adapter);
4133 break;
4134 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004135 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4136 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004137 break;
4138 case REQUEST_UNMAP_RSP:
4139 handle_request_unmap_rsp(crq, adapter);
4140 break;
4141 case REQUEST_CAPABILITY_RSP:
4142 handle_request_cap_rsp(crq, adapter);
4143 break;
4144 case LOGIN_RSP:
4145 netdev_dbg(netdev, "Got Login Response\n");
4146 handle_login_rsp(crq, adapter);
4147 break;
4148 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004149 netdev_dbg(netdev,
4150 "Got Logical Link State Response, state: %d rc: %d\n",
4151 crq->logical_link_state_rsp.link_state,
4152 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004153 adapter->logical_link_state =
4154 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004155 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4156 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004157 break;
4158 case LINK_STATE_INDICATION:
4159 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4160 adapter->phys_link_state =
4161 crq->link_state_indication.phys_link_state;
4162 adapter->logical_link_state =
4163 crq->link_state_indication.logical_link_state;
4164 break;
4165 case CHANGE_MAC_ADDR_RSP:
4166 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004167 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168 break;
4169 case ERROR_INDICATION:
4170 netdev_dbg(netdev, "Got Error Indication\n");
4171 handle_error_indication(crq, adapter);
4172 break;
4173 case REQUEST_ERROR_RSP:
4174 netdev_dbg(netdev, "Got Error Detail Response\n");
4175 handle_error_info_rsp(crq, adapter);
4176 break;
4177 case REQUEST_STATISTICS_RSP:
4178 netdev_dbg(netdev, "Got Statistics Response\n");
4179 complete(&adapter->stats_done);
4180 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004181 case QUERY_IP_OFFLOAD_RSP:
4182 netdev_dbg(netdev, "Got Query IP offload Response\n");
4183 handle_query_ip_offload_rsp(adapter);
4184 break;
4185 case MULTICAST_CTRL_RSP:
4186 netdev_dbg(netdev, "Got multicast control Response\n");
4187 break;
4188 case CONTROL_IP_OFFLOAD_RSP:
4189 netdev_dbg(netdev, "Got Control IP offload Response\n");
4190 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4191 sizeof(adapter->ip_offload_ctrl),
4192 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004193 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004194 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004195 case COLLECT_FW_TRACE_RSP:
4196 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4197 complete(&adapter->fw_done);
4198 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004199 case GET_VPD_SIZE_RSP:
4200 handle_vpd_size_rsp(crq, adapter);
4201 break;
4202 case GET_VPD_RSP:
4203 handle_vpd_rsp(crq, adapter);
4204 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004205 default:
4206 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4207 gen_crq->cmd);
4208 }
4209}
4210
4211static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4212{
4213 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004214
Thomas Falcon6c267b32017-02-15 12:17:58 -06004215 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004216 return IRQ_HANDLED;
4217}
4218
4219static void ibmvnic_tasklet(void *data)
4220{
4221 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004222 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004223 union ibmvnic_crq *crq;
4224 unsigned long flags;
4225 bool done = false;
4226
4227 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004228 while (!done) {
4229 /* Pull all the valid messages off the CRQ */
4230 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4231 ibmvnic_handle_crq(crq, adapter);
4232 crq->generic.first = 0;
4233 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004234
4235 /* remain in tasklet until all
4236 * capabilities responses are received
4237 */
4238 if (!adapter->wait_capability)
4239 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004240 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004241 /* if capabilities CRQ's were sent in this tasklet, the following
4242 * tasklet must wait until all responses are received
4243 */
4244 if (atomic_read(&adapter->running_cap_crqs) != 0)
4245 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004246 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004247}
4248
4249static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4250{
4251 struct vio_dev *vdev = adapter->vdev;
4252 int rc;
4253
4254 do {
4255 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4256 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4257
4258 if (rc)
4259 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4260
4261 return rc;
4262}
4263
4264static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4265{
4266 struct ibmvnic_crq_queue *crq = &adapter->crq;
4267 struct device *dev = &adapter->vdev->dev;
4268 struct vio_dev *vdev = adapter->vdev;
4269 int rc;
4270
4271 /* Close the CRQ */
4272 do {
4273 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4274 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4275
4276 /* Clean out the queue */
4277 memset(crq->msgs, 0, PAGE_SIZE);
4278 crq->cur = 0;
4279
4280 /* And re-open it again */
4281 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4282 crq->msg_token, PAGE_SIZE);
4283
4284 if (rc == H_CLOSED)
4285 /* Adapter is good, but other end is not ready */
4286 dev_warn(dev, "Partner adapter not ready\n");
4287 else if (rc != 0)
4288 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4289
4290 return rc;
4291}
4292
Nathan Fontenotf9928872017-03-30 02:48:54 -04004293static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004294{
4295 struct ibmvnic_crq_queue *crq = &adapter->crq;
4296 struct vio_dev *vdev = adapter->vdev;
4297 long rc;
4298
Nathan Fontenotf9928872017-03-30 02:48:54 -04004299 if (!crq->msgs)
4300 return;
4301
Thomas Falcon032c5e82015-12-21 11:26:06 -06004302 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4303 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004304 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004305 do {
4306 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4307 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4308
4309 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4310 DMA_BIDIRECTIONAL);
4311 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004312 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004313}
4314
Nathan Fontenotf9928872017-03-30 02:48:54 -04004315static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004316{
4317 struct ibmvnic_crq_queue *crq = &adapter->crq;
4318 struct device *dev = &adapter->vdev->dev;
4319 struct vio_dev *vdev = adapter->vdev;
4320 int rc, retrc = -ENOMEM;
4321
Nathan Fontenotf9928872017-03-30 02:48:54 -04004322 if (crq->msgs)
4323 return 0;
4324
Thomas Falcon032c5e82015-12-21 11:26:06 -06004325 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4326 /* Should we allocate more than one page? */
4327
4328 if (!crq->msgs)
4329 return -ENOMEM;
4330
4331 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4332 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4333 DMA_BIDIRECTIONAL);
4334 if (dma_mapping_error(dev, crq->msg_token))
4335 goto map_failed;
4336
4337 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4338 crq->msg_token, PAGE_SIZE);
4339
4340 if (rc == H_RESOURCE)
4341 /* maybe kexecing and resource is busy. try a reset */
4342 rc = ibmvnic_reset_crq(adapter);
4343 retrc = rc;
4344
4345 if (rc == H_CLOSED) {
4346 dev_warn(dev, "Partner adapter not ready\n");
4347 } else if (rc) {
4348 dev_warn(dev, "Error %d opening adapter\n", rc);
4349 goto reg_crq_failed;
4350 }
4351
4352 retrc = 0;
4353
Thomas Falcon6c267b32017-02-15 12:17:58 -06004354 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4355 (unsigned long)adapter);
4356
Thomas Falcon032c5e82015-12-21 11:26:06 -06004357 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4358 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4359 adapter);
4360 if (rc) {
4361 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4362 vdev->irq, rc);
4363 goto req_irq_failed;
4364 }
4365
4366 rc = vio_enable_interrupts(vdev);
4367 if (rc) {
4368 dev_err(dev, "Error %d enabling interrupts\n", rc);
4369 goto req_irq_failed;
4370 }
4371
4372 crq->cur = 0;
4373 spin_lock_init(&crq->lock);
4374
4375 return retrc;
4376
4377req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004378 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004379 do {
4380 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4381 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4382reg_crq_failed:
4383 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4384map_failed:
4385 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004386 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004387 return retrc;
4388}
4389
John Allenf6ef6402017-03-17 17:13:42 -05004390static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4391{
4392 struct device *dev = &adapter->vdev->dev;
4393 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004394 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004395 int rc;
4396
John Allenc26eba02017-10-26 16:23:25 -05004397 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004398 rc = ibmvnic_reset_crq(adapter);
4399 if (!rc)
4400 rc = vio_enable_interrupts(adapter->vdev);
4401 } else {
4402 rc = init_crq_queue(adapter);
4403 }
4404
John Allenf6ef6402017-03-17 17:13:42 -05004405 if (rc) {
4406 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4407 return rc;
4408 }
4409
John Allen017892c12017-05-26 10:30:19 -04004410 adapter->from_passive_init = false;
4411
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004412 old_num_rx_queues = adapter->req_rx_queues;
4413 old_num_tx_queues = adapter->req_tx_queues;
4414
John Allenf6ef6402017-03-17 17:13:42 -05004415 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004416 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004417 ibmvnic_send_crq_init(adapter);
4418 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4419 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004420 return -1;
4421 }
4422
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004423 if (adapter->init_done_rc) {
4424 release_crq_queue(adapter);
4425 return adapter->init_done_rc;
4426 }
4427
John Allen017892c12017-05-26 10:30:19 -04004428 if (adapter->from_passive_init) {
4429 adapter->state = VNIC_OPEN;
4430 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004431 return -1;
4432 }
4433
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004434 if (adapter->resetting && !adapter->wait_for_reset) {
4435 if (adapter->req_rx_queues != old_num_rx_queues ||
4436 adapter->req_tx_queues != old_num_tx_queues) {
4437 release_sub_crqs(adapter, 0);
4438 rc = init_sub_crqs(adapter);
4439 } else {
4440 rc = reset_sub_crq_queues(adapter);
4441 }
4442 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004443 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004444 }
4445
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004446 if (rc) {
4447 dev_err(dev, "Initialization of sub crqs failed\n");
4448 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004449 return rc;
4450 }
4451
4452 rc = init_sub_crq_irqs(adapter);
4453 if (rc) {
4454 dev_err(dev, "Failed to initialize sub crq irqs\n");
4455 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004456 }
4457
Thomas Falcon53cc7722018-02-26 18:10:56 -06004458 rc = init_stats_buffers(adapter);
4459 if (rc)
4460 return rc;
4461
4462 rc = init_stats_token(adapter);
4463 if (rc)
4464 return rc;
4465
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004466 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004467}
4468
Thomas Falcon40c9db82017-06-12 12:35:04 -05004469static struct device_attribute dev_attr_failover;
4470
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4472{
4473 struct ibmvnic_adapter *adapter;
4474 struct net_device *netdev;
4475 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004476 int rc;
4477
4478 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4479 dev->unit_address);
4480
4481 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4482 VETH_MAC_ADDR, NULL);
4483 if (!mac_addr_p) {
4484 dev_err(&dev->dev,
4485 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4486 __FILE__, __LINE__);
4487 return 0;
4488 }
4489
4490 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004491 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004492 if (!netdev)
4493 return -ENOMEM;
4494
4495 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004496 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004497 dev_set_drvdata(&dev->dev, netdev);
4498 adapter->vdev = dev;
4499 adapter->netdev = netdev;
4500
4501 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4502 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4503 netdev->irq = dev->irq;
4504 netdev->netdev_ops = &ibmvnic_netdev_ops;
4505 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4506 SET_NETDEV_DEV(netdev, &dev->dev);
4507
4508 spin_lock_init(&adapter->stats_lock);
4509
Thomas Falcon032c5e82015-12-21 11:26:06 -06004510 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004511 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004512
Nathan Fontenoted651a12017-05-03 14:04:38 -04004513 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4514 INIT_LIST_HEAD(&adapter->rwi_list);
4515 mutex_init(&adapter->reset_lock);
4516 mutex_init(&adapter->rwi_lock);
4517 adapter->resetting = false;
4518
John Allenc26eba02017-10-26 16:23:25 -05004519 adapter->mac_change_pending = false;
4520
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004521 do {
4522 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004523 if (rc && rc != EAGAIN)
4524 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004525 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004526
Thomas Falconf39f0d12017-02-14 10:22:59 -06004527 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004528 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4529 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004530
Thomas Falcon40c9db82017-06-12 12:35:04 -05004531 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004532 if (rc)
4533 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004534
Mick Tarsele876a8a2017-09-28 13:53:18 -07004535 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004536 rc = register_netdev(netdev);
4537 if (rc) {
4538 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004539 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004540 }
4541 dev_info(&dev->dev, "ibmvnic registered\n");
4542
Nathan Fontenot90c80142017-05-03 14:04:32 -04004543 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004544
4545 adapter->wait_for_reset = false;
4546
Thomas Falcon032c5e82015-12-21 11:26:06 -06004547 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004548
4549ibmvnic_register_fail:
4550 device_remove_file(&dev->dev, &dev_attr_failover);
4551
4552ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004553 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004554 release_crq_queue(adapter);
4555 free_netdev(netdev);
4556
4557 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558}
4559
4560static int ibmvnic_remove(struct vio_dev *dev)
4561{
4562 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004563 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004564
Nathan Fontenot90c80142017-05-03 14:04:32 -04004565 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004566 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004567 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004568
4569 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004570 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004571 release_crq_queue(adapter);
4572
Thomas Falcon53cc7722018-02-26 18:10:56 -06004573 release_stats_token(adapter);
4574 release_stats_buffers(adapter);
4575
Nathan Fontenot90c80142017-05-03 14:04:32 -04004576 adapter->state = VNIC_REMOVED;
4577
Nathan Fontenoted651a12017-05-03 14:04:38 -04004578 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004579 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004580 free_netdev(netdev);
4581 dev_set_drvdata(&dev->dev, NULL);
4582
4583 return 0;
4584}
4585
Thomas Falcon40c9db82017-06-12 12:35:04 -05004586static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4587 const char *buf, size_t count)
4588{
4589 struct net_device *netdev = dev_get_drvdata(dev);
4590 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4591 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4592 __be64 session_token;
4593 long rc;
4594
4595 if (!sysfs_streq(buf, "1"))
4596 return -EINVAL;
4597
4598 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4599 H_GET_SESSION_TOKEN, 0, 0, 0);
4600 if (rc) {
4601 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4602 rc);
4603 return -EINVAL;
4604 }
4605
4606 session_token = (__be64)retbuf[0];
4607 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4608 be64_to_cpu(session_token));
4609 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4610 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4611 if (rc) {
4612 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4613 rc);
4614 return -EINVAL;
4615 }
4616
4617 return count;
4618}
4619
Joe Perches6cbaefb2017-12-19 10:15:09 -08004620static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004621
Thomas Falcon032c5e82015-12-21 11:26:06 -06004622static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4623{
4624 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4625 struct ibmvnic_adapter *adapter;
4626 struct iommu_table *tbl;
4627 unsigned long ret = 0;
4628 int i;
4629
4630 tbl = get_iommu_table_base(&vdev->dev);
4631
4632 /* netdev inits at probe time along with the structures we need below*/
4633 if (!netdev)
4634 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4635
4636 adapter = netdev_priv(netdev);
4637
4638 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004639 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4640
4641 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4642 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4643
4644 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4645 i++)
4646 ret += adapter->rx_pool[i].size *
4647 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4648
4649 return ret;
4650}
4651
4652static int ibmvnic_resume(struct device *dev)
4653{
4654 struct net_device *netdev = dev_get_drvdata(dev);
4655 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004656
John Allencb89ba22017-06-19 11:27:53 -05004657 if (adapter->state != VNIC_OPEN)
4658 return 0;
4659
John Allena2488782017-07-24 13:26:06 -05004660 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004661
4662 return 0;
4663}
4664
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304665static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004666 {"network", "IBM,vnic"},
4667 {"", "" }
4668};
4669MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4670
4671static const struct dev_pm_ops ibmvnic_pm_ops = {
4672 .resume = ibmvnic_resume
4673};
4674
4675static struct vio_driver ibmvnic_driver = {
4676 .id_table = ibmvnic_device_table,
4677 .probe = ibmvnic_probe,
4678 .remove = ibmvnic_remove,
4679 .get_desired_dma = ibmvnic_get_desired_dma,
4680 .name = ibmvnic_driver_name,
4681 .pm = &ibmvnic_pm_ops,
4682};
4683
4684/* module functions */
4685static int __init ibmvnic_module_init(void)
4686{
4687 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4688 IBMVNIC_DRIVER_VERSION);
4689
4690 return vio_register_driver(&ibmvnic_driver);
4691}
4692
4693static void __exit ibmvnic_module_exit(void)
4694{
4695 vio_unregister_driver(&ibmvnic_driver);
4696}
4697
4698module_init(ibmvnic_module_init);
4699module_exit(ibmvnic_module_exit);