blob: 14f00810db3346d29b39c4d8c9cff6c28832a0bf [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 Falcon032c5e82015-12-21 11:26:06 -06001343static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1344{
1345 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1346 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001347 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001348 struct device *dev = &adapter->vdev->dev;
1349 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001350 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001351 struct ibmvnic_tx_pool *tx_pool;
1352 unsigned int tx_send_failed = 0;
1353 unsigned int tx_map_failed = 0;
1354 unsigned int tx_dropped = 0;
1355 unsigned int tx_packets = 0;
1356 unsigned int tx_bytes = 0;
1357 dma_addr_t data_dma_addr;
1358 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001359 unsigned long lpar_rc;
1360 union sub_crq tx_crq;
1361 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001362 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001363 unsigned char *dst;
1364 u64 *handle_array;
1365 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001366 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001367 int ret = 0;
1368
Nathan Fontenoted651a12017-05-03 14:04:38 -04001369 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001370 if (!netif_subqueue_stopped(netdev, skb))
1371 netif_stop_subqueue(netdev, queue_num);
1372 dev_kfree_skb_any(skb);
1373
Thomas Falcon032c5e82015-12-21 11:26:06 -06001374 tx_send_failed++;
1375 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001376 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001377 goto out;
1378 }
1379
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001380 tx_pool = &adapter->tx_pool[queue_num];
1381 tx_scrq = adapter->tx_scrq[queue_num];
1382 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1383 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1384 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1385
Thomas Falcon032c5e82015-12-21 11:26:06 -06001386 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001387
1388 if (skb_is_gso(skb)) {
1389 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1390 dst = tx_pool->tso_ltb.buff + offset;
1391 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1392 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1393 tx_pool->tso_index++;
1394 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1395 tx_pool->tso_index = 0;
1396 } else {
Thomas Falcon8dff66c2018-03-12 11:51:03 -05001397 offset = index * (adapter->req_mtu + VLAN_HLEN);
Thomas Falconfdb06102017-10-17 12:36:55 -05001398 dst = tx_pool->long_term_buff.buff + offset;
Thomas Falcon8dff66c2018-03-12 11:51:03 -05001399 memset(dst, 0, adapter->req_mtu + VLAN_HLEN);
Thomas Falconfdb06102017-10-17 12:36:55 -05001400 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1401 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001402
Thomas Falcon15482052017-10-17 12:36:54 -05001403 if (skb_shinfo(skb)->nr_frags) {
1404 int cur, i;
1405
1406 /* Copy the head */
1407 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1408 cur = skb_headlen(skb);
1409
1410 /* Copy the frags */
1411 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1412 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1413
1414 memcpy(dst + cur,
1415 page_address(skb_frag_page(frag)) +
1416 frag->page_offset, skb_frag_size(frag));
1417 cur += skb_frag_size(frag);
1418 }
1419 } else {
1420 skb_copy_from_linear_data(skb, dst, skb->len);
1421 }
1422
Thomas Falcon032c5e82015-12-21 11:26:06 -06001423 tx_pool->consumer_index =
1424 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001425 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001426
1427 tx_buff = &tx_pool->tx_buff[index];
1428 tx_buff->skb = skb;
1429 tx_buff->data_dma[0] = data_dma_addr;
1430 tx_buff->data_len[0] = skb->len;
1431 tx_buff->index = index;
1432 tx_buff->pool_index = queue_num;
1433 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001434
1435 memset(&tx_crq, 0, sizeof(tx_crq));
1436 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1437 tx_crq.v1.type = IBMVNIC_TX_DESC;
1438 tx_crq.v1.n_crq_elem = 1;
1439 tx_crq.v1.n_sge = 1;
1440 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1441 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001442 if (skb_is_gso(skb))
1443 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1444 else
1445 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001446 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1447 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1448
1449 if (adapter->vlan_header_insertion) {
1450 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1451 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1452 }
1453
1454 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001455 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1456 proto = ip_hdr(skb)->protocol;
1457 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1458 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1459 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001460 }
1461
Thomas Falcona0dca102018-01-18 19:29:48 -06001462 if (proto == IPPROTO_TCP)
1463 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1464 else if (proto == IPPROTO_UDP)
1465 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1466
Thomas Falconad7775d2016-04-01 17:20:34 -05001467 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001468 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001469 hdrs += 2;
1470 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001471 if (skb_is_gso(skb)) {
1472 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1473 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1474 hdrs += 2;
1475 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001476 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001477 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001478 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1479 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001480 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001481 tx_buff->indir_arr[0] = tx_crq;
1482 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1483 sizeof(tx_buff->indir_arr),
1484 DMA_TO_DEVICE);
1485 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001486 dev_kfree_skb_any(skb);
1487 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001488 if (!firmware_has_feature(FW_FEATURE_CMO))
1489 dev_err(dev, "tx: unable to map descriptor array\n");
1490 tx_map_failed++;
1491 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001492 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001493 goto out;
1494 }
John Allen498cd8e2016-04-06 11:49:55 -05001495 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001496 (u64)tx_buff->indir_dma,
1497 (u64)num_entries);
1498 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001499 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001500 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1501 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001502 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001503 if (lpar_rc != H_SUCCESS) {
1504 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1505
1506 if (tx_pool->consumer_index == 0)
1507 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001508 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001509 else
1510 tx_pool->consumer_index--;
1511
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001512 dev_kfree_skb_any(skb);
1513 tx_buff->skb = NULL;
1514
Thomas Falconb8c80b82017-05-26 10:30:42 -04001515 if (lpar_rc == H_CLOSED) {
1516 /* Disable TX and report carrier off if queue is closed.
1517 * Firmware guarantees that a signal will be sent to the
1518 * driver, triggering a reset or some other action.
1519 */
1520 netif_tx_stop_all_queues(netdev);
1521 netif_carrier_off(netdev);
1522 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001523
Thomas Falcon032c5e82015-12-21 11:26:06 -06001524 tx_send_failed++;
1525 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001526 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001527 goto out;
1528 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001529
Thomas Falconffc385b2018-02-18 10:08:41 -06001530 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001531 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001532 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001533 netif_stop_subqueue(netdev, queue_num);
1534 }
1535
Thomas Falcon032c5e82015-12-21 11:26:06 -06001536 tx_packets++;
1537 tx_bytes += skb->len;
1538 txq->trans_start = jiffies;
1539 ret = NETDEV_TX_OK;
1540
1541out:
1542 netdev->stats.tx_dropped += tx_dropped;
1543 netdev->stats.tx_bytes += tx_bytes;
1544 netdev->stats.tx_packets += tx_packets;
1545 adapter->tx_send_failed += tx_send_failed;
1546 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001547 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1548 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1549 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001550
1551 return ret;
1552}
1553
1554static void ibmvnic_set_multi(struct net_device *netdev)
1555{
1556 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1557 struct netdev_hw_addr *ha;
1558 union ibmvnic_crq crq;
1559
1560 memset(&crq, 0, sizeof(crq));
1561 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1562 crq.request_capability.cmd = REQUEST_CAPABILITY;
1563
1564 if (netdev->flags & IFF_PROMISC) {
1565 if (!adapter->promisc_supported)
1566 return;
1567 } else {
1568 if (netdev->flags & IFF_ALLMULTI) {
1569 /* Accept all multicast */
1570 memset(&crq, 0, sizeof(crq));
1571 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1572 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1573 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1574 ibmvnic_send_crq(adapter, &crq);
1575 } else if (netdev_mc_empty(netdev)) {
1576 /* Reject all multicast */
1577 memset(&crq, 0, sizeof(crq));
1578 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1579 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1580 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1581 ibmvnic_send_crq(adapter, &crq);
1582 } else {
1583 /* Accept one or more multicast(s) */
1584 netdev_for_each_mc_addr(ha, netdev) {
1585 memset(&crq, 0, sizeof(crq));
1586 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1587 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1588 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1589 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1590 ha->addr);
1591 ibmvnic_send_crq(adapter, &crq);
1592 }
1593 }
1594 }
1595}
1596
John Allenc26eba02017-10-26 16:23:25 -05001597static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001598{
1599 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1600 struct sockaddr *addr = p;
1601 union ibmvnic_crq crq;
1602
1603 if (!is_valid_ether_addr(addr->sa_data))
1604 return -EADDRNOTAVAIL;
1605
1606 memset(&crq, 0, sizeof(crq));
1607 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1608 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1609 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001610
1611 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001613 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001615 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616}
1617
John Allenc26eba02017-10-26 16:23:25 -05001618static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1619{
1620 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1621 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001622 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001623
Thomas Falcon3d166132018-01-10 19:39:52 -06001624 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001625 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1626 adapter->mac_change_pending = true;
1627 return 0;
1628 }
1629
Thomas Falconf8136142018-01-29 13:45:05 -06001630 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001631
Thomas Falconf8136142018-01-29 13:45:05 -06001632 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001633}
1634
Nathan Fontenoted651a12017-05-03 14:04:38 -04001635/**
1636 * do_reset returns zero if we are able to keep processing reset events, or
1637 * non-zero if we hit a fatal error and must halt.
1638 */
1639static int do_reset(struct ibmvnic_adapter *adapter,
1640 struct ibmvnic_rwi *rwi, u32 reset_state)
1641{
John Allen896d8692018-01-18 16:26:31 -06001642 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001643 struct net_device *netdev = adapter->netdev;
1644 int i, rc;
1645
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001646 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1647 rwi->reset_reason);
1648
Nathan Fontenoted651a12017-05-03 14:04:38 -04001649 netif_carrier_off(netdev);
1650 adapter->reset_reason = rwi->reset_reason;
1651
John Allen896d8692018-01-18 16:26:31 -06001652 old_num_rx_queues = adapter->req_rx_queues;
1653 old_num_tx_queues = adapter->req_tx_queues;
1654
Nathan Fontenoted651a12017-05-03 14:04:38 -04001655 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1656 rc = ibmvnic_reenable_crq_queue(adapter);
1657 if (rc)
1658 return 0;
Thomas Falcon18b8d6b2018-03-07 17:51:47 -06001659 ibmvnic_cleanup(netdev);
1660 } else if (rwi->reset_reason == VNIC_RESET_FAILOVER) {
1661 ibmvnic_cleanup(netdev);
1662 } else {
1663 rc = __ibmvnic_close(netdev);
1664 if (rc)
1665 return rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001666 }
1667
John Allenc26eba02017-10-26 16:23:25 -05001668 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1669 adapter->wait_for_reset) {
1670 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001671 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001672 release_crq_queue(adapter);
1673 }
1674
John Allen8cb31cf2017-05-26 10:30:37 -04001675 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1676 /* remove the closed state so when we call open it appears
1677 * we are coming from the probed state.
1678 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001679 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001680
John Allen8cb31cf2017-05-26 10:30:37 -04001681 rc = ibmvnic_init(adapter);
1682 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001683 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001684
1685 /* If the adapter was in PROBE state prior to the reset,
1686 * exit here.
1687 */
1688 if (reset_state == VNIC_PROBED)
1689 return 0;
1690
1691 rc = ibmvnic_login(netdev);
1692 if (rc) {
1693 adapter->state = VNIC_PROBED;
1694 return 0;
1695 }
1696
John Allenc26eba02017-10-26 16:23:25 -05001697 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1698 adapter->wait_for_reset) {
1699 rc = init_resources(adapter);
1700 if (rc)
1701 return rc;
John Allen896d8692018-01-18 16:26:31 -06001702 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1703 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001704 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001705 release_rx_pools(adapter);
1706 release_tx_pools(adapter);
1707 init_rx_pools(netdev);
1708 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001709
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001710 release_napi(adapter);
1711 init_napi(adapter);
John Allenc26eba02017-10-26 16:23:25 -05001712 } else {
1713 rc = reset_tx_pools(adapter);
1714 if (rc)
1715 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001716
John Allenc26eba02017-10-26 16:23:25 -05001717 rc = reset_rx_pools(adapter);
1718 if (rc)
1719 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001720
John Allenc26eba02017-10-26 16:23:25 -05001721 if (reset_state == VNIC_CLOSED)
1722 return 0;
1723 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001724 }
1725
Nathan Fontenoted651a12017-05-03 14:04:38 -04001726 rc = __ibmvnic_open(netdev);
1727 if (rc) {
1728 if (list_empty(&adapter->rwi_list))
1729 adapter->state = VNIC_CLOSED;
1730 else
1731 adapter->state = reset_state;
1732
1733 return 0;
1734 }
1735
Nathan Fontenoted651a12017-05-03 14:04:38 -04001736 /* kick napi */
1737 for (i = 0; i < adapter->req_rx_queues; i++)
1738 napi_schedule(&adapter->napi[i]);
1739
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001740 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1741 netdev_notify_peers(netdev);
1742
Thomas Falconcc85c022018-02-13 15:32:50 -06001743 netif_carrier_on(netdev);
1744
Nathan Fontenoted651a12017-05-03 14:04:38 -04001745 return 0;
1746}
1747
1748static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1749{
1750 struct ibmvnic_rwi *rwi;
1751
1752 mutex_lock(&adapter->rwi_lock);
1753
1754 if (!list_empty(&adapter->rwi_list)) {
1755 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1756 list);
1757 list_del(&rwi->list);
1758 } else {
1759 rwi = NULL;
1760 }
1761
1762 mutex_unlock(&adapter->rwi_lock);
1763 return rwi;
1764}
1765
1766static void free_all_rwi(struct ibmvnic_adapter *adapter)
1767{
1768 struct ibmvnic_rwi *rwi;
1769
1770 rwi = get_next_rwi(adapter);
1771 while (rwi) {
1772 kfree(rwi);
1773 rwi = get_next_rwi(adapter);
1774 }
1775}
1776
1777static void __ibmvnic_reset(struct work_struct *work)
1778{
1779 struct ibmvnic_rwi *rwi;
1780 struct ibmvnic_adapter *adapter;
1781 struct net_device *netdev;
1782 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001783 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001784
1785 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1786 netdev = adapter->netdev;
1787
1788 mutex_lock(&adapter->reset_lock);
1789 adapter->resetting = true;
1790 reset_state = adapter->state;
1791
1792 rwi = get_next_rwi(adapter);
1793 while (rwi) {
1794 rc = do_reset(adapter, rwi, reset_state);
1795 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001796 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001797 break;
1798
1799 rwi = get_next_rwi(adapter);
1800 }
1801
John Allenc26eba02017-10-26 16:23:25 -05001802 if (adapter->wait_for_reset) {
1803 adapter->wait_for_reset = false;
1804 adapter->reset_done_rc = rc;
1805 complete(&adapter->reset_done);
1806 }
1807
Nathan Fontenoted651a12017-05-03 14:04:38 -04001808 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001809 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001810 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001811 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001812 return;
1813 }
1814
1815 adapter->resetting = false;
1816 mutex_unlock(&adapter->reset_lock);
1817}
1818
1819static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1820 enum ibmvnic_reset_reason reason)
1821{
1822 struct ibmvnic_rwi *rwi, *tmp;
1823 struct net_device *netdev = adapter->netdev;
1824 struct list_head *entry;
1825
1826 if (adapter->state == VNIC_REMOVING ||
1827 adapter->state == VNIC_REMOVED) {
1828 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1829 return;
1830 }
1831
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001832 if (adapter->state == VNIC_PROBING) {
1833 netdev_warn(netdev, "Adapter reset during probe\n");
1834 adapter->init_done_rc = EAGAIN;
1835 return;
1836 }
1837
Nathan Fontenoted651a12017-05-03 14:04:38 -04001838 mutex_lock(&adapter->rwi_lock);
1839
1840 list_for_each(entry, &adapter->rwi_list) {
1841 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1842 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001843 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001844 mutex_unlock(&adapter->rwi_lock);
1845 return;
1846 }
1847 }
1848
1849 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1850 if (!rwi) {
1851 mutex_unlock(&adapter->rwi_lock);
1852 ibmvnic_close(netdev);
1853 return;
1854 }
1855
1856 rwi->reset_reason = reason;
1857 list_add_tail(&rwi->list, &adapter->rwi_list);
1858 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001859
1860 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001861 schedule_work(&adapter->ibmvnic_reset);
1862}
1863
Thomas Falcon032c5e82015-12-21 11:26:06 -06001864static void ibmvnic_tx_timeout(struct net_device *dev)
1865{
1866 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867
Nathan Fontenoted651a12017-05-03 14:04:38 -04001868 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001869}
1870
1871static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1872 struct ibmvnic_rx_buff *rx_buff)
1873{
1874 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1875
1876 rx_buff->skb = NULL;
1877
1878 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1879 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1880
1881 atomic_dec(&pool->available);
1882}
1883
1884static int ibmvnic_poll(struct napi_struct *napi, int budget)
1885{
1886 struct net_device *netdev = napi->dev;
1887 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1888 int scrq_num = (int)(napi - adapter->napi);
1889 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001890
Thomas Falcon032c5e82015-12-21 11:26:06 -06001891restart_poll:
1892 while (frames_processed < budget) {
1893 struct sk_buff *skb;
1894 struct ibmvnic_rx_buff *rx_buff;
1895 union sub_crq *next;
1896 u32 length;
1897 u16 offset;
1898 u8 flags = 0;
1899
John Allen34686562018-02-06 16:21:49 -06001900 if (unlikely(adapter->resetting &&
1901 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001902 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1903 napi_complete_done(napi, frames_processed);
1904 return frames_processed;
1905 }
1906
Thomas Falcon032c5e82015-12-21 11:26:06 -06001907 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1908 break;
1909 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1910 rx_buff =
1911 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1912 rx_comp.correlator);
1913 /* do error checking */
1914 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001915 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1916 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001917 /* free the entry */
1918 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001919 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001920 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001921 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06001922 } else if (!rx_buff->skb) {
1923 /* free the entry */
1924 next->rx_comp.first = 0;
1925 remove_buff_from_pool(adapter, rx_buff);
1926 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001927 }
1928
1929 length = be32_to_cpu(next->rx_comp.len);
1930 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1931 flags = next->rx_comp.flags;
1932 skb = rx_buff->skb;
1933 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1934 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001935
1936 /* VLAN Header has been stripped by the system firmware and
1937 * needs to be inserted by the driver
1938 */
1939 if (adapter->rx_vlan_header_insertion &&
1940 (flags & IBMVNIC_VLAN_STRIPPED))
1941 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1942 ntohs(next->rx_comp.vlan_tci));
1943
Thomas Falcon032c5e82015-12-21 11:26:06 -06001944 /* free the entry */
1945 next->rx_comp.first = 0;
1946 remove_buff_from_pool(adapter, rx_buff);
1947
1948 skb_put(skb, length);
1949 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001950 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001951
1952 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1953 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1954 skb->ip_summed = CHECKSUM_UNNECESSARY;
1955 }
1956
1957 length = skb->len;
1958 napi_gro_receive(napi, skb); /* send it up */
1959 netdev->stats.rx_packets++;
1960 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001961 adapter->rx_stats_buffers[scrq_num].packets++;
1962 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001963 frames_processed++;
1964 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001965
1966 if (adapter->state != VNIC_CLOSING)
1967 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001968
1969 if (frames_processed < budget) {
1970 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001971 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001972 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1973 napi_reschedule(napi)) {
1974 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1975 goto restart_poll;
1976 }
1977 }
1978 return frames_processed;
1979}
1980
1981#ifdef CONFIG_NET_POLL_CONTROLLER
1982static void ibmvnic_netpoll_controller(struct net_device *dev)
1983{
1984 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1985 int i;
1986
1987 replenish_pools(netdev_priv(dev));
1988 for (i = 0; i < adapter->req_rx_queues; i++)
1989 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1990 adapter->rx_scrq[i]);
1991}
1992#endif
1993
John Allenc26eba02017-10-26 16:23:25 -05001994static int wait_for_reset(struct ibmvnic_adapter *adapter)
1995{
1996 adapter->fallback.mtu = adapter->req_mtu;
1997 adapter->fallback.rx_queues = adapter->req_rx_queues;
1998 adapter->fallback.tx_queues = adapter->req_tx_queues;
1999 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2000 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2001
2002 init_completion(&adapter->reset_done);
2003 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2004 adapter->wait_for_reset = true;
2005 wait_for_completion(&adapter->reset_done);
2006
2007 if (adapter->reset_done_rc) {
2008 adapter->desired.mtu = adapter->fallback.mtu;
2009 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2010 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2011 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2012 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2013
2014 init_completion(&adapter->reset_done);
2015 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2016 wait_for_completion(&adapter->reset_done);
2017 }
2018 adapter->wait_for_reset = false;
2019
2020 return adapter->reset_done_rc;
2021}
2022
John Allen3a807b72017-06-06 16:55:52 -05002023static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2024{
John Allenc26eba02017-10-26 16:23:25 -05002025 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2026
2027 adapter->desired.mtu = new_mtu + ETH_HLEN;
2028
2029 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002030}
2031
Thomas Falcon032c5e82015-12-21 11:26:06 -06002032static const struct net_device_ops ibmvnic_netdev_ops = {
2033 .ndo_open = ibmvnic_open,
2034 .ndo_stop = ibmvnic_close,
2035 .ndo_start_xmit = ibmvnic_xmit,
2036 .ndo_set_rx_mode = ibmvnic_set_multi,
2037 .ndo_set_mac_address = ibmvnic_set_mac,
2038 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002039 .ndo_tx_timeout = ibmvnic_tx_timeout,
2040#ifdef CONFIG_NET_POLL_CONTROLLER
2041 .ndo_poll_controller = ibmvnic_netpoll_controller,
2042#endif
John Allen3a807b72017-06-06 16:55:52 -05002043 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002044};
2045
2046/* ethtool functions */
2047
Philippe Reynes8a433792017-01-07 22:37:29 +01002048static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2049 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002050{
Philippe Reynes8a433792017-01-07 22:37:29 +01002051 u32 supported, advertising;
2052
2053 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002054 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002055 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002056 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002057 cmd->base.speed = SPEED_1000;
2058 cmd->base.duplex = DUPLEX_FULL;
2059 cmd->base.port = PORT_FIBRE;
2060 cmd->base.phy_address = 0;
2061 cmd->base.autoneg = AUTONEG_ENABLE;
2062
2063 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2064 supported);
2065 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2066 advertising);
2067
Thomas Falcon032c5e82015-12-21 11:26:06 -06002068 return 0;
2069}
2070
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002071static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002072 struct ethtool_drvinfo *info)
2073{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002074 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2075
Thomas Falcon032c5e82015-12-21 11:26:06 -06002076 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2077 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002078 strlcpy(info->fw_version, adapter->fw_version,
2079 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002080}
2081
2082static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2083{
2084 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2085
2086 return adapter->msg_enable;
2087}
2088
2089static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2090{
2091 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2092
2093 adapter->msg_enable = data;
2094}
2095
2096static u32 ibmvnic_get_link(struct net_device *netdev)
2097{
2098 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2099
2100 /* Don't need to send a query because we request a logical link up at
2101 * init and then we wait for link state indications
2102 */
2103 return adapter->logical_link_state;
2104}
2105
2106static void ibmvnic_get_ringparam(struct net_device *netdev,
2107 struct ethtool_ringparam *ring)
2108{
John Allenbc131b32017-08-02 16:46:30 -05002109 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2110
2111 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2112 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002113 ring->rx_mini_max_pending = 0;
2114 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002115 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2116 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002117 ring->rx_mini_pending = 0;
2118 ring->rx_jumbo_pending = 0;
2119}
2120
John Allenc26eba02017-10-26 16:23:25 -05002121static int ibmvnic_set_ringparam(struct net_device *netdev,
2122 struct ethtool_ringparam *ring)
2123{
2124 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2125
2126 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2127 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2128 netdev_err(netdev, "Invalid request.\n");
2129 netdev_err(netdev, "Max tx buffers = %llu\n",
2130 adapter->max_rx_add_entries_per_subcrq);
2131 netdev_err(netdev, "Max rx buffers = %llu\n",
2132 adapter->max_tx_entries_per_subcrq);
2133 return -EINVAL;
2134 }
2135
2136 adapter->desired.rx_entries = ring->rx_pending;
2137 adapter->desired.tx_entries = ring->tx_pending;
2138
2139 return wait_for_reset(adapter);
2140}
2141
John Allenc2dbeb62017-08-02 16:47:17 -05002142static void ibmvnic_get_channels(struct net_device *netdev,
2143 struct ethtool_channels *channels)
2144{
2145 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2146
2147 channels->max_rx = adapter->max_rx_queues;
2148 channels->max_tx = adapter->max_tx_queues;
2149 channels->max_other = 0;
2150 channels->max_combined = 0;
2151 channels->rx_count = adapter->req_rx_queues;
2152 channels->tx_count = adapter->req_tx_queues;
2153 channels->other_count = 0;
2154 channels->combined_count = 0;
2155}
2156
John Allenc26eba02017-10-26 16:23:25 -05002157static int ibmvnic_set_channels(struct net_device *netdev,
2158 struct ethtool_channels *channels)
2159{
2160 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2161
2162 adapter->desired.rx_queues = channels->rx_count;
2163 adapter->desired.tx_queues = channels->tx_count;
2164
2165 return wait_for_reset(adapter);
2166}
2167
Thomas Falcon032c5e82015-12-21 11:26:06 -06002168static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2169{
John Allen3d52b592017-08-02 16:44:14 -05002170 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002171 int i;
2172
2173 if (stringset != ETH_SS_STATS)
2174 return;
2175
2176 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2177 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002178
2179 for (i = 0; i < adapter->req_tx_queues; i++) {
2180 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2181 data += ETH_GSTRING_LEN;
2182
2183 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2184 data += ETH_GSTRING_LEN;
2185
2186 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2187 data += ETH_GSTRING_LEN;
2188 }
2189
2190 for (i = 0; i < adapter->req_rx_queues; i++) {
2191 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2192 data += ETH_GSTRING_LEN;
2193
2194 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2195 data += ETH_GSTRING_LEN;
2196
2197 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2198 data += ETH_GSTRING_LEN;
2199 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002200}
2201
2202static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2203{
John Allen3d52b592017-08-02 16:44:14 -05002204 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2205
Thomas Falcon032c5e82015-12-21 11:26:06 -06002206 switch (sset) {
2207 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002208 return ARRAY_SIZE(ibmvnic_stats) +
2209 adapter->req_tx_queues * NUM_TX_STATS +
2210 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002211 default:
2212 return -EOPNOTSUPP;
2213 }
2214}
2215
2216static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2217 struct ethtool_stats *stats, u64 *data)
2218{
2219 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2220 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002221 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002222
2223 memset(&crq, 0, sizeof(crq));
2224 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2225 crq.request_statistics.cmd = REQUEST_STATISTICS;
2226 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2227 crq.request_statistics.len =
2228 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002229
2230 /* Wait for data to be written */
2231 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002232 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002233 wait_for_completion(&adapter->stats_done);
2234
2235 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002236 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2237 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002238
2239 for (j = 0; j < adapter->req_tx_queues; j++) {
2240 data[i] = adapter->tx_stats_buffers[j].packets;
2241 i++;
2242 data[i] = adapter->tx_stats_buffers[j].bytes;
2243 i++;
2244 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2245 i++;
2246 }
2247
2248 for (j = 0; j < adapter->req_rx_queues; j++) {
2249 data[i] = adapter->rx_stats_buffers[j].packets;
2250 i++;
2251 data[i] = adapter->rx_stats_buffers[j].bytes;
2252 i++;
2253 data[i] = adapter->rx_stats_buffers[j].interrupts;
2254 i++;
2255 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002256}
2257
2258static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002259 .get_drvinfo = ibmvnic_get_drvinfo,
2260 .get_msglevel = ibmvnic_get_msglevel,
2261 .set_msglevel = ibmvnic_set_msglevel,
2262 .get_link = ibmvnic_get_link,
2263 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002264 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002265 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002266 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267 .get_strings = ibmvnic_get_strings,
2268 .get_sset_count = ibmvnic_get_sset_count,
2269 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002270 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002271};
2272
2273/* Routines for managing CRQs/sCRQs */
2274
Nathan Fontenot57a49432017-05-26 10:31:12 -04002275static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2276 struct ibmvnic_sub_crq_queue *scrq)
2277{
2278 int rc;
2279
2280 if (scrq->irq) {
2281 free_irq(scrq->irq, scrq);
2282 irq_dispose_mapping(scrq->irq);
2283 scrq->irq = 0;
2284 }
2285
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002286 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002287 scrq->cur = 0;
2288
2289 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2290 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2291 return rc;
2292}
2293
2294static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2295{
2296 int i, rc;
2297
2298 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002299 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002300 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2301 if (rc)
2302 return rc;
2303 }
2304
2305 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002306 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002307 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2308 if (rc)
2309 return rc;
2310 }
2311
Nathan Fontenot57a49432017-05-26 10:31:12 -04002312 return rc;
2313}
2314
Thomas Falcon032c5e82015-12-21 11:26:06 -06002315static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002316 struct ibmvnic_sub_crq_queue *scrq,
2317 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002318{
2319 struct device *dev = &adapter->vdev->dev;
2320 long rc;
2321
2322 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2323
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002324 if (do_h_free) {
2325 /* Close the sub-crqs */
2326 do {
2327 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2328 adapter->vdev->unit_address,
2329 scrq->crq_num);
2330 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002331
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002332 if (rc) {
2333 netdev_err(adapter->netdev,
2334 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2335 scrq->crq_num, rc);
2336 }
Thomas Falconffa73852017-04-19 13:44:29 -04002337 }
2338
Thomas Falcon032c5e82015-12-21 11:26:06 -06002339 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2340 DMA_BIDIRECTIONAL);
2341 free_pages((unsigned long)scrq->msgs, 2);
2342 kfree(scrq);
2343}
2344
2345static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2346 *adapter)
2347{
2348 struct device *dev = &adapter->vdev->dev;
2349 struct ibmvnic_sub_crq_queue *scrq;
2350 int rc;
2351
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002352 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002353 if (!scrq)
2354 return NULL;
2355
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002356 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002357 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002358 if (!scrq->msgs) {
2359 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2360 goto zero_page_failed;
2361 }
2362
2363 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2364 DMA_BIDIRECTIONAL);
2365 if (dma_mapping_error(dev, scrq->msg_token)) {
2366 dev_warn(dev, "Couldn't map crq queue messages page\n");
2367 goto map_failed;
2368 }
2369
2370 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2371 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2372
2373 if (rc == H_RESOURCE)
2374 rc = ibmvnic_reset_crq(adapter);
2375
2376 if (rc == H_CLOSED) {
2377 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2378 } else if (rc) {
2379 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2380 goto reg_failed;
2381 }
2382
Thomas Falcon032c5e82015-12-21 11:26:06 -06002383 scrq->adapter = adapter;
2384 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002385 spin_lock_init(&scrq->lock);
2386
2387 netdev_dbg(adapter->netdev,
2388 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2389 scrq->crq_num, scrq->hw_irq, scrq->irq);
2390
2391 return scrq;
2392
Thomas Falcon032c5e82015-12-21 11:26:06 -06002393reg_failed:
2394 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2395 DMA_BIDIRECTIONAL);
2396map_failed:
2397 free_pages((unsigned long)scrq->msgs, 2);
2398zero_page_failed:
2399 kfree(scrq);
2400
2401 return NULL;
2402}
2403
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002404static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002405{
2406 int i;
2407
2408 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002409 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002410 if (!adapter->tx_scrq[i])
2411 continue;
2412
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002413 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2414 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002415 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 free_irq(adapter->tx_scrq[i]->irq,
2417 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002418 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002419 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002421
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002422 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2423 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002424 }
2425
Nathan Fontenot9501df32017-03-15 23:38:07 -04002426 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002427 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002428 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002429 }
2430
2431 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002432 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002433 if (!adapter->rx_scrq[i])
2434 continue;
2435
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002436 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2437 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002438 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 free_irq(adapter->rx_scrq[i]->irq,
2440 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002441 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002442 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002443 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002444
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002445 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2446 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002447 }
2448
Nathan Fontenot9501df32017-03-15 23:38:07 -04002449 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002450 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002451 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002452 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002453}
2454
2455static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2456 struct ibmvnic_sub_crq_queue *scrq)
2457{
2458 struct device *dev = &adapter->vdev->dev;
2459 unsigned long rc;
2460
2461 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2462 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2463 if (rc)
2464 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2465 scrq->hw_irq, rc);
2466 return rc;
2467}
2468
2469static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2470 struct ibmvnic_sub_crq_queue *scrq)
2471{
2472 struct device *dev = &adapter->vdev->dev;
2473 unsigned long rc;
2474
2475 if (scrq->hw_irq > 0x100000000ULL) {
2476 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2477 return 1;
2478 }
2479
2480 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2481 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2482 if (rc)
2483 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2484 scrq->hw_irq, rc);
2485 return rc;
2486}
2487
2488static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2489 struct ibmvnic_sub_crq_queue *scrq)
2490{
2491 struct device *dev = &adapter->vdev->dev;
2492 struct ibmvnic_tx_buff *txbuff;
2493 union sub_crq *next;
2494 int index;
2495 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002496 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002497
2498restart_loop:
2499 while (pending_scrq(adapter, scrq)) {
2500 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002501 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002502
2503 next = ibmvnic_next_scrq(adapter, scrq);
2504 for (i = 0; i < next->tx_comp.num_comps; i++) {
2505 if (next->tx_comp.rcs[i]) {
2506 dev_err(dev, "tx error %x\n",
2507 next->tx_comp.rcs[i]);
2508 continue;
2509 }
2510 index = be32_to_cpu(next->tx_comp.correlators[i]);
2511 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2512
2513 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2514 if (!txbuff->data_dma[j])
2515 continue;
2516
2517 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002518 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002519 /* if sub_crq was sent indirectly */
2520 first = txbuff->indir_arr[0].generic.first;
2521 if (first == IBMVNIC_CRQ_CMD) {
2522 dma_unmap_single(dev, txbuff->indir_dma,
2523 sizeof(txbuff->indir_arr),
2524 DMA_TO_DEVICE);
2525 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002526
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002527 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002528 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002529 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002530 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002531
Thomas Falconffc385b2018-02-18 10:08:41 -06002532 num_entries += txbuff->num_entries;
2533
Thomas Falcon032c5e82015-12-21 11:26:06 -06002534 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2535 producer_index] = index;
2536 adapter->tx_pool[pool].producer_index =
2537 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002538 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539 }
2540 /* remove tx_comp scrq*/
2541 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002542
Thomas Falconffc385b2018-02-18 10:08:41 -06002543 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002544 (adapter->req_tx_entries_per_subcrq / 2) &&
2545 __netif_subqueue_stopped(adapter->netdev,
2546 scrq->pool_index)) {
2547 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002548 netdev_dbg(adapter->netdev, "Started queue %d\n",
2549 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002550 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002551 }
2552
2553 enable_scrq_irq(adapter, scrq);
2554
2555 if (pending_scrq(adapter, scrq)) {
2556 disable_scrq_irq(adapter, scrq);
2557 goto restart_loop;
2558 }
2559
2560 return 0;
2561}
2562
2563static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2564{
2565 struct ibmvnic_sub_crq_queue *scrq = instance;
2566 struct ibmvnic_adapter *adapter = scrq->adapter;
2567
2568 disable_scrq_irq(adapter, scrq);
2569 ibmvnic_complete_tx(adapter, scrq);
2570
2571 return IRQ_HANDLED;
2572}
2573
2574static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2575{
2576 struct ibmvnic_sub_crq_queue *scrq = instance;
2577 struct ibmvnic_adapter *adapter = scrq->adapter;
2578
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002579 /* When booting a kdump kernel we can hit pending interrupts
2580 * prior to completing driver initialization.
2581 */
2582 if (unlikely(adapter->state != VNIC_OPEN))
2583 return IRQ_NONE;
2584
John Allen3d52b592017-08-02 16:44:14 -05002585 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2586
Thomas Falcon032c5e82015-12-21 11:26:06 -06002587 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2588 disable_scrq_irq(adapter, scrq);
2589 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2590 }
2591
2592 return IRQ_HANDLED;
2593}
2594
Thomas Falconea22d512016-07-06 15:35:17 -05002595static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2596{
2597 struct device *dev = &adapter->vdev->dev;
2598 struct ibmvnic_sub_crq_queue *scrq;
2599 int i = 0, j = 0;
2600 int rc = 0;
2601
2602 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002603 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2604 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002605 scrq = adapter->tx_scrq[i];
2606 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2607
Michael Ellerman99c17902016-09-10 19:59:05 +10002608 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002609 rc = -EINVAL;
2610 dev_err(dev, "Error mapping irq\n");
2611 goto req_tx_irq_failed;
2612 }
2613
2614 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2615 0, "ibmvnic_tx", scrq);
2616
2617 if (rc) {
2618 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2619 scrq->irq, rc);
2620 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002621 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002622 }
2623 }
2624
2625 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002626 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2627 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002628 scrq = adapter->rx_scrq[i];
2629 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002630 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002631 rc = -EINVAL;
2632 dev_err(dev, "Error mapping irq\n");
2633 goto req_rx_irq_failed;
2634 }
2635 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2636 0, "ibmvnic_rx", scrq);
2637 if (rc) {
2638 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2639 scrq->irq, rc);
2640 irq_dispose_mapping(scrq->irq);
2641 goto req_rx_irq_failed;
2642 }
2643 }
2644 return rc;
2645
2646req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002647 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002648 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2649 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002650 }
Thomas Falconea22d512016-07-06 15:35:17 -05002651 i = adapter->req_tx_queues;
2652req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002653 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002654 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2655 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002656 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002657 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002658 return rc;
2659}
2660
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002661static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002662{
2663 struct device *dev = &adapter->vdev->dev;
2664 struct ibmvnic_sub_crq_queue **allqueues;
2665 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 int total_queues;
2667 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002668 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002669
Thomas Falcon032c5e82015-12-21 11:26:06 -06002670 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2671
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002672 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002673 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002674 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002675
2676 for (i = 0; i < total_queues; i++) {
2677 allqueues[i] = init_sub_crq_queue(adapter);
2678 if (!allqueues[i]) {
2679 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2680 break;
2681 }
2682 registered_queues++;
2683 }
2684
2685 /* Make sure we were able to register the minimum number of queues */
2686 if (registered_queues <
2687 adapter->min_tx_queues + adapter->min_rx_queues) {
2688 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2689 goto tx_failed;
2690 }
2691
2692 /* Distribute the failed allocated queues*/
2693 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2694 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2695 switch (i % 3) {
2696 case 0:
2697 if (adapter->req_rx_queues > adapter->min_rx_queues)
2698 adapter->req_rx_queues--;
2699 else
2700 more++;
2701 break;
2702 case 1:
2703 if (adapter->req_tx_queues > adapter->min_tx_queues)
2704 adapter->req_tx_queues--;
2705 else
2706 more++;
2707 break;
2708 }
2709 }
2710
2711 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002712 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713 if (!adapter->tx_scrq)
2714 goto tx_failed;
2715
2716 for (i = 0; i < adapter->req_tx_queues; i++) {
2717 adapter->tx_scrq[i] = allqueues[i];
2718 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002719 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002720 }
2721
2722 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002723 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 if (!adapter->rx_scrq)
2725 goto rx_failed;
2726
2727 for (i = 0; i < adapter->req_rx_queues; i++) {
2728 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2729 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002730 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 }
2732
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002733 kfree(allqueues);
2734 return 0;
2735
2736rx_failed:
2737 kfree(adapter->tx_scrq);
2738 adapter->tx_scrq = NULL;
2739tx_failed:
2740 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002741 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002742 kfree(allqueues);
2743 return -1;
2744}
2745
2746static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2747{
2748 struct device *dev = &adapter->vdev->dev;
2749 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002750 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002751
2752 if (!retry) {
2753 /* Sub-CRQ entries are 32 byte long */
2754 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2755
2756 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2757 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2758 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2759 return;
2760 }
2761
John Allenc26eba02017-10-26 16:23:25 -05002762 if (adapter->desired.mtu)
2763 adapter->req_mtu = adapter->desired.mtu;
2764 else
2765 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002766
John Allenc26eba02017-10-26 16:23:25 -05002767 if (!adapter->desired.tx_entries)
2768 adapter->desired.tx_entries =
2769 adapter->max_tx_entries_per_subcrq;
2770 if (!adapter->desired.rx_entries)
2771 adapter->desired.rx_entries =
2772 adapter->max_rx_add_entries_per_subcrq;
2773
2774 max_entries = IBMVNIC_MAX_LTB_SIZE /
2775 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2776
2777 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2778 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2779 adapter->desired.tx_entries = max_entries;
2780 }
2781
2782 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2783 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2784 adapter->desired.rx_entries = max_entries;
2785 }
2786
2787 if (adapter->desired.tx_entries)
2788 adapter->req_tx_entries_per_subcrq =
2789 adapter->desired.tx_entries;
2790 else
2791 adapter->req_tx_entries_per_subcrq =
2792 adapter->max_tx_entries_per_subcrq;
2793
2794 if (adapter->desired.rx_entries)
2795 adapter->req_rx_add_entries_per_subcrq =
2796 adapter->desired.rx_entries;
2797 else
2798 adapter->req_rx_add_entries_per_subcrq =
2799 adapter->max_rx_add_entries_per_subcrq;
2800
2801 if (adapter->desired.tx_queues)
2802 adapter->req_tx_queues =
2803 adapter->desired.tx_queues;
2804 else
2805 adapter->req_tx_queues =
2806 adapter->opt_tx_comp_sub_queues;
2807
2808 if (adapter->desired.rx_queues)
2809 adapter->req_rx_queues =
2810 adapter->desired.rx_queues;
2811 else
2812 adapter->req_rx_queues =
2813 adapter->opt_rx_comp_queues;
2814
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002815 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002816 }
2817
Thomas Falcon032c5e82015-12-21 11:26:06 -06002818 memset(&crq, 0, sizeof(crq));
2819 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2820 crq.request_capability.cmd = REQUEST_CAPABILITY;
2821
2822 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002823 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002824 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002825 ibmvnic_send_crq(adapter, &crq);
2826
2827 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002828 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002829 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002830 ibmvnic_send_crq(adapter, &crq);
2831
2832 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002833 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002834 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002835 ibmvnic_send_crq(adapter, &crq);
2836
2837 crq.request_capability.capability =
2838 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2839 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002840 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002841 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002842 ibmvnic_send_crq(adapter, &crq);
2843
2844 crq.request_capability.capability =
2845 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2846 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002847 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002848 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002849 ibmvnic_send_crq(adapter, &crq);
2850
2851 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002852 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002853 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002854 ibmvnic_send_crq(adapter, &crq);
2855
2856 if (adapter->netdev->flags & IFF_PROMISC) {
2857 if (adapter->promisc_supported) {
2858 crq.request_capability.capability =
2859 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002860 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002861 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002862 ibmvnic_send_crq(adapter, &crq);
2863 }
2864 } else {
2865 crq.request_capability.capability =
2866 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002867 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002868 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869 ibmvnic_send_crq(adapter, &crq);
2870 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002871}
2872
2873static int pending_scrq(struct ibmvnic_adapter *adapter,
2874 struct ibmvnic_sub_crq_queue *scrq)
2875{
2876 union sub_crq *entry = &scrq->msgs[scrq->cur];
2877
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002878 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002879 return 1;
2880 else
2881 return 0;
2882}
2883
2884static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2885 struct ibmvnic_sub_crq_queue *scrq)
2886{
2887 union sub_crq *entry;
2888 unsigned long flags;
2889
2890 spin_lock_irqsave(&scrq->lock, flags);
2891 entry = &scrq->msgs[scrq->cur];
2892 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2893 if (++scrq->cur == scrq->size)
2894 scrq->cur = 0;
2895 } else {
2896 entry = NULL;
2897 }
2898 spin_unlock_irqrestore(&scrq->lock, flags);
2899
2900 return entry;
2901}
2902
2903static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2904{
2905 struct ibmvnic_crq_queue *queue = &adapter->crq;
2906 union ibmvnic_crq *crq;
2907
2908 crq = &queue->msgs[queue->cur];
2909 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2910 if (++queue->cur == queue->size)
2911 queue->cur = 0;
2912 } else {
2913 crq = NULL;
2914 }
2915
2916 return crq;
2917}
2918
2919static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2920 union sub_crq *sub_crq)
2921{
2922 unsigned int ua = adapter->vdev->unit_address;
2923 struct device *dev = &adapter->vdev->dev;
2924 u64 *u64_crq = (u64 *)sub_crq;
2925 int rc;
2926
2927 netdev_dbg(adapter->netdev,
2928 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2929 (unsigned long int)cpu_to_be64(remote_handle),
2930 (unsigned long int)cpu_to_be64(u64_crq[0]),
2931 (unsigned long int)cpu_to_be64(u64_crq[1]),
2932 (unsigned long int)cpu_to_be64(u64_crq[2]),
2933 (unsigned long int)cpu_to_be64(u64_crq[3]));
2934
2935 /* Make sure the hypervisor sees the complete request */
2936 mb();
2937
2938 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2939 cpu_to_be64(remote_handle),
2940 cpu_to_be64(u64_crq[0]),
2941 cpu_to_be64(u64_crq[1]),
2942 cpu_to_be64(u64_crq[2]),
2943 cpu_to_be64(u64_crq[3]));
2944
2945 if (rc) {
2946 if (rc == H_CLOSED)
2947 dev_warn(dev, "CRQ Queue closed\n");
2948 dev_err(dev, "Send error (rc=%d)\n", rc);
2949 }
2950
2951 return rc;
2952}
2953
Thomas Falconad7775d2016-04-01 17:20:34 -05002954static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2955 u64 remote_handle, u64 ioba, u64 num_entries)
2956{
2957 unsigned int ua = adapter->vdev->unit_address;
2958 struct device *dev = &adapter->vdev->dev;
2959 int rc;
2960
2961 /* Make sure the hypervisor sees the complete request */
2962 mb();
2963 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2964 cpu_to_be64(remote_handle),
2965 ioba, num_entries);
2966
2967 if (rc) {
2968 if (rc == H_CLOSED)
2969 dev_warn(dev, "CRQ Queue closed\n");
2970 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2971 }
2972
2973 return rc;
2974}
2975
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2977 union ibmvnic_crq *crq)
2978{
2979 unsigned int ua = adapter->vdev->unit_address;
2980 struct device *dev = &adapter->vdev->dev;
2981 u64 *u64_crq = (u64 *)crq;
2982 int rc;
2983
2984 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2985 (unsigned long int)cpu_to_be64(u64_crq[0]),
2986 (unsigned long int)cpu_to_be64(u64_crq[1]));
2987
2988 /* Make sure the hypervisor sees the complete request */
2989 mb();
2990
2991 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2992 cpu_to_be64(u64_crq[0]),
2993 cpu_to_be64(u64_crq[1]));
2994
2995 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002996 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002997 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002998 if (adapter->resetting)
2999 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3000 }
3001
Thomas Falcon032c5e82015-12-21 11:26:06 -06003002 dev_warn(dev, "Send error (rc=%d)\n", rc);
3003 }
3004
3005 return rc;
3006}
3007
3008static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3009{
3010 union ibmvnic_crq crq;
3011
3012 memset(&crq, 0, sizeof(crq));
3013 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3014 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3015 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3016
3017 return ibmvnic_send_crq(adapter, &crq);
3018}
3019
Thomas Falcon032c5e82015-12-21 11:26:06 -06003020static int send_version_xchg(struct ibmvnic_adapter *adapter)
3021{
3022 union ibmvnic_crq crq;
3023
3024 memset(&crq, 0, sizeof(crq));
3025 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3026 crq.version_exchange.cmd = VERSION_EXCHANGE;
3027 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3028
3029 return ibmvnic_send_crq(adapter, &crq);
3030}
3031
Nathan Fontenot37798d02017-11-08 11:23:56 -06003032struct vnic_login_client_data {
3033 u8 type;
3034 __be16 len;
3035 char name;
3036} __packed;
3037
3038static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3039{
3040 int len;
3041
3042 /* Calculate the amount of buffer space needed for the
3043 * vnic client data in the login buffer. There are four entries,
3044 * OS name, LPAR name, device name, and a null last entry.
3045 */
3046 len = 4 * sizeof(struct vnic_login_client_data);
3047 len += 6; /* "Linux" plus NULL */
3048 len += strlen(utsname()->nodename) + 1;
3049 len += strlen(adapter->netdev->name) + 1;
3050
3051 return len;
3052}
3053
3054static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3055 struct vnic_login_client_data *vlcd)
3056{
3057 const char *os_name = "Linux";
3058 int len;
3059
3060 /* Type 1 - LPAR OS */
3061 vlcd->type = 1;
3062 len = strlen(os_name) + 1;
3063 vlcd->len = cpu_to_be16(len);
3064 strncpy(&vlcd->name, os_name, len);
3065 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3066
3067 /* Type 2 - LPAR name */
3068 vlcd->type = 2;
3069 len = strlen(utsname()->nodename) + 1;
3070 vlcd->len = cpu_to_be16(len);
3071 strncpy(&vlcd->name, utsname()->nodename, len);
3072 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3073
3074 /* Type 3 - device name */
3075 vlcd->type = 3;
3076 len = strlen(adapter->netdev->name) + 1;
3077 vlcd->len = cpu_to_be16(len);
3078 strncpy(&vlcd->name, adapter->netdev->name, len);
3079}
3080
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003081static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082{
3083 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3084 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003085 struct device *dev = &adapter->vdev->dev;
3086 dma_addr_t rsp_buffer_token;
3087 dma_addr_t buffer_token;
3088 size_t rsp_buffer_size;
3089 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003090 size_t buffer_size;
3091 __be64 *tx_list_p;
3092 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003093 int client_data_len;
3094 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003095 int i;
3096
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003097 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3098 netdev_err(adapter->netdev,
3099 "RX or TX queues are not allocated, device login failed\n");
3100 return -1;
3101 }
3102
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003103 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003104 client_data_len = vnic_client_data_len(adapter);
3105
Thomas Falcon032c5e82015-12-21 11:26:06 -06003106 buffer_size =
3107 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003108 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3109 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003110
Nathan Fontenot37798d02017-11-08 11:23:56 -06003111 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003112 if (!login_buffer)
3113 goto buf_alloc_failed;
3114
3115 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3116 DMA_TO_DEVICE);
3117 if (dma_mapping_error(dev, buffer_token)) {
3118 dev_err(dev, "Couldn't map login buffer\n");
3119 goto buf_map_failed;
3120 }
3121
John Allen498cd8e2016-04-06 11:49:55 -05003122 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3123 sizeof(u64) * adapter->req_tx_queues +
3124 sizeof(u64) * adapter->req_rx_queues +
3125 sizeof(u64) * adapter->req_rx_queues +
3126 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127
3128 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3129 if (!login_rsp_buffer)
3130 goto buf_rsp_alloc_failed;
3131
3132 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3133 rsp_buffer_size, DMA_FROM_DEVICE);
3134 if (dma_mapping_error(dev, rsp_buffer_token)) {
3135 dev_err(dev, "Couldn't map login rsp buffer\n");
3136 goto buf_rsp_map_failed;
3137 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003138
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139 adapter->login_buf = login_buffer;
3140 adapter->login_buf_token = buffer_token;
3141 adapter->login_buf_sz = buffer_size;
3142 adapter->login_rsp_buf = login_rsp_buffer;
3143 adapter->login_rsp_buf_token = rsp_buffer_token;
3144 adapter->login_rsp_buf_sz = rsp_buffer_size;
3145
3146 login_buffer->len = cpu_to_be32(buffer_size);
3147 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3148 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3149 login_buffer->off_txcomp_subcrqs =
3150 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3151 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3152 login_buffer->off_rxcomp_subcrqs =
3153 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3154 sizeof(u64) * adapter->req_tx_queues);
3155 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3156 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3157
3158 tx_list_p = (__be64 *)((char *)login_buffer +
3159 sizeof(struct ibmvnic_login_buffer));
3160 rx_list_p = (__be64 *)((char *)login_buffer +
3161 sizeof(struct ibmvnic_login_buffer) +
3162 sizeof(u64) * adapter->req_tx_queues);
3163
3164 for (i = 0; i < adapter->req_tx_queues; i++) {
3165 if (adapter->tx_scrq[i]) {
3166 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3167 crq_num);
3168 }
3169 }
3170
3171 for (i = 0; i < adapter->req_rx_queues; i++) {
3172 if (adapter->rx_scrq[i]) {
3173 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3174 crq_num);
3175 }
3176 }
3177
Nathan Fontenot37798d02017-11-08 11:23:56 -06003178 /* Insert vNIC login client data */
3179 vlcd = (struct vnic_login_client_data *)
3180 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3181 login_buffer->client_data_offset =
3182 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3183 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3184
3185 vnic_add_client_data(adapter, vlcd);
3186
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3188 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3189 netdev_dbg(adapter->netdev, "%016lx\n",
3190 ((unsigned long int *)(adapter->login_buf))[i]);
3191 }
3192
3193 memset(&crq, 0, sizeof(crq));
3194 crq.login.first = IBMVNIC_CRQ_CMD;
3195 crq.login.cmd = LOGIN;
3196 crq.login.ioba = cpu_to_be32(buffer_token);
3197 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003198 ibmvnic_send_crq(adapter, &crq);
3199
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003200 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201
Thomas Falcon032c5e82015-12-21 11:26:06 -06003202buf_rsp_map_failed:
3203 kfree(login_rsp_buffer);
3204buf_rsp_alloc_failed:
3205 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3206buf_map_failed:
3207 kfree(login_buffer);
3208buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003209 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210}
3211
3212static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3213 u32 len, u8 map_id)
3214{
3215 union ibmvnic_crq crq;
3216
3217 memset(&crq, 0, sizeof(crq));
3218 crq.request_map.first = IBMVNIC_CRQ_CMD;
3219 crq.request_map.cmd = REQUEST_MAP;
3220 crq.request_map.map_id = map_id;
3221 crq.request_map.ioba = cpu_to_be32(addr);
3222 crq.request_map.len = cpu_to_be32(len);
3223 ibmvnic_send_crq(adapter, &crq);
3224}
3225
3226static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3227{
3228 union ibmvnic_crq crq;
3229
3230 memset(&crq, 0, sizeof(crq));
3231 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3232 crq.request_unmap.cmd = REQUEST_UNMAP;
3233 crq.request_unmap.map_id = map_id;
3234 ibmvnic_send_crq(adapter, &crq);
3235}
3236
3237static void send_map_query(struct ibmvnic_adapter *adapter)
3238{
3239 union ibmvnic_crq crq;
3240
3241 memset(&crq, 0, sizeof(crq));
3242 crq.query_map.first = IBMVNIC_CRQ_CMD;
3243 crq.query_map.cmd = QUERY_MAP;
3244 ibmvnic_send_crq(adapter, &crq);
3245}
3246
3247/* Send a series of CRQs requesting various capabilities of the VNIC server */
3248static void send_cap_queries(struct ibmvnic_adapter *adapter)
3249{
3250 union ibmvnic_crq crq;
3251
Thomas Falcon901e0402017-02-15 12:17:59 -06003252 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003253 memset(&crq, 0, sizeof(crq));
3254 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3255 crq.query_capability.cmd = QUERY_CAPABILITY;
3256
3257 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003258 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003259 ibmvnic_send_crq(adapter, &crq);
3260
3261 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003262 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003263 ibmvnic_send_crq(adapter, &crq);
3264
3265 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003266 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003267 ibmvnic_send_crq(adapter, &crq);
3268
3269 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003270 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003271 ibmvnic_send_crq(adapter, &crq);
3272
3273 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003274 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003275 ibmvnic_send_crq(adapter, &crq);
3276
3277 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003278 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003279 ibmvnic_send_crq(adapter, &crq);
3280
3281 crq.query_capability.capability =
3282 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003283 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003284 ibmvnic_send_crq(adapter, &crq);
3285
3286 crq.query_capability.capability =
3287 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003288 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003289 ibmvnic_send_crq(adapter, &crq);
3290
3291 crq.query_capability.capability =
3292 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003293 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003294 ibmvnic_send_crq(adapter, &crq);
3295
3296 crq.query_capability.capability =
3297 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003298 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299 ibmvnic_send_crq(adapter, &crq);
3300
3301 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003302 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303 ibmvnic_send_crq(adapter, &crq);
3304
3305 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003306 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003307 ibmvnic_send_crq(adapter, &crq);
3308
3309 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003310 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003311 ibmvnic_send_crq(adapter, &crq);
3312
3313 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003314 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003315 ibmvnic_send_crq(adapter, &crq);
3316
3317 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003318 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003319 ibmvnic_send_crq(adapter, &crq);
3320
3321 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003322 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323 ibmvnic_send_crq(adapter, &crq);
3324
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003325 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3326 atomic_inc(&adapter->running_cap_crqs);
3327 ibmvnic_send_crq(adapter, &crq);
3328
Thomas Falcon032c5e82015-12-21 11:26:06 -06003329 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003330 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003331 ibmvnic_send_crq(adapter, &crq);
3332
3333 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003334 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003335 ibmvnic_send_crq(adapter, &crq);
3336
3337 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003338 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003339 ibmvnic_send_crq(adapter, &crq);
3340
3341 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003342 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343 ibmvnic_send_crq(adapter, &crq);
3344
3345 crq.query_capability.capability =
3346 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003347 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348 ibmvnic_send_crq(adapter, &crq);
3349
3350 crq.query_capability.capability =
3351 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
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 =
3356 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003357 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003358 ibmvnic_send_crq(adapter, &crq);
3359
3360 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003361 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003362 ibmvnic_send_crq(adapter, &crq);
3363}
3364
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003365static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3366 struct ibmvnic_adapter *adapter)
3367{
3368 struct device *dev = &adapter->vdev->dev;
3369
3370 if (crq->get_vpd_size_rsp.rc.code) {
3371 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3372 crq->get_vpd_size_rsp.rc.code);
3373 complete(&adapter->fw_done);
3374 return;
3375 }
3376
3377 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3378 complete(&adapter->fw_done);
3379}
3380
3381static void handle_vpd_rsp(union ibmvnic_crq *crq,
3382 struct ibmvnic_adapter *adapter)
3383{
3384 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003385 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003386 u8 fw_level_len = 0;
3387
3388 memset(adapter->fw_version, 0, 32);
3389
3390 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3391 DMA_FROM_DEVICE);
3392
3393 if (crq->get_vpd_rsp.rc.code) {
3394 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3395 crq->get_vpd_rsp.rc.code);
3396 goto complete;
3397 }
3398
3399 /* get the position of the firmware version info
3400 * located after the ASCII 'RM' substring in the buffer
3401 */
3402 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3403 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003404 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 -02003405 goto complete;
3406 }
3407
3408 /* get length of firmware level ASCII substring */
3409 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3410 fw_level_len = *(substr + 2);
3411 } else {
3412 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3413 goto complete;
3414 }
3415
3416 /* copy firmware version string from vpd into adapter */
3417 if ((substr + 3 + fw_level_len) <
3418 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003419 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003420 } else {
3421 dev_info(dev, "FW substr extrapolated VPD buff\n");
3422 }
3423
3424complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003425 if (adapter->fw_version[0] == '\0')
3426 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003427 complete(&adapter->fw_done);
3428}
3429
Thomas Falcon032c5e82015-12-21 11:26:06 -06003430static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3431{
3432 struct device *dev = &adapter->vdev->dev;
3433 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3434 union ibmvnic_crq crq;
3435 int i;
3436
3437 dma_unmap_single(dev, adapter->ip_offload_tok,
3438 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3439
3440 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3441 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3442 netdev_dbg(adapter->netdev, "%016lx\n",
3443 ((unsigned long int *)(buf))[i]);
3444
3445 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3446 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3447 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3448 buf->tcp_ipv4_chksum);
3449 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3450 buf->tcp_ipv6_chksum);
3451 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3452 buf->udp_ipv4_chksum);
3453 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3454 buf->udp_ipv6_chksum);
3455 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3456 buf->large_tx_ipv4);
3457 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3458 buf->large_tx_ipv6);
3459 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3460 buf->large_rx_ipv4);
3461 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3462 buf->large_rx_ipv6);
3463 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3464 buf->max_ipv4_header_size);
3465 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3466 buf->max_ipv6_header_size);
3467 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3468 buf->max_tcp_header_size);
3469 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3470 buf->max_udp_header_size);
3471 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3472 buf->max_large_tx_size);
3473 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3474 buf->max_large_rx_size);
3475 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3476 buf->ipv6_extension_header);
3477 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3478 buf->tcp_pseudosum_req);
3479 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3480 buf->num_ipv6_ext_headers);
3481 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3482 buf->off_ipv6_ext_headers);
3483
3484 adapter->ip_offload_ctrl_tok =
3485 dma_map_single(dev, &adapter->ip_offload_ctrl,
3486 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3487
3488 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3489 dev_err(dev, "Couldn't map ip offload control buffer\n");
3490 return;
3491 }
3492
Thomas Falconf6897942018-01-18 19:05:01 -06003493 adapter->ip_offload_ctrl.len =
3494 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003495 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003496 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3497 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3499 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3500 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3501 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003502 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3503 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003504
Thomas Falconfdb06102017-10-17 12:36:55 -05003505 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003506 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3507 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3508
Thomas Falcon15482052017-10-17 12:36:54 -05003509 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003510
3511 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3512 adapter->netdev->features |= NETIF_F_IP_CSUM;
3513
3514 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3515 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3516
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003517 if ((adapter->netdev->features &
3518 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3519 adapter->netdev->features |= NETIF_F_RXCSUM;
3520
Thomas Falconfdb06102017-10-17 12:36:55 -05003521 if (buf->large_tx_ipv4)
3522 adapter->netdev->features |= NETIF_F_TSO;
3523 if (buf->large_tx_ipv6)
3524 adapter->netdev->features |= NETIF_F_TSO6;
3525
Thomas Falconaa0bf852017-10-17 12:36:56 -05003526 adapter->netdev->hw_features |= adapter->netdev->features;
3527
Thomas Falcon032c5e82015-12-21 11:26:06 -06003528 memset(&crq, 0, sizeof(crq));
3529 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3530 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3531 crq.control_ip_offload.len =
3532 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3533 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3534 ibmvnic_send_crq(adapter, &crq);
3535}
3536
3537static void handle_error_info_rsp(union ibmvnic_crq *crq,
3538 struct ibmvnic_adapter *adapter)
3539{
3540 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003541 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003542 unsigned long flags;
3543 bool found = false;
3544 int i;
3545
3546 if (!crq->request_error_rsp.rc.code) {
3547 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3548 crq->request_error_rsp.rc.code);
3549 return;
3550 }
3551
3552 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003553 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003554 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3555 found = true;
3556 list_del(&error_buff->list);
3557 break;
3558 }
3559 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3560
3561 if (!found) {
3562 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003563 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003564 return;
3565 }
3566
3567 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003568 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003569
3570 for (i = 0; i < error_buff->len; i++) {
3571 pr_cont("%02x", (int)error_buff->buff[i]);
3572 if (i % 8 == 7)
3573 pr_cont(" ");
3574 }
3575 pr_cont("\n");
3576
3577 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3578 DMA_FROM_DEVICE);
3579 kfree(error_buff->buff);
3580 kfree(error_buff);
3581}
3582
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003583static void request_error_information(struct ibmvnic_adapter *adapter,
3584 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003585{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003586 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003587 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003588 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003589 unsigned long timeout = msecs_to_jiffies(30000);
3590 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003591 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003592 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003593
3594 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3595 if (!error_buff)
3596 return;
3597
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003598 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003599 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3600 if (!error_buff->buff) {
3601 kfree(error_buff);
3602 return;
3603 }
3604
3605 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3606 DMA_FROM_DEVICE);
3607 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003608 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003609 kfree(error_buff->buff);
3610 kfree(error_buff);
3611 return;
3612 }
3613
Thomas Falcon032c5e82015-12-21 11:26:06 -06003614 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003615 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616
3617 spin_lock_irqsave(&adapter->error_list_lock, flags);
3618 list_add_tail(&error_buff->list, &adapter->errors);
3619 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3620
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003621 memset(&crq, 0, sizeof(crq));
3622 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3623 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3624 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3625 crq.request_error_info.len = cpu_to_be32(detail_len);
3626 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3627
3628 rc = ibmvnic_send_crq(adapter, &crq);
3629 if (rc) {
3630 netdev_err(netdev, "failed to request error information\n");
3631 goto err_info_fail;
3632 }
3633
3634 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3635 netdev_err(netdev, "timeout waiting for error information\n");
3636 goto err_info_fail;
3637 }
3638
3639 return;
3640
3641err_info_fail:
3642 spin_lock_irqsave(&adapter->error_list_lock, flags);
3643 list_del(&error_buff->list);
3644 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3645
3646 kfree(error_buff->buff);
3647 kfree(error_buff);
3648}
3649
3650static void handle_error_indication(union ibmvnic_crq *crq,
3651 struct ibmvnic_adapter *adapter)
3652{
3653 struct device *dev = &adapter->vdev->dev;
3654
3655 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3656 crq->error_indication.flags
3657 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3658 be32_to_cpu(crq->error_indication.error_id),
3659 be16_to_cpu(crq->error_indication.error_cause));
3660
3661 if (be32_to_cpu(crq->error_indication.error_id))
3662 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003663
3664 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3665 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003666 else
3667 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003668}
3669
Thomas Falconf8136142018-01-29 13:45:05 -06003670static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3671 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003672{
3673 struct net_device *netdev = adapter->netdev;
3674 struct device *dev = &adapter->vdev->dev;
3675 long rc;
3676
3677 rc = crq->change_mac_addr_rsp.rc.code;
3678 if (rc) {
3679 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003680 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003681 }
3682 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3683 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003684out:
3685 complete(&adapter->fw_done);
3686 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003687}
3688
3689static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3690 struct ibmvnic_adapter *adapter)
3691{
3692 struct device *dev = &adapter->vdev->dev;
3693 u64 *req_value;
3694 char *name;
3695
Thomas Falcon901e0402017-02-15 12:17:59 -06003696 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003697 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3698 case REQ_TX_QUEUES:
3699 req_value = &adapter->req_tx_queues;
3700 name = "tx";
3701 break;
3702 case REQ_RX_QUEUES:
3703 req_value = &adapter->req_rx_queues;
3704 name = "rx";
3705 break;
3706 case REQ_RX_ADD_QUEUES:
3707 req_value = &adapter->req_rx_add_queues;
3708 name = "rx_add";
3709 break;
3710 case REQ_TX_ENTRIES_PER_SUBCRQ:
3711 req_value = &adapter->req_tx_entries_per_subcrq;
3712 name = "tx_entries_per_subcrq";
3713 break;
3714 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3715 req_value = &adapter->req_rx_add_entries_per_subcrq;
3716 name = "rx_add_entries_per_subcrq";
3717 break;
3718 case REQ_MTU:
3719 req_value = &adapter->req_mtu;
3720 name = "mtu";
3721 break;
3722 case PROMISC_REQUESTED:
3723 req_value = &adapter->promisc;
3724 name = "promisc";
3725 break;
3726 default:
3727 dev_err(dev, "Got invalid cap request rsp %d\n",
3728 crq->request_capability.capability);
3729 return;
3730 }
3731
3732 switch (crq->request_capability_rsp.rc.code) {
3733 case SUCCESS:
3734 break;
3735 case PARTIALSUCCESS:
3736 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3737 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003738 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003739 number), name);
John Allene7913802018-01-18 16:27:12 -06003740
3741 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3742 REQ_MTU) {
3743 pr_err("mtu of %llu is not supported. Reverting.\n",
3744 *req_value);
3745 *req_value = adapter->fallback.mtu;
3746 } else {
3747 *req_value =
3748 be64_to_cpu(crq->request_capability_rsp.number);
3749 }
3750
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003751 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003752 return;
3753 default:
3754 dev_err(dev, "Error %d in request cap rsp\n",
3755 crq->request_capability_rsp.rc.code);
3756 return;
3757 }
3758
3759 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003760 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003761 union ibmvnic_crq newcrq;
3762 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3763 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3764 &adapter->ip_offload_buf;
3765
Thomas Falcon249168a2017-02-15 12:18:00 -06003766 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003767 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3768 buf_sz,
3769 DMA_FROM_DEVICE);
3770
3771 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3772 if (!firmware_has_feature(FW_FEATURE_CMO))
3773 dev_err(dev, "Couldn't map offload buffer\n");
3774 return;
3775 }
3776
3777 memset(&newcrq, 0, sizeof(newcrq));
3778 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3779 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3780 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3781 newcrq.query_ip_offload.ioba =
3782 cpu_to_be32(adapter->ip_offload_tok);
3783
3784 ibmvnic_send_crq(adapter, &newcrq);
3785 }
3786}
3787
3788static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3789 struct ibmvnic_adapter *adapter)
3790{
3791 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003792 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003793 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3794 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003795 int i;
3796
3797 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3798 DMA_BIDIRECTIONAL);
3799 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3800 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3801
John Allen498cd8e2016-04-06 11:49:55 -05003802 /* If the number of queues requested can't be allocated by the
3803 * server, the login response will return with code 1. We will need
3804 * to resend the login buffer with fewer queues requested.
3805 */
3806 if (login_rsp_crq->generic.rc.code) {
3807 adapter->renegotiate = true;
3808 complete(&adapter->init_done);
3809 return 0;
3810 }
3811
John Allenc26eba02017-10-26 16:23:25 -05003812 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3813
Thomas Falcon032c5e82015-12-21 11:26:06 -06003814 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3815 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3816 netdev_dbg(adapter->netdev, "%016lx\n",
3817 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3818 }
3819
3820 /* Sanity checks */
3821 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3822 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3823 adapter->req_rx_add_queues !=
3824 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3825 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3826 ibmvnic_remove(adapter->vdev);
3827 return -EIO;
3828 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003829 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830 complete(&adapter->init_done);
3831
Thomas Falcon032c5e82015-12-21 11:26:06 -06003832 return 0;
3833}
3834
Thomas Falcon032c5e82015-12-21 11:26:06 -06003835static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3836 struct ibmvnic_adapter *adapter)
3837{
3838 struct device *dev = &adapter->vdev->dev;
3839 long rc;
3840
3841 rc = crq->request_unmap_rsp.rc.code;
3842 if (rc)
3843 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3844}
3845
3846static void handle_query_map_rsp(union ibmvnic_crq *crq,
3847 struct ibmvnic_adapter *adapter)
3848{
3849 struct net_device *netdev = adapter->netdev;
3850 struct device *dev = &adapter->vdev->dev;
3851 long rc;
3852
3853 rc = crq->query_map_rsp.rc.code;
3854 if (rc) {
3855 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3856 return;
3857 }
3858 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3859 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3860 crq->query_map_rsp.free_pages);
3861}
3862
3863static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3864 struct ibmvnic_adapter *adapter)
3865{
3866 struct net_device *netdev = adapter->netdev;
3867 struct device *dev = &adapter->vdev->dev;
3868 long rc;
3869
Thomas Falcon901e0402017-02-15 12:17:59 -06003870 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003871 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003872 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003873 rc = crq->query_capability.rc.code;
3874 if (rc) {
3875 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3876 goto out;
3877 }
3878
3879 switch (be16_to_cpu(crq->query_capability.capability)) {
3880 case MIN_TX_QUEUES:
3881 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003882 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3884 adapter->min_tx_queues);
3885 break;
3886 case MIN_RX_QUEUES:
3887 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003888 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003889 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3890 adapter->min_rx_queues);
3891 break;
3892 case MIN_RX_ADD_QUEUES:
3893 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003894 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003895 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3896 adapter->min_rx_add_queues);
3897 break;
3898 case MAX_TX_QUEUES:
3899 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003900 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3902 adapter->max_tx_queues);
3903 break;
3904 case MAX_RX_QUEUES:
3905 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003906 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3908 adapter->max_rx_queues);
3909 break;
3910 case MAX_RX_ADD_QUEUES:
3911 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003912 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003913 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3914 adapter->max_rx_add_queues);
3915 break;
3916 case MIN_TX_ENTRIES_PER_SUBCRQ:
3917 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003918 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003919 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3920 adapter->min_tx_entries_per_subcrq);
3921 break;
3922 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3923 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003924 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003925 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3926 adapter->min_rx_add_entries_per_subcrq);
3927 break;
3928 case MAX_TX_ENTRIES_PER_SUBCRQ:
3929 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003930 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3932 adapter->max_tx_entries_per_subcrq);
3933 break;
3934 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3935 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003936 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3938 adapter->max_rx_add_entries_per_subcrq);
3939 break;
3940 case TCP_IP_OFFLOAD:
3941 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003942 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003943 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3944 adapter->tcp_ip_offload);
3945 break;
3946 case PROMISC_SUPPORTED:
3947 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003948 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003949 netdev_dbg(netdev, "promisc_supported = %lld\n",
3950 adapter->promisc_supported);
3951 break;
3952 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003953 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003954 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003955 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3956 break;
3957 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003958 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003959 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003960 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3961 break;
3962 case MAX_MULTICAST_FILTERS:
3963 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003964 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3966 adapter->max_multicast_filters);
3967 break;
3968 case VLAN_HEADER_INSERTION:
3969 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003970 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003971 if (adapter->vlan_header_insertion)
3972 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3973 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3974 adapter->vlan_header_insertion);
3975 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003976 case RX_VLAN_HEADER_INSERTION:
3977 adapter->rx_vlan_header_insertion =
3978 be64_to_cpu(crq->query_capability.number);
3979 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3980 adapter->rx_vlan_header_insertion);
3981 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003982 case MAX_TX_SG_ENTRIES:
3983 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003984 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3986 adapter->max_tx_sg_entries);
3987 break;
3988 case RX_SG_SUPPORTED:
3989 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003990 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003991 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3992 adapter->rx_sg_supported);
3993 break;
3994 case OPT_TX_COMP_SUB_QUEUES:
3995 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003996 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003997 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3998 adapter->opt_tx_comp_sub_queues);
3999 break;
4000 case OPT_RX_COMP_QUEUES:
4001 adapter->opt_rx_comp_queues =
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, "opt_rx_comp_queues = %lld\n",
4004 adapter->opt_rx_comp_queues);
4005 break;
4006 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4007 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004008 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4010 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4011 break;
4012 case OPT_TX_ENTRIES_PER_SUBCRQ:
4013 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004014 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004015 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4016 adapter->opt_tx_entries_per_subcrq);
4017 break;
4018 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4019 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004020 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004021 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4022 adapter->opt_rxba_entries_per_subcrq);
4023 break;
4024 case TX_RX_DESC_REQ:
4025 adapter->tx_rx_desc_req = crq->query_capability.number;
4026 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4027 adapter->tx_rx_desc_req);
4028 break;
4029
4030 default:
4031 netdev_err(netdev, "Got invalid cap rsp %d\n",
4032 crq->query_capability.capability);
4033 }
4034
4035out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004036 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4037 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004038 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004039 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004040}
4041
Thomas Falcon032c5e82015-12-21 11:26:06 -06004042static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4043 struct ibmvnic_adapter *adapter)
4044{
4045 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4046 struct net_device *netdev = adapter->netdev;
4047 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004048 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004049 long rc;
4050
4051 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004052 (unsigned long int)cpu_to_be64(u64_crq[0]),
4053 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004054 switch (gen_crq->first) {
4055 case IBMVNIC_CRQ_INIT_RSP:
4056 switch (gen_crq->cmd) {
4057 case IBMVNIC_CRQ_INIT:
4058 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004059 adapter->from_passive_init = true;
4060 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004061 break;
4062 case IBMVNIC_CRQ_INIT_COMPLETE:
4063 dev_info(dev, "Partner initialization complete\n");
4064 send_version_xchg(adapter);
4065 break;
4066 default:
4067 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4068 }
4069 return;
4070 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004071 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004073 dev_info(dev, "Migrated, re-enabling adapter\n");
4074 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004075 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4076 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004077 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004078 } else {
4079 /* The adapter lost the connection */
4080 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4081 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004082 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004083 }
4084 return;
4085 case IBMVNIC_CRQ_CMD_RSP:
4086 break;
4087 default:
4088 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4089 gen_crq->first);
4090 return;
4091 }
4092
4093 switch (gen_crq->cmd) {
4094 case VERSION_EXCHANGE_RSP:
4095 rc = crq->version_exchange_rsp.rc.code;
4096 if (rc) {
4097 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4098 break;
4099 }
4100 dev_info(dev, "Partner protocol version is %d\n",
4101 crq->version_exchange_rsp.version);
4102 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4103 ibmvnic_version)
4104 ibmvnic_version =
4105 be16_to_cpu(crq->version_exchange_rsp.version);
4106 send_cap_queries(adapter);
4107 break;
4108 case QUERY_CAPABILITY_RSP:
4109 handle_query_cap_rsp(crq, adapter);
4110 break;
4111 case QUERY_MAP_RSP:
4112 handle_query_map_rsp(crq, adapter);
4113 break;
4114 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004115 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4116 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004117 break;
4118 case REQUEST_UNMAP_RSP:
4119 handle_request_unmap_rsp(crq, adapter);
4120 break;
4121 case REQUEST_CAPABILITY_RSP:
4122 handle_request_cap_rsp(crq, adapter);
4123 break;
4124 case LOGIN_RSP:
4125 netdev_dbg(netdev, "Got Login Response\n");
4126 handle_login_rsp(crq, adapter);
4127 break;
4128 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004129 netdev_dbg(netdev,
4130 "Got Logical Link State Response, state: %d rc: %d\n",
4131 crq->logical_link_state_rsp.link_state,
4132 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004133 adapter->logical_link_state =
4134 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004135 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4136 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004137 break;
4138 case LINK_STATE_INDICATION:
4139 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4140 adapter->phys_link_state =
4141 crq->link_state_indication.phys_link_state;
4142 adapter->logical_link_state =
4143 crq->link_state_indication.logical_link_state;
4144 break;
4145 case CHANGE_MAC_ADDR_RSP:
4146 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004147 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004148 break;
4149 case ERROR_INDICATION:
4150 netdev_dbg(netdev, "Got Error Indication\n");
4151 handle_error_indication(crq, adapter);
4152 break;
4153 case REQUEST_ERROR_RSP:
4154 netdev_dbg(netdev, "Got Error Detail Response\n");
4155 handle_error_info_rsp(crq, adapter);
4156 break;
4157 case REQUEST_STATISTICS_RSP:
4158 netdev_dbg(netdev, "Got Statistics Response\n");
4159 complete(&adapter->stats_done);
4160 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004161 case QUERY_IP_OFFLOAD_RSP:
4162 netdev_dbg(netdev, "Got Query IP offload Response\n");
4163 handle_query_ip_offload_rsp(adapter);
4164 break;
4165 case MULTICAST_CTRL_RSP:
4166 netdev_dbg(netdev, "Got multicast control Response\n");
4167 break;
4168 case CONTROL_IP_OFFLOAD_RSP:
4169 netdev_dbg(netdev, "Got Control IP offload Response\n");
4170 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4171 sizeof(adapter->ip_offload_ctrl),
4172 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004173 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004174 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004175 case COLLECT_FW_TRACE_RSP:
4176 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4177 complete(&adapter->fw_done);
4178 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004179 case GET_VPD_SIZE_RSP:
4180 handle_vpd_size_rsp(crq, adapter);
4181 break;
4182 case GET_VPD_RSP:
4183 handle_vpd_rsp(crq, adapter);
4184 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004185 default:
4186 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4187 gen_crq->cmd);
4188 }
4189}
4190
4191static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4192{
4193 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004194
Thomas Falcon6c267b32017-02-15 12:17:58 -06004195 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004196 return IRQ_HANDLED;
4197}
4198
4199static void ibmvnic_tasklet(void *data)
4200{
4201 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004203 union ibmvnic_crq *crq;
4204 unsigned long flags;
4205 bool done = false;
4206
4207 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004208 while (!done) {
4209 /* Pull all the valid messages off the CRQ */
4210 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4211 ibmvnic_handle_crq(crq, adapter);
4212 crq->generic.first = 0;
4213 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004214
4215 /* remain in tasklet until all
4216 * capabilities responses are received
4217 */
4218 if (!adapter->wait_capability)
4219 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004220 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004221 /* if capabilities CRQ's were sent in this tasklet, the following
4222 * tasklet must wait until all responses are received
4223 */
4224 if (atomic_read(&adapter->running_cap_crqs) != 0)
4225 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004226 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004227}
4228
4229static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4230{
4231 struct vio_dev *vdev = adapter->vdev;
4232 int rc;
4233
4234 do {
4235 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4236 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4237
4238 if (rc)
4239 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4240
4241 return rc;
4242}
4243
4244static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4245{
4246 struct ibmvnic_crq_queue *crq = &adapter->crq;
4247 struct device *dev = &adapter->vdev->dev;
4248 struct vio_dev *vdev = adapter->vdev;
4249 int rc;
4250
4251 /* Close the CRQ */
4252 do {
4253 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4254 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4255
4256 /* Clean out the queue */
4257 memset(crq->msgs, 0, PAGE_SIZE);
4258 crq->cur = 0;
4259
4260 /* And re-open it again */
4261 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4262 crq->msg_token, PAGE_SIZE);
4263
4264 if (rc == H_CLOSED)
4265 /* Adapter is good, but other end is not ready */
4266 dev_warn(dev, "Partner adapter not ready\n");
4267 else if (rc != 0)
4268 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4269
4270 return rc;
4271}
4272
Nathan Fontenotf9928872017-03-30 02:48:54 -04004273static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004274{
4275 struct ibmvnic_crq_queue *crq = &adapter->crq;
4276 struct vio_dev *vdev = adapter->vdev;
4277 long rc;
4278
Nathan Fontenotf9928872017-03-30 02:48:54 -04004279 if (!crq->msgs)
4280 return;
4281
Thomas Falcon032c5e82015-12-21 11:26:06 -06004282 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4283 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004284 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004285 do {
4286 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4287 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4288
4289 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4290 DMA_BIDIRECTIONAL);
4291 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004292 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004293}
4294
Nathan Fontenotf9928872017-03-30 02:48:54 -04004295static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004296{
4297 struct ibmvnic_crq_queue *crq = &adapter->crq;
4298 struct device *dev = &adapter->vdev->dev;
4299 struct vio_dev *vdev = adapter->vdev;
4300 int rc, retrc = -ENOMEM;
4301
Nathan Fontenotf9928872017-03-30 02:48:54 -04004302 if (crq->msgs)
4303 return 0;
4304
Thomas Falcon032c5e82015-12-21 11:26:06 -06004305 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4306 /* Should we allocate more than one page? */
4307
4308 if (!crq->msgs)
4309 return -ENOMEM;
4310
4311 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4312 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4313 DMA_BIDIRECTIONAL);
4314 if (dma_mapping_error(dev, crq->msg_token))
4315 goto map_failed;
4316
4317 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4318 crq->msg_token, PAGE_SIZE);
4319
4320 if (rc == H_RESOURCE)
4321 /* maybe kexecing and resource is busy. try a reset */
4322 rc = ibmvnic_reset_crq(adapter);
4323 retrc = rc;
4324
4325 if (rc == H_CLOSED) {
4326 dev_warn(dev, "Partner adapter not ready\n");
4327 } else if (rc) {
4328 dev_warn(dev, "Error %d opening adapter\n", rc);
4329 goto reg_crq_failed;
4330 }
4331
4332 retrc = 0;
4333
Thomas Falcon6c267b32017-02-15 12:17:58 -06004334 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4335 (unsigned long)adapter);
4336
Thomas Falcon032c5e82015-12-21 11:26:06 -06004337 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4338 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4339 adapter);
4340 if (rc) {
4341 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4342 vdev->irq, rc);
4343 goto req_irq_failed;
4344 }
4345
4346 rc = vio_enable_interrupts(vdev);
4347 if (rc) {
4348 dev_err(dev, "Error %d enabling interrupts\n", rc);
4349 goto req_irq_failed;
4350 }
4351
4352 crq->cur = 0;
4353 spin_lock_init(&crq->lock);
4354
4355 return retrc;
4356
4357req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004358 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004359 do {
4360 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4361 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4362reg_crq_failed:
4363 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4364map_failed:
4365 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004366 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004367 return retrc;
4368}
4369
John Allenf6ef6402017-03-17 17:13:42 -05004370static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4371{
4372 struct device *dev = &adapter->vdev->dev;
4373 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004374 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004375 int rc;
4376
John Allenc26eba02017-10-26 16:23:25 -05004377 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004378 rc = ibmvnic_reset_crq(adapter);
4379 if (!rc)
4380 rc = vio_enable_interrupts(adapter->vdev);
4381 } else {
4382 rc = init_crq_queue(adapter);
4383 }
4384
John Allenf6ef6402017-03-17 17:13:42 -05004385 if (rc) {
4386 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4387 return rc;
4388 }
4389
John Allen017892c12017-05-26 10:30:19 -04004390 adapter->from_passive_init = false;
4391
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004392 old_num_rx_queues = adapter->req_rx_queues;
4393 old_num_tx_queues = adapter->req_tx_queues;
4394
John Allenf6ef6402017-03-17 17:13:42 -05004395 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004396 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004397 ibmvnic_send_crq_init(adapter);
4398 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4399 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004400 return -1;
4401 }
4402
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004403 if (adapter->init_done_rc) {
4404 release_crq_queue(adapter);
4405 return adapter->init_done_rc;
4406 }
4407
John Allen017892c12017-05-26 10:30:19 -04004408 if (adapter->from_passive_init) {
4409 adapter->state = VNIC_OPEN;
4410 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004411 return -1;
4412 }
4413
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004414 if (adapter->resetting && !adapter->wait_for_reset) {
4415 if (adapter->req_rx_queues != old_num_rx_queues ||
4416 adapter->req_tx_queues != old_num_tx_queues) {
4417 release_sub_crqs(adapter, 0);
4418 rc = init_sub_crqs(adapter);
4419 } else {
4420 rc = reset_sub_crq_queues(adapter);
4421 }
4422 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004423 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004424 }
4425
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004426 if (rc) {
4427 dev_err(dev, "Initialization of sub crqs failed\n");
4428 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004429 return rc;
4430 }
4431
4432 rc = init_sub_crq_irqs(adapter);
4433 if (rc) {
4434 dev_err(dev, "Failed to initialize sub crq irqs\n");
4435 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004436 }
4437
Thomas Falcon53cc7722018-02-26 18:10:56 -06004438 rc = init_stats_buffers(adapter);
4439 if (rc)
4440 return rc;
4441
4442 rc = init_stats_token(adapter);
4443 if (rc)
4444 return rc;
4445
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004446 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004447}
4448
Thomas Falcon40c9db82017-06-12 12:35:04 -05004449static struct device_attribute dev_attr_failover;
4450
Thomas Falcon032c5e82015-12-21 11:26:06 -06004451static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4452{
4453 struct ibmvnic_adapter *adapter;
4454 struct net_device *netdev;
4455 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004456 int rc;
4457
4458 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4459 dev->unit_address);
4460
4461 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4462 VETH_MAC_ADDR, NULL);
4463 if (!mac_addr_p) {
4464 dev_err(&dev->dev,
4465 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4466 __FILE__, __LINE__);
4467 return 0;
4468 }
4469
4470 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004471 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004472 if (!netdev)
4473 return -ENOMEM;
4474
4475 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004476 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004477 dev_set_drvdata(&dev->dev, netdev);
4478 adapter->vdev = dev;
4479 adapter->netdev = netdev;
4480
4481 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4482 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4483 netdev->irq = dev->irq;
4484 netdev->netdev_ops = &ibmvnic_netdev_ops;
4485 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4486 SET_NETDEV_DEV(netdev, &dev->dev);
4487
4488 spin_lock_init(&adapter->stats_lock);
4489
Thomas Falcon032c5e82015-12-21 11:26:06 -06004490 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004491 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004492
Nathan Fontenoted651a12017-05-03 14:04:38 -04004493 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4494 INIT_LIST_HEAD(&adapter->rwi_list);
4495 mutex_init(&adapter->reset_lock);
4496 mutex_init(&adapter->rwi_lock);
4497 adapter->resetting = false;
4498
John Allenc26eba02017-10-26 16:23:25 -05004499 adapter->mac_change_pending = false;
4500
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004501 do {
4502 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004503 if (rc && rc != EAGAIN)
4504 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004505 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004506
Thomas Falconf39f0d12017-02-14 10:22:59 -06004507 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004508 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4509 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004510
Thomas Falcon40c9db82017-06-12 12:35:04 -05004511 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004512 if (rc)
4513 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004514
Mick Tarsele876a8a2017-09-28 13:53:18 -07004515 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004516 rc = register_netdev(netdev);
4517 if (rc) {
4518 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004519 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004520 }
4521 dev_info(&dev->dev, "ibmvnic registered\n");
4522
Nathan Fontenot90c80142017-05-03 14:04:32 -04004523 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004524
4525 adapter->wait_for_reset = false;
4526
Thomas Falcon032c5e82015-12-21 11:26:06 -06004527 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004528
4529ibmvnic_register_fail:
4530 device_remove_file(&dev->dev, &dev_attr_failover);
4531
4532ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004533 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004534 release_crq_queue(adapter);
4535 free_netdev(netdev);
4536
4537 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004538}
4539
4540static int ibmvnic_remove(struct vio_dev *dev)
4541{
4542 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004543 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004544
Nathan Fontenot90c80142017-05-03 14:04:32 -04004545 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004546 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004547 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004548
4549 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004550 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004551 release_crq_queue(adapter);
4552
Thomas Falcon53cc7722018-02-26 18:10:56 -06004553 release_stats_token(adapter);
4554 release_stats_buffers(adapter);
4555
Nathan Fontenot90c80142017-05-03 14:04:32 -04004556 adapter->state = VNIC_REMOVED;
4557
Nathan Fontenoted651a12017-05-03 14:04:38 -04004558 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004559 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004560 free_netdev(netdev);
4561 dev_set_drvdata(&dev->dev, NULL);
4562
4563 return 0;
4564}
4565
Thomas Falcon40c9db82017-06-12 12:35:04 -05004566static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4567 const char *buf, size_t count)
4568{
4569 struct net_device *netdev = dev_get_drvdata(dev);
4570 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4571 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4572 __be64 session_token;
4573 long rc;
4574
4575 if (!sysfs_streq(buf, "1"))
4576 return -EINVAL;
4577
4578 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4579 H_GET_SESSION_TOKEN, 0, 0, 0);
4580 if (rc) {
4581 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4582 rc);
4583 return -EINVAL;
4584 }
4585
4586 session_token = (__be64)retbuf[0];
4587 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4588 be64_to_cpu(session_token));
4589 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4590 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4591 if (rc) {
4592 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4593 rc);
4594 return -EINVAL;
4595 }
4596
4597 return count;
4598}
4599
Joe Perches6cbaefb2017-12-19 10:15:09 -08004600static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004601
Thomas Falcon032c5e82015-12-21 11:26:06 -06004602static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4603{
4604 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4605 struct ibmvnic_adapter *adapter;
4606 struct iommu_table *tbl;
4607 unsigned long ret = 0;
4608 int i;
4609
4610 tbl = get_iommu_table_base(&vdev->dev);
4611
4612 /* netdev inits at probe time along with the structures we need below*/
4613 if (!netdev)
4614 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4615
4616 adapter = netdev_priv(netdev);
4617
4618 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004619 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4620
4621 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4622 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4623
4624 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4625 i++)
4626 ret += adapter->rx_pool[i].size *
4627 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4628
4629 return ret;
4630}
4631
4632static int ibmvnic_resume(struct device *dev)
4633{
4634 struct net_device *netdev = dev_get_drvdata(dev);
4635 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004636
John Allencb89ba22017-06-19 11:27:53 -05004637 if (adapter->state != VNIC_OPEN)
4638 return 0;
4639
John Allena2488782017-07-24 13:26:06 -05004640 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004641
4642 return 0;
4643}
4644
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304645static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004646 {"network", "IBM,vnic"},
4647 {"", "" }
4648};
4649MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4650
4651static const struct dev_pm_ops ibmvnic_pm_ops = {
4652 .resume = ibmvnic_resume
4653};
4654
4655static struct vio_driver ibmvnic_driver = {
4656 .id_table = ibmvnic_device_table,
4657 .probe = ibmvnic_probe,
4658 .remove = ibmvnic_remove,
4659 .get_desired_dma = ibmvnic_get_desired_dma,
4660 .name = ibmvnic_driver_name,
4661 .pm = &ibmvnic_pm_ops,
4662};
4663
4664/* module functions */
4665static int __init ibmvnic_module_init(void)
4666{
4667 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4668 IBMVNIC_DRIVER_VERSION);
4669
4670 return vio_register_driver(&ibmvnic_driver);
4671}
4672
4673static void __exit ibmvnic_module_exit(void)
4674{
4675 vio_unregister_driver(&ibmvnic_driver);
4676}
4677
4678module_init(ibmvnic_module_init);
4679module_exit(ibmvnic_module_exit);