blob: e02d3b9aaf152c9893368b39be4d5d3e0338b632 [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 Falconf10b09e2018-03-12 11:51:05 -05002052static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2053 struct net_device *dev,
2054 netdev_features_t features)
2055{
2056 /* Some backing hardware adapters can not
2057 * handle packets with a MSS less than 224
2058 * or with only one segment.
2059 */
2060 if (skb_is_gso(skb)) {
2061 if (skb_shinfo(skb)->gso_size < 224 ||
2062 skb_shinfo(skb)->gso_segs == 1)
2063 features &= ~NETIF_F_GSO_MASK;
2064 }
2065
2066 return features;
2067}
2068
Thomas Falcon032c5e82015-12-21 11:26:06 -06002069static const struct net_device_ops ibmvnic_netdev_ops = {
2070 .ndo_open = ibmvnic_open,
2071 .ndo_stop = ibmvnic_close,
2072 .ndo_start_xmit = ibmvnic_xmit,
2073 .ndo_set_rx_mode = ibmvnic_set_multi,
2074 .ndo_set_mac_address = ibmvnic_set_mac,
2075 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002076 .ndo_tx_timeout = ibmvnic_tx_timeout,
2077#ifdef CONFIG_NET_POLL_CONTROLLER
2078 .ndo_poll_controller = ibmvnic_netpoll_controller,
2079#endif
John Allen3a807b72017-06-06 16:55:52 -05002080 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falconf10b09e2018-03-12 11:51:05 -05002081 .ndo_features_check = ibmvnic_features_check,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002082};
2083
2084/* ethtool functions */
2085
Philippe Reynes8a433792017-01-07 22:37:29 +01002086static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2087 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088{
Philippe Reynes8a433792017-01-07 22:37:29 +01002089 u32 supported, advertising;
2090
2091 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002092 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002093 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002094 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002095 cmd->base.speed = SPEED_1000;
2096 cmd->base.duplex = DUPLEX_FULL;
2097 cmd->base.port = PORT_FIBRE;
2098 cmd->base.phy_address = 0;
2099 cmd->base.autoneg = AUTONEG_ENABLE;
2100
2101 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2102 supported);
2103 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2104 advertising);
2105
Thomas Falcon032c5e82015-12-21 11:26:06 -06002106 return 0;
2107}
2108
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002109static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002110 struct ethtool_drvinfo *info)
2111{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002112 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2113
Thomas Falcon032c5e82015-12-21 11:26:06 -06002114 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2115 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002116 strlcpy(info->fw_version, adapter->fw_version,
2117 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002118}
2119
2120static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2121{
2122 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2123
2124 return adapter->msg_enable;
2125}
2126
2127static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2128{
2129 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2130
2131 adapter->msg_enable = data;
2132}
2133
2134static u32 ibmvnic_get_link(struct net_device *netdev)
2135{
2136 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2137
2138 /* Don't need to send a query because we request a logical link up at
2139 * init and then we wait for link state indications
2140 */
2141 return adapter->logical_link_state;
2142}
2143
2144static void ibmvnic_get_ringparam(struct net_device *netdev,
2145 struct ethtool_ringparam *ring)
2146{
John Allenbc131b32017-08-02 16:46:30 -05002147 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2148
2149 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2150 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002151 ring->rx_mini_max_pending = 0;
2152 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002153 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2154 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002155 ring->rx_mini_pending = 0;
2156 ring->rx_jumbo_pending = 0;
2157}
2158
John Allenc26eba02017-10-26 16:23:25 -05002159static int ibmvnic_set_ringparam(struct net_device *netdev,
2160 struct ethtool_ringparam *ring)
2161{
2162 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2163
2164 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2165 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2166 netdev_err(netdev, "Invalid request.\n");
2167 netdev_err(netdev, "Max tx buffers = %llu\n",
2168 adapter->max_rx_add_entries_per_subcrq);
2169 netdev_err(netdev, "Max rx buffers = %llu\n",
2170 adapter->max_tx_entries_per_subcrq);
2171 return -EINVAL;
2172 }
2173
2174 adapter->desired.rx_entries = ring->rx_pending;
2175 adapter->desired.tx_entries = ring->tx_pending;
2176
2177 return wait_for_reset(adapter);
2178}
2179
John Allenc2dbeb62017-08-02 16:47:17 -05002180static void ibmvnic_get_channels(struct net_device *netdev,
2181 struct ethtool_channels *channels)
2182{
2183 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2184
2185 channels->max_rx = adapter->max_rx_queues;
2186 channels->max_tx = adapter->max_tx_queues;
2187 channels->max_other = 0;
2188 channels->max_combined = 0;
2189 channels->rx_count = adapter->req_rx_queues;
2190 channels->tx_count = adapter->req_tx_queues;
2191 channels->other_count = 0;
2192 channels->combined_count = 0;
2193}
2194
John Allenc26eba02017-10-26 16:23:25 -05002195static int ibmvnic_set_channels(struct net_device *netdev,
2196 struct ethtool_channels *channels)
2197{
2198 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2199
2200 adapter->desired.rx_queues = channels->rx_count;
2201 adapter->desired.tx_queues = channels->tx_count;
2202
2203 return wait_for_reset(adapter);
2204}
2205
Thomas Falcon032c5e82015-12-21 11:26:06 -06002206static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2207{
John Allen3d52b592017-08-02 16:44:14 -05002208 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002209 int i;
2210
2211 if (stringset != ETH_SS_STATS)
2212 return;
2213
2214 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2215 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002216
2217 for (i = 0; i < adapter->req_tx_queues; i++) {
2218 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2219 data += ETH_GSTRING_LEN;
2220
2221 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2222 data += ETH_GSTRING_LEN;
2223
2224 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2225 data += ETH_GSTRING_LEN;
2226 }
2227
2228 for (i = 0; i < adapter->req_rx_queues; i++) {
2229 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2230 data += ETH_GSTRING_LEN;
2231
2232 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2233 data += ETH_GSTRING_LEN;
2234
2235 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2236 data += ETH_GSTRING_LEN;
2237 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002238}
2239
2240static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2241{
John Allen3d52b592017-08-02 16:44:14 -05002242 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2243
Thomas Falcon032c5e82015-12-21 11:26:06 -06002244 switch (sset) {
2245 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002246 return ARRAY_SIZE(ibmvnic_stats) +
2247 adapter->req_tx_queues * NUM_TX_STATS +
2248 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002249 default:
2250 return -EOPNOTSUPP;
2251 }
2252}
2253
2254static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2255 struct ethtool_stats *stats, u64 *data)
2256{
2257 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2258 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002259 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002260
2261 memset(&crq, 0, sizeof(crq));
2262 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2263 crq.request_statistics.cmd = REQUEST_STATISTICS;
2264 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2265 crq.request_statistics.len =
2266 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267
2268 /* Wait for data to be written */
2269 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002270 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002271 wait_for_completion(&adapter->stats_done);
2272
2273 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002274 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2275 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002276
2277 for (j = 0; j < adapter->req_tx_queues; j++) {
2278 data[i] = adapter->tx_stats_buffers[j].packets;
2279 i++;
2280 data[i] = adapter->tx_stats_buffers[j].bytes;
2281 i++;
2282 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2283 i++;
2284 }
2285
2286 for (j = 0; j < adapter->req_rx_queues; j++) {
2287 data[i] = adapter->rx_stats_buffers[j].packets;
2288 i++;
2289 data[i] = adapter->rx_stats_buffers[j].bytes;
2290 i++;
2291 data[i] = adapter->rx_stats_buffers[j].interrupts;
2292 i++;
2293 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002294}
2295
2296static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002297 .get_drvinfo = ibmvnic_get_drvinfo,
2298 .get_msglevel = ibmvnic_get_msglevel,
2299 .set_msglevel = ibmvnic_set_msglevel,
2300 .get_link = ibmvnic_get_link,
2301 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002302 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002303 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002304 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002305 .get_strings = ibmvnic_get_strings,
2306 .get_sset_count = ibmvnic_get_sset_count,
2307 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002308 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002309};
2310
2311/* Routines for managing CRQs/sCRQs */
2312
Nathan Fontenot57a49432017-05-26 10:31:12 -04002313static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2314 struct ibmvnic_sub_crq_queue *scrq)
2315{
2316 int rc;
2317
2318 if (scrq->irq) {
2319 free_irq(scrq->irq, scrq);
2320 irq_dispose_mapping(scrq->irq);
2321 scrq->irq = 0;
2322 }
2323
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002324 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002325 scrq->cur = 0;
2326
2327 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2328 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2329 return rc;
2330}
2331
2332static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2333{
2334 int i, rc;
2335
2336 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002337 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002338 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2339 if (rc)
2340 return rc;
2341 }
2342
2343 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002344 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002345 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2346 if (rc)
2347 return rc;
2348 }
2349
Nathan Fontenot57a49432017-05-26 10:31:12 -04002350 return rc;
2351}
2352
Thomas Falcon032c5e82015-12-21 11:26:06 -06002353static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002354 struct ibmvnic_sub_crq_queue *scrq,
2355 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002356{
2357 struct device *dev = &adapter->vdev->dev;
2358 long rc;
2359
2360 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2361
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002362 if (do_h_free) {
2363 /* Close the sub-crqs */
2364 do {
2365 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2366 adapter->vdev->unit_address,
2367 scrq->crq_num);
2368 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002369
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002370 if (rc) {
2371 netdev_err(adapter->netdev,
2372 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2373 scrq->crq_num, rc);
2374 }
Thomas Falconffa73852017-04-19 13:44:29 -04002375 }
2376
Thomas Falcon032c5e82015-12-21 11:26:06 -06002377 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2378 DMA_BIDIRECTIONAL);
2379 free_pages((unsigned long)scrq->msgs, 2);
2380 kfree(scrq);
2381}
2382
2383static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2384 *adapter)
2385{
2386 struct device *dev = &adapter->vdev->dev;
2387 struct ibmvnic_sub_crq_queue *scrq;
2388 int rc;
2389
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002390 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002391 if (!scrq)
2392 return NULL;
2393
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002394 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002395 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002396 if (!scrq->msgs) {
2397 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2398 goto zero_page_failed;
2399 }
2400
2401 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2402 DMA_BIDIRECTIONAL);
2403 if (dma_mapping_error(dev, scrq->msg_token)) {
2404 dev_warn(dev, "Couldn't map crq queue messages page\n");
2405 goto map_failed;
2406 }
2407
2408 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2409 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2410
2411 if (rc == H_RESOURCE)
2412 rc = ibmvnic_reset_crq(adapter);
2413
2414 if (rc == H_CLOSED) {
2415 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2416 } else if (rc) {
2417 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2418 goto reg_failed;
2419 }
2420
Thomas Falcon032c5e82015-12-21 11:26:06 -06002421 scrq->adapter = adapter;
2422 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002423 spin_lock_init(&scrq->lock);
2424
2425 netdev_dbg(adapter->netdev,
2426 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2427 scrq->crq_num, scrq->hw_irq, scrq->irq);
2428
2429 return scrq;
2430
Thomas Falcon032c5e82015-12-21 11:26:06 -06002431reg_failed:
2432 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2433 DMA_BIDIRECTIONAL);
2434map_failed:
2435 free_pages((unsigned long)scrq->msgs, 2);
2436zero_page_failed:
2437 kfree(scrq);
2438
2439 return NULL;
2440}
2441
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002442static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002443{
2444 int i;
2445
2446 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002447 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002448 if (!adapter->tx_scrq[i])
2449 continue;
2450
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002451 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2452 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002453 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002454 free_irq(adapter->tx_scrq[i]->irq,
2455 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002456 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002457 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002458 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002459
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002460 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2461 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002462 }
2463
Nathan Fontenot9501df32017-03-15 23:38:07 -04002464 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002465 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002466 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002467 }
2468
2469 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002470 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002471 if (!adapter->rx_scrq[i])
2472 continue;
2473
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002474 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2475 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002476 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002477 free_irq(adapter->rx_scrq[i]->irq,
2478 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002479 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002480 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002481 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002482
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002483 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2484 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002485 }
2486
Nathan Fontenot9501df32017-03-15 23:38:07 -04002487 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002488 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002489 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002490 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002491}
2492
2493static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2494 struct ibmvnic_sub_crq_queue *scrq)
2495{
2496 struct device *dev = &adapter->vdev->dev;
2497 unsigned long rc;
2498
2499 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2500 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2501 if (rc)
2502 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2503 scrq->hw_irq, rc);
2504 return rc;
2505}
2506
2507static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2508 struct ibmvnic_sub_crq_queue *scrq)
2509{
2510 struct device *dev = &adapter->vdev->dev;
2511 unsigned long rc;
2512
2513 if (scrq->hw_irq > 0x100000000ULL) {
2514 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2515 return 1;
2516 }
2517
2518 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2519 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2520 if (rc)
2521 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2522 scrq->hw_irq, rc);
2523 return rc;
2524}
2525
2526static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2527 struct ibmvnic_sub_crq_queue *scrq)
2528{
2529 struct device *dev = &adapter->vdev->dev;
2530 struct ibmvnic_tx_buff *txbuff;
2531 union sub_crq *next;
2532 int index;
2533 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002534 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002535
2536restart_loop:
2537 while (pending_scrq(adapter, scrq)) {
2538 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002539 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002540
2541 next = ibmvnic_next_scrq(adapter, scrq);
2542 for (i = 0; i < next->tx_comp.num_comps; i++) {
2543 if (next->tx_comp.rcs[i]) {
2544 dev_err(dev, "tx error %x\n",
2545 next->tx_comp.rcs[i]);
2546 continue;
2547 }
2548 index = be32_to_cpu(next->tx_comp.correlators[i]);
2549 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2550
2551 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2552 if (!txbuff->data_dma[j])
2553 continue;
2554
2555 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002556 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002557 /* if sub_crq was sent indirectly */
2558 first = txbuff->indir_arr[0].generic.first;
2559 if (first == IBMVNIC_CRQ_CMD) {
2560 dma_unmap_single(dev, txbuff->indir_dma,
2561 sizeof(txbuff->indir_arr),
2562 DMA_TO_DEVICE);
2563 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002564
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002565 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002566 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002567 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002568 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002569
Thomas Falconffc385b2018-02-18 10:08:41 -06002570 num_entries += txbuff->num_entries;
2571
Thomas Falcon032c5e82015-12-21 11:26:06 -06002572 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2573 producer_index] = index;
2574 adapter->tx_pool[pool].producer_index =
2575 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002576 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002577 }
2578 /* remove tx_comp scrq*/
2579 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002580
Thomas Falconffc385b2018-02-18 10:08:41 -06002581 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002582 (adapter->req_tx_entries_per_subcrq / 2) &&
2583 __netif_subqueue_stopped(adapter->netdev,
2584 scrq->pool_index)) {
2585 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002586 netdev_dbg(adapter->netdev, "Started queue %d\n",
2587 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002588 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002589 }
2590
2591 enable_scrq_irq(adapter, scrq);
2592
2593 if (pending_scrq(adapter, scrq)) {
2594 disable_scrq_irq(adapter, scrq);
2595 goto restart_loop;
2596 }
2597
2598 return 0;
2599}
2600
2601static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2602{
2603 struct ibmvnic_sub_crq_queue *scrq = instance;
2604 struct ibmvnic_adapter *adapter = scrq->adapter;
2605
2606 disable_scrq_irq(adapter, scrq);
2607 ibmvnic_complete_tx(adapter, scrq);
2608
2609 return IRQ_HANDLED;
2610}
2611
2612static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2613{
2614 struct ibmvnic_sub_crq_queue *scrq = instance;
2615 struct ibmvnic_adapter *adapter = scrq->adapter;
2616
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002617 /* When booting a kdump kernel we can hit pending interrupts
2618 * prior to completing driver initialization.
2619 */
2620 if (unlikely(adapter->state != VNIC_OPEN))
2621 return IRQ_NONE;
2622
John Allen3d52b592017-08-02 16:44:14 -05002623 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2624
Thomas Falcon032c5e82015-12-21 11:26:06 -06002625 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2626 disable_scrq_irq(adapter, scrq);
2627 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2628 }
2629
2630 return IRQ_HANDLED;
2631}
2632
Thomas Falconea22d512016-07-06 15:35:17 -05002633static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2634{
2635 struct device *dev = &adapter->vdev->dev;
2636 struct ibmvnic_sub_crq_queue *scrq;
2637 int i = 0, j = 0;
2638 int rc = 0;
2639
2640 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002641 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2642 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002643 scrq = adapter->tx_scrq[i];
2644 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2645
Michael Ellerman99c17902016-09-10 19:59:05 +10002646 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002647 rc = -EINVAL;
2648 dev_err(dev, "Error mapping irq\n");
2649 goto req_tx_irq_failed;
2650 }
2651
2652 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2653 0, "ibmvnic_tx", scrq);
2654
2655 if (rc) {
2656 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2657 scrq->irq, rc);
2658 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002659 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002660 }
2661 }
2662
2663 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002664 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2665 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002666 scrq = adapter->rx_scrq[i];
2667 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002668 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002669 rc = -EINVAL;
2670 dev_err(dev, "Error mapping irq\n");
2671 goto req_rx_irq_failed;
2672 }
2673 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2674 0, "ibmvnic_rx", scrq);
2675 if (rc) {
2676 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2677 scrq->irq, rc);
2678 irq_dispose_mapping(scrq->irq);
2679 goto req_rx_irq_failed;
2680 }
2681 }
2682 return rc;
2683
2684req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002685 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002686 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2687 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002688 }
Thomas Falconea22d512016-07-06 15:35:17 -05002689 i = adapter->req_tx_queues;
2690req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002691 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002692 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2693 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002694 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002695 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002696 return rc;
2697}
2698
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002699static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700{
2701 struct device *dev = &adapter->vdev->dev;
2702 struct ibmvnic_sub_crq_queue **allqueues;
2703 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002704 int total_queues;
2705 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002706 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2709
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002710 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002711 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002712 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713
2714 for (i = 0; i < total_queues; i++) {
2715 allqueues[i] = init_sub_crq_queue(adapter);
2716 if (!allqueues[i]) {
2717 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2718 break;
2719 }
2720 registered_queues++;
2721 }
2722
2723 /* Make sure we were able to register the minimum number of queues */
2724 if (registered_queues <
2725 adapter->min_tx_queues + adapter->min_rx_queues) {
2726 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2727 goto tx_failed;
2728 }
2729
2730 /* Distribute the failed allocated queues*/
2731 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2732 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2733 switch (i % 3) {
2734 case 0:
2735 if (adapter->req_rx_queues > adapter->min_rx_queues)
2736 adapter->req_rx_queues--;
2737 else
2738 more++;
2739 break;
2740 case 1:
2741 if (adapter->req_tx_queues > adapter->min_tx_queues)
2742 adapter->req_tx_queues--;
2743 else
2744 more++;
2745 break;
2746 }
2747 }
2748
2749 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002750 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 if (!adapter->tx_scrq)
2752 goto tx_failed;
2753
2754 for (i = 0; i < adapter->req_tx_queues; i++) {
2755 adapter->tx_scrq[i] = allqueues[i];
2756 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002757 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002758 }
2759
2760 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002761 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002762 if (!adapter->rx_scrq)
2763 goto rx_failed;
2764
2765 for (i = 0; i < adapter->req_rx_queues; i++) {
2766 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2767 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002768 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002769 }
2770
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002771 kfree(allqueues);
2772 return 0;
2773
2774rx_failed:
2775 kfree(adapter->tx_scrq);
2776 adapter->tx_scrq = NULL;
2777tx_failed:
2778 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002779 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002780 kfree(allqueues);
2781 return -1;
2782}
2783
2784static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2785{
2786 struct device *dev = &adapter->vdev->dev;
2787 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002788 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002789
2790 if (!retry) {
2791 /* Sub-CRQ entries are 32 byte long */
2792 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2793
2794 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2795 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2796 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2797 return;
2798 }
2799
John Allenc26eba02017-10-26 16:23:25 -05002800 if (adapter->desired.mtu)
2801 adapter->req_mtu = adapter->desired.mtu;
2802 else
2803 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002804
John Allenc26eba02017-10-26 16:23:25 -05002805 if (!adapter->desired.tx_entries)
2806 adapter->desired.tx_entries =
2807 adapter->max_tx_entries_per_subcrq;
2808 if (!adapter->desired.rx_entries)
2809 adapter->desired.rx_entries =
2810 adapter->max_rx_add_entries_per_subcrq;
2811
2812 max_entries = IBMVNIC_MAX_LTB_SIZE /
2813 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2814
2815 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2816 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2817 adapter->desired.tx_entries = max_entries;
2818 }
2819
2820 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2821 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2822 adapter->desired.rx_entries = max_entries;
2823 }
2824
2825 if (adapter->desired.tx_entries)
2826 adapter->req_tx_entries_per_subcrq =
2827 adapter->desired.tx_entries;
2828 else
2829 adapter->req_tx_entries_per_subcrq =
2830 adapter->max_tx_entries_per_subcrq;
2831
2832 if (adapter->desired.rx_entries)
2833 adapter->req_rx_add_entries_per_subcrq =
2834 adapter->desired.rx_entries;
2835 else
2836 adapter->req_rx_add_entries_per_subcrq =
2837 adapter->max_rx_add_entries_per_subcrq;
2838
2839 if (adapter->desired.tx_queues)
2840 adapter->req_tx_queues =
2841 adapter->desired.tx_queues;
2842 else
2843 adapter->req_tx_queues =
2844 adapter->opt_tx_comp_sub_queues;
2845
2846 if (adapter->desired.rx_queues)
2847 adapter->req_rx_queues =
2848 adapter->desired.rx_queues;
2849 else
2850 adapter->req_rx_queues =
2851 adapter->opt_rx_comp_queues;
2852
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002853 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002854 }
2855
Thomas Falcon032c5e82015-12-21 11:26:06 -06002856 memset(&crq, 0, sizeof(crq));
2857 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2858 crq.request_capability.cmd = REQUEST_CAPABILITY;
2859
2860 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002861 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002862 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002863 ibmvnic_send_crq(adapter, &crq);
2864
2865 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002866 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002867 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002868 ibmvnic_send_crq(adapter, &crq);
2869
2870 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002871 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002872 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002873 ibmvnic_send_crq(adapter, &crq);
2874
2875 crq.request_capability.capability =
2876 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2877 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002878 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002879 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002880 ibmvnic_send_crq(adapter, &crq);
2881
2882 crq.request_capability.capability =
2883 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2884 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002885 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002886 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002887 ibmvnic_send_crq(adapter, &crq);
2888
2889 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002890 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002891 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002892 ibmvnic_send_crq(adapter, &crq);
2893
2894 if (adapter->netdev->flags & IFF_PROMISC) {
2895 if (adapter->promisc_supported) {
2896 crq.request_capability.capability =
2897 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002898 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002899 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002900 ibmvnic_send_crq(adapter, &crq);
2901 }
2902 } else {
2903 crq.request_capability.capability =
2904 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002905 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002906 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002907 ibmvnic_send_crq(adapter, &crq);
2908 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002909}
2910
2911static int pending_scrq(struct ibmvnic_adapter *adapter,
2912 struct ibmvnic_sub_crq_queue *scrq)
2913{
2914 union sub_crq *entry = &scrq->msgs[scrq->cur];
2915
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002916 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002917 return 1;
2918 else
2919 return 0;
2920}
2921
2922static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2923 struct ibmvnic_sub_crq_queue *scrq)
2924{
2925 union sub_crq *entry;
2926 unsigned long flags;
2927
2928 spin_lock_irqsave(&scrq->lock, flags);
2929 entry = &scrq->msgs[scrq->cur];
2930 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2931 if (++scrq->cur == scrq->size)
2932 scrq->cur = 0;
2933 } else {
2934 entry = NULL;
2935 }
2936 spin_unlock_irqrestore(&scrq->lock, flags);
2937
2938 return entry;
2939}
2940
2941static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2942{
2943 struct ibmvnic_crq_queue *queue = &adapter->crq;
2944 union ibmvnic_crq *crq;
2945
2946 crq = &queue->msgs[queue->cur];
2947 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2948 if (++queue->cur == queue->size)
2949 queue->cur = 0;
2950 } else {
2951 crq = NULL;
2952 }
2953
2954 return crq;
2955}
2956
2957static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2958 union sub_crq *sub_crq)
2959{
2960 unsigned int ua = adapter->vdev->unit_address;
2961 struct device *dev = &adapter->vdev->dev;
2962 u64 *u64_crq = (u64 *)sub_crq;
2963 int rc;
2964
2965 netdev_dbg(adapter->netdev,
2966 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2967 (unsigned long int)cpu_to_be64(remote_handle),
2968 (unsigned long int)cpu_to_be64(u64_crq[0]),
2969 (unsigned long int)cpu_to_be64(u64_crq[1]),
2970 (unsigned long int)cpu_to_be64(u64_crq[2]),
2971 (unsigned long int)cpu_to_be64(u64_crq[3]));
2972
2973 /* Make sure the hypervisor sees the complete request */
2974 mb();
2975
2976 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2977 cpu_to_be64(remote_handle),
2978 cpu_to_be64(u64_crq[0]),
2979 cpu_to_be64(u64_crq[1]),
2980 cpu_to_be64(u64_crq[2]),
2981 cpu_to_be64(u64_crq[3]));
2982
2983 if (rc) {
2984 if (rc == H_CLOSED)
2985 dev_warn(dev, "CRQ Queue closed\n");
2986 dev_err(dev, "Send error (rc=%d)\n", rc);
2987 }
2988
2989 return rc;
2990}
2991
Thomas Falconad7775d2016-04-01 17:20:34 -05002992static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2993 u64 remote_handle, u64 ioba, u64 num_entries)
2994{
2995 unsigned int ua = adapter->vdev->unit_address;
2996 struct device *dev = &adapter->vdev->dev;
2997 int rc;
2998
2999 /* Make sure the hypervisor sees the complete request */
3000 mb();
3001 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3002 cpu_to_be64(remote_handle),
3003 ioba, num_entries);
3004
3005 if (rc) {
3006 if (rc == H_CLOSED)
3007 dev_warn(dev, "CRQ Queue closed\n");
3008 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3009 }
3010
3011 return rc;
3012}
3013
Thomas Falcon032c5e82015-12-21 11:26:06 -06003014static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3015 union ibmvnic_crq *crq)
3016{
3017 unsigned int ua = adapter->vdev->unit_address;
3018 struct device *dev = &adapter->vdev->dev;
3019 u64 *u64_crq = (u64 *)crq;
3020 int rc;
3021
3022 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3023 (unsigned long int)cpu_to_be64(u64_crq[0]),
3024 (unsigned long int)cpu_to_be64(u64_crq[1]));
3025
3026 /* Make sure the hypervisor sees the complete request */
3027 mb();
3028
3029 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3030 cpu_to_be64(u64_crq[0]),
3031 cpu_to_be64(u64_crq[1]));
3032
3033 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003034 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003035 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06003036 if (adapter->resetting)
3037 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3038 }
3039
Thomas Falcon032c5e82015-12-21 11:26:06 -06003040 dev_warn(dev, "Send error (rc=%d)\n", rc);
3041 }
3042
3043 return rc;
3044}
3045
3046static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3047{
3048 union ibmvnic_crq crq;
3049
3050 memset(&crq, 0, sizeof(crq));
3051 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3052 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3053 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3054
3055 return ibmvnic_send_crq(adapter, &crq);
3056}
3057
Thomas Falcon032c5e82015-12-21 11:26:06 -06003058static int send_version_xchg(struct ibmvnic_adapter *adapter)
3059{
3060 union ibmvnic_crq crq;
3061
3062 memset(&crq, 0, sizeof(crq));
3063 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3064 crq.version_exchange.cmd = VERSION_EXCHANGE;
3065 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3066
3067 return ibmvnic_send_crq(adapter, &crq);
3068}
3069
Nathan Fontenot37798d02017-11-08 11:23:56 -06003070struct vnic_login_client_data {
3071 u8 type;
3072 __be16 len;
3073 char name;
3074} __packed;
3075
3076static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3077{
3078 int len;
3079
3080 /* Calculate the amount of buffer space needed for the
3081 * vnic client data in the login buffer. There are four entries,
3082 * OS name, LPAR name, device name, and a null last entry.
3083 */
3084 len = 4 * sizeof(struct vnic_login_client_data);
3085 len += 6; /* "Linux" plus NULL */
3086 len += strlen(utsname()->nodename) + 1;
3087 len += strlen(adapter->netdev->name) + 1;
3088
3089 return len;
3090}
3091
3092static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3093 struct vnic_login_client_data *vlcd)
3094{
3095 const char *os_name = "Linux";
3096 int len;
3097
3098 /* Type 1 - LPAR OS */
3099 vlcd->type = 1;
3100 len = strlen(os_name) + 1;
3101 vlcd->len = cpu_to_be16(len);
3102 strncpy(&vlcd->name, os_name, len);
3103 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3104
3105 /* Type 2 - LPAR name */
3106 vlcd->type = 2;
3107 len = strlen(utsname()->nodename) + 1;
3108 vlcd->len = cpu_to_be16(len);
3109 strncpy(&vlcd->name, utsname()->nodename, len);
3110 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3111
3112 /* Type 3 - device name */
3113 vlcd->type = 3;
3114 len = strlen(adapter->netdev->name) + 1;
3115 vlcd->len = cpu_to_be16(len);
3116 strncpy(&vlcd->name, adapter->netdev->name, len);
3117}
3118
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003119static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003120{
3121 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3122 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003123 struct device *dev = &adapter->vdev->dev;
3124 dma_addr_t rsp_buffer_token;
3125 dma_addr_t buffer_token;
3126 size_t rsp_buffer_size;
3127 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003128 size_t buffer_size;
3129 __be64 *tx_list_p;
3130 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003131 int client_data_len;
3132 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 int i;
3134
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003135 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3136 netdev_err(adapter->netdev,
3137 "RX or TX queues are not allocated, device login failed\n");
3138 return -1;
3139 }
3140
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003141 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003142 client_data_len = vnic_client_data_len(adapter);
3143
Thomas Falcon032c5e82015-12-21 11:26:06 -06003144 buffer_size =
3145 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003146 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3147 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003148
Nathan Fontenot37798d02017-11-08 11:23:56 -06003149 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003150 if (!login_buffer)
3151 goto buf_alloc_failed;
3152
3153 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3154 DMA_TO_DEVICE);
3155 if (dma_mapping_error(dev, buffer_token)) {
3156 dev_err(dev, "Couldn't map login buffer\n");
3157 goto buf_map_failed;
3158 }
3159
John Allen498cd8e2016-04-06 11:49:55 -05003160 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3161 sizeof(u64) * adapter->req_tx_queues +
3162 sizeof(u64) * adapter->req_rx_queues +
3163 sizeof(u64) * adapter->req_rx_queues +
3164 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003165
3166 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3167 if (!login_rsp_buffer)
3168 goto buf_rsp_alloc_failed;
3169
3170 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3171 rsp_buffer_size, DMA_FROM_DEVICE);
3172 if (dma_mapping_error(dev, rsp_buffer_token)) {
3173 dev_err(dev, "Couldn't map login rsp buffer\n");
3174 goto buf_rsp_map_failed;
3175 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003176
Thomas Falcon032c5e82015-12-21 11:26:06 -06003177 adapter->login_buf = login_buffer;
3178 adapter->login_buf_token = buffer_token;
3179 adapter->login_buf_sz = buffer_size;
3180 adapter->login_rsp_buf = login_rsp_buffer;
3181 adapter->login_rsp_buf_token = rsp_buffer_token;
3182 adapter->login_rsp_buf_sz = rsp_buffer_size;
3183
3184 login_buffer->len = cpu_to_be32(buffer_size);
3185 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3186 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3187 login_buffer->off_txcomp_subcrqs =
3188 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3189 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3190 login_buffer->off_rxcomp_subcrqs =
3191 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3192 sizeof(u64) * adapter->req_tx_queues);
3193 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3194 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3195
3196 tx_list_p = (__be64 *)((char *)login_buffer +
3197 sizeof(struct ibmvnic_login_buffer));
3198 rx_list_p = (__be64 *)((char *)login_buffer +
3199 sizeof(struct ibmvnic_login_buffer) +
3200 sizeof(u64) * adapter->req_tx_queues);
3201
3202 for (i = 0; i < adapter->req_tx_queues; i++) {
3203 if (adapter->tx_scrq[i]) {
3204 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3205 crq_num);
3206 }
3207 }
3208
3209 for (i = 0; i < adapter->req_rx_queues; i++) {
3210 if (adapter->rx_scrq[i]) {
3211 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3212 crq_num);
3213 }
3214 }
3215
Nathan Fontenot37798d02017-11-08 11:23:56 -06003216 /* Insert vNIC login client data */
3217 vlcd = (struct vnic_login_client_data *)
3218 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3219 login_buffer->client_data_offset =
3220 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3221 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3222
3223 vnic_add_client_data(adapter, vlcd);
3224
Thomas Falcon032c5e82015-12-21 11:26:06 -06003225 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3226 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3227 netdev_dbg(adapter->netdev, "%016lx\n",
3228 ((unsigned long int *)(adapter->login_buf))[i]);
3229 }
3230
3231 memset(&crq, 0, sizeof(crq));
3232 crq.login.first = IBMVNIC_CRQ_CMD;
3233 crq.login.cmd = LOGIN;
3234 crq.login.ioba = cpu_to_be32(buffer_token);
3235 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003236 ibmvnic_send_crq(adapter, &crq);
3237
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003238 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003239
Thomas Falcon032c5e82015-12-21 11:26:06 -06003240buf_rsp_map_failed:
3241 kfree(login_rsp_buffer);
3242buf_rsp_alloc_failed:
3243 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3244buf_map_failed:
3245 kfree(login_buffer);
3246buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003247 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003248}
3249
3250static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3251 u32 len, u8 map_id)
3252{
3253 union ibmvnic_crq crq;
3254
3255 memset(&crq, 0, sizeof(crq));
3256 crq.request_map.first = IBMVNIC_CRQ_CMD;
3257 crq.request_map.cmd = REQUEST_MAP;
3258 crq.request_map.map_id = map_id;
3259 crq.request_map.ioba = cpu_to_be32(addr);
3260 crq.request_map.len = cpu_to_be32(len);
3261 ibmvnic_send_crq(adapter, &crq);
3262}
3263
3264static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3265{
3266 union ibmvnic_crq crq;
3267
3268 memset(&crq, 0, sizeof(crq));
3269 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3270 crq.request_unmap.cmd = REQUEST_UNMAP;
3271 crq.request_unmap.map_id = map_id;
3272 ibmvnic_send_crq(adapter, &crq);
3273}
3274
3275static void send_map_query(struct ibmvnic_adapter *adapter)
3276{
3277 union ibmvnic_crq crq;
3278
3279 memset(&crq, 0, sizeof(crq));
3280 crq.query_map.first = IBMVNIC_CRQ_CMD;
3281 crq.query_map.cmd = QUERY_MAP;
3282 ibmvnic_send_crq(adapter, &crq);
3283}
3284
3285/* Send a series of CRQs requesting various capabilities of the VNIC server */
3286static void send_cap_queries(struct ibmvnic_adapter *adapter)
3287{
3288 union ibmvnic_crq crq;
3289
Thomas Falcon901e0402017-02-15 12:17:59 -06003290 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003291 memset(&crq, 0, sizeof(crq));
3292 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3293 crq.query_capability.cmd = QUERY_CAPABILITY;
3294
3295 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003296 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 ibmvnic_send_crq(adapter, &crq);
3298
3299 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003300 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 ibmvnic_send_crq(adapter, &crq);
3302
3303 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003304 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003305 ibmvnic_send_crq(adapter, &crq);
3306
3307 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
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 = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003312 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 ibmvnic_send_crq(adapter, &crq);
3314
3315 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003316 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003317 ibmvnic_send_crq(adapter, &crq);
3318
3319 crq.query_capability.capability =
3320 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003321 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 ibmvnic_send_crq(adapter, &crq);
3323
3324 crq.query_capability.capability =
3325 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
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 =
3330 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003331 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003332 ibmvnic_send_crq(adapter, &crq);
3333
3334 crq.query_capability.capability =
3335 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003336 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337 ibmvnic_send_crq(adapter, &crq);
3338
3339 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003340 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003341 ibmvnic_send_crq(adapter, &crq);
3342
3343 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003344 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003345 ibmvnic_send_crq(adapter, &crq);
3346
3347 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003348 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003349 ibmvnic_send_crq(adapter, &crq);
3350
3351 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003352 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 ibmvnic_send_crq(adapter, &crq);
3354
3355 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003356 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003357 ibmvnic_send_crq(adapter, &crq);
3358
3359 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003360 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003361 ibmvnic_send_crq(adapter, &crq);
3362
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003363 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3364 atomic_inc(&adapter->running_cap_crqs);
3365 ibmvnic_send_crq(adapter, &crq);
3366
Thomas Falcon032c5e82015-12-21 11:26:06 -06003367 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003368 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003369 ibmvnic_send_crq(adapter, &crq);
3370
3371 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
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 = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003376 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003377 ibmvnic_send_crq(adapter, &crq);
3378
3379 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003380 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003381 ibmvnic_send_crq(adapter, &crq);
3382
3383 crq.query_capability.capability =
3384 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003385 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386 ibmvnic_send_crq(adapter, &crq);
3387
3388 crq.query_capability.capability =
3389 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003390 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003391 ibmvnic_send_crq(adapter, &crq);
3392
3393 crq.query_capability.capability =
3394 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003395 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003396 ibmvnic_send_crq(adapter, &crq);
3397
3398 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003399 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003400 ibmvnic_send_crq(adapter, &crq);
3401}
3402
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003403static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3404 struct ibmvnic_adapter *adapter)
3405{
3406 struct device *dev = &adapter->vdev->dev;
3407
3408 if (crq->get_vpd_size_rsp.rc.code) {
3409 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3410 crq->get_vpd_size_rsp.rc.code);
3411 complete(&adapter->fw_done);
3412 return;
3413 }
3414
3415 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3416 complete(&adapter->fw_done);
3417}
3418
3419static void handle_vpd_rsp(union ibmvnic_crq *crq,
3420 struct ibmvnic_adapter *adapter)
3421{
3422 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003423 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003424 u8 fw_level_len = 0;
3425
3426 memset(adapter->fw_version, 0, 32);
3427
3428 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3429 DMA_FROM_DEVICE);
3430
3431 if (crq->get_vpd_rsp.rc.code) {
3432 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3433 crq->get_vpd_rsp.rc.code);
3434 goto complete;
3435 }
3436
3437 /* get the position of the firmware version info
3438 * located after the ASCII 'RM' substring in the buffer
3439 */
3440 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3441 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003442 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 -02003443 goto complete;
3444 }
3445
3446 /* get length of firmware level ASCII substring */
3447 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3448 fw_level_len = *(substr + 2);
3449 } else {
3450 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3451 goto complete;
3452 }
3453
3454 /* copy firmware version string from vpd into adapter */
3455 if ((substr + 3 + fw_level_len) <
3456 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003457 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003458 } else {
3459 dev_info(dev, "FW substr extrapolated VPD buff\n");
3460 }
3461
3462complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003463 if (adapter->fw_version[0] == '\0')
3464 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003465 complete(&adapter->fw_done);
3466}
3467
Thomas Falcon032c5e82015-12-21 11:26:06 -06003468static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3469{
3470 struct device *dev = &adapter->vdev->dev;
3471 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3472 union ibmvnic_crq crq;
3473 int i;
3474
3475 dma_unmap_single(dev, adapter->ip_offload_tok,
3476 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3477
3478 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3479 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3480 netdev_dbg(adapter->netdev, "%016lx\n",
3481 ((unsigned long int *)(buf))[i]);
3482
3483 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3484 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3485 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3486 buf->tcp_ipv4_chksum);
3487 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3488 buf->tcp_ipv6_chksum);
3489 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3490 buf->udp_ipv4_chksum);
3491 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3492 buf->udp_ipv6_chksum);
3493 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3494 buf->large_tx_ipv4);
3495 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3496 buf->large_tx_ipv6);
3497 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3498 buf->large_rx_ipv4);
3499 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3500 buf->large_rx_ipv6);
3501 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3502 buf->max_ipv4_header_size);
3503 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3504 buf->max_ipv6_header_size);
3505 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3506 buf->max_tcp_header_size);
3507 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3508 buf->max_udp_header_size);
3509 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3510 buf->max_large_tx_size);
3511 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3512 buf->max_large_rx_size);
3513 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3514 buf->ipv6_extension_header);
3515 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3516 buf->tcp_pseudosum_req);
3517 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3518 buf->num_ipv6_ext_headers);
3519 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3520 buf->off_ipv6_ext_headers);
3521
3522 adapter->ip_offload_ctrl_tok =
3523 dma_map_single(dev, &adapter->ip_offload_ctrl,
3524 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3525
3526 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3527 dev_err(dev, "Couldn't map ip offload control buffer\n");
3528 return;
3529 }
3530
Thomas Falconf6897942018-01-18 19:05:01 -06003531 adapter->ip_offload_ctrl.len =
3532 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003533 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003534 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3535 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003536 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3537 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3538 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3539 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003540 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3541 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003542
Thomas Falconfdb06102017-10-17 12:36:55 -05003543 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003544 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3545 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3546
Thomas Falcon15482052017-10-17 12:36:54 -05003547 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003548
3549 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3550 adapter->netdev->features |= NETIF_F_IP_CSUM;
3551
3552 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3553 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3554
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003555 if ((adapter->netdev->features &
3556 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3557 adapter->netdev->features |= NETIF_F_RXCSUM;
3558
Thomas Falconfdb06102017-10-17 12:36:55 -05003559 if (buf->large_tx_ipv4)
3560 adapter->netdev->features |= NETIF_F_TSO;
3561 if (buf->large_tx_ipv6)
3562 adapter->netdev->features |= NETIF_F_TSO6;
3563
Thomas Falconaa0bf852017-10-17 12:36:56 -05003564 adapter->netdev->hw_features |= adapter->netdev->features;
3565
Thomas Falcon032c5e82015-12-21 11:26:06 -06003566 memset(&crq, 0, sizeof(crq));
3567 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3568 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3569 crq.control_ip_offload.len =
3570 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3571 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3572 ibmvnic_send_crq(adapter, &crq);
3573}
3574
3575static void handle_error_info_rsp(union ibmvnic_crq *crq,
3576 struct ibmvnic_adapter *adapter)
3577{
3578 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003579 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003580 unsigned long flags;
3581 bool found = false;
3582 int i;
3583
3584 if (!crq->request_error_rsp.rc.code) {
3585 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3586 crq->request_error_rsp.rc.code);
3587 return;
3588 }
3589
3590 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003591 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3593 found = true;
3594 list_del(&error_buff->list);
3595 break;
3596 }
3597 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3598
3599 if (!found) {
3600 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003601 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003602 return;
3603 }
3604
3605 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003606 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003607
3608 for (i = 0; i < error_buff->len; i++) {
3609 pr_cont("%02x", (int)error_buff->buff[i]);
3610 if (i % 8 == 7)
3611 pr_cont(" ");
3612 }
3613 pr_cont("\n");
3614
3615 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3616 DMA_FROM_DEVICE);
3617 kfree(error_buff->buff);
3618 kfree(error_buff);
3619}
3620
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003621static void request_error_information(struct ibmvnic_adapter *adapter,
3622 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003623{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003624 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003625 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003626 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003627 unsigned long timeout = msecs_to_jiffies(30000);
3628 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003629 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003630 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003631
3632 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3633 if (!error_buff)
3634 return;
3635
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003636 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003637 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3638 if (!error_buff->buff) {
3639 kfree(error_buff);
3640 return;
3641 }
3642
3643 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3644 DMA_FROM_DEVICE);
3645 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003646 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003647 kfree(error_buff->buff);
3648 kfree(error_buff);
3649 return;
3650 }
3651
Thomas Falcon032c5e82015-12-21 11:26:06 -06003652 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003653 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003654
3655 spin_lock_irqsave(&adapter->error_list_lock, flags);
3656 list_add_tail(&error_buff->list, &adapter->errors);
3657 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3658
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003659 memset(&crq, 0, sizeof(crq));
3660 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3661 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3662 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3663 crq.request_error_info.len = cpu_to_be32(detail_len);
3664 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3665
3666 rc = ibmvnic_send_crq(adapter, &crq);
3667 if (rc) {
3668 netdev_err(netdev, "failed to request error information\n");
3669 goto err_info_fail;
3670 }
3671
3672 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3673 netdev_err(netdev, "timeout waiting for error information\n");
3674 goto err_info_fail;
3675 }
3676
3677 return;
3678
3679err_info_fail:
3680 spin_lock_irqsave(&adapter->error_list_lock, flags);
3681 list_del(&error_buff->list);
3682 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3683
3684 kfree(error_buff->buff);
3685 kfree(error_buff);
3686}
3687
3688static void handle_error_indication(union ibmvnic_crq *crq,
3689 struct ibmvnic_adapter *adapter)
3690{
3691 struct device *dev = &adapter->vdev->dev;
3692
3693 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3694 crq->error_indication.flags
3695 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3696 be32_to_cpu(crq->error_indication.error_id),
3697 be16_to_cpu(crq->error_indication.error_cause));
3698
3699 if (be32_to_cpu(crq->error_indication.error_id))
3700 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003701
3702 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3703 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003704 else
3705 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003706}
3707
Thomas Falconf8136142018-01-29 13:45:05 -06003708static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3709 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003710{
3711 struct net_device *netdev = adapter->netdev;
3712 struct device *dev = &adapter->vdev->dev;
3713 long rc;
3714
3715 rc = crq->change_mac_addr_rsp.rc.code;
3716 if (rc) {
3717 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003718 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003719 }
3720 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3721 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003722out:
3723 complete(&adapter->fw_done);
3724 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003725}
3726
3727static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3728 struct ibmvnic_adapter *adapter)
3729{
3730 struct device *dev = &adapter->vdev->dev;
3731 u64 *req_value;
3732 char *name;
3733
Thomas Falcon901e0402017-02-15 12:17:59 -06003734 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003735 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3736 case REQ_TX_QUEUES:
3737 req_value = &adapter->req_tx_queues;
3738 name = "tx";
3739 break;
3740 case REQ_RX_QUEUES:
3741 req_value = &adapter->req_rx_queues;
3742 name = "rx";
3743 break;
3744 case REQ_RX_ADD_QUEUES:
3745 req_value = &adapter->req_rx_add_queues;
3746 name = "rx_add";
3747 break;
3748 case REQ_TX_ENTRIES_PER_SUBCRQ:
3749 req_value = &adapter->req_tx_entries_per_subcrq;
3750 name = "tx_entries_per_subcrq";
3751 break;
3752 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3753 req_value = &adapter->req_rx_add_entries_per_subcrq;
3754 name = "rx_add_entries_per_subcrq";
3755 break;
3756 case REQ_MTU:
3757 req_value = &adapter->req_mtu;
3758 name = "mtu";
3759 break;
3760 case PROMISC_REQUESTED:
3761 req_value = &adapter->promisc;
3762 name = "promisc";
3763 break;
3764 default:
3765 dev_err(dev, "Got invalid cap request rsp %d\n",
3766 crq->request_capability.capability);
3767 return;
3768 }
3769
3770 switch (crq->request_capability_rsp.rc.code) {
3771 case SUCCESS:
3772 break;
3773 case PARTIALSUCCESS:
3774 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3775 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003776 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003777 number), name);
John Allene7913802018-01-18 16:27:12 -06003778
3779 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3780 REQ_MTU) {
3781 pr_err("mtu of %llu is not supported. Reverting.\n",
3782 *req_value);
3783 *req_value = adapter->fallback.mtu;
3784 } else {
3785 *req_value =
3786 be64_to_cpu(crq->request_capability_rsp.number);
3787 }
3788
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003789 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003790 return;
3791 default:
3792 dev_err(dev, "Error %d in request cap rsp\n",
3793 crq->request_capability_rsp.rc.code);
3794 return;
3795 }
3796
3797 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003798 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003799 union ibmvnic_crq newcrq;
3800 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3801 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3802 &adapter->ip_offload_buf;
3803
Thomas Falcon249168a2017-02-15 12:18:00 -06003804 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003805 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3806 buf_sz,
3807 DMA_FROM_DEVICE);
3808
3809 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3810 if (!firmware_has_feature(FW_FEATURE_CMO))
3811 dev_err(dev, "Couldn't map offload buffer\n");
3812 return;
3813 }
3814
3815 memset(&newcrq, 0, sizeof(newcrq));
3816 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3817 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3818 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3819 newcrq.query_ip_offload.ioba =
3820 cpu_to_be32(adapter->ip_offload_tok);
3821
3822 ibmvnic_send_crq(adapter, &newcrq);
3823 }
3824}
3825
3826static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3827 struct ibmvnic_adapter *adapter)
3828{
3829 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003830 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003831 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3832 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003833 int i;
3834
3835 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3836 DMA_BIDIRECTIONAL);
3837 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3838 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3839
John Allen498cd8e2016-04-06 11:49:55 -05003840 /* If the number of queues requested can't be allocated by the
3841 * server, the login response will return with code 1. We will need
3842 * to resend the login buffer with fewer queues requested.
3843 */
3844 if (login_rsp_crq->generic.rc.code) {
3845 adapter->renegotiate = true;
3846 complete(&adapter->init_done);
3847 return 0;
3848 }
3849
John Allenc26eba02017-10-26 16:23:25 -05003850 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3851
Thomas Falcon032c5e82015-12-21 11:26:06 -06003852 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3853 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3854 netdev_dbg(adapter->netdev, "%016lx\n",
3855 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3856 }
3857
3858 /* Sanity checks */
3859 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3860 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3861 adapter->req_rx_add_queues !=
3862 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3863 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3864 ibmvnic_remove(adapter->vdev);
3865 return -EIO;
3866 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003867 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003868 complete(&adapter->init_done);
3869
Thomas Falcon032c5e82015-12-21 11:26:06 -06003870 return 0;
3871}
3872
Thomas Falcon032c5e82015-12-21 11:26:06 -06003873static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3874 struct ibmvnic_adapter *adapter)
3875{
3876 struct device *dev = &adapter->vdev->dev;
3877 long rc;
3878
3879 rc = crq->request_unmap_rsp.rc.code;
3880 if (rc)
3881 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3882}
3883
3884static void handle_query_map_rsp(union ibmvnic_crq *crq,
3885 struct ibmvnic_adapter *adapter)
3886{
3887 struct net_device *netdev = adapter->netdev;
3888 struct device *dev = &adapter->vdev->dev;
3889 long rc;
3890
3891 rc = crq->query_map_rsp.rc.code;
3892 if (rc) {
3893 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3894 return;
3895 }
3896 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3897 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3898 crq->query_map_rsp.free_pages);
3899}
3900
3901static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3902 struct ibmvnic_adapter *adapter)
3903{
3904 struct net_device *netdev = adapter->netdev;
3905 struct device *dev = &adapter->vdev->dev;
3906 long rc;
3907
Thomas Falcon901e0402017-02-15 12:17:59 -06003908 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003910 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003911 rc = crq->query_capability.rc.code;
3912 if (rc) {
3913 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3914 goto out;
3915 }
3916
3917 switch (be16_to_cpu(crq->query_capability.capability)) {
3918 case MIN_TX_QUEUES:
3919 adapter->min_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, "min_tx_queues = %lld\n",
3922 adapter->min_tx_queues);
3923 break;
3924 case MIN_RX_QUEUES:
3925 adapter->min_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, "min_rx_queues = %lld\n",
3928 adapter->min_rx_queues);
3929 break;
3930 case MIN_RX_ADD_QUEUES:
3931 adapter->min_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, "min_rx_add_queues = %lld\n",
3934 adapter->min_rx_add_queues);
3935 break;
3936 case MAX_TX_QUEUES:
3937 adapter->max_tx_queues =
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, "max_tx_queues = %lld\n",
3940 adapter->max_tx_queues);
3941 break;
3942 case MAX_RX_QUEUES:
3943 adapter->max_rx_queues =
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, "max_rx_queues = %lld\n",
3946 adapter->max_rx_queues);
3947 break;
3948 case MAX_RX_ADD_QUEUES:
3949 adapter->max_rx_add_queues =
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_rx_add_queues = %lld\n",
3952 adapter->max_rx_add_queues);
3953 break;
3954 case MIN_TX_ENTRIES_PER_SUBCRQ:
3955 adapter->min_tx_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, "min_tx_entries_per_subcrq = %lld\n",
3958 adapter->min_tx_entries_per_subcrq);
3959 break;
3960 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3961 adapter->min_rx_add_entries_per_subcrq =
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, "min_rx_add_entrs_per_subcrq = %lld\n",
3964 adapter->min_rx_add_entries_per_subcrq);
3965 break;
3966 case MAX_TX_ENTRIES_PER_SUBCRQ:
3967 adapter->max_tx_entries_per_subcrq =
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, "max_tx_entries_per_subcrq = %lld\n",
3970 adapter->max_tx_entries_per_subcrq);
3971 break;
3972 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3973 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003974 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003975 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3976 adapter->max_rx_add_entries_per_subcrq);
3977 break;
3978 case TCP_IP_OFFLOAD:
3979 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003980 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3982 adapter->tcp_ip_offload);
3983 break;
3984 case PROMISC_SUPPORTED:
3985 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003986 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003987 netdev_dbg(netdev, "promisc_supported = %lld\n",
3988 adapter->promisc_supported);
3989 break;
3990 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003991 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003992 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003993 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3994 break;
3995 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003996 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003997 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003998 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3999 break;
4000 case MAX_MULTICAST_FILTERS:
4001 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06004002 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4004 adapter->max_multicast_filters);
4005 break;
4006 case VLAN_HEADER_INSERTION:
4007 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06004008 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 if (adapter->vlan_header_insertion)
4010 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4011 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4012 adapter->vlan_header_insertion);
4013 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04004014 case RX_VLAN_HEADER_INSERTION:
4015 adapter->rx_vlan_header_insertion =
4016 be64_to_cpu(crq->query_capability.number);
4017 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4018 adapter->rx_vlan_header_insertion);
4019 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004020 case MAX_TX_SG_ENTRIES:
4021 adapter->max_tx_sg_entries =
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, "max_tx_sg_entries = %lld\n",
4024 adapter->max_tx_sg_entries);
4025 break;
4026 case RX_SG_SUPPORTED:
4027 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
4030 adapter->rx_sg_supported);
4031 break;
4032 case OPT_TX_COMP_SUB_QUEUES:
4033 adapter->opt_tx_comp_sub_queues =
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_comp_sub_queues = %lld\n",
4036 adapter->opt_tx_comp_sub_queues);
4037 break;
4038 case OPT_RX_COMP_QUEUES:
4039 adapter->opt_rx_comp_queues =
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_rx_comp_queues = %lld\n",
4042 adapter->opt_rx_comp_queues);
4043 break;
4044 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4045 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004046 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004047 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4048 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4049 break;
4050 case OPT_TX_ENTRIES_PER_SUBCRQ:
4051 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004052 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004053 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4054 adapter->opt_tx_entries_per_subcrq);
4055 break;
4056 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4057 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004058 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004059 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4060 adapter->opt_rxba_entries_per_subcrq);
4061 break;
4062 case TX_RX_DESC_REQ:
4063 adapter->tx_rx_desc_req = crq->query_capability.number;
4064 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4065 adapter->tx_rx_desc_req);
4066 break;
4067
4068 default:
4069 netdev_err(netdev, "Got invalid cap rsp %d\n",
4070 crq->query_capability.capability);
4071 }
4072
4073out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004074 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4075 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004076 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004077 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004078}
4079
Thomas Falcon032c5e82015-12-21 11:26:06 -06004080static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4081 struct ibmvnic_adapter *adapter)
4082{
4083 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4084 struct net_device *netdev = adapter->netdev;
4085 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004086 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004087 long rc;
4088
4089 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004090 (unsigned long int)cpu_to_be64(u64_crq[0]),
4091 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004092 switch (gen_crq->first) {
4093 case IBMVNIC_CRQ_INIT_RSP:
4094 switch (gen_crq->cmd) {
4095 case IBMVNIC_CRQ_INIT:
4096 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004097 adapter->from_passive_init = true;
4098 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004099 break;
4100 case IBMVNIC_CRQ_INIT_COMPLETE:
4101 dev_info(dev, "Partner initialization complete\n");
4102 send_version_xchg(adapter);
4103 break;
4104 default:
4105 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4106 }
4107 return;
4108 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004109 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004110 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004111 dev_info(dev, "Migrated, re-enabling adapter\n");
4112 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004113 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4114 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004115 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004116 } else {
4117 /* The adapter lost the connection */
4118 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4119 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004120 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004121 }
4122 return;
4123 case IBMVNIC_CRQ_CMD_RSP:
4124 break;
4125 default:
4126 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4127 gen_crq->first);
4128 return;
4129 }
4130
4131 switch (gen_crq->cmd) {
4132 case VERSION_EXCHANGE_RSP:
4133 rc = crq->version_exchange_rsp.rc.code;
4134 if (rc) {
4135 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4136 break;
4137 }
4138 dev_info(dev, "Partner protocol version is %d\n",
4139 crq->version_exchange_rsp.version);
4140 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4141 ibmvnic_version)
4142 ibmvnic_version =
4143 be16_to_cpu(crq->version_exchange_rsp.version);
4144 send_cap_queries(adapter);
4145 break;
4146 case QUERY_CAPABILITY_RSP:
4147 handle_query_cap_rsp(crq, adapter);
4148 break;
4149 case QUERY_MAP_RSP:
4150 handle_query_map_rsp(crq, adapter);
4151 break;
4152 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004153 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4154 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004155 break;
4156 case REQUEST_UNMAP_RSP:
4157 handle_request_unmap_rsp(crq, adapter);
4158 break;
4159 case REQUEST_CAPABILITY_RSP:
4160 handle_request_cap_rsp(crq, adapter);
4161 break;
4162 case LOGIN_RSP:
4163 netdev_dbg(netdev, "Got Login Response\n");
4164 handle_login_rsp(crq, adapter);
4165 break;
4166 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004167 netdev_dbg(netdev,
4168 "Got Logical Link State Response, state: %d rc: %d\n",
4169 crq->logical_link_state_rsp.link_state,
4170 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004171 adapter->logical_link_state =
4172 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004173 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4174 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175 break;
4176 case LINK_STATE_INDICATION:
4177 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4178 adapter->phys_link_state =
4179 crq->link_state_indication.phys_link_state;
4180 adapter->logical_link_state =
4181 crq->link_state_indication.logical_link_state;
4182 break;
4183 case CHANGE_MAC_ADDR_RSP:
4184 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004185 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004186 break;
4187 case ERROR_INDICATION:
4188 netdev_dbg(netdev, "Got Error Indication\n");
4189 handle_error_indication(crq, adapter);
4190 break;
4191 case REQUEST_ERROR_RSP:
4192 netdev_dbg(netdev, "Got Error Detail Response\n");
4193 handle_error_info_rsp(crq, adapter);
4194 break;
4195 case REQUEST_STATISTICS_RSP:
4196 netdev_dbg(netdev, "Got Statistics Response\n");
4197 complete(&adapter->stats_done);
4198 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004199 case QUERY_IP_OFFLOAD_RSP:
4200 netdev_dbg(netdev, "Got Query IP offload Response\n");
4201 handle_query_ip_offload_rsp(adapter);
4202 break;
4203 case MULTICAST_CTRL_RSP:
4204 netdev_dbg(netdev, "Got multicast control Response\n");
4205 break;
4206 case CONTROL_IP_OFFLOAD_RSP:
4207 netdev_dbg(netdev, "Got Control IP offload Response\n");
4208 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4209 sizeof(adapter->ip_offload_ctrl),
4210 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004211 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004212 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004213 case COLLECT_FW_TRACE_RSP:
4214 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4215 complete(&adapter->fw_done);
4216 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004217 case GET_VPD_SIZE_RSP:
4218 handle_vpd_size_rsp(crq, adapter);
4219 break;
4220 case GET_VPD_RSP:
4221 handle_vpd_rsp(crq, adapter);
4222 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004223 default:
4224 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4225 gen_crq->cmd);
4226 }
4227}
4228
4229static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4230{
4231 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004232
Thomas Falcon6c267b32017-02-15 12:17:58 -06004233 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004234 return IRQ_HANDLED;
4235}
4236
4237static void ibmvnic_tasklet(void *data)
4238{
4239 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004240 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004241 union ibmvnic_crq *crq;
4242 unsigned long flags;
4243 bool done = false;
4244
4245 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004246 while (!done) {
4247 /* Pull all the valid messages off the CRQ */
4248 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4249 ibmvnic_handle_crq(crq, adapter);
4250 crq->generic.first = 0;
4251 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004252
4253 /* remain in tasklet until all
4254 * capabilities responses are received
4255 */
4256 if (!adapter->wait_capability)
4257 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004258 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004259 /* if capabilities CRQ's were sent in this tasklet, the following
4260 * tasklet must wait until all responses are received
4261 */
4262 if (atomic_read(&adapter->running_cap_crqs) != 0)
4263 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004264 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004265}
4266
4267static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4268{
4269 struct vio_dev *vdev = adapter->vdev;
4270 int rc;
4271
4272 do {
4273 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4274 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4275
4276 if (rc)
4277 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4278
4279 return rc;
4280}
4281
4282static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4283{
4284 struct ibmvnic_crq_queue *crq = &adapter->crq;
4285 struct device *dev = &adapter->vdev->dev;
4286 struct vio_dev *vdev = adapter->vdev;
4287 int rc;
4288
4289 /* Close the CRQ */
4290 do {
4291 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4292 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4293
4294 /* Clean out the queue */
4295 memset(crq->msgs, 0, PAGE_SIZE);
4296 crq->cur = 0;
4297
4298 /* And re-open it again */
4299 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4300 crq->msg_token, PAGE_SIZE);
4301
4302 if (rc == H_CLOSED)
4303 /* Adapter is good, but other end is not ready */
4304 dev_warn(dev, "Partner adapter not ready\n");
4305 else if (rc != 0)
4306 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4307
4308 return rc;
4309}
4310
Nathan Fontenotf9928872017-03-30 02:48:54 -04004311static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312{
4313 struct ibmvnic_crq_queue *crq = &adapter->crq;
4314 struct vio_dev *vdev = adapter->vdev;
4315 long rc;
4316
Nathan Fontenotf9928872017-03-30 02:48:54 -04004317 if (!crq->msgs)
4318 return;
4319
Thomas Falcon032c5e82015-12-21 11:26:06 -06004320 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4321 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004322 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004323 do {
4324 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4325 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4326
4327 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4328 DMA_BIDIRECTIONAL);
4329 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004330 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004331}
4332
Nathan Fontenotf9928872017-03-30 02:48:54 -04004333static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004334{
4335 struct ibmvnic_crq_queue *crq = &adapter->crq;
4336 struct device *dev = &adapter->vdev->dev;
4337 struct vio_dev *vdev = adapter->vdev;
4338 int rc, retrc = -ENOMEM;
4339
Nathan Fontenotf9928872017-03-30 02:48:54 -04004340 if (crq->msgs)
4341 return 0;
4342
Thomas Falcon032c5e82015-12-21 11:26:06 -06004343 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4344 /* Should we allocate more than one page? */
4345
4346 if (!crq->msgs)
4347 return -ENOMEM;
4348
4349 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4350 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4351 DMA_BIDIRECTIONAL);
4352 if (dma_mapping_error(dev, crq->msg_token))
4353 goto map_failed;
4354
4355 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4356 crq->msg_token, PAGE_SIZE);
4357
4358 if (rc == H_RESOURCE)
4359 /* maybe kexecing and resource is busy. try a reset */
4360 rc = ibmvnic_reset_crq(adapter);
4361 retrc = rc;
4362
4363 if (rc == H_CLOSED) {
4364 dev_warn(dev, "Partner adapter not ready\n");
4365 } else if (rc) {
4366 dev_warn(dev, "Error %d opening adapter\n", rc);
4367 goto reg_crq_failed;
4368 }
4369
4370 retrc = 0;
4371
Thomas Falcon6c267b32017-02-15 12:17:58 -06004372 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4373 (unsigned long)adapter);
4374
Thomas Falcon032c5e82015-12-21 11:26:06 -06004375 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4376 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4377 adapter);
4378 if (rc) {
4379 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4380 vdev->irq, rc);
4381 goto req_irq_failed;
4382 }
4383
4384 rc = vio_enable_interrupts(vdev);
4385 if (rc) {
4386 dev_err(dev, "Error %d enabling interrupts\n", rc);
4387 goto req_irq_failed;
4388 }
4389
4390 crq->cur = 0;
4391 spin_lock_init(&crq->lock);
4392
4393 return retrc;
4394
4395req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004396 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004397 do {
4398 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4399 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4400reg_crq_failed:
4401 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4402map_failed:
4403 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004404 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004405 return retrc;
4406}
4407
John Allenf6ef6402017-03-17 17:13:42 -05004408static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4409{
4410 struct device *dev = &adapter->vdev->dev;
4411 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004412 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004413 int rc;
4414
John Allenc26eba02017-10-26 16:23:25 -05004415 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004416 rc = ibmvnic_reset_crq(adapter);
4417 if (!rc)
4418 rc = vio_enable_interrupts(adapter->vdev);
4419 } else {
4420 rc = init_crq_queue(adapter);
4421 }
4422
John Allenf6ef6402017-03-17 17:13:42 -05004423 if (rc) {
4424 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4425 return rc;
4426 }
4427
John Allen017892c12017-05-26 10:30:19 -04004428 adapter->from_passive_init = false;
4429
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004430 old_num_rx_queues = adapter->req_rx_queues;
4431 old_num_tx_queues = adapter->req_tx_queues;
4432
John Allenf6ef6402017-03-17 17:13:42 -05004433 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004434 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004435 ibmvnic_send_crq_init(adapter);
4436 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4437 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004438 return -1;
4439 }
4440
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004441 if (adapter->init_done_rc) {
4442 release_crq_queue(adapter);
4443 return adapter->init_done_rc;
4444 }
4445
John Allen017892c12017-05-26 10:30:19 -04004446 if (adapter->from_passive_init) {
4447 adapter->state = VNIC_OPEN;
4448 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004449 return -1;
4450 }
4451
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004452 if (adapter->resetting && !adapter->wait_for_reset) {
4453 if (adapter->req_rx_queues != old_num_rx_queues ||
4454 adapter->req_tx_queues != old_num_tx_queues) {
4455 release_sub_crqs(adapter, 0);
4456 rc = init_sub_crqs(adapter);
4457 } else {
4458 rc = reset_sub_crq_queues(adapter);
4459 }
4460 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004461 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004462 }
4463
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004464 if (rc) {
4465 dev_err(dev, "Initialization of sub crqs failed\n");
4466 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004467 return rc;
4468 }
4469
4470 rc = init_sub_crq_irqs(adapter);
4471 if (rc) {
4472 dev_err(dev, "Failed to initialize sub crq irqs\n");
4473 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004474 }
4475
Thomas Falcon53cc7722018-02-26 18:10:56 -06004476 rc = init_stats_buffers(adapter);
4477 if (rc)
4478 return rc;
4479
4480 rc = init_stats_token(adapter);
4481 if (rc)
4482 return rc;
4483
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004484 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004485}
4486
Thomas Falcon40c9db82017-06-12 12:35:04 -05004487static struct device_attribute dev_attr_failover;
4488
Thomas Falcon032c5e82015-12-21 11:26:06 -06004489static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4490{
4491 struct ibmvnic_adapter *adapter;
4492 struct net_device *netdev;
4493 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004494 int rc;
4495
4496 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4497 dev->unit_address);
4498
4499 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4500 VETH_MAC_ADDR, NULL);
4501 if (!mac_addr_p) {
4502 dev_err(&dev->dev,
4503 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4504 __FILE__, __LINE__);
4505 return 0;
4506 }
4507
4508 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004509 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004510 if (!netdev)
4511 return -ENOMEM;
4512
4513 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004514 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004515 dev_set_drvdata(&dev->dev, netdev);
4516 adapter->vdev = dev;
4517 adapter->netdev = netdev;
4518
4519 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4520 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4521 netdev->irq = dev->irq;
4522 netdev->netdev_ops = &ibmvnic_netdev_ops;
4523 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4524 SET_NETDEV_DEV(netdev, &dev->dev);
4525
4526 spin_lock_init(&adapter->stats_lock);
4527
Thomas Falcon032c5e82015-12-21 11:26:06 -06004528 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004529 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004530
Nathan Fontenoted651a12017-05-03 14:04:38 -04004531 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4532 INIT_LIST_HEAD(&adapter->rwi_list);
4533 mutex_init(&adapter->reset_lock);
4534 mutex_init(&adapter->rwi_lock);
4535 adapter->resetting = false;
4536
John Allenc26eba02017-10-26 16:23:25 -05004537 adapter->mac_change_pending = false;
4538
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004539 do {
4540 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004541 if (rc && rc != EAGAIN)
4542 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004543 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004544
Thomas Falconf39f0d12017-02-14 10:22:59 -06004545 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004546 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4547 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004548
Thomas Falcon40c9db82017-06-12 12:35:04 -05004549 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004550 if (rc)
4551 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004552
Mick Tarsele876a8a2017-09-28 13:53:18 -07004553 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004554 rc = register_netdev(netdev);
4555 if (rc) {
4556 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004557 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004558 }
4559 dev_info(&dev->dev, "ibmvnic registered\n");
4560
Nathan Fontenot90c80142017-05-03 14:04:32 -04004561 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004562
4563 adapter->wait_for_reset = false;
4564
Thomas Falcon032c5e82015-12-21 11:26:06 -06004565 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004566
4567ibmvnic_register_fail:
4568 device_remove_file(&dev->dev, &dev_attr_failover);
4569
4570ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004571 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004572 release_crq_queue(adapter);
4573 free_netdev(netdev);
4574
4575 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004576}
4577
4578static int ibmvnic_remove(struct vio_dev *dev)
4579{
4580 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004581 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004582
Nathan Fontenot90c80142017-05-03 14:04:32 -04004583 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004584 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004585 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004586
4587 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004588 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004589 release_crq_queue(adapter);
4590
Thomas Falcon53cc7722018-02-26 18:10:56 -06004591 release_stats_token(adapter);
4592 release_stats_buffers(adapter);
4593
Nathan Fontenot90c80142017-05-03 14:04:32 -04004594 adapter->state = VNIC_REMOVED;
4595
Nathan Fontenoted651a12017-05-03 14:04:38 -04004596 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004597 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004598 free_netdev(netdev);
4599 dev_set_drvdata(&dev->dev, NULL);
4600
4601 return 0;
4602}
4603
Thomas Falcon40c9db82017-06-12 12:35:04 -05004604static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4605 const char *buf, size_t count)
4606{
4607 struct net_device *netdev = dev_get_drvdata(dev);
4608 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4609 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4610 __be64 session_token;
4611 long rc;
4612
4613 if (!sysfs_streq(buf, "1"))
4614 return -EINVAL;
4615
4616 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4617 H_GET_SESSION_TOKEN, 0, 0, 0);
4618 if (rc) {
4619 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4620 rc);
4621 return -EINVAL;
4622 }
4623
4624 session_token = (__be64)retbuf[0];
4625 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4626 be64_to_cpu(session_token));
4627 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4628 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4629 if (rc) {
4630 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4631 rc);
4632 return -EINVAL;
4633 }
4634
4635 return count;
4636}
4637
Joe Perches6cbaefb2017-12-19 10:15:09 -08004638static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004639
Thomas Falcon032c5e82015-12-21 11:26:06 -06004640static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4641{
4642 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4643 struct ibmvnic_adapter *adapter;
4644 struct iommu_table *tbl;
4645 unsigned long ret = 0;
4646 int i;
4647
4648 tbl = get_iommu_table_base(&vdev->dev);
4649
4650 /* netdev inits at probe time along with the structures we need below*/
4651 if (!netdev)
4652 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4653
4654 adapter = netdev_priv(netdev);
4655
4656 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004657 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4658
4659 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4660 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4661
4662 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4663 i++)
4664 ret += adapter->rx_pool[i].size *
4665 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4666
4667 return ret;
4668}
4669
4670static int ibmvnic_resume(struct device *dev)
4671{
4672 struct net_device *netdev = dev_get_drvdata(dev);
4673 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004674
John Allencb89ba22017-06-19 11:27:53 -05004675 if (adapter->state != VNIC_OPEN)
4676 return 0;
4677
John Allena2488782017-07-24 13:26:06 -05004678 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004679
4680 return 0;
4681}
4682
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304683static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004684 {"network", "IBM,vnic"},
4685 {"", "" }
4686};
4687MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4688
4689static const struct dev_pm_ops ibmvnic_pm_ops = {
4690 .resume = ibmvnic_resume
4691};
4692
4693static struct vio_driver ibmvnic_driver = {
4694 .id_table = ibmvnic_device_table,
4695 .probe = ibmvnic_probe,
4696 .remove = ibmvnic_remove,
4697 .get_desired_dma = ibmvnic_get_desired_dma,
4698 .name = ibmvnic_driver_name,
4699 .pm = &ibmvnic_pm_ops,
4700};
4701
4702/* module functions */
4703static int __init ibmvnic_module_init(void)
4704{
4705 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4706 IBMVNIC_DRIVER_VERSION);
4707
4708 return vio_register_driver(&ibmvnic_driver);
4709}
4710
4711static void __exit ibmvnic_module_exit(void)
4712{
4713 vio_unregister_driver(&ibmvnic_driver);
4714}
4715
4716module_init(ibmvnic_module_init);
4717module_exit(ibmvnic_module_exit);