blob: 765407179fdd6a5352d1ff970ff700e140096efb [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 *
662 adapter->req_mtu)) {
663 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
Nathan Fontenoted651a12017-05-03 14:04:38 -04001146static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001147{
1148 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001149 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -05001150 int i;
1151
Nathan Fontenot90c80142017-05-03 14:04:32 -04001152 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001153
1154 /* ensure that transmissions are stopped if called by do_reset */
1155 if (adapter->resetting)
1156 netif_tx_disable(netdev);
1157 else
1158 netif_tx_stop_all_queues(netdev);
1159
John Allend944c3d62017-05-26 10:30:13 -04001160 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001161
1162 if (adapter->tx_scrq) {
1163 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001164 if (adapter->tx_scrq[i]->irq) {
1165 netdev_dbg(adapter->netdev,
1166 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001167 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001168 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001169 }
1170
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001171 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001172 if (rc)
1173 return rc;
1174
1175 if (adapter->rx_scrq) {
1176 for (i = 0; i < adapter->req_rx_queues; i++) {
1177 int retries = 10;
1178
1179 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1180 retries--;
1181 mdelay(100);
1182
1183 if (retries == 0)
1184 break;
1185 }
1186
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001187 if (adapter->rx_scrq[i]->irq) {
1188 netdev_dbg(adapter->netdev,
1189 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001190 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001191 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001192 }
1193 }
Thomas Falcond0869c02018-02-13 18:23:43 -06001194 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001195 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001196 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001197 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001198}
1199
Nathan Fontenoted651a12017-05-03 14:04:38 -04001200static int ibmvnic_close(struct net_device *netdev)
1201{
1202 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1203 int rc;
1204
1205 mutex_lock(&adapter->reset_lock);
1206 rc = __ibmvnic_close(netdev);
1207 mutex_unlock(&adapter->reset_lock);
1208
1209 return rc;
1210}
1211
Thomas Falconad7775d2016-04-01 17:20:34 -05001212/**
1213 * build_hdr_data - creates L2/L3/L4 header data buffer
1214 * @hdr_field - bitfield determining needed headers
1215 * @skb - socket buffer
1216 * @hdr_len - array of header lengths
1217 * @tot_len - total length of data
1218 *
1219 * Reads hdr_field to determine which headers are needed by firmware.
1220 * Builds a buffer containing these headers. Saves individual header
1221 * lengths and total buffer length to be used to build descriptors.
1222 */
1223static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1224 int *hdr_len, u8 *hdr_data)
1225{
1226 int len = 0;
1227 u8 *hdr;
1228
1229 hdr_len[0] = sizeof(struct ethhdr);
1230
1231 if (skb->protocol == htons(ETH_P_IP)) {
1232 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1233 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1234 hdr_len[2] = tcp_hdrlen(skb);
1235 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1236 hdr_len[2] = sizeof(struct udphdr);
1237 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1238 hdr_len[1] = sizeof(struct ipv6hdr);
1239 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1240 hdr_len[2] = tcp_hdrlen(skb);
1241 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1242 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001243 } else if (skb->protocol == htons(ETH_P_ARP)) {
1244 hdr_len[1] = arp_hdr_len(skb->dev);
1245 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001246 }
1247
1248 memset(hdr_data, 0, 120);
1249 if ((hdr_field >> 6) & 1) {
1250 hdr = skb_mac_header(skb);
1251 memcpy(hdr_data, hdr, hdr_len[0]);
1252 len += hdr_len[0];
1253 }
1254
1255 if ((hdr_field >> 5) & 1) {
1256 hdr = skb_network_header(skb);
1257 memcpy(hdr_data + len, hdr, hdr_len[1]);
1258 len += hdr_len[1];
1259 }
1260
1261 if ((hdr_field >> 4) & 1) {
1262 hdr = skb_transport_header(skb);
1263 memcpy(hdr_data + len, hdr, hdr_len[2]);
1264 len += hdr_len[2];
1265 }
1266 return len;
1267}
1268
1269/**
1270 * create_hdr_descs - create header and header extension descriptors
1271 * @hdr_field - bitfield determining needed headers
1272 * @data - buffer containing header data
1273 * @len - length of data buffer
1274 * @hdr_len - array of individual header lengths
1275 * @scrq_arr - descriptor array
1276 *
1277 * Creates header and, if needed, header extension descriptors and
1278 * places them in a descriptor array, scrq_arr
1279 */
1280
Thomas Falcon2de09682017-10-16 10:02:11 -05001281static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1282 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001283{
1284 union sub_crq hdr_desc;
1285 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001286 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001287 u8 *data, *cur;
1288 int tmp;
1289
1290 while (tmp_len > 0) {
1291 cur = hdr_data + len - tmp_len;
1292
1293 memset(&hdr_desc, 0, sizeof(hdr_desc));
1294 if (cur != hdr_data) {
1295 data = hdr_desc.hdr_ext.data;
1296 tmp = tmp_len > 29 ? 29 : tmp_len;
1297 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1298 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1299 hdr_desc.hdr_ext.len = tmp;
1300 } else {
1301 data = hdr_desc.hdr.data;
1302 tmp = tmp_len > 24 ? 24 : tmp_len;
1303 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1304 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1305 hdr_desc.hdr.len = tmp;
1306 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1307 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1308 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1309 hdr_desc.hdr.flag = hdr_field << 1;
1310 }
1311 memcpy(data, cur, tmp);
1312 tmp_len -= tmp;
1313 *scrq_arr = hdr_desc;
1314 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001315 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001316 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001317
1318 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001319}
1320
1321/**
1322 * build_hdr_descs_arr - build a header descriptor array
1323 * @skb - socket buffer
1324 * @num_entries - number of descriptors to be sent
1325 * @subcrq - first TX descriptor
1326 * @hdr_field - bit field determining which headers will be sent
1327 *
1328 * This function will build a TX descriptor array with applicable
1329 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1330 */
1331
1332static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1333 int *num_entries, u8 hdr_field)
1334{
1335 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001336 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001337 u8 *hdr_data = txbuff->hdr_data;
1338
1339 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1340 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001341 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001342 txbuff->indir_arr + 1);
1343}
1344
Thomas Falcon032c5e82015-12-21 11:26:06 -06001345static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1346{
1347 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1348 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001349 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001350 struct device *dev = &adapter->vdev->dev;
1351 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001352 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001353 struct ibmvnic_tx_pool *tx_pool;
1354 unsigned int tx_send_failed = 0;
1355 unsigned int tx_map_failed = 0;
1356 unsigned int tx_dropped = 0;
1357 unsigned int tx_packets = 0;
1358 unsigned int tx_bytes = 0;
1359 dma_addr_t data_dma_addr;
1360 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001361 unsigned long lpar_rc;
1362 union sub_crq tx_crq;
1363 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001364 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001365 unsigned char *dst;
1366 u64 *handle_array;
1367 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001368 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001369 int ret = 0;
1370
Nathan Fontenoted651a12017-05-03 14:04:38 -04001371 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001372 if (!netif_subqueue_stopped(netdev, skb))
1373 netif_stop_subqueue(netdev, queue_num);
1374 dev_kfree_skb_any(skb);
1375
Thomas Falcon032c5e82015-12-21 11:26:06 -06001376 tx_send_failed++;
1377 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001378 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001379 goto out;
1380 }
1381
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001382 tx_pool = &adapter->tx_pool[queue_num];
1383 tx_scrq = adapter->tx_scrq[queue_num];
1384 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1385 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1386 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1387
Thomas Falcon032c5e82015-12-21 11:26:06 -06001388 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001389
1390 if (skb_is_gso(skb)) {
1391 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1392 dst = tx_pool->tso_ltb.buff + offset;
1393 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1394 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1395 tx_pool->tso_index++;
1396 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1397 tx_pool->tso_index = 0;
1398 } else {
1399 offset = index * adapter->req_mtu;
1400 dst = tx_pool->long_term_buff.buff + offset;
1401 memset(dst, 0, adapter->req_mtu);
1402 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1403 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001404
Thomas Falcon15482052017-10-17 12:36:54 -05001405 if (skb_shinfo(skb)->nr_frags) {
1406 int cur, i;
1407
1408 /* Copy the head */
1409 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1410 cur = skb_headlen(skb);
1411
1412 /* Copy the frags */
1413 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1414 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1415
1416 memcpy(dst + cur,
1417 page_address(skb_frag_page(frag)) +
1418 frag->page_offset, skb_frag_size(frag));
1419 cur += skb_frag_size(frag);
1420 }
1421 } else {
1422 skb_copy_from_linear_data(skb, dst, skb->len);
1423 }
1424
Thomas Falcon032c5e82015-12-21 11:26:06 -06001425 tx_pool->consumer_index =
1426 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001427 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001428
1429 tx_buff = &tx_pool->tx_buff[index];
1430 tx_buff->skb = skb;
1431 tx_buff->data_dma[0] = data_dma_addr;
1432 tx_buff->data_len[0] = skb->len;
1433 tx_buff->index = index;
1434 tx_buff->pool_index = queue_num;
1435 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001436
1437 memset(&tx_crq, 0, sizeof(tx_crq));
1438 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1439 tx_crq.v1.type = IBMVNIC_TX_DESC;
1440 tx_crq.v1.n_crq_elem = 1;
1441 tx_crq.v1.n_sge = 1;
1442 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1443 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001444 if (skb_is_gso(skb))
1445 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1446 else
1447 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001448 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1449 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1450
1451 if (adapter->vlan_header_insertion) {
1452 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1453 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1454 }
1455
1456 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001457 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1458 proto = ip_hdr(skb)->protocol;
1459 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1460 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1461 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001462 }
1463
Thomas Falcona0dca102018-01-18 19:29:48 -06001464 if (proto == IPPROTO_TCP)
1465 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1466 else if (proto == IPPROTO_UDP)
1467 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1468
Thomas Falconad7775d2016-04-01 17:20:34 -05001469 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001470 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001471 hdrs += 2;
1472 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001473 if (skb_is_gso(skb)) {
1474 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1475 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1476 hdrs += 2;
1477 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001478 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001479 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001480 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1481 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconecba6162018-02-26 18:10:55 -06001482 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001483 tx_buff->indir_arr[0] = tx_crq;
1484 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1485 sizeof(tx_buff->indir_arr),
1486 DMA_TO_DEVICE);
1487 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001488 dev_kfree_skb_any(skb);
1489 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001490 if (!firmware_has_feature(FW_FEATURE_CMO))
1491 dev_err(dev, "tx: unable to map descriptor array\n");
1492 tx_map_failed++;
1493 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001494 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001495 goto out;
1496 }
John Allen498cd8e2016-04-06 11:49:55 -05001497 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001498 (u64)tx_buff->indir_dma,
1499 (u64)num_entries);
1500 } else {
Thomas Falconecba6162018-02-26 18:10:55 -06001501 tx_buff->num_entries = num_entries;
John Allen498cd8e2016-04-06 11:49:55 -05001502 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1503 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001504 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001505 if (lpar_rc != H_SUCCESS) {
1506 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1507
1508 if (tx_pool->consumer_index == 0)
1509 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001510 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001511 else
1512 tx_pool->consumer_index--;
1513
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001514 dev_kfree_skb_any(skb);
1515 tx_buff->skb = NULL;
1516
Thomas Falconb8c80b82017-05-26 10:30:42 -04001517 if (lpar_rc == H_CLOSED) {
1518 /* Disable TX and report carrier off if queue is closed.
1519 * Firmware guarantees that a signal will be sent to the
1520 * driver, triggering a reset or some other action.
1521 */
1522 netif_tx_stop_all_queues(netdev);
1523 netif_carrier_off(netdev);
1524 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001525
Thomas Falcon032c5e82015-12-21 11:26:06 -06001526 tx_send_failed++;
1527 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001528 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001529 goto out;
1530 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001531
Thomas Falconffc385b2018-02-18 10:08:41 -06001532 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001533 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon0aecb132018-02-26 18:10:58 -06001534 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001535 netif_stop_subqueue(netdev, queue_num);
1536 }
1537
Thomas Falcon032c5e82015-12-21 11:26:06 -06001538 tx_packets++;
1539 tx_bytes += skb->len;
1540 txq->trans_start = jiffies;
1541 ret = NETDEV_TX_OK;
1542
1543out:
1544 netdev->stats.tx_dropped += tx_dropped;
1545 netdev->stats.tx_bytes += tx_bytes;
1546 netdev->stats.tx_packets += tx_packets;
1547 adapter->tx_send_failed += tx_send_failed;
1548 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001549 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1550 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1551 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001552
1553 return ret;
1554}
1555
1556static void ibmvnic_set_multi(struct net_device *netdev)
1557{
1558 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1559 struct netdev_hw_addr *ha;
1560 union ibmvnic_crq crq;
1561
1562 memset(&crq, 0, sizeof(crq));
1563 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1564 crq.request_capability.cmd = REQUEST_CAPABILITY;
1565
1566 if (netdev->flags & IFF_PROMISC) {
1567 if (!adapter->promisc_supported)
1568 return;
1569 } else {
1570 if (netdev->flags & IFF_ALLMULTI) {
1571 /* Accept all multicast */
1572 memset(&crq, 0, sizeof(crq));
1573 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1574 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1575 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1576 ibmvnic_send_crq(adapter, &crq);
1577 } else if (netdev_mc_empty(netdev)) {
1578 /* Reject all multicast */
1579 memset(&crq, 0, sizeof(crq));
1580 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1581 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1582 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1583 ibmvnic_send_crq(adapter, &crq);
1584 } else {
1585 /* Accept one or more multicast(s) */
1586 netdev_for_each_mc_addr(ha, netdev) {
1587 memset(&crq, 0, sizeof(crq));
1588 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1589 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1590 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1591 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1592 ha->addr);
1593 ibmvnic_send_crq(adapter, &crq);
1594 }
1595 }
1596 }
1597}
1598
John Allenc26eba02017-10-26 16:23:25 -05001599static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001600{
1601 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1602 struct sockaddr *addr = p;
1603 union ibmvnic_crq crq;
1604
1605 if (!is_valid_ether_addr(addr->sa_data))
1606 return -EADDRNOTAVAIL;
1607
1608 memset(&crq, 0, sizeof(crq));
1609 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1610 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1611 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001612
1613 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001615 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001617 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001618}
1619
John Allenc26eba02017-10-26 16:23:25 -05001620static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1621{
1622 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1623 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001624 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001625
Thomas Falcon3d166132018-01-10 19:39:52 -06001626 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001627 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1628 adapter->mac_change_pending = true;
1629 return 0;
1630 }
1631
Thomas Falconf8136142018-01-29 13:45:05 -06001632 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001633
Thomas Falconf8136142018-01-29 13:45:05 -06001634 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001635}
1636
Nathan Fontenoted651a12017-05-03 14:04:38 -04001637/**
1638 * do_reset returns zero if we are able to keep processing reset events, or
1639 * non-zero if we hit a fatal error and must halt.
1640 */
1641static int do_reset(struct ibmvnic_adapter *adapter,
1642 struct ibmvnic_rwi *rwi, u32 reset_state)
1643{
John Allen896d8692018-01-18 16:26:31 -06001644 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001645 struct net_device *netdev = adapter->netdev;
1646 int i, rc;
1647
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001648 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1649 rwi->reset_reason);
1650
Nathan Fontenoted651a12017-05-03 14:04:38 -04001651 netif_carrier_off(netdev);
1652 adapter->reset_reason = rwi->reset_reason;
1653
John Allen896d8692018-01-18 16:26:31 -06001654 old_num_rx_queues = adapter->req_rx_queues;
1655 old_num_tx_queues = adapter->req_tx_queues;
1656
Nathan Fontenoted651a12017-05-03 14:04:38 -04001657 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1658 rc = ibmvnic_reenable_crq_queue(adapter);
1659 if (rc)
1660 return 0;
1661 }
1662
1663 rc = __ibmvnic_close(netdev);
1664 if (rc)
1665 return rc;
1666
John Allenc26eba02017-10-26 16:23:25 -05001667 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1668 adapter->wait_for_reset) {
1669 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001670 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001671 release_crq_queue(adapter);
1672 }
1673
John Allen8cb31cf2017-05-26 10:30:37 -04001674 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1675 /* remove the closed state so when we call open it appears
1676 * we are coming from the probed state.
1677 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001678 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001679
John Allen8cb31cf2017-05-26 10:30:37 -04001680 rc = ibmvnic_init(adapter);
1681 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001682 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001683
1684 /* If the adapter was in PROBE state prior to the reset,
1685 * exit here.
1686 */
1687 if (reset_state == VNIC_PROBED)
1688 return 0;
1689
1690 rc = ibmvnic_login(netdev);
1691 if (rc) {
1692 adapter->state = VNIC_PROBED;
1693 return 0;
1694 }
1695
John Allenc26eba02017-10-26 16:23:25 -05001696 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1697 adapter->wait_for_reset) {
1698 rc = init_resources(adapter);
1699 if (rc)
1700 return rc;
John Allen896d8692018-01-18 16:26:31 -06001701 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1702 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001703 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001704 release_rx_pools(adapter);
1705 release_tx_pools(adapter);
1706 init_rx_pools(netdev);
1707 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001708
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001709 release_napi(adapter);
1710 init_napi(adapter);
John Allenc26eba02017-10-26 16:23:25 -05001711 } else {
1712 rc = reset_tx_pools(adapter);
1713 if (rc)
1714 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001715
John Allenc26eba02017-10-26 16:23:25 -05001716 rc = reset_rx_pools(adapter);
1717 if (rc)
1718 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001719
John Allenc26eba02017-10-26 16:23:25 -05001720 if (reset_state == VNIC_CLOSED)
1721 return 0;
1722 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001723 }
1724
Nathan Fontenoted651a12017-05-03 14:04:38 -04001725 rc = __ibmvnic_open(netdev);
1726 if (rc) {
1727 if (list_empty(&adapter->rwi_list))
1728 adapter->state = VNIC_CLOSED;
1729 else
1730 adapter->state = reset_state;
1731
1732 return 0;
1733 }
1734
Nathan Fontenoted651a12017-05-03 14:04:38 -04001735 /* kick napi */
1736 for (i = 0; i < adapter->req_rx_queues; i++)
1737 napi_schedule(&adapter->napi[i]);
1738
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001739 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1740 netdev_notify_peers(netdev);
1741
Thomas Falconcc85c022018-02-13 15:32:50 -06001742 netif_carrier_on(netdev);
1743
Nathan Fontenoted651a12017-05-03 14:04:38 -04001744 return 0;
1745}
1746
1747static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1748{
1749 struct ibmvnic_rwi *rwi;
1750
1751 mutex_lock(&adapter->rwi_lock);
1752
1753 if (!list_empty(&adapter->rwi_list)) {
1754 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1755 list);
1756 list_del(&rwi->list);
1757 } else {
1758 rwi = NULL;
1759 }
1760
1761 mutex_unlock(&adapter->rwi_lock);
1762 return rwi;
1763}
1764
1765static void free_all_rwi(struct ibmvnic_adapter *adapter)
1766{
1767 struct ibmvnic_rwi *rwi;
1768
1769 rwi = get_next_rwi(adapter);
1770 while (rwi) {
1771 kfree(rwi);
1772 rwi = get_next_rwi(adapter);
1773 }
1774}
1775
1776static void __ibmvnic_reset(struct work_struct *work)
1777{
1778 struct ibmvnic_rwi *rwi;
1779 struct ibmvnic_adapter *adapter;
1780 struct net_device *netdev;
1781 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001782 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001783
1784 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1785 netdev = adapter->netdev;
1786
1787 mutex_lock(&adapter->reset_lock);
1788 adapter->resetting = true;
1789 reset_state = adapter->state;
1790
1791 rwi = get_next_rwi(adapter);
1792 while (rwi) {
1793 rc = do_reset(adapter, rwi, reset_state);
1794 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001795 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001796 break;
1797
1798 rwi = get_next_rwi(adapter);
1799 }
1800
John Allenc26eba02017-10-26 16:23:25 -05001801 if (adapter->wait_for_reset) {
1802 adapter->wait_for_reset = false;
1803 adapter->reset_done_rc = rc;
1804 complete(&adapter->reset_done);
1805 }
1806
Nathan Fontenoted651a12017-05-03 14:04:38 -04001807 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001808 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001809 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001810 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001811 return;
1812 }
1813
1814 adapter->resetting = false;
1815 mutex_unlock(&adapter->reset_lock);
1816}
1817
1818static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1819 enum ibmvnic_reset_reason reason)
1820{
1821 struct ibmvnic_rwi *rwi, *tmp;
1822 struct net_device *netdev = adapter->netdev;
1823 struct list_head *entry;
1824
1825 if (adapter->state == VNIC_REMOVING ||
1826 adapter->state == VNIC_REMOVED) {
1827 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1828 return;
1829 }
1830
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001831 if (adapter->state == VNIC_PROBING) {
1832 netdev_warn(netdev, "Adapter reset during probe\n");
1833 adapter->init_done_rc = EAGAIN;
1834 return;
1835 }
1836
Nathan Fontenoted651a12017-05-03 14:04:38 -04001837 mutex_lock(&adapter->rwi_lock);
1838
1839 list_for_each(entry, &adapter->rwi_list) {
1840 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1841 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001842 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001843 mutex_unlock(&adapter->rwi_lock);
1844 return;
1845 }
1846 }
1847
1848 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1849 if (!rwi) {
1850 mutex_unlock(&adapter->rwi_lock);
1851 ibmvnic_close(netdev);
1852 return;
1853 }
1854
1855 rwi->reset_reason = reason;
1856 list_add_tail(&rwi->list, &adapter->rwi_list);
1857 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001858
1859 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001860 schedule_work(&adapter->ibmvnic_reset);
1861}
1862
Thomas Falcon032c5e82015-12-21 11:26:06 -06001863static void ibmvnic_tx_timeout(struct net_device *dev)
1864{
1865 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001866
Nathan Fontenoted651a12017-05-03 14:04:38 -04001867 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001868}
1869
1870static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1871 struct ibmvnic_rx_buff *rx_buff)
1872{
1873 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1874
1875 rx_buff->skb = NULL;
1876
1877 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1878 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1879
1880 atomic_dec(&pool->available);
1881}
1882
1883static int ibmvnic_poll(struct napi_struct *napi, int budget)
1884{
1885 struct net_device *netdev = napi->dev;
1886 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1887 int scrq_num = (int)(napi - adapter->napi);
1888 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001889
Thomas Falcon032c5e82015-12-21 11:26:06 -06001890restart_poll:
1891 while (frames_processed < budget) {
1892 struct sk_buff *skb;
1893 struct ibmvnic_rx_buff *rx_buff;
1894 union sub_crq *next;
1895 u32 length;
1896 u16 offset;
1897 u8 flags = 0;
1898
John Allen34686562018-02-06 16:21:49 -06001899 if (unlikely(adapter->resetting &&
1900 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001901 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1902 napi_complete_done(napi, frames_processed);
1903 return frames_processed;
1904 }
1905
Thomas Falcon032c5e82015-12-21 11:26:06 -06001906 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1907 break;
1908 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1909 rx_buff =
1910 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1911 rx_comp.correlator);
1912 /* do error checking */
1913 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001914 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1915 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001916 /* free the entry */
1917 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001918 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001920 continue;
Thomas Falconabe27a82018-02-19 20:12:57 -06001921 } else if (!rx_buff->skb) {
1922 /* free the entry */
1923 next->rx_comp.first = 0;
1924 remove_buff_from_pool(adapter, rx_buff);
1925 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001926 }
1927
1928 length = be32_to_cpu(next->rx_comp.len);
1929 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1930 flags = next->rx_comp.flags;
1931 skb = rx_buff->skb;
1932 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1933 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001934
1935 /* VLAN Header has been stripped by the system firmware and
1936 * needs to be inserted by the driver
1937 */
1938 if (adapter->rx_vlan_header_insertion &&
1939 (flags & IBMVNIC_VLAN_STRIPPED))
1940 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1941 ntohs(next->rx_comp.vlan_tci));
1942
Thomas Falcon032c5e82015-12-21 11:26:06 -06001943 /* free the entry */
1944 next->rx_comp.first = 0;
1945 remove_buff_from_pool(adapter, rx_buff);
1946
1947 skb_put(skb, length);
1948 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001949 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001950
1951 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1952 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1953 skb->ip_summed = CHECKSUM_UNNECESSARY;
1954 }
1955
1956 length = skb->len;
1957 napi_gro_receive(napi, skb); /* send it up */
1958 netdev->stats.rx_packets++;
1959 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001960 adapter->rx_stats_buffers[scrq_num].packets++;
1961 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001962 frames_processed++;
1963 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001964
1965 if (adapter->state != VNIC_CLOSING)
1966 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001967
1968 if (frames_processed < budget) {
1969 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001970 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001971 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1972 napi_reschedule(napi)) {
1973 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1974 goto restart_poll;
1975 }
1976 }
1977 return frames_processed;
1978}
1979
1980#ifdef CONFIG_NET_POLL_CONTROLLER
1981static void ibmvnic_netpoll_controller(struct net_device *dev)
1982{
1983 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1984 int i;
1985
1986 replenish_pools(netdev_priv(dev));
1987 for (i = 0; i < adapter->req_rx_queues; i++)
1988 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1989 adapter->rx_scrq[i]);
1990}
1991#endif
1992
John Allenc26eba02017-10-26 16:23:25 -05001993static int wait_for_reset(struct ibmvnic_adapter *adapter)
1994{
1995 adapter->fallback.mtu = adapter->req_mtu;
1996 adapter->fallback.rx_queues = adapter->req_rx_queues;
1997 adapter->fallback.tx_queues = adapter->req_tx_queues;
1998 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1999 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2000
2001 init_completion(&adapter->reset_done);
2002 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2003 adapter->wait_for_reset = true;
2004 wait_for_completion(&adapter->reset_done);
2005
2006 if (adapter->reset_done_rc) {
2007 adapter->desired.mtu = adapter->fallback.mtu;
2008 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2009 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2010 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2011 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2012
2013 init_completion(&adapter->reset_done);
2014 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2015 wait_for_completion(&adapter->reset_done);
2016 }
2017 adapter->wait_for_reset = false;
2018
2019 return adapter->reset_done_rc;
2020}
2021
John Allen3a807b72017-06-06 16:55:52 -05002022static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2023{
John Allenc26eba02017-10-26 16:23:25 -05002024 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2025
2026 adapter->desired.mtu = new_mtu + ETH_HLEN;
2027
2028 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002029}
2030
Thomas Falcon032c5e82015-12-21 11:26:06 -06002031static const struct net_device_ops ibmvnic_netdev_ops = {
2032 .ndo_open = ibmvnic_open,
2033 .ndo_stop = ibmvnic_close,
2034 .ndo_start_xmit = ibmvnic_xmit,
2035 .ndo_set_rx_mode = ibmvnic_set_multi,
2036 .ndo_set_mac_address = ibmvnic_set_mac,
2037 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002038 .ndo_tx_timeout = ibmvnic_tx_timeout,
2039#ifdef CONFIG_NET_POLL_CONTROLLER
2040 .ndo_poll_controller = ibmvnic_netpoll_controller,
2041#endif
John Allen3a807b72017-06-06 16:55:52 -05002042 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002043};
2044
2045/* ethtool functions */
2046
Philippe Reynes8a433792017-01-07 22:37:29 +01002047static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2048 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002049{
Philippe Reynes8a433792017-01-07 22:37:29 +01002050 u32 supported, advertising;
2051
2052 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002053 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002054 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002056 cmd->base.speed = SPEED_1000;
2057 cmd->base.duplex = DUPLEX_FULL;
2058 cmd->base.port = PORT_FIBRE;
2059 cmd->base.phy_address = 0;
2060 cmd->base.autoneg = AUTONEG_ENABLE;
2061
2062 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2063 supported);
2064 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2065 advertising);
2066
Thomas Falcon032c5e82015-12-21 11:26:06 -06002067 return 0;
2068}
2069
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002070static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002071 struct ethtool_drvinfo *info)
2072{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002073 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2074
Thomas Falcon032c5e82015-12-21 11:26:06 -06002075 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2076 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002077 strlcpy(info->fw_version, adapter->fw_version,
2078 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002079}
2080
2081static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2082{
2083 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2084
2085 return adapter->msg_enable;
2086}
2087
2088static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2089{
2090 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2091
2092 adapter->msg_enable = data;
2093}
2094
2095static u32 ibmvnic_get_link(struct net_device *netdev)
2096{
2097 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2098
2099 /* Don't need to send a query because we request a logical link up at
2100 * init and then we wait for link state indications
2101 */
2102 return adapter->logical_link_state;
2103}
2104
2105static void ibmvnic_get_ringparam(struct net_device *netdev,
2106 struct ethtool_ringparam *ring)
2107{
John Allenbc131b32017-08-02 16:46:30 -05002108 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2109
2110 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2111 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002112 ring->rx_mini_max_pending = 0;
2113 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002114 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2115 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002116 ring->rx_mini_pending = 0;
2117 ring->rx_jumbo_pending = 0;
2118}
2119
John Allenc26eba02017-10-26 16:23:25 -05002120static int ibmvnic_set_ringparam(struct net_device *netdev,
2121 struct ethtool_ringparam *ring)
2122{
2123 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2124
2125 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2126 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2127 netdev_err(netdev, "Invalid request.\n");
2128 netdev_err(netdev, "Max tx buffers = %llu\n",
2129 adapter->max_rx_add_entries_per_subcrq);
2130 netdev_err(netdev, "Max rx buffers = %llu\n",
2131 adapter->max_tx_entries_per_subcrq);
2132 return -EINVAL;
2133 }
2134
2135 adapter->desired.rx_entries = ring->rx_pending;
2136 adapter->desired.tx_entries = ring->tx_pending;
2137
2138 return wait_for_reset(adapter);
2139}
2140
John Allenc2dbeb62017-08-02 16:47:17 -05002141static void ibmvnic_get_channels(struct net_device *netdev,
2142 struct ethtool_channels *channels)
2143{
2144 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2145
2146 channels->max_rx = adapter->max_rx_queues;
2147 channels->max_tx = adapter->max_tx_queues;
2148 channels->max_other = 0;
2149 channels->max_combined = 0;
2150 channels->rx_count = adapter->req_rx_queues;
2151 channels->tx_count = adapter->req_tx_queues;
2152 channels->other_count = 0;
2153 channels->combined_count = 0;
2154}
2155
John Allenc26eba02017-10-26 16:23:25 -05002156static int ibmvnic_set_channels(struct net_device *netdev,
2157 struct ethtool_channels *channels)
2158{
2159 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2160
2161 adapter->desired.rx_queues = channels->rx_count;
2162 adapter->desired.tx_queues = channels->tx_count;
2163
2164 return wait_for_reset(adapter);
2165}
2166
Thomas Falcon032c5e82015-12-21 11:26:06 -06002167static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2168{
John Allen3d52b592017-08-02 16:44:14 -05002169 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002170 int i;
2171
2172 if (stringset != ETH_SS_STATS)
2173 return;
2174
2175 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2176 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002177
2178 for (i = 0; i < adapter->req_tx_queues; i++) {
2179 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2180 data += ETH_GSTRING_LEN;
2181
2182 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2183 data += ETH_GSTRING_LEN;
2184
2185 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2186 data += ETH_GSTRING_LEN;
2187 }
2188
2189 for (i = 0; i < adapter->req_rx_queues; i++) {
2190 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2191 data += ETH_GSTRING_LEN;
2192
2193 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2194 data += ETH_GSTRING_LEN;
2195
2196 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2197 data += ETH_GSTRING_LEN;
2198 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002199}
2200
2201static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2202{
John Allen3d52b592017-08-02 16:44:14 -05002203 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2204
Thomas Falcon032c5e82015-12-21 11:26:06 -06002205 switch (sset) {
2206 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002207 return ARRAY_SIZE(ibmvnic_stats) +
2208 adapter->req_tx_queues * NUM_TX_STATS +
2209 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002210 default:
2211 return -EOPNOTSUPP;
2212 }
2213}
2214
2215static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2216 struct ethtool_stats *stats, u64 *data)
2217{
2218 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2219 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002220 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002221
2222 memset(&crq, 0, sizeof(crq));
2223 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2224 crq.request_statistics.cmd = REQUEST_STATISTICS;
2225 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2226 crq.request_statistics.len =
2227 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002228
2229 /* Wait for data to be written */
2230 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002231 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002232 wait_for_completion(&adapter->stats_done);
2233
2234 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002235 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2236 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002237
2238 for (j = 0; j < adapter->req_tx_queues; j++) {
2239 data[i] = adapter->tx_stats_buffers[j].packets;
2240 i++;
2241 data[i] = adapter->tx_stats_buffers[j].bytes;
2242 i++;
2243 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2244 i++;
2245 }
2246
2247 for (j = 0; j < adapter->req_rx_queues; j++) {
2248 data[i] = adapter->rx_stats_buffers[j].packets;
2249 i++;
2250 data[i] = adapter->rx_stats_buffers[j].bytes;
2251 i++;
2252 data[i] = adapter->rx_stats_buffers[j].interrupts;
2253 i++;
2254 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002255}
2256
2257static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002258 .get_drvinfo = ibmvnic_get_drvinfo,
2259 .get_msglevel = ibmvnic_get_msglevel,
2260 .set_msglevel = ibmvnic_set_msglevel,
2261 .get_link = ibmvnic_get_link,
2262 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002263 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002264 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002265 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002266 .get_strings = ibmvnic_get_strings,
2267 .get_sset_count = ibmvnic_get_sset_count,
2268 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002269 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002270};
2271
2272/* Routines for managing CRQs/sCRQs */
2273
Nathan Fontenot57a49432017-05-26 10:31:12 -04002274static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2275 struct ibmvnic_sub_crq_queue *scrq)
2276{
2277 int rc;
2278
2279 if (scrq->irq) {
2280 free_irq(scrq->irq, scrq);
2281 irq_dispose_mapping(scrq->irq);
2282 scrq->irq = 0;
2283 }
2284
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002285 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002286 scrq->cur = 0;
2287
2288 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2289 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2290 return rc;
2291}
2292
2293static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2294{
2295 int i, rc;
2296
2297 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002298 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002299 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2300 if (rc)
2301 return rc;
2302 }
2303
2304 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002305 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002306 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2307 if (rc)
2308 return rc;
2309 }
2310
Nathan Fontenot57a49432017-05-26 10:31:12 -04002311 return rc;
2312}
2313
Thomas Falcon032c5e82015-12-21 11:26:06 -06002314static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002315 struct ibmvnic_sub_crq_queue *scrq,
2316 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002317{
2318 struct device *dev = &adapter->vdev->dev;
2319 long rc;
2320
2321 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2322
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002323 if (do_h_free) {
2324 /* Close the sub-crqs */
2325 do {
2326 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2327 adapter->vdev->unit_address,
2328 scrq->crq_num);
2329 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002330
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002331 if (rc) {
2332 netdev_err(adapter->netdev,
2333 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2334 scrq->crq_num, rc);
2335 }
Thomas Falconffa73852017-04-19 13:44:29 -04002336 }
2337
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2339 DMA_BIDIRECTIONAL);
2340 free_pages((unsigned long)scrq->msgs, 2);
2341 kfree(scrq);
2342}
2343
2344static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2345 *adapter)
2346{
2347 struct device *dev = &adapter->vdev->dev;
2348 struct ibmvnic_sub_crq_queue *scrq;
2349 int rc;
2350
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002351 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002352 if (!scrq)
2353 return NULL;
2354
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002355 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002356 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002357 if (!scrq->msgs) {
2358 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2359 goto zero_page_failed;
2360 }
2361
2362 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2363 DMA_BIDIRECTIONAL);
2364 if (dma_mapping_error(dev, scrq->msg_token)) {
2365 dev_warn(dev, "Couldn't map crq queue messages page\n");
2366 goto map_failed;
2367 }
2368
2369 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2370 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2371
2372 if (rc == H_RESOURCE)
2373 rc = ibmvnic_reset_crq(adapter);
2374
2375 if (rc == H_CLOSED) {
2376 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2377 } else if (rc) {
2378 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2379 goto reg_failed;
2380 }
2381
Thomas Falcon032c5e82015-12-21 11:26:06 -06002382 scrq->adapter = adapter;
2383 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002384 spin_lock_init(&scrq->lock);
2385
2386 netdev_dbg(adapter->netdev,
2387 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2388 scrq->crq_num, scrq->hw_irq, scrq->irq);
2389
2390 return scrq;
2391
Thomas Falcon032c5e82015-12-21 11:26:06 -06002392reg_failed:
2393 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2394 DMA_BIDIRECTIONAL);
2395map_failed:
2396 free_pages((unsigned long)scrq->msgs, 2);
2397zero_page_failed:
2398 kfree(scrq);
2399
2400 return NULL;
2401}
2402
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002403static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002404{
2405 int i;
2406
2407 if (adapter->tx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002408 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002409 if (!adapter->tx_scrq[i])
2410 continue;
2411
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002412 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2413 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002414 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002415 free_irq(adapter->tx_scrq[i]->irq,
2416 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002417 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002418 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002419 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002420
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002421 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2422 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002423 }
2424
Nathan Fontenot9501df32017-03-15 23:38:07 -04002425 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002426 adapter->tx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002427 adapter->num_active_tx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002428 }
2429
2430 if (adapter->rx_scrq) {
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002431 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002432 if (!adapter->rx_scrq[i])
2433 continue;
2434
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002435 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2436 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002437 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002438 free_irq(adapter->rx_scrq[i]->irq,
2439 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002440 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002441 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002442 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002443
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002444 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2445 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002446 }
2447
Nathan Fontenot9501df32017-03-15 23:38:07 -04002448 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002449 adapter->rx_scrq = NULL;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002450 adapter->num_active_rx_scrqs = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002451 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002452}
2453
2454static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2455 struct ibmvnic_sub_crq_queue *scrq)
2456{
2457 struct device *dev = &adapter->vdev->dev;
2458 unsigned long rc;
2459
2460 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2461 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2462 if (rc)
2463 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2464 scrq->hw_irq, rc);
2465 return rc;
2466}
2467
2468static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2469 struct ibmvnic_sub_crq_queue *scrq)
2470{
2471 struct device *dev = &adapter->vdev->dev;
2472 unsigned long rc;
2473
2474 if (scrq->hw_irq > 0x100000000ULL) {
2475 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2476 return 1;
2477 }
2478
2479 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2480 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2481 if (rc)
2482 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2483 scrq->hw_irq, rc);
2484 return rc;
2485}
2486
2487static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2488 struct ibmvnic_sub_crq_queue *scrq)
2489{
2490 struct device *dev = &adapter->vdev->dev;
2491 struct ibmvnic_tx_buff *txbuff;
2492 union sub_crq *next;
2493 int index;
2494 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002495 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002496
2497restart_loop:
2498 while (pending_scrq(adapter, scrq)) {
2499 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002500 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002501
2502 next = ibmvnic_next_scrq(adapter, scrq);
2503 for (i = 0; i < next->tx_comp.num_comps; i++) {
2504 if (next->tx_comp.rcs[i]) {
2505 dev_err(dev, "tx error %x\n",
2506 next->tx_comp.rcs[i]);
2507 continue;
2508 }
2509 index = be32_to_cpu(next->tx_comp.correlators[i]);
2510 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2511
2512 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2513 if (!txbuff->data_dma[j])
2514 continue;
2515
2516 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002517 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002518 /* if sub_crq was sent indirectly */
2519 first = txbuff->indir_arr[0].generic.first;
2520 if (first == IBMVNIC_CRQ_CMD) {
2521 dma_unmap_single(dev, txbuff->indir_dma,
2522 sizeof(txbuff->indir_arr),
2523 DMA_TO_DEVICE);
2524 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002525
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002526 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002528 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002529 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530
Thomas Falconffc385b2018-02-18 10:08:41 -06002531 num_entries += txbuff->num_entries;
2532
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2534 producer_index] = index;
2535 adapter->tx_pool[pool].producer_index =
2536 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002537 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002538 }
2539 /* remove tx_comp scrq*/
2540 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002541
Thomas Falconffc385b2018-02-18 10:08:41 -06002542 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002543 (adapter->req_tx_entries_per_subcrq / 2) &&
2544 __netif_subqueue_stopped(adapter->netdev,
2545 scrq->pool_index)) {
2546 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
Thomas Falcon0aecb132018-02-26 18:10:58 -06002547 netdev_dbg(adapter->netdev, "Started queue %d\n",
2548 scrq->pool_index);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002549 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002550 }
2551
2552 enable_scrq_irq(adapter, scrq);
2553
2554 if (pending_scrq(adapter, scrq)) {
2555 disable_scrq_irq(adapter, scrq);
2556 goto restart_loop;
2557 }
2558
2559 return 0;
2560}
2561
2562static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2563{
2564 struct ibmvnic_sub_crq_queue *scrq = instance;
2565 struct ibmvnic_adapter *adapter = scrq->adapter;
2566
2567 disable_scrq_irq(adapter, scrq);
2568 ibmvnic_complete_tx(adapter, scrq);
2569
2570 return IRQ_HANDLED;
2571}
2572
2573static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2574{
2575 struct ibmvnic_sub_crq_queue *scrq = instance;
2576 struct ibmvnic_adapter *adapter = scrq->adapter;
2577
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002578 /* When booting a kdump kernel we can hit pending interrupts
2579 * prior to completing driver initialization.
2580 */
2581 if (unlikely(adapter->state != VNIC_OPEN))
2582 return IRQ_NONE;
2583
John Allen3d52b592017-08-02 16:44:14 -05002584 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2585
Thomas Falcon032c5e82015-12-21 11:26:06 -06002586 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2587 disable_scrq_irq(adapter, scrq);
2588 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2589 }
2590
2591 return IRQ_HANDLED;
2592}
2593
Thomas Falconea22d512016-07-06 15:35:17 -05002594static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2595{
2596 struct device *dev = &adapter->vdev->dev;
2597 struct ibmvnic_sub_crq_queue *scrq;
2598 int i = 0, j = 0;
2599 int rc = 0;
2600
2601 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002602 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2603 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002604 scrq = adapter->tx_scrq[i];
2605 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2606
Michael Ellerman99c17902016-09-10 19:59:05 +10002607 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002608 rc = -EINVAL;
2609 dev_err(dev, "Error mapping irq\n");
2610 goto req_tx_irq_failed;
2611 }
2612
2613 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2614 0, "ibmvnic_tx", scrq);
2615
2616 if (rc) {
2617 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2618 scrq->irq, rc);
2619 irq_dispose_mapping(scrq->irq);
Nathan Fontenotaf9090c2018-02-20 11:04:18 -06002620 goto req_tx_irq_failed;
Thomas Falconea22d512016-07-06 15:35:17 -05002621 }
2622 }
2623
2624 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002625 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2626 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002627 scrq = adapter->rx_scrq[i];
2628 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002629 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002630 rc = -EINVAL;
2631 dev_err(dev, "Error mapping irq\n");
2632 goto req_rx_irq_failed;
2633 }
2634 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2635 0, "ibmvnic_rx", scrq);
2636 if (rc) {
2637 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2638 scrq->irq, rc);
2639 irq_dispose_mapping(scrq->irq);
2640 goto req_rx_irq_failed;
2641 }
2642 }
2643 return rc;
2644
2645req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002646 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002647 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2648 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002649 }
Thomas Falconea22d512016-07-06 15:35:17 -05002650 i = adapter->req_tx_queues;
2651req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002652 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002653 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2654 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002655 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002656 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002657 return rc;
2658}
2659
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002660static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002661{
2662 struct device *dev = &adapter->vdev->dev;
2663 struct ibmvnic_sub_crq_queue **allqueues;
2664 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002665 int total_queues;
2666 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002667 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668
Thomas Falcon032c5e82015-12-21 11:26:06 -06002669 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2670
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002671 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002672 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002673 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002674
2675 for (i = 0; i < total_queues; i++) {
2676 allqueues[i] = init_sub_crq_queue(adapter);
2677 if (!allqueues[i]) {
2678 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2679 break;
2680 }
2681 registered_queues++;
2682 }
2683
2684 /* Make sure we were able to register the minimum number of queues */
2685 if (registered_queues <
2686 adapter->min_tx_queues + adapter->min_rx_queues) {
2687 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2688 goto tx_failed;
2689 }
2690
2691 /* Distribute the failed allocated queues*/
2692 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2693 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2694 switch (i % 3) {
2695 case 0:
2696 if (adapter->req_rx_queues > adapter->min_rx_queues)
2697 adapter->req_rx_queues--;
2698 else
2699 more++;
2700 break;
2701 case 1:
2702 if (adapter->req_tx_queues > adapter->min_tx_queues)
2703 adapter->req_tx_queues--;
2704 else
2705 more++;
2706 break;
2707 }
2708 }
2709
2710 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002711 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 if (!adapter->tx_scrq)
2713 goto tx_failed;
2714
2715 for (i = 0; i < adapter->req_tx_queues; i++) {
2716 adapter->tx_scrq[i] = allqueues[i];
2717 adapter->tx_scrq[i]->pool_index = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002718 adapter->num_active_tx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 }
2720
2721 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002722 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 if (!adapter->rx_scrq)
2724 goto rx_failed;
2725
2726 for (i = 0; i < adapter->req_rx_queues; i++) {
2727 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2728 adapter->rx_scrq[i]->scrq_num = i;
Nathan Fontenot82e3be32018-02-21 21:33:56 -06002729 adapter->num_active_rx_scrqs++;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 }
2731
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002732 kfree(allqueues);
2733 return 0;
2734
2735rx_failed:
2736 kfree(adapter->tx_scrq);
2737 adapter->tx_scrq = NULL;
2738tx_failed:
2739 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002740 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002741 kfree(allqueues);
2742 return -1;
2743}
2744
2745static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2746{
2747 struct device *dev = &adapter->vdev->dev;
2748 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002749 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002750
2751 if (!retry) {
2752 /* Sub-CRQ entries are 32 byte long */
2753 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2754
2755 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2756 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2757 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2758 return;
2759 }
2760
John Allenc26eba02017-10-26 16:23:25 -05002761 if (adapter->desired.mtu)
2762 adapter->req_mtu = adapter->desired.mtu;
2763 else
2764 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002765
John Allenc26eba02017-10-26 16:23:25 -05002766 if (!adapter->desired.tx_entries)
2767 adapter->desired.tx_entries =
2768 adapter->max_tx_entries_per_subcrq;
2769 if (!adapter->desired.rx_entries)
2770 adapter->desired.rx_entries =
2771 adapter->max_rx_add_entries_per_subcrq;
2772
2773 max_entries = IBMVNIC_MAX_LTB_SIZE /
2774 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2775
2776 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2777 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2778 adapter->desired.tx_entries = max_entries;
2779 }
2780
2781 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2782 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2783 adapter->desired.rx_entries = max_entries;
2784 }
2785
2786 if (adapter->desired.tx_entries)
2787 adapter->req_tx_entries_per_subcrq =
2788 adapter->desired.tx_entries;
2789 else
2790 adapter->req_tx_entries_per_subcrq =
2791 adapter->max_tx_entries_per_subcrq;
2792
2793 if (adapter->desired.rx_entries)
2794 adapter->req_rx_add_entries_per_subcrq =
2795 adapter->desired.rx_entries;
2796 else
2797 adapter->req_rx_add_entries_per_subcrq =
2798 adapter->max_rx_add_entries_per_subcrq;
2799
2800 if (adapter->desired.tx_queues)
2801 adapter->req_tx_queues =
2802 adapter->desired.tx_queues;
2803 else
2804 adapter->req_tx_queues =
2805 adapter->opt_tx_comp_sub_queues;
2806
2807 if (adapter->desired.rx_queues)
2808 adapter->req_rx_queues =
2809 adapter->desired.rx_queues;
2810 else
2811 adapter->req_rx_queues =
2812 adapter->opt_rx_comp_queues;
2813
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002814 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002815 }
2816
Thomas Falcon032c5e82015-12-21 11:26:06 -06002817 memset(&crq, 0, sizeof(crq));
2818 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2819 crq.request_capability.cmd = REQUEST_CAPABILITY;
2820
2821 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002822 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002823 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002824 ibmvnic_send_crq(adapter, &crq);
2825
2826 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002827 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002828 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002829 ibmvnic_send_crq(adapter, &crq);
2830
2831 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002832 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002833 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834 ibmvnic_send_crq(adapter, &crq);
2835
2836 crq.request_capability.capability =
2837 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2838 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002839 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002840 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841 ibmvnic_send_crq(adapter, &crq);
2842
2843 crq.request_capability.capability =
2844 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2845 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002846 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002847 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002848 ibmvnic_send_crq(adapter, &crq);
2849
2850 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002851 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002852 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853 ibmvnic_send_crq(adapter, &crq);
2854
2855 if (adapter->netdev->flags & IFF_PROMISC) {
2856 if (adapter->promisc_supported) {
2857 crq.request_capability.capability =
2858 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002859 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002860 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002861 ibmvnic_send_crq(adapter, &crq);
2862 }
2863 } else {
2864 crq.request_capability.capability =
2865 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002866 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002867 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002868 ibmvnic_send_crq(adapter, &crq);
2869 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002870}
2871
2872static int pending_scrq(struct ibmvnic_adapter *adapter,
2873 struct ibmvnic_sub_crq_queue *scrq)
2874{
2875 union sub_crq *entry = &scrq->msgs[scrq->cur];
2876
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002877 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002878 return 1;
2879 else
2880 return 0;
2881}
2882
2883static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2884 struct ibmvnic_sub_crq_queue *scrq)
2885{
2886 union sub_crq *entry;
2887 unsigned long flags;
2888
2889 spin_lock_irqsave(&scrq->lock, flags);
2890 entry = &scrq->msgs[scrq->cur];
2891 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2892 if (++scrq->cur == scrq->size)
2893 scrq->cur = 0;
2894 } else {
2895 entry = NULL;
2896 }
2897 spin_unlock_irqrestore(&scrq->lock, flags);
2898
2899 return entry;
2900}
2901
2902static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2903{
2904 struct ibmvnic_crq_queue *queue = &adapter->crq;
2905 union ibmvnic_crq *crq;
2906
2907 crq = &queue->msgs[queue->cur];
2908 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2909 if (++queue->cur == queue->size)
2910 queue->cur = 0;
2911 } else {
2912 crq = NULL;
2913 }
2914
2915 return crq;
2916}
2917
2918static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2919 union sub_crq *sub_crq)
2920{
2921 unsigned int ua = adapter->vdev->unit_address;
2922 struct device *dev = &adapter->vdev->dev;
2923 u64 *u64_crq = (u64 *)sub_crq;
2924 int rc;
2925
2926 netdev_dbg(adapter->netdev,
2927 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2928 (unsigned long int)cpu_to_be64(remote_handle),
2929 (unsigned long int)cpu_to_be64(u64_crq[0]),
2930 (unsigned long int)cpu_to_be64(u64_crq[1]),
2931 (unsigned long int)cpu_to_be64(u64_crq[2]),
2932 (unsigned long int)cpu_to_be64(u64_crq[3]));
2933
2934 /* Make sure the hypervisor sees the complete request */
2935 mb();
2936
2937 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2938 cpu_to_be64(remote_handle),
2939 cpu_to_be64(u64_crq[0]),
2940 cpu_to_be64(u64_crq[1]),
2941 cpu_to_be64(u64_crq[2]),
2942 cpu_to_be64(u64_crq[3]));
2943
2944 if (rc) {
2945 if (rc == H_CLOSED)
2946 dev_warn(dev, "CRQ Queue closed\n");
2947 dev_err(dev, "Send error (rc=%d)\n", rc);
2948 }
2949
2950 return rc;
2951}
2952
Thomas Falconad7775d2016-04-01 17:20:34 -05002953static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2954 u64 remote_handle, u64 ioba, u64 num_entries)
2955{
2956 unsigned int ua = adapter->vdev->unit_address;
2957 struct device *dev = &adapter->vdev->dev;
2958 int rc;
2959
2960 /* Make sure the hypervisor sees the complete request */
2961 mb();
2962 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2963 cpu_to_be64(remote_handle),
2964 ioba, num_entries);
2965
2966 if (rc) {
2967 if (rc == H_CLOSED)
2968 dev_warn(dev, "CRQ Queue closed\n");
2969 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2970 }
2971
2972 return rc;
2973}
2974
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2976 union ibmvnic_crq *crq)
2977{
2978 unsigned int ua = adapter->vdev->unit_address;
2979 struct device *dev = &adapter->vdev->dev;
2980 u64 *u64_crq = (u64 *)crq;
2981 int rc;
2982
2983 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2984 (unsigned long int)cpu_to_be64(u64_crq[0]),
2985 (unsigned long int)cpu_to_be64(u64_crq[1]));
2986
2987 /* Make sure the hypervisor sees the complete request */
2988 mb();
2989
2990 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2991 cpu_to_be64(u64_crq[0]),
2992 cpu_to_be64(u64_crq[1]));
2993
2994 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002995 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002996 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002997 if (adapter->resetting)
2998 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2999 }
3000
Thomas Falcon032c5e82015-12-21 11:26:06 -06003001 dev_warn(dev, "Send error (rc=%d)\n", rc);
3002 }
3003
3004 return rc;
3005}
3006
3007static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3008{
3009 union ibmvnic_crq crq;
3010
3011 memset(&crq, 0, sizeof(crq));
3012 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3013 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3014 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3015
3016 return ibmvnic_send_crq(adapter, &crq);
3017}
3018
Thomas Falcon032c5e82015-12-21 11:26:06 -06003019static int send_version_xchg(struct ibmvnic_adapter *adapter)
3020{
3021 union ibmvnic_crq crq;
3022
3023 memset(&crq, 0, sizeof(crq));
3024 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3025 crq.version_exchange.cmd = VERSION_EXCHANGE;
3026 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3027
3028 return ibmvnic_send_crq(adapter, &crq);
3029}
3030
Nathan Fontenot37798d02017-11-08 11:23:56 -06003031struct vnic_login_client_data {
3032 u8 type;
3033 __be16 len;
3034 char name;
3035} __packed;
3036
3037static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3038{
3039 int len;
3040
3041 /* Calculate the amount of buffer space needed for the
3042 * vnic client data in the login buffer. There are four entries,
3043 * OS name, LPAR name, device name, and a null last entry.
3044 */
3045 len = 4 * sizeof(struct vnic_login_client_data);
3046 len += 6; /* "Linux" plus NULL */
3047 len += strlen(utsname()->nodename) + 1;
3048 len += strlen(adapter->netdev->name) + 1;
3049
3050 return len;
3051}
3052
3053static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3054 struct vnic_login_client_data *vlcd)
3055{
3056 const char *os_name = "Linux";
3057 int len;
3058
3059 /* Type 1 - LPAR OS */
3060 vlcd->type = 1;
3061 len = strlen(os_name) + 1;
3062 vlcd->len = cpu_to_be16(len);
3063 strncpy(&vlcd->name, os_name, len);
3064 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3065
3066 /* Type 2 - LPAR name */
3067 vlcd->type = 2;
3068 len = strlen(utsname()->nodename) + 1;
3069 vlcd->len = cpu_to_be16(len);
3070 strncpy(&vlcd->name, utsname()->nodename, len);
3071 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3072
3073 /* Type 3 - device name */
3074 vlcd->type = 3;
3075 len = strlen(adapter->netdev->name) + 1;
3076 vlcd->len = cpu_to_be16(len);
3077 strncpy(&vlcd->name, adapter->netdev->name, len);
3078}
3079
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003080static int send_login(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003081{
3082 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3083 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003084 struct device *dev = &adapter->vdev->dev;
3085 dma_addr_t rsp_buffer_token;
3086 dma_addr_t buffer_token;
3087 size_t rsp_buffer_size;
3088 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 size_t buffer_size;
3090 __be64 *tx_list_p;
3091 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003092 int client_data_len;
3093 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003094 int i;
3095
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003096 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3097 netdev_err(adapter->netdev,
3098 "RX or TX queues are not allocated, device login failed\n");
3099 return -1;
3100 }
3101
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003102 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003103 client_data_len = vnic_client_data_len(adapter);
3104
Thomas Falcon032c5e82015-12-21 11:26:06 -06003105 buffer_size =
3106 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003107 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3108 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109
Nathan Fontenot37798d02017-11-08 11:23:56 -06003110 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003111 if (!login_buffer)
3112 goto buf_alloc_failed;
3113
3114 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3115 DMA_TO_DEVICE);
3116 if (dma_mapping_error(dev, buffer_token)) {
3117 dev_err(dev, "Couldn't map login buffer\n");
3118 goto buf_map_failed;
3119 }
3120
John Allen498cd8e2016-04-06 11:49:55 -05003121 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3122 sizeof(u64) * adapter->req_tx_queues +
3123 sizeof(u64) * adapter->req_rx_queues +
3124 sizeof(u64) * adapter->req_rx_queues +
3125 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003126
3127 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3128 if (!login_rsp_buffer)
3129 goto buf_rsp_alloc_failed;
3130
3131 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3132 rsp_buffer_size, DMA_FROM_DEVICE);
3133 if (dma_mapping_error(dev, rsp_buffer_token)) {
3134 dev_err(dev, "Couldn't map login rsp buffer\n");
3135 goto buf_rsp_map_failed;
3136 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003137
Thomas Falcon032c5e82015-12-21 11:26:06 -06003138 adapter->login_buf = login_buffer;
3139 adapter->login_buf_token = buffer_token;
3140 adapter->login_buf_sz = buffer_size;
3141 adapter->login_rsp_buf = login_rsp_buffer;
3142 adapter->login_rsp_buf_token = rsp_buffer_token;
3143 adapter->login_rsp_buf_sz = rsp_buffer_size;
3144
3145 login_buffer->len = cpu_to_be32(buffer_size);
3146 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3147 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3148 login_buffer->off_txcomp_subcrqs =
3149 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3150 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3151 login_buffer->off_rxcomp_subcrqs =
3152 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3153 sizeof(u64) * adapter->req_tx_queues);
3154 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3155 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3156
3157 tx_list_p = (__be64 *)((char *)login_buffer +
3158 sizeof(struct ibmvnic_login_buffer));
3159 rx_list_p = (__be64 *)((char *)login_buffer +
3160 sizeof(struct ibmvnic_login_buffer) +
3161 sizeof(u64) * adapter->req_tx_queues);
3162
3163 for (i = 0; i < adapter->req_tx_queues; i++) {
3164 if (adapter->tx_scrq[i]) {
3165 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3166 crq_num);
3167 }
3168 }
3169
3170 for (i = 0; i < adapter->req_rx_queues; i++) {
3171 if (adapter->rx_scrq[i]) {
3172 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3173 crq_num);
3174 }
3175 }
3176
Nathan Fontenot37798d02017-11-08 11:23:56 -06003177 /* Insert vNIC login client data */
3178 vlcd = (struct vnic_login_client_data *)
3179 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3180 login_buffer->client_data_offset =
3181 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3182 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3183
3184 vnic_add_client_data(adapter, vlcd);
3185
Thomas Falcon032c5e82015-12-21 11:26:06 -06003186 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3187 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3188 netdev_dbg(adapter->netdev, "%016lx\n",
3189 ((unsigned long int *)(adapter->login_buf))[i]);
3190 }
3191
3192 memset(&crq, 0, sizeof(crq));
3193 crq.login.first = IBMVNIC_CRQ_CMD;
3194 crq.login.cmd = LOGIN;
3195 crq.login.ioba = cpu_to_be32(buffer_token);
3196 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003197 ibmvnic_send_crq(adapter, &crq);
3198
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003199 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003200
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201buf_rsp_map_failed:
3202 kfree(login_rsp_buffer);
3203buf_rsp_alloc_failed:
3204 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3205buf_map_failed:
3206 kfree(login_buffer);
3207buf_alloc_failed:
Thomas Falcon20a8ab72018-02-26 18:10:59 -06003208 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209}
3210
3211static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3212 u32 len, u8 map_id)
3213{
3214 union ibmvnic_crq crq;
3215
3216 memset(&crq, 0, sizeof(crq));
3217 crq.request_map.first = IBMVNIC_CRQ_CMD;
3218 crq.request_map.cmd = REQUEST_MAP;
3219 crq.request_map.map_id = map_id;
3220 crq.request_map.ioba = cpu_to_be32(addr);
3221 crq.request_map.len = cpu_to_be32(len);
3222 ibmvnic_send_crq(adapter, &crq);
3223}
3224
3225static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3226{
3227 union ibmvnic_crq crq;
3228
3229 memset(&crq, 0, sizeof(crq));
3230 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3231 crq.request_unmap.cmd = REQUEST_UNMAP;
3232 crq.request_unmap.map_id = map_id;
3233 ibmvnic_send_crq(adapter, &crq);
3234}
3235
3236static void send_map_query(struct ibmvnic_adapter *adapter)
3237{
3238 union ibmvnic_crq crq;
3239
3240 memset(&crq, 0, sizeof(crq));
3241 crq.query_map.first = IBMVNIC_CRQ_CMD;
3242 crq.query_map.cmd = QUERY_MAP;
3243 ibmvnic_send_crq(adapter, &crq);
3244}
3245
3246/* Send a series of CRQs requesting various capabilities of the VNIC server */
3247static void send_cap_queries(struct ibmvnic_adapter *adapter)
3248{
3249 union ibmvnic_crq crq;
3250
Thomas Falcon901e0402017-02-15 12:17:59 -06003251 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003252 memset(&crq, 0, sizeof(crq));
3253 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3254 crq.query_capability.cmd = QUERY_CAPABILITY;
3255
3256 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003257 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003258 ibmvnic_send_crq(adapter, &crq);
3259
3260 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003261 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003262 ibmvnic_send_crq(adapter, &crq);
3263
3264 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003265 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003266 ibmvnic_send_crq(adapter, &crq);
3267
3268 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003269 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003270 ibmvnic_send_crq(adapter, &crq);
3271
3272 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003273 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274 ibmvnic_send_crq(adapter, &crq);
3275
3276 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003277 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003278 ibmvnic_send_crq(adapter, &crq);
3279
3280 crq.query_capability.capability =
3281 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003282 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003283 ibmvnic_send_crq(adapter, &crq);
3284
3285 crq.query_capability.capability =
3286 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003287 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003288 ibmvnic_send_crq(adapter, &crq);
3289
3290 crq.query_capability.capability =
3291 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003292 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003293 ibmvnic_send_crq(adapter, &crq);
3294
3295 crq.query_capability.capability =
3296 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003297 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003298 ibmvnic_send_crq(adapter, &crq);
3299
3300 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003301 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302 ibmvnic_send_crq(adapter, &crq);
3303
3304 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003305 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003306 ibmvnic_send_crq(adapter, &crq);
3307
3308 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003309 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310 ibmvnic_send_crq(adapter, &crq);
3311
3312 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003313 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003314 ibmvnic_send_crq(adapter, &crq);
3315
3316 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003317 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003318 ibmvnic_send_crq(adapter, &crq);
3319
3320 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003321 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 ibmvnic_send_crq(adapter, &crq);
3323
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003324 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3325 atomic_inc(&adapter->running_cap_crqs);
3326 ibmvnic_send_crq(adapter, &crq);
3327
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003329 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003330 ibmvnic_send_crq(adapter, &crq);
3331
3332 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003333 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003334 ibmvnic_send_crq(adapter, &crq);
3335
3336 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003337 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003338 ibmvnic_send_crq(adapter, &crq);
3339
3340 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003341 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003342 ibmvnic_send_crq(adapter, &crq);
3343
3344 crq.query_capability.capability =
3345 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003346 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003347 ibmvnic_send_crq(adapter, &crq);
3348
3349 crq.query_capability.capability =
3350 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003351 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352 ibmvnic_send_crq(adapter, &crq);
3353
3354 crq.query_capability.capability =
3355 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003356 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003357 ibmvnic_send_crq(adapter, &crq);
3358
3359 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003360 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003361 ibmvnic_send_crq(adapter, &crq);
3362}
3363
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003364static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3365 struct ibmvnic_adapter *adapter)
3366{
3367 struct device *dev = &adapter->vdev->dev;
3368
3369 if (crq->get_vpd_size_rsp.rc.code) {
3370 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3371 crq->get_vpd_size_rsp.rc.code);
3372 complete(&adapter->fw_done);
3373 return;
3374 }
3375
3376 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3377 complete(&adapter->fw_done);
3378}
3379
3380static void handle_vpd_rsp(union ibmvnic_crq *crq,
3381 struct ibmvnic_adapter *adapter)
3382{
3383 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003384 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003385 u8 fw_level_len = 0;
3386
3387 memset(adapter->fw_version, 0, 32);
3388
3389 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3390 DMA_FROM_DEVICE);
3391
3392 if (crq->get_vpd_rsp.rc.code) {
3393 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3394 crq->get_vpd_rsp.rc.code);
3395 goto complete;
3396 }
3397
3398 /* get the position of the firmware version info
3399 * located after the ASCII 'RM' substring in the buffer
3400 */
3401 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3402 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003403 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 -02003404 goto complete;
3405 }
3406
3407 /* get length of firmware level ASCII substring */
3408 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3409 fw_level_len = *(substr + 2);
3410 } else {
3411 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3412 goto complete;
3413 }
3414
3415 /* copy firmware version string from vpd into adapter */
3416 if ((substr + 3 + fw_level_len) <
3417 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003418 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003419 } else {
3420 dev_info(dev, "FW substr extrapolated VPD buff\n");
3421 }
3422
3423complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003424 if (adapter->fw_version[0] == '\0')
3425 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003426 complete(&adapter->fw_done);
3427}
3428
Thomas Falcon032c5e82015-12-21 11:26:06 -06003429static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3430{
3431 struct device *dev = &adapter->vdev->dev;
3432 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3433 union ibmvnic_crq crq;
3434 int i;
3435
3436 dma_unmap_single(dev, adapter->ip_offload_tok,
3437 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3438
3439 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3440 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3441 netdev_dbg(adapter->netdev, "%016lx\n",
3442 ((unsigned long int *)(buf))[i]);
3443
3444 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3445 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3446 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3447 buf->tcp_ipv4_chksum);
3448 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3449 buf->tcp_ipv6_chksum);
3450 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3451 buf->udp_ipv4_chksum);
3452 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3453 buf->udp_ipv6_chksum);
3454 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3455 buf->large_tx_ipv4);
3456 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3457 buf->large_tx_ipv6);
3458 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3459 buf->large_rx_ipv4);
3460 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3461 buf->large_rx_ipv6);
3462 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3463 buf->max_ipv4_header_size);
3464 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3465 buf->max_ipv6_header_size);
3466 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3467 buf->max_tcp_header_size);
3468 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3469 buf->max_udp_header_size);
3470 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3471 buf->max_large_tx_size);
3472 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3473 buf->max_large_rx_size);
3474 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3475 buf->ipv6_extension_header);
3476 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3477 buf->tcp_pseudosum_req);
3478 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3479 buf->num_ipv6_ext_headers);
3480 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3481 buf->off_ipv6_ext_headers);
3482
3483 adapter->ip_offload_ctrl_tok =
3484 dma_map_single(dev, &adapter->ip_offload_ctrl,
3485 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3486
3487 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3488 dev_err(dev, "Couldn't map ip offload control buffer\n");
3489 return;
3490 }
3491
Thomas Falconf6897942018-01-18 19:05:01 -06003492 adapter->ip_offload_ctrl.len =
3493 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003494 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003495 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3496 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003497 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3498 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3499 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3500 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003501 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3502 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003503
Thomas Falconfdb06102017-10-17 12:36:55 -05003504 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003505 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3506 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3507
Thomas Falcon15482052017-10-17 12:36:54 -05003508 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003509
3510 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3511 adapter->netdev->features |= NETIF_F_IP_CSUM;
3512
3513 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3514 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3515
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003516 if ((adapter->netdev->features &
3517 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3518 adapter->netdev->features |= NETIF_F_RXCSUM;
3519
Thomas Falconfdb06102017-10-17 12:36:55 -05003520 if (buf->large_tx_ipv4)
3521 adapter->netdev->features |= NETIF_F_TSO;
3522 if (buf->large_tx_ipv6)
3523 adapter->netdev->features |= NETIF_F_TSO6;
3524
Thomas Falconaa0bf852017-10-17 12:36:56 -05003525 adapter->netdev->hw_features |= adapter->netdev->features;
3526
Thomas Falcon032c5e82015-12-21 11:26:06 -06003527 memset(&crq, 0, sizeof(crq));
3528 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3529 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3530 crq.control_ip_offload.len =
3531 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3532 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3533 ibmvnic_send_crq(adapter, &crq);
3534}
3535
3536static void handle_error_info_rsp(union ibmvnic_crq *crq,
3537 struct ibmvnic_adapter *adapter)
3538{
3539 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003540 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003541 unsigned long flags;
3542 bool found = false;
3543 int i;
3544
3545 if (!crq->request_error_rsp.rc.code) {
3546 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3547 crq->request_error_rsp.rc.code);
3548 return;
3549 }
3550
3551 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003552 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003553 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3554 found = true;
3555 list_del(&error_buff->list);
3556 break;
3557 }
3558 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3559
3560 if (!found) {
3561 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003562 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003563 return;
3564 }
3565
3566 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003567 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003568
3569 for (i = 0; i < error_buff->len; i++) {
3570 pr_cont("%02x", (int)error_buff->buff[i]);
3571 if (i % 8 == 7)
3572 pr_cont(" ");
3573 }
3574 pr_cont("\n");
3575
3576 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3577 DMA_FROM_DEVICE);
3578 kfree(error_buff->buff);
3579 kfree(error_buff);
3580}
3581
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003582static void request_error_information(struct ibmvnic_adapter *adapter,
3583 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003584{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003585 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003586 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003587 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003588 unsigned long timeout = msecs_to_jiffies(30000);
3589 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003591 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592
3593 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3594 if (!error_buff)
3595 return;
3596
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003597 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003598 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3599 if (!error_buff->buff) {
3600 kfree(error_buff);
3601 return;
3602 }
3603
3604 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3605 DMA_FROM_DEVICE);
3606 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003607 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 kfree(error_buff->buff);
3609 kfree(error_buff);
3610 return;
3611 }
3612
Thomas Falcon032c5e82015-12-21 11:26:06 -06003613 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003614 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003615
3616 spin_lock_irqsave(&adapter->error_list_lock, flags);
3617 list_add_tail(&error_buff->list, &adapter->errors);
3618 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3619
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003620 memset(&crq, 0, sizeof(crq));
3621 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3622 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3623 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3624 crq.request_error_info.len = cpu_to_be32(detail_len);
3625 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3626
3627 rc = ibmvnic_send_crq(adapter, &crq);
3628 if (rc) {
3629 netdev_err(netdev, "failed to request error information\n");
3630 goto err_info_fail;
3631 }
3632
3633 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3634 netdev_err(netdev, "timeout waiting for error information\n");
3635 goto err_info_fail;
3636 }
3637
3638 return;
3639
3640err_info_fail:
3641 spin_lock_irqsave(&adapter->error_list_lock, flags);
3642 list_del(&error_buff->list);
3643 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3644
3645 kfree(error_buff->buff);
3646 kfree(error_buff);
3647}
3648
3649static void handle_error_indication(union ibmvnic_crq *crq,
3650 struct ibmvnic_adapter *adapter)
3651{
3652 struct device *dev = &adapter->vdev->dev;
3653
3654 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3655 crq->error_indication.flags
3656 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3657 be32_to_cpu(crq->error_indication.error_id),
3658 be16_to_cpu(crq->error_indication.error_cause));
3659
3660 if (be32_to_cpu(crq->error_indication.error_id))
3661 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003662
3663 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3664 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003665 else
3666 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003667}
3668
Thomas Falconf8136142018-01-29 13:45:05 -06003669static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3670 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671{
3672 struct net_device *netdev = adapter->netdev;
3673 struct device *dev = &adapter->vdev->dev;
3674 long rc;
3675
3676 rc = crq->change_mac_addr_rsp.rc.code;
3677 if (rc) {
3678 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003679 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003680 }
3681 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3682 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003683out:
3684 complete(&adapter->fw_done);
3685 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003686}
3687
3688static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3689 struct ibmvnic_adapter *adapter)
3690{
3691 struct device *dev = &adapter->vdev->dev;
3692 u64 *req_value;
3693 char *name;
3694
Thomas Falcon901e0402017-02-15 12:17:59 -06003695 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003696 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3697 case REQ_TX_QUEUES:
3698 req_value = &adapter->req_tx_queues;
3699 name = "tx";
3700 break;
3701 case REQ_RX_QUEUES:
3702 req_value = &adapter->req_rx_queues;
3703 name = "rx";
3704 break;
3705 case REQ_RX_ADD_QUEUES:
3706 req_value = &adapter->req_rx_add_queues;
3707 name = "rx_add";
3708 break;
3709 case REQ_TX_ENTRIES_PER_SUBCRQ:
3710 req_value = &adapter->req_tx_entries_per_subcrq;
3711 name = "tx_entries_per_subcrq";
3712 break;
3713 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3714 req_value = &adapter->req_rx_add_entries_per_subcrq;
3715 name = "rx_add_entries_per_subcrq";
3716 break;
3717 case REQ_MTU:
3718 req_value = &adapter->req_mtu;
3719 name = "mtu";
3720 break;
3721 case PROMISC_REQUESTED:
3722 req_value = &adapter->promisc;
3723 name = "promisc";
3724 break;
3725 default:
3726 dev_err(dev, "Got invalid cap request rsp %d\n",
3727 crq->request_capability.capability);
3728 return;
3729 }
3730
3731 switch (crq->request_capability_rsp.rc.code) {
3732 case SUCCESS:
3733 break;
3734 case PARTIALSUCCESS:
3735 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3736 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003737 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003738 number), name);
John Allene7913802018-01-18 16:27:12 -06003739
3740 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3741 REQ_MTU) {
3742 pr_err("mtu of %llu is not supported. Reverting.\n",
3743 *req_value);
3744 *req_value = adapter->fallback.mtu;
3745 } else {
3746 *req_value =
3747 be64_to_cpu(crq->request_capability_rsp.number);
3748 }
3749
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003750 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003751 return;
3752 default:
3753 dev_err(dev, "Error %d in request cap rsp\n",
3754 crq->request_capability_rsp.rc.code);
3755 return;
3756 }
3757
3758 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003759 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003760 union ibmvnic_crq newcrq;
3761 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3762 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3763 &adapter->ip_offload_buf;
3764
Thomas Falcon249168a2017-02-15 12:18:00 -06003765 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003766 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3767 buf_sz,
3768 DMA_FROM_DEVICE);
3769
3770 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3771 if (!firmware_has_feature(FW_FEATURE_CMO))
3772 dev_err(dev, "Couldn't map offload buffer\n");
3773 return;
3774 }
3775
3776 memset(&newcrq, 0, sizeof(newcrq));
3777 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3778 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3779 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3780 newcrq.query_ip_offload.ioba =
3781 cpu_to_be32(adapter->ip_offload_tok);
3782
3783 ibmvnic_send_crq(adapter, &newcrq);
3784 }
3785}
3786
3787static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3788 struct ibmvnic_adapter *adapter)
3789{
3790 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003791 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003792 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3793 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003794 int i;
3795
3796 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3797 DMA_BIDIRECTIONAL);
3798 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3799 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3800
John Allen498cd8e2016-04-06 11:49:55 -05003801 /* If the number of queues requested can't be allocated by the
3802 * server, the login response will return with code 1. We will need
3803 * to resend the login buffer with fewer queues requested.
3804 */
3805 if (login_rsp_crq->generic.rc.code) {
3806 adapter->renegotiate = true;
3807 complete(&adapter->init_done);
3808 return 0;
3809 }
3810
John Allenc26eba02017-10-26 16:23:25 -05003811 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3812
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3814 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3815 netdev_dbg(adapter->netdev, "%016lx\n",
3816 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3817 }
3818
3819 /* Sanity checks */
3820 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3821 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3822 adapter->req_rx_add_queues !=
3823 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3824 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3825 ibmvnic_remove(adapter->vdev);
3826 return -EIO;
3827 }
Thomas Falcona2c0f032018-02-21 18:18:30 -06003828 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003829 complete(&adapter->init_done);
3830
Thomas Falcon032c5e82015-12-21 11:26:06 -06003831 return 0;
3832}
3833
Thomas Falcon032c5e82015-12-21 11:26:06 -06003834static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3835 struct ibmvnic_adapter *adapter)
3836{
3837 struct device *dev = &adapter->vdev->dev;
3838 long rc;
3839
3840 rc = crq->request_unmap_rsp.rc.code;
3841 if (rc)
3842 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3843}
3844
3845static void handle_query_map_rsp(union ibmvnic_crq *crq,
3846 struct ibmvnic_adapter *adapter)
3847{
3848 struct net_device *netdev = adapter->netdev;
3849 struct device *dev = &adapter->vdev->dev;
3850 long rc;
3851
3852 rc = crq->query_map_rsp.rc.code;
3853 if (rc) {
3854 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3855 return;
3856 }
3857 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3858 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3859 crq->query_map_rsp.free_pages);
3860}
3861
3862static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3863 struct ibmvnic_adapter *adapter)
3864{
3865 struct net_device *netdev = adapter->netdev;
3866 struct device *dev = &adapter->vdev->dev;
3867 long rc;
3868
Thomas Falcon901e0402017-02-15 12:17:59 -06003869 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003870 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003871 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003872 rc = crq->query_capability.rc.code;
3873 if (rc) {
3874 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3875 goto out;
3876 }
3877
3878 switch (be16_to_cpu(crq->query_capability.capability)) {
3879 case MIN_TX_QUEUES:
3880 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003881 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003882 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3883 adapter->min_tx_queues);
3884 break;
3885 case MIN_RX_QUEUES:
3886 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003887 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3889 adapter->min_rx_queues);
3890 break;
3891 case MIN_RX_ADD_QUEUES:
3892 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003893 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003894 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3895 adapter->min_rx_add_queues);
3896 break;
3897 case MAX_TX_QUEUES:
3898 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003899 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003900 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3901 adapter->max_tx_queues);
3902 break;
3903 case MAX_RX_QUEUES:
3904 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003905 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003906 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3907 adapter->max_rx_queues);
3908 break;
3909 case MAX_RX_ADD_QUEUES:
3910 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003911 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003912 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3913 adapter->max_rx_add_queues);
3914 break;
3915 case MIN_TX_ENTRIES_PER_SUBCRQ:
3916 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003917 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003918 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3919 adapter->min_tx_entries_per_subcrq);
3920 break;
3921 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3922 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003923 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003924 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3925 adapter->min_rx_add_entries_per_subcrq);
3926 break;
3927 case MAX_TX_ENTRIES_PER_SUBCRQ:
3928 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003929 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003930 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3931 adapter->max_tx_entries_per_subcrq);
3932 break;
3933 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3934 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003935 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003936 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3937 adapter->max_rx_add_entries_per_subcrq);
3938 break;
3939 case TCP_IP_OFFLOAD:
3940 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003941 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003942 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3943 adapter->tcp_ip_offload);
3944 break;
3945 case PROMISC_SUPPORTED:
3946 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003947 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003948 netdev_dbg(netdev, "promisc_supported = %lld\n",
3949 adapter->promisc_supported);
3950 break;
3951 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003952 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003953 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003954 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3955 break;
3956 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003957 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003958 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003959 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3960 break;
3961 case MAX_MULTICAST_FILTERS:
3962 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003963 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003964 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3965 adapter->max_multicast_filters);
3966 break;
3967 case VLAN_HEADER_INSERTION:
3968 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003969 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003970 if (adapter->vlan_header_insertion)
3971 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3972 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3973 adapter->vlan_header_insertion);
3974 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003975 case RX_VLAN_HEADER_INSERTION:
3976 adapter->rx_vlan_header_insertion =
3977 be64_to_cpu(crq->query_capability.number);
3978 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3979 adapter->rx_vlan_header_insertion);
3980 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981 case MAX_TX_SG_ENTRIES:
3982 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003983 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003984 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3985 adapter->max_tx_sg_entries);
3986 break;
3987 case RX_SG_SUPPORTED:
3988 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003989 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003990 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3991 adapter->rx_sg_supported);
3992 break;
3993 case OPT_TX_COMP_SUB_QUEUES:
3994 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003995 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003996 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3997 adapter->opt_tx_comp_sub_queues);
3998 break;
3999 case OPT_RX_COMP_QUEUES:
4000 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06004001 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004002 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4003 adapter->opt_rx_comp_queues);
4004 break;
4005 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4006 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004007 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004008 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4009 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4010 break;
4011 case OPT_TX_ENTRIES_PER_SUBCRQ:
4012 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004013 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004014 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4015 adapter->opt_tx_entries_per_subcrq);
4016 break;
4017 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4018 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004019 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004020 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4021 adapter->opt_rxba_entries_per_subcrq);
4022 break;
4023 case TX_RX_DESC_REQ:
4024 adapter->tx_rx_desc_req = crq->query_capability.number;
4025 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4026 adapter->tx_rx_desc_req);
4027 break;
4028
4029 default:
4030 netdev_err(netdev, "Got invalid cap rsp %d\n",
4031 crq->query_capability.capability);
4032 }
4033
4034out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004035 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4036 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004037 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004038 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004039}
4040
Thomas Falcon032c5e82015-12-21 11:26:06 -06004041static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4042 struct ibmvnic_adapter *adapter)
4043{
4044 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4045 struct net_device *netdev = adapter->netdev;
4046 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004047 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004048 long rc;
4049
4050 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004051 (unsigned long int)cpu_to_be64(u64_crq[0]),
4052 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004053 switch (gen_crq->first) {
4054 case IBMVNIC_CRQ_INIT_RSP:
4055 switch (gen_crq->cmd) {
4056 case IBMVNIC_CRQ_INIT:
4057 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004058 adapter->from_passive_init = true;
4059 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004060 break;
4061 case IBMVNIC_CRQ_INIT_COMPLETE:
4062 dev_info(dev, "Partner initialization complete\n");
4063 send_version_xchg(adapter);
4064 break;
4065 default:
4066 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4067 }
4068 return;
4069 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004070 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004071 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004072 dev_info(dev, "Migrated, re-enabling adapter\n");
4073 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004074 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4075 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004076 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004077 } else {
4078 /* The adapter lost the connection */
4079 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4080 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004081 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004082 }
4083 return;
4084 case IBMVNIC_CRQ_CMD_RSP:
4085 break;
4086 default:
4087 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4088 gen_crq->first);
4089 return;
4090 }
4091
4092 switch (gen_crq->cmd) {
4093 case VERSION_EXCHANGE_RSP:
4094 rc = crq->version_exchange_rsp.rc.code;
4095 if (rc) {
4096 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4097 break;
4098 }
4099 dev_info(dev, "Partner protocol version is %d\n",
4100 crq->version_exchange_rsp.version);
4101 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4102 ibmvnic_version)
4103 ibmvnic_version =
4104 be16_to_cpu(crq->version_exchange_rsp.version);
4105 send_cap_queries(adapter);
4106 break;
4107 case QUERY_CAPABILITY_RSP:
4108 handle_query_cap_rsp(crq, adapter);
4109 break;
4110 case QUERY_MAP_RSP:
4111 handle_query_map_rsp(crq, adapter);
4112 break;
4113 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004114 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4115 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004116 break;
4117 case REQUEST_UNMAP_RSP:
4118 handle_request_unmap_rsp(crq, adapter);
4119 break;
4120 case REQUEST_CAPABILITY_RSP:
4121 handle_request_cap_rsp(crq, adapter);
4122 break;
4123 case LOGIN_RSP:
4124 netdev_dbg(netdev, "Got Login Response\n");
4125 handle_login_rsp(crq, adapter);
4126 break;
4127 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004128 netdev_dbg(netdev,
4129 "Got Logical Link State Response, state: %d rc: %d\n",
4130 crq->logical_link_state_rsp.link_state,
4131 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004132 adapter->logical_link_state =
4133 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004134 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4135 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004136 break;
4137 case LINK_STATE_INDICATION:
4138 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4139 adapter->phys_link_state =
4140 crq->link_state_indication.phys_link_state;
4141 adapter->logical_link_state =
4142 crq->link_state_indication.logical_link_state;
4143 break;
4144 case CHANGE_MAC_ADDR_RSP:
4145 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004146 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004147 break;
4148 case ERROR_INDICATION:
4149 netdev_dbg(netdev, "Got Error Indication\n");
4150 handle_error_indication(crq, adapter);
4151 break;
4152 case REQUEST_ERROR_RSP:
4153 netdev_dbg(netdev, "Got Error Detail Response\n");
4154 handle_error_info_rsp(crq, adapter);
4155 break;
4156 case REQUEST_STATISTICS_RSP:
4157 netdev_dbg(netdev, "Got Statistics Response\n");
4158 complete(&adapter->stats_done);
4159 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004160 case QUERY_IP_OFFLOAD_RSP:
4161 netdev_dbg(netdev, "Got Query IP offload Response\n");
4162 handle_query_ip_offload_rsp(adapter);
4163 break;
4164 case MULTICAST_CTRL_RSP:
4165 netdev_dbg(netdev, "Got multicast control Response\n");
4166 break;
4167 case CONTROL_IP_OFFLOAD_RSP:
4168 netdev_dbg(netdev, "Got Control IP offload Response\n");
4169 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4170 sizeof(adapter->ip_offload_ctrl),
4171 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004172 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004173 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004174 case COLLECT_FW_TRACE_RSP:
4175 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4176 complete(&adapter->fw_done);
4177 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004178 case GET_VPD_SIZE_RSP:
4179 handle_vpd_size_rsp(crq, adapter);
4180 break;
4181 case GET_VPD_RSP:
4182 handle_vpd_rsp(crq, adapter);
4183 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004184 default:
4185 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4186 gen_crq->cmd);
4187 }
4188}
4189
4190static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4191{
4192 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004193
Thomas Falcon6c267b32017-02-15 12:17:58 -06004194 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004195 return IRQ_HANDLED;
4196}
4197
4198static void ibmvnic_tasklet(void *data)
4199{
4200 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004201 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202 union ibmvnic_crq *crq;
4203 unsigned long flags;
4204 bool done = false;
4205
4206 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004207 while (!done) {
4208 /* Pull all the valid messages off the CRQ */
4209 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4210 ibmvnic_handle_crq(crq, adapter);
4211 crq->generic.first = 0;
4212 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004213
4214 /* remain in tasklet until all
4215 * capabilities responses are received
4216 */
4217 if (!adapter->wait_capability)
4218 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004219 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004220 /* if capabilities CRQ's were sent in this tasklet, the following
4221 * tasklet must wait until all responses are received
4222 */
4223 if (atomic_read(&adapter->running_cap_crqs) != 0)
4224 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004225 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004226}
4227
4228static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4229{
4230 struct vio_dev *vdev = adapter->vdev;
4231 int rc;
4232
4233 do {
4234 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4235 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4236
4237 if (rc)
4238 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4239
4240 return rc;
4241}
4242
4243static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4244{
4245 struct ibmvnic_crq_queue *crq = &adapter->crq;
4246 struct device *dev = &adapter->vdev->dev;
4247 struct vio_dev *vdev = adapter->vdev;
4248 int rc;
4249
4250 /* Close the CRQ */
4251 do {
4252 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4253 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4254
4255 /* Clean out the queue */
4256 memset(crq->msgs, 0, PAGE_SIZE);
4257 crq->cur = 0;
4258
4259 /* And re-open it again */
4260 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4261 crq->msg_token, PAGE_SIZE);
4262
4263 if (rc == H_CLOSED)
4264 /* Adapter is good, but other end is not ready */
4265 dev_warn(dev, "Partner adapter not ready\n");
4266 else if (rc != 0)
4267 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4268
4269 return rc;
4270}
4271
Nathan Fontenotf9928872017-03-30 02:48:54 -04004272static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004273{
4274 struct ibmvnic_crq_queue *crq = &adapter->crq;
4275 struct vio_dev *vdev = adapter->vdev;
4276 long rc;
4277
Nathan Fontenotf9928872017-03-30 02:48:54 -04004278 if (!crq->msgs)
4279 return;
4280
Thomas Falcon032c5e82015-12-21 11:26:06 -06004281 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4282 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004283 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004284 do {
4285 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4286 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4287
4288 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4289 DMA_BIDIRECTIONAL);
4290 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004291 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004292}
4293
Nathan Fontenotf9928872017-03-30 02:48:54 -04004294static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004295{
4296 struct ibmvnic_crq_queue *crq = &adapter->crq;
4297 struct device *dev = &adapter->vdev->dev;
4298 struct vio_dev *vdev = adapter->vdev;
4299 int rc, retrc = -ENOMEM;
4300
Nathan Fontenotf9928872017-03-30 02:48:54 -04004301 if (crq->msgs)
4302 return 0;
4303
Thomas Falcon032c5e82015-12-21 11:26:06 -06004304 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4305 /* Should we allocate more than one page? */
4306
4307 if (!crq->msgs)
4308 return -ENOMEM;
4309
4310 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4311 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4312 DMA_BIDIRECTIONAL);
4313 if (dma_mapping_error(dev, crq->msg_token))
4314 goto map_failed;
4315
4316 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4317 crq->msg_token, PAGE_SIZE);
4318
4319 if (rc == H_RESOURCE)
4320 /* maybe kexecing and resource is busy. try a reset */
4321 rc = ibmvnic_reset_crq(adapter);
4322 retrc = rc;
4323
4324 if (rc == H_CLOSED) {
4325 dev_warn(dev, "Partner adapter not ready\n");
4326 } else if (rc) {
4327 dev_warn(dev, "Error %d opening adapter\n", rc);
4328 goto reg_crq_failed;
4329 }
4330
4331 retrc = 0;
4332
Thomas Falcon6c267b32017-02-15 12:17:58 -06004333 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4334 (unsigned long)adapter);
4335
Thomas Falcon032c5e82015-12-21 11:26:06 -06004336 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4337 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4338 adapter);
4339 if (rc) {
4340 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4341 vdev->irq, rc);
4342 goto req_irq_failed;
4343 }
4344
4345 rc = vio_enable_interrupts(vdev);
4346 if (rc) {
4347 dev_err(dev, "Error %d enabling interrupts\n", rc);
4348 goto req_irq_failed;
4349 }
4350
4351 crq->cur = 0;
4352 spin_lock_init(&crq->lock);
4353
4354 return retrc;
4355
4356req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004357 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004358 do {
4359 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4360 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4361reg_crq_failed:
4362 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4363map_failed:
4364 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004365 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004366 return retrc;
4367}
4368
John Allenf6ef6402017-03-17 17:13:42 -05004369static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4370{
4371 struct device *dev = &adapter->vdev->dev;
4372 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004373 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004374 int rc;
4375
John Allenc26eba02017-10-26 16:23:25 -05004376 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004377 rc = ibmvnic_reset_crq(adapter);
4378 if (!rc)
4379 rc = vio_enable_interrupts(adapter->vdev);
4380 } else {
4381 rc = init_crq_queue(adapter);
4382 }
4383
John Allenf6ef6402017-03-17 17:13:42 -05004384 if (rc) {
4385 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4386 return rc;
4387 }
4388
John Allen017892c12017-05-26 10:30:19 -04004389 adapter->from_passive_init = false;
4390
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004391 old_num_rx_queues = adapter->req_rx_queues;
4392 old_num_tx_queues = adapter->req_tx_queues;
4393
John Allenf6ef6402017-03-17 17:13:42 -05004394 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004395 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004396 ibmvnic_send_crq_init(adapter);
4397 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4398 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004399 return -1;
4400 }
4401
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004402 if (adapter->init_done_rc) {
4403 release_crq_queue(adapter);
4404 return adapter->init_done_rc;
4405 }
4406
John Allen017892c12017-05-26 10:30:19 -04004407 if (adapter->from_passive_init) {
4408 adapter->state = VNIC_OPEN;
4409 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004410 return -1;
4411 }
4412
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004413 if (adapter->resetting && !adapter->wait_for_reset) {
4414 if (adapter->req_rx_queues != old_num_rx_queues ||
4415 adapter->req_tx_queues != old_num_tx_queues) {
4416 release_sub_crqs(adapter, 0);
4417 rc = init_sub_crqs(adapter);
4418 } else {
4419 rc = reset_sub_crq_queues(adapter);
4420 }
4421 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004422 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004423 }
4424
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004425 if (rc) {
4426 dev_err(dev, "Initialization of sub crqs failed\n");
4427 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004428 return rc;
4429 }
4430
4431 rc = init_sub_crq_irqs(adapter);
4432 if (rc) {
4433 dev_err(dev, "Failed to initialize sub crq irqs\n");
4434 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004435 }
4436
Thomas Falcon53cc7722018-02-26 18:10:56 -06004437 rc = init_stats_buffers(adapter);
4438 if (rc)
4439 return rc;
4440
4441 rc = init_stats_token(adapter);
4442 if (rc)
4443 return rc;
4444
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004445 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004446}
4447
Thomas Falcon40c9db82017-06-12 12:35:04 -05004448static struct device_attribute dev_attr_failover;
4449
Thomas Falcon032c5e82015-12-21 11:26:06 -06004450static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4451{
4452 struct ibmvnic_adapter *adapter;
4453 struct net_device *netdev;
4454 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004455 int rc;
4456
4457 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4458 dev->unit_address);
4459
4460 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4461 VETH_MAC_ADDR, NULL);
4462 if (!mac_addr_p) {
4463 dev_err(&dev->dev,
4464 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4465 __FILE__, __LINE__);
4466 return 0;
4467 }
4468
4469 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004470 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471 if (!netdev)
4472 return -ENOMEM;
4473
4474 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004475 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004476 dev_set_drvdata(&dev->dev, netdev);
4477 adapter->vdev = dev;
4478 adapter->netdev = netdev;
4479
4480 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4481 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4482 netdev->irq = dev->irq;
4483 netdev->netdev_ops = &ibmvnic_netdev_ops;
4484 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4485 SET_NETDEV_DEV(netdev, &dev->dev);
4486
4487 spin_lock_init(&adapter->stats_lock);
4488
Thomas Falcon032c5e82015-12-21 11:26:06 -06004489 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004490 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004491
Nathan Fontenoted651a12017-05-03 14:04:38 -04004492 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4493 INIT_LIST_HEAD(&adapter->rwi_list);
4494 mutex_init(&adapter->reset_lock);
4495 mutex_init(&adapter->rwi_lock);
4496 adapter->resetting = false;
4497
John Allenc26eba02017-10-26 16:23:25 -05004498 adapter->mac_change_pending = false;
4499
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004500 do {
4501 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004502 if (rc && rc != EAGAIN)
4503 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004504 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004505
Thomas Falconf39f0d12017-02-14 10:22:59 -06004506 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004507 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4508 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004509
Thomas Falcon40c9db82017-06-12 12:35:04 -05004510 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004511 if (rc)
4512 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004513
Mick Tarsele876a8a2017-09-28 13:53:18 -07004514 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004515 rc = register_netdev(netdev);
4516 if (rc) {
4517 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004518 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004519 }
4520 dev_info(&dev->dev, "ibmvnic registered\n");
4521
Nathan Fontenot90c80142017-05-03 14:04:32 -04004522 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004523
4524 adapter->wait_for_reset = false;
4525
Thomas Falcon032c5e82015-12-21 11:26:06 -06004526 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004527
4528ibmvnic_register_fail:
4529 device_remove_file(&dev->dev, &dev_attr_failover);
4530
4531ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004532 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004533 release_crq_queue(adapter);
4534 free_netdev(netdev);
4535
4536 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004537}
4538
4539static int ibmvnic_remove(struct vio_dev *dev)
4540{
4541 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004542 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004543
Nathan Fontenot90c80142017-05-03 14:04:32 -04004544 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004545 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004546 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004547
4548 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004549 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004550 release_crq_queue(adapter);
4551
Thomas Falcon53cc7722018-02-26 18:10:56 -06004552 release_stats_token(adapter);
4553 release_stats_buffers(adapter);
4554
Nathan Fontenot90c80142017-05-03 14:04:32 -04004555 adapter->state = VNIC_REMOVED;
4556
Nathan Fontenoted651a12017-05-03 14:04:38 -04004557 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004558 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004559 free_netdev(netdev);
4560 dev_set_drvdata(&dev->dev, NULL);
4561
4562 return 0;
4563}
4564
Thomas Falcon40c9db82017-06-12 12:35:04 -05004565static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4566 const char *buf, size_t count)
4567{
4568 struct net_device *netdev = dev_get_drvdata(dev);
4569 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4570 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4571 __be64 session_token;
4572 long rc;
4573
4574 if (!sysfs_streq(buf, "1"))
4575 return -EINVAL;
4576
4577 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4578 H_GET_SESSION_TOKEN, 0, 0, 0);
4579 if (rc) {
4580 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4581 rc);
4582 return -EINVAL;
4583 }
4584
4585 session_token = (__be64)retbuf[0];
4586 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4587 be64_to_cpu(session_token));
4588 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4589 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4590 if (rc) {
4591 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4592 rc);
4593 return -EINVAL;
4594 }
4595
4596 return count;
4597}
4598
Joe Perches6cbaefb2017-12-19 10:15:09 -08004599static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004600
Thomas Falcon032c5e82015-12-21 11:26:06 -06004601static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4602{
4603 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4604 struct ibmvnic_adapter *adapter;
4605 struct iommu_table *tbl;
4606 unsigned long ret = 0;
4607 int i;
4608
4609 tbl = get_iommu_table_base(&vdev->dev);
4610
4611 /* netdev inits at probe time along with the structures we need below*/
4612 if (!netdev)
4613 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4614
4615 adapter = netdev_priv(netdev);
4616
4617 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004618 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4619
4620 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4621 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4622
4623 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4624 i++)
4625 ret += adapter->rx_pool[i].size *
4626 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4627
4628 return ret;
4629}
4630
4631static int ibmvnic_resume(struct device *dev)
4632{
4633 struct net_device *netdev = dev_get_drvdata(dev);
4634 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004635
John Allencb89ba22017-06-19 11:27:53 -05004636 if (adapter->state != VNIC_OPEN)
4637 return 0;
4638
John Allena2488782017-07-24 13:26:06 -05004639 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004640
4641 return 0;
4642}
4643
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304644static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004645 {"network", "IBM,vnic"},
4646 {"", "" }
4647};
4648MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4649
4650static const struct dev_pm_ops ibmvnic_pm_ops = {
4651 .resume = ibmvnic_resume
4652};
4653
4654static struct vio_driver ibmvnic_driver = {
4655 .id_table = ibmvnic_device_table,
4656 .probe = ibmvnic_probe,
4657 .remove = ibmvnic_remove,
4658 .get_desired_dma = ibmvnic_get_desired_dma,
4659 .name = ibmvnic_driver_name,
4660 .pm = &ibmvnic_pm_ops,
4661};
4662
4663/* module functions */
4664static int __init ibmvnic_module_init(void)
4665{
4666 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4667 IBMVNIC_DRIVER_VERSION);
4668
4669 return vio_register_driver(&ibmvnic_driver);
4670}
4671
4672static void __exit ibmvnic_module_exit(void)
4673{
4674 vio_unregister_driver(&ibmvnic_driver);
4675}
4676
4677module_init(ibmvnic_module_init);
4678module_exit(ibmvnic_module_exit);