blob: ad0f9cc1be0498d16b37faa945c7e1a077d9225b [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);
John Allenbd0b6722017-03-17 17:13:40 -0500114static void send_login(struct ibmvnic_adapter *adapter);
115static 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 Fontenot88625412018-02-19 13:30:05 -0600464 for (i = 0; i < adapter->num_active_rx_scrqs; 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;
487}
488
489static int init_rx_pools(struct net_device *netdev)
490{
491 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
492 struct device *dev = &adapter->vdev->dev;
493 struct ibmvnic_rx_pool *rx_pool;
494 int rxadd_subcrqs;
495 u64 *size_array;
496 int i, j;
497
498 rxadd_subcrqs =
499 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
500 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
501 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
502
503 adapter->rx_pool = kcalloc(rxadd_subcrqs,
504 sizeof(struct ibmvnic_rx_pool),
505 GFP_KERNEL);
506 if (!adapter->rx_pool) {
507 dev_err(dev, "Failed to allocate rx pools\n");
508 return -1;
509 }
510
511 for (i = 0; i < rxadd_subcrqs; i++) {
512 rx_pool = &adapter->rx_pool[i];
513
514 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500515 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400516 i, adapter->req_rx_add_entries_per_subcrq,
517 be64_to_cpu(size_array[i]));
518
519 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
520 rx_pool->index = i;
521 rx_pool->buff_size = be64_to_cpu(size_array[i]);
522 rx_pool->active = 1;
523
524 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
525 GFP_KERNEL);
526 if (!rx_pool->free_map) {
527 release_rx_pools(adapter);
528 return -1;
529 }
530
531 rx_pool->rx_buff = kcalloc(rx_pool->size,
532 sizeof(struct ibmvnic_rx_buff),
533 GFP_KERNEL);
534 if (!rx_pool->rx_buff) {
535 dev_err(dev, "Couldn't alloc rx buffers\n");
536 release_rx_pools(adapter);
537 return -1;
538 }
539
540 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
541 rx_pool->size * rx_pool->buff_size)) {
542 release_rx_pools(adapter);
543 return -1;
544 }
545
546 for (j = 0; j < rx_pool->size; ++j)
547 rx_pool->free_map[j] = j;
548
549 atomic_set(&rx_pool->available, 0);
550 rx_pool->next_alloc = 0;
551 rx_pool->next_free = 0;
552 }
553
554 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600555}
556
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400557static int reset_tx_pools(struct ibmvnic_adapter *adapter)
558{
559 struct ibmvnic_tx_pool *tx_pool;
560 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500561 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400562
563 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
564 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500565 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
566
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400567 tx_pool = &adapter->tx_pool[i];
568
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500569 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
570 if (rc)
571 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400572
Thomas Falconfdb06102017-10-17 12:36:55 -0500573 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
574 if (rc)
575 return rc;
576
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400577 memset(tx_pool->tx_buff, 0,
578 adapter->req_tx_entries_per_subcrq *
579 sizeof(struct ibmvnic_tx_buff));
580
581 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
582 tx_pool->free_map[j] = j;
583
584 tx_pool->consumer_index = 0;
585 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500586 tx_pool->tso_index = 0;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400587 }
588
589 return 0;
590}
591
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200592static void release_vpd_data(struct ibmvnic_adapter *adapter)
593{
594 if (!adapter->vpd)
595 return;
596
597 kfree(adapter->vpd->buff);
598 kfree(adapter->vpd);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600599
600 adapter->vpd = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200601}
602
Nathan Fontenotc657e322017-03-30 02:49:06 -0400603static void release_tx_pools(struct ibmvnic_adapter *adapter)
604{
605 struct ibmvnic_tx_pool *tx_pool;
John Allen896d8692018-01-18 16:26:31 -0600606 int i;
Nathan Fontenotc657e322017-03-30 02:49:06 -0400607
608 if (!adapter->tx_pool)
609 return;
610
Nathan Fontenot88625412018-02-19 13:30:05 -0600611 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500612 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400613 tx_pool = &adapter->tx_pool[i];
614 kfree(tx_pool->tx_buff);
615 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500616 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400617 kfree(tx_pool->free_map);
618 }
619
620 kfree(adapter->tx_pool);
621 adapter->tx_pool = NULL;
622}
623
624static int init_tx_pools(struct net_device *netdev)
625{
626 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
627 struct device *dev = &adapter->vdev->dev;
628 struct ibmvnic_tx_pool *tx_pool;
629 int tx_subcrqs;
630 int i, j;
631
632 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
633 adapter->tx_pool = kcalloc(tx_subcrqs,
634 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
635 if (!adapter->tx_pool)
636 return -1;
637
638 for (i = 0; i < tx_subcrqs; i++) {
639 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500640
641 netdev_dbg(adapter->netdev,
642 "Initializing tx_pool[%d], %lld buffs\n",
643 i, adapter->req_tx_entries_per_subcrq);
644
Nathan Fontenotc657e322017-03-30 02:49:06 -0400645 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
646 sizeof(struct ibmvnic_tx_buff),
647 GFP_KERNEL);
648 if (!tx_pool->tx_buff) {
649 dev_err(dev, "tx pool buffer allocation failed\n");
650 release_tx_pools(adapter);
651 return -1;
652 }
653
654 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
655 adapter->req_tx_entries_per_subcrq *
656 adapter->req_mtu)) {
657 release_tx_pools(adapter);
658 return -1;
659 }
660
Thomas Falconfdb06102017-10-17 12:36:55 -0500661 /* alloc TSO ltb */
662 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
663 IBMVNIC_TSO_BUFS *
664 IBMVNIC_TSO_BUF_SZ)) {
665 release_tx_pools(adapter);
666 return -1;
667 }
668
669 tx_pool->tso_index = 0;
670
Nathan Fontenotc657e322017-03-30 02:49:06 -0400671 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
672 sizeof(int), GFP_KERNEL);
673 if (!tx_pool->free_map) {
674 release_tx_pools(adapter);
675 return -1;
676 }
677
678 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
679 tx_pool->free_map[j] = j;
680
681 tx_pool->consumer_index = 0;
682 tx_pool->producer_index = 0;
683 }
684
685 return 0;
686}
687
Nathan Fontenot661a2622017-04-19 13:44:58 -0400688static void release_error_buffers(struct ibmvnic_adapter *adapter)
689{
690 struct device *dev = &adapter->vdev->dev;
691 struct ibmvnic_error_buff *error_buff, *tmp;
692 unsigned long flags;
693
694 spin_lock_irqsave(&adapter->error_list_lock, flags);
695 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
696 list_del(&error_buff->list);
697 dma_unmap_single(dev, error_buff->dma, error_buff->len,
698 DMA_FROM_DEVICE);
699 kfree(error_buff->buff);
700 kfree(error_buff);
701 }
702 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
703}
704
John Allend944c3d62017-05-26 10:30:13 -0400705static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
706{
707 int i;
708
709 if (adapter->napi_enabled)
710 return;
711
712 for (i = 0; i < adapter->req_rx_queues; i++)
713 napi_enable(&adapter->napi[i]);
714
715 adapter->napi_enabled = true;
716}
717
718static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
719{
720 int i;
721
722 if (!adapter->napi_enabled)
723 return;
724
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500725 for (i = 0; i < adapter->req_rx_queues; i++) {
726 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400727 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500728 }
John Allend944c3d62017-05-26 10:30:13 -0400729
730 adapter->napi_enabled = false;
731}
732
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600733static int init_napi(struct ibmvnic_adapter *adapter)
734{
735 int i;
736
737 adapter->napi = kcalloc(adapter->req_rx_queues,
738 sizeof(struct napi_struct), GFP_KERNEL);
739 if (!adapter->napi)
740 return -ENOMEM;
741
742 for (i = 0; i < adapter->req_rx_queues; i++) {
743 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
744 netif_napi_add(adapter->netdev, &adapter->napi[i],
745 ibmvnic_poll, NAPI_POLL_WEIGHT);
746 }
747
748 return 0;
749}
750
751static void release_napi(struct ibmvnic_adapter *adapter)
752{
753 int i;
754
755 if (!adapter->napi)
756 return;
757
758 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
759 if (&adapter->napi[i]) {
760 netdev_dbg(adapter->netdev,
761 "Releasing napi[%d]\n", i);
762 netif_napi_del(&adapter->napi[i]);
763 }
764 }
765
766 kfree(adapter->napi);
767 adapter->napi = NULL;
768}
769
John Allena57a5d22017-03-17 17:13:41 -0500770static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600771{
772 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500773 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600774 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500775 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600776
John Allenbd0b6722017-03-17 17:13:40 -0500777 do {
778 if (adapter->renegotiate) {
779 adapter->renegotiate = false;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600780 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500781
782 reinit_completion(&adapter->init_done);
783 send_cap_queries(adapter);
784 if (!wait_for_completion_timeout(&adapter->init_done,
785 timeout)) {
786 dev_err(dev, "Capabilities query timeout\n");
787 return -1;
788 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500789 rc = init_sub_crqs(adapter);
790 if (rc) {
791 dev_err(dev,
792 "Initialization of SCRQ's failed\n");
793 return -1;
794 }
795 rc = init_sub_crq_irqs(adapter);
796 if (rc) {
797 dev_err(dev,
798 "Initialization of SCRQ's irqs failed\n");
799 return -1;
800 }
John Allenbd0b6722017-03-17 17:13:40 -0500801 }
802
803 reinit_completion(&adapter->init_done);
804 send_login(adapter);
805 if (!wait_for_completion_timeout(&adapter->init_done,
806 timeout)) {
807 dev_err(dev, "Login timeout\n");
808 return -1;
809 }
810 } while (adapter->renegotiate);
811
Thomas Falcon3d166132018-01-10 19:39:52 -0600812 /* handle pending MAC address changes after successful login */
813 if (adapter->mac_change_pending) {
814 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
815 adapter->mac_change_pending = false;
816 }
817
John Allena57a5d22017-03-17 17:13:41 -0500818 return 0;
819}
820
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600821static void release_login_buffer(struct ibmvnic_adapter *adapter)
822{
823 kfree(adapter->login_buf);
824 adapter->login_buf = NULL;
825}
826
827static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
828{
829 kfree(adapter->login_rsp_buf);
830 adapter->login_rsp_buf = NULL;
831}
832
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400833static void release_resources(struct ibmvnic_adapter *adapter)
834{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200835 release_vpd_data(adapter);
836
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400837 release_tx_pools(adapter);
838 release_rx_pools(adapter);
839
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400840 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500841 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400842 release_error_buffers(adapter);
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600843 release_napi(adapter);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600844 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400845}
846
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400847static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
848{
849 struct net_device *netdev = adapter->netdev;
850 unsigned long timeout = msecs_to_jiffies(30000);
851 union ibmvnic_crq crq;
852 bool resend;
853 int rc;
854
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500855 netdev_dbg(netdev, "setting link state %d\n", link_state);
856
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400857 memset(&crq, 0, sizeof(crq));
858 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
859 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
860 crq.logical_link_state.link_state = link_state;
861
862 do {
863 resend = false;
864
865 reinit_completion(&adapter->init_done);
866 rc = ibmvnic_send_crq(adapter, &crq);
867 if (rc) {
868 netdev_err(netdev, "Failed to set link state\n");
869 return rc;
870 }
871
872 if (!wait_for_completion_timeout(&adapter->init_done,
873 timeout)) {
874 netdev_err(netdev, "timeout setting link state\n");
875 return -1;
876 }
877
878 if (adapter->init_done_rc == 1) {
879 /* Partuial success, delay and re-send */
880 mdelay(1000);
881 resend = true;
882 }
883 } while (resend);
884
885 return 0;
886}
887
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400888static int set_real_num_queues(struct net_device *netdev)
889{
890 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
891 int rc;
892
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500893 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
894 adapter->req_tx_queues, adapter->req_rx_queues);
895
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400896 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
897 if (rc) {
898 netdev_err(netdev, "failed to set the number of tx queues\n");
899 return rc;
900 }
901
902 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
903 if (rc)
904 netdev_err(netdev, "failed to set the number of rx queues\n");
905
906 return rc;
907}
908
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200909static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
910{
911 struct device *dev = &adapter->vdev->dev;
912 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200913 int len = 0;
914
915 if (adapter->vpd->buff)
916 len = adapter->vpd->len;
917
John Allen69d08dc2018-01-18 16:27:58 -0600918 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200919 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
920 crq.get_vpd_size.cmd = GET_VPD_SIZE;
921 ibmvnic_send_crq(adapter, &crq);
922 wait_for_completion(&adapter->fw_done);
923
924 if (!adapter->vpd->len)
925 return -ENODATA;
926
927 if (!adapter->vpd->buff)
928 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
929 else if (adapter->vpd->len != len)
930 adapter->vpd->buff =
931 krealloc(adapter->vpd->buff,
932 adapter->vpd->len, GFP_KERNEL);
933
934 if (!adapter->vpd->buff) {
935 dev_err(dev, "Could allocate VPD buffer\n");
936 return -ENOMEM;
937 }
938
939 adapter->vpd->dma_addr =
940 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
941 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200942 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200943 dev_err(dev, "Could not map VPD buffer\n");
944 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600945 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200946 return -ENOMEM;
947 }
948
949 reinit_completion(&adapter->fw_done);
950 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
951 crq.get_vpd.cmd = GET_VPD;
952 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
953 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
954 ibmvnic_send_crq(adapter, &crq);
955 wait_for_completion(&adapter->fw_done);
956
957 return 0;
958}
959
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400960static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500961{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400962 struct net_device *netdev = adapter->netdev;
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600963 int rc;
John Allena57a5d22017-03-17 17:13:41 -0500964
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400965 rc = set_real_num_queues(netdev);
966 if (rc)
967 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500968
John Allen3d52b592017-08-02 16:44:14 -0500969 rc = init_stats_buffers(adapter);
970 if (rc)
971 return rc;
972
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400973 rc = init_stats_token(adapter);
974 if (rc)
975 return rc;
976
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200977 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
978 if (!adapter->vpd)
979 return -ENOMEM;
980
John Allen69d08dc2018-01-18 16:27:58 -0600981 /* Vital Product Data (VPD) */
982 rc = ibmvnic_get_vpd(adapter);
983 if (rc) {
984 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
985 return rc;
986 }
987
Thomas Falcon032c5e82015-12-21 11:26:06 -0600988 adapter->map_id = 1;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400989
Nathan Fontenot86f669b2018-02-19 13:30:39 -0600990 rc = init_napi(adapter);
991 if (rc)
992 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600993
Thomas Falcon032c5e82015-12-21 11:26:06 -0600994 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400995
996 rc = init_rx_pools(netdev);
997 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400998 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600999
Nathan Fontenotc657e322017-03-30 02:49:06 -04001000 rc = init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001001
1002 adapter->num_active_tx_scrqs = adapter->req_tx_queues;
1003 adapter->num_active_rx_scrqs = adapter->req_rx_queues;
1004
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001005 return rc;
1006}
1007
Nathan Fontenoted651a12017-05-03 14:04:38 -04001008static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001009{
1010 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001011 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001012 int i, rc;
1013
Nathan Fontenot90c80142017-05-03 14:04:32 -04001014 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001015 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -04001016 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001017
Thomas Falcon032c5e82015-12-21 11:26:06 -06001018 /* We're ready to receive frames, enable the sub-crq interrupts and
1019 * set the logical link state to up
1020 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001021 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001022 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001023 if (prev_state == VNIC_CLOSED)
1024 enable_irq(adapter->rx_scrq[i]->irq);
1025 else
1026 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1027 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001028
Nathan Fontenoted651a12017-05-03 14:04:38 -04001029 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001030 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001031 if (prev_state == VNIC_CLOSED)
1032 enable_irq(adapter->tx_scrq[i]->irq);
1033 else
1034 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1035 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001036
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001037 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001038 if (rc) {
1039 for (i = 0; i < adapter->req_rx_queues; i++)
1040 napi_disable(&adapter->napi[i]);
1041 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001042 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001043 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001044
Nathan Fontenoted651a12017-05-03 14:04:38 -04001045 netif_tx_start_all_queues(netdev);
1046
1047 if (prev_state == VNIC_CLOSED) {
1048 for (i = 0; i < adapter->req_rx_queues; i++)
1049 napi_schedule(&adapter->napi[i]);
1050 }
1051
1052 adapter->state = VNIC_OPEN;
1053 return rc;
1054}
1055
1056static int ibmvnic_open(struct net_device *netdev)
1057{
1058 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001059 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001060
1061 mutex_lock(&adapter->reset_lock);
1062
1063 if (adapter->state != VNIC_CLOSED) {
1064 rc = ibmvnic_login(netdev);
1065 if (rc) {
1066 mutex_unlock(&adapter->reset_lock);
1067 return rc;
1068 }
1069
1070 rc = init_resources(adapter);
1071 if (rc) {
1072 netdev_err(netdev, "failed to initialize resources\n");
1073 release_resources(adapter);
1074 mutex_unlock(&adapter->reset_lock);
1075 return rc;
1076 }
1077 }
1078
1079 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001080 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001081
Nathan Fontenoted651a12017-05-03 14:04:38 -04001082 mutex_unlock(&adapter->reset_lock);
1083
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001084 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001085}
1086
Thomas Falcond0869c02018-02-13 18:23:43 -06001087static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1088{
1089 struct ibmvnic_rx_pool *rx_pool;
1090 u64 rx_entries;
1091 int rx_scrqs;
1092 int i, j;
1093
1094 if (!adapter->rx_pool)
1095 return;
1096
1097 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1098 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1099
1100 /* Free any remaining skbs in the rx buffer pools */
1101 for (i = 0; i < rx_scrqs; i++) {
1102 rx_pool = &adapter->rx_pool[i];
1103 if (!rx_pool)
1104 continue;
1105
1106 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1107 for (j = 0; j < rx_entries; j++) {
1108 if (rx_pool->rx_buff[j].skb) {
1109 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
1110 rx_pool->rx_buff[j].skb = NULL;
1111 }
1112 }
1113 }
1114}
1115
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001116static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1117{
1118 struct ibmvnic_tx_pool *tx_pool;
1119 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];
1132 if (!tx_pool)
1133 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++) {
1137 if (tx_pool->tx_buff[j].skb) {
1138 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1139 tx_pool->tx_buff[j].skb = NULL;
1140 }
1141 }
1142 }
1143}
1144
Nathan Fontenoted651a12017-05-03 14:04:38 -04001145static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001146{
1147 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001148 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -05001149 int i;
1150
Nathan Fontenot90c80142017-05-03 14:04:32 -04001151 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001152
1153 /* ensure that transmissions are stopped if called by do_reset */
1154 if (adapter->resetting)
1155 netif_tx_disable(netdev);
1156 else
1157 netif_tx_stop_all_queues(netdev);
1158
John Allend944c3d62017-05-26 10:30:13 -04001159 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001160
1161 if (adapter->tx_scrq) {
1162 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001163 if (adapter->tx_scrq[i]->irq) {
1164 netdev_dbg(adapter->netdev,
1165 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001166 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001167 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001168 }
1169
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001170 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001171 if (rc)
1172 return rc;
1173
1174 if (adapter->rx_scrq) {
1175 for (i = 0; i < adapter->req_rx_queues; i++) {
1176 int retries = 10;
1177
1178 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1179 retries--;
1180 mdelay(100);
1181
1182 if (retries == 0)
1183 break;
1184 }
1185
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001186 if (adapter->rx_scrq[i]->irq) {
1187 netdev_dbg(adapter->netdev,
1188 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001189 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001190 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001191 }
1192 }
Thomas Falcond0869c02018-02-13 18:23:43 -06001193 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001194 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001195 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001196 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001197}
1198
Nathan Fontenoted651a12017-05-03 14:04:38 -04001199static int ibmvnic_close(struct net_device *netdev)
1200{
1201 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1202 int rc;
1203
1204 mutex_lock(&adapter->reset_lock);
1205 rc = __ibmvnic_close(netdev);
1206 mutex_unlock(&adapter->reset_lock);
1207
1208 return rc;
1209}
1210
Thomas Falconad7775d2016-04-01 17:20:34 -05001211/**
1212 * build_hdr_data - creates L2/L3/L4 header data buffer
1213 * @hdr_field - bitfield determining needed headers
1214 * @skb - socket buffer
1215 * @hdr_len - array of header lengths
1216 * @tot_len - total length of data
1217 *
1218 * Reads hdr_field to determine which headers are needed by firmware.
1219 * Builds a buffer containing these headers. Saves individual header
1220 * lengths and total buffer length to be used to build descriptors.
1221 */
1222static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1223 int *hdr_len, u8 *hdr_data)
1224{
1225 int len = 0;
1226 u8 *hdr;
1227
1228 hdr_len[0] = sizeof(struct ethhdr);
1229
1230 if (skb->protocol == htons(ETH_P_IP)) {
1231 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1232 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1233 hdr_len[2] = tcp_hdrlen(skb);
1234 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1235 hdr_len[2] = sizeof(struct udphdr);
1236 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1237 hdr_len[1] = sizeof(struct ipv6hdr);
1238 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1239 hdr_len[2] = tcp_hdrlen(skb);
1240 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1241 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001242 } else if (skb->protocol == htons(ETH_P_ARP)) {
1243 hdr_len[1] = arp_hdr_len(skb->dev);
1244 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001245 }
1246
1247 memset(hdr_data, 0, 120);
1248 if ((hdr_field >> 6) & 1) {
1249 hdr = skb_mac_header(skb);
1250 memcpy(hdr_data, hdr, hdr_len[0]);
1251 len += hdr_len[0];
1252 }
1253
1254 if ((hdr_field >> 5) & 1) {
1255 hdr = skb_network_header(skb);
1256 memcpy(hdr_data + len, hdr, hdr_len[1]);
1257 len += hdr_len[1];
1258 }
1259
1260 if ((hdr_field >> 4) & 1) {
1261 hdr = skb_transport_header(skb);
1262 memcpy(hdr_data + len, hdr, hdr_len[2]);
1263 len += hdr_len[2];
1264 }
1265 return len;
1266}
1267
1268/**
1269 * create_hdr_descs - create header and header extension descriptors
1270 * @hdr_field - bitfield determining needed headers
1271 * @data - buffer containing header data
1272 * @len - length of data buffer
1273 * @hdr_len - array of individual header lengths
1274 * @scrq_arr - descriptor array
1275 *
1276 * Creates header and, if needed, header extension descriptors and
1277 * places them in a descriptor array, scrq_arr
1278 */
1279
Thomas Falcon2de09682017-10-16 10:02:11 -05001280static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1281 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001282{
1283 union sub_crq hdr_desc;
1284 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001285 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001286 u8 *data, *cur;
1287 int tmp;
1288
1289 while (tmp_len > 0) {
1290 cur = hdr_data + len - tmp_len;
1291
1292 memset(&hdr_desc, 0, sizeof(hdr_desc));
1293 if (cur != hdr_data) {
1294 data = hdr_desc.hdr_ext.data;
1295 tmp = tmp_len > 29 ? 29 : tmp_len;
1296 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1297 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1298 hdr_desc.hdr_ext.len = tmp;
1299 } else {
1300 data = hdr_desc.hdr.data;
1301 tmp = tmp_len > 24 ? 24 : tmp_len;
1302 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1303 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1304 hdr_desc.hdr.len = tmp;
1305 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1306 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1307 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1308 hdr_desc.hdr.flag = hdr_field << 1;
1309 }
1310 memcpy(data, cur, tmp);
1311 tmp_len -= tmp;
1312 *scrq_arr = hdr_desc;
1313 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001314 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001315 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001316
1317 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001318}
1319
1320/**
1321 * build_hdr_descs_arr - build a header descriptor array
1322 * @skb - socket buffer
1323 * @num_entries - number of descriptors to be sent
1324 * @subcrq - first TX descriptor
1325 * @hdr_field - bit field determining which headers will be sent
1326 *
1327 * This function will build a TX descriptor array with applicable
1328 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1329 */
1330
1331static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1332 int *num_entries, u8 hdr_field)
1333{
1334 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001335 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001336 u8 *hdr_data = txbuff->hdr_data;
1337
1338 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1339 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001340 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001341 txbuff->indir_arr + 1);
1342}
1343
Thomas Falcon032c5e82015-12-21 11:26:06 -06001344static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1345{
1346 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1347 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001348 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001349 struct device *dev = &adapter->vdev->dev;
1350 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001351 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001352 struct ibmvnic_tx_pool *tx_pool;
1353 unsigned int tx_send_failed = 0;
1354 unsigned int tx_map_failed = 0;
1355 unsigned int tx_dropped = 0;
1356 unsigned int tx_packets = 0;
1357 unsigned int tx_bytes = 0;
1358 dma_addr_t data_dma_addr;
1359 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001360 unsigned long lpar_rc;
1361 union sub_crq tx_crq;
1362 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001363 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001364 unsigned char *dst;
1365 u64 *handle_array;
1366 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001367 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001368 int ret = 0;
1369
Nathan Fontenoted651a12017-05-03 14:04:38 -04001370 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001371 if (!netif_subqueue_stopped(netdev, skb))
1372 netif_stop_subqueue(netdev, queue_num);
1373 dev_kfree_skb_any(skb);
1374
Thomas Falcon032c5e82015-12-21 11:26:06 -06001375 tx_send_failed++;
1376 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001377 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001378 goto out;
1379 }
1380
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001381 tx_pool = &adapter->tx_pool[queue_num];
1382 tx_scrq = adapter->tx_scrq[queue_num];
1383 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1384 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1385 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1386
Thomas Falcon032c5e82015-12-21 11:26:06 -06001387 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001388
1389 if (skb_is_gso(skb)) {
1390 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1391 dst = tx_pool->tso_ltb.buff + offset;
1392 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1393 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1394 tx_pool->tso_index++;
1395 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1396 tx_pool->tso_index = 0;
1397 } else {
1398 offset = index * adapter->req_mtu;
1399 dst = tx_pool->long_term_buff.buff + offset;
1400 memset(dst, 0, adapter->req_mtu);
1401 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1402 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001403
Thomas Falcon15482052017-10-17 12:36:54 -05001404 if (skb_shinfo(skb)->nr_frags) {
1405 int cur, i;
1406
1407 /* Copy the head */
1408 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1409 cur = skb_headlen(skb);
1410
1411 /* Copy the frags */
1412 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1413 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1414
1415 memcpy(dst + cur,
1416 page_address(skb_frag_page(frag)) +
1417 frag->page_offset, skb_frag_size(frag));
1418 cur += skb_frag_size(frag);
1419 }
1420 } else {
1421 skb_copy_from_linear_data(skb, dst, skb->len);
1422 }
1423
Thomas Falcon032c5e82015-12-21 11:26:06 -06001424 tx_pool->consumer_index =
1425 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001426 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001427
1428 tx_buff = &tx_pool->tx_buff[index];
1429 tx_buff->skb = skb;
1430 tx_buff->data_dma[0] = data_dma_addr;
1431 tx_buff->data_len[0] = skb->len;
1432 tx_buff->index = index;
1433 tx_buff->pool_index = queue_num;
1434 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001435
1436 memset(&tx_crq, 0, sizeof(tx_crq));
1437 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1438 tx_crq.v1.type = IBMVNIC_TX_DESC;
1439 tx_crq.v1.n_crq_elem = 1;
1440 tx_crq.v1.n_sge = 1;
1441 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1442 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001443 if (skb_is_gso(skb))
1444 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1445 else
1446 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001447 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1448 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1449
1450 if (adapter->vlan_header_insertion) {
1451 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1452 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1453 }
1454
1455 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001456 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1457 proto = ip_hdr(skb)->protocol;
1458 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1459 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1460 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001461 }
1462
Thomas Falcona0dca102018-01-18 19:29:48 -06001463 if (proto == IPPROTO_TCP)
1464 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1465 else if (proto == IPPROTO_UDP)
1466 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1467
Thomas Falconad7775d2016-04-01 17:20:34 -05001468 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001469 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001470 hdrs += 2;
1471 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001472 if (skb_is_gso(skb)) {
1473 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1474 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1475 hdrs += 2;
1476 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001477 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001478 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001479 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1480 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconffc385b2018-02-18 10:08:41 -06001481 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001482 tx_buff->indir_arr[0] = tx_crq;
1483 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1484 sizeof(tx_buff->indir_arr),
1485 DMA_TO_DEVICE);
1486 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001487 dev_kfree_skb_any(skb);
1488 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001489 if (!firmware_has_feature(FW_FEATURE_CMO))
1490 dev_err(dev, "tx: unable to map descriptor array\n");
1491 tx_map_failed++;
1492 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001493 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001494 goto out;
1495 }
John Allen498cd8e2016-04-06 11:49:55 -05001496 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001497 (u64)tx_buff->indir_dma,
1498 (u64)num_entries);
1499 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001500 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1501 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001502 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001503 if (lpar_rc != H_SUCCESS) {
1504 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1505
1506 if (tx_pool->consumer_index == 0)
1507 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001508 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001509 else
1510 tx_pool->consumer_index--;
1511
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001512 dev_kfree_skb_any(skb);
1513 tx_buff->skb = NULL;
1514
Thomas Falconb8c80b82017-05-26 10:30:42 -04001515 if (lpar_rc == H_CLOSED) {
1516 /* Disable TX and report carrier off if queue is closed.
1517 * Firmware guarantees that a signal will be sent to the
1518 * driver, triggering a reset or some other action.
1519 */
1520 netif_tx_stop_all_queues(netdev);
1521 netif_carrier_off(netdev);
1522 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001523
Thomas Falcon032c5e82015-12-21 11:26:06 -06001524 tx_send_failed++;
1525 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001526 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001527 goto out;
1528 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001529
Thomas Falconffc385b2018-02-18 10:08:41 -06001530 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001531 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001532 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1533 netif_stop_subqueue(netdev, queue_num);
1534 }
1535
Thomas Falcon032c5e82015-12-21 11:26:06 -06001536 tx_packets++;
1537 tx_bytes += skb->len;
1538 txq->trans_start = jiffies;
1539 ret = NETDEV_TX_OK;
1540
1541out:
1542 netdev->stats.tx_dropped += tx_dropped;
1543 netdev->stats.tx_bytes += tx_bytes;
1544 netdev->stats.tx_packets += tx_packets;
1545 adapter->tx_send_failed += tx_send_failed;
1546 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001547 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1548 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1549 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001550
1551 return ret;
1552}
1553
1554static void ibmvnic_set_multi(struct net_device *netdev)
1555{
1556 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1557 struct netdev_hw_addr *ha;
1558 union ibmvnic_crq crq;
1559
1560 memset(&crq, 0, sizeof(crq));
1561 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1562 crq.request_capability.cmd = REQUEST_CAPABILITY;
1563
1564 if (netdev->flags & IFF_PROMISC) {
1565 if (!adapter->promisc_supported)
1566 return;
1567 } else {
1568 if (netdev->flags & IFF_ALLMULTI) {
1569 /* Accept all multicast */
1570 memset(&crq, 0, sizeof(crq));
1571 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1572 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1573 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1574 ibmvnic_send_crq(adapter, &crq);
1575 } else if (netdev_mc_empty(netdev)) {
1576 /* Reject all multicast */
1577 memset(&crq, 0, sizeof(crq));
1578 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1579 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1580 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1581 ibmvnic_send_crq(adapter, &crq);
1582 } else {
1583 /* Accept one or more multicast(s) */
1584 netdev_for_each_mc_addr(ha, netdev) {
1585 memset(&crq, 0, sizeof(crq));
1586 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1587 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1588 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1589 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1590 ha->addr);
1591 ibmvnic_send_crq(adapter, &crq);
1592 }
1593 }
1594 }
1595}
1596
John Allenc26eba02017-10-26 16:23:25 -05001597static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001598{
1599 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1600 struct sockaddr *addr = p;
1601 union ibmvnic_crq crq;
1602
1603 if (!is_valid_ether_addr(addr->sa_data))
1604 return -EADDRNOTAVAIL;
1605
1606 memset(&crq, 0, sizeof(crq));
1607 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1608 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1609 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001610
1611 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001613 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001614 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001615 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001616}
1617
John Allenc26eba02017-10-26 16:23:25 -05001618static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1619{
1620 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1621 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001622 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001623
Thomas Falcon3d166132018-01-10 19:39:52 -06001624 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001625 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1626 adapter->mac_change_pending = true;
1627 return 0;
1628 }
1629
Thomas Falconf8136142018-01-29 13:45:05 -06001630 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001631
Thomas Falconf8136142018-01-29 13:45:05 -06001632 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001633}
1634
Nathan Fontenoted651a12017-05-03 14:04:38 -04001635/**
1636 * do_reset returns zero if we are able to keep processing reset events, or
1637 * non-zero if we hit a fatal error and must halt.
1638 */
1639static int do_reset(struct ibmvnic_adapter *adapter,
1640 struct ibmvnic_rwi *rwi, u32 reset_state)
1641{
John Allen896d8692018-01-18 16:26:31 -06001642 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001643 struct net_device *netdev = adapter->netdev;
1644 int i, rc;
1645
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001646 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1647 rwi->reset_reason);
1648
Nathan Fontenoted651a12017-05-03 14:04:38 -04001649 netif_carrier_off(netdev);
1650 adapter->reset_reason = rwi->reset_reason;
1651
John Allen896d8692018-01-18 16:26:31 -06001652 old_num_rx_queues = adapter->req_rx_queues;
1653 old_num_tx_queues = adapter->req_tx_queues;
1654
Nathan Fontenoted651a12017-05-03 14:04:38 -04001655 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1656 rc = ibmvnic_reenable_crq_queue(adapter);
1657 if (rc)
1658 return 0;
1659 }
1660
1661 rc = __ibmvnic_close(netdev);
1662 if (rc)
1663 return rc;
1664
John Allenc26eba02017-10-26 16:23:25 -05001665 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1666 adapter->wait_for_reset) {
1667 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001668 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001669 release_crq_queue(adapter);
1670 }
1671
John Allen8cb31cf2017-05-26 10:30:37 -04001672 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1673 /* remove the closed state so when we call open it appears
1674 * we are coming from the probed state.
1675 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001676 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001677
John Allen8cb31cf2017-05-26 10:30:37 -04001678 rc = ibmvnic_init(adapter);
1679 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001680 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001681
1682 /* If the adapter was in PROBE state prior to the reset,
1683 * exit here.
1684 */
1685 if (reset_state == VNIC_PROBED)
1686 return 0;
1687
1688 rc = ibmvnic_login(netdev);
1689 if (rc) {
1690 adapter->state = VNIC_PROBED;
1691 return 0;
1692 }
1693
John Allenc26eba02017-10-26 16:23:25 -05001694 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1695 adapter->wait_for_reset) {
1696 rc = init_resources(adapter);
1697 if (rc)
1698 return rc;
John Allen896d8692018-01-18 16:26:31 -06001699 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1700 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001701 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001702 release_rx_pools(adapter);
1703 release_tx_pools(adapter);
1704 init_rx_pools(netdev);
1705 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001706
Nathan Fontenot86f669b2018-02-19 13:30:39 -06001707 release_napi(adapter);
1708 init_napi(adapter);
1709
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001710 adapter->num_active_tx_scrqs = adapter->req_tx_queues;
1711 adapter->num_active_rx_scrqs = adapter->req_rx_queues;
John Allenc26eba02017-10-26 16:23:25 -05001712 } else {
1713 rc = reset_tx_pools(adapter);
1714 if (rc)
1715 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001716
John Allenc26eba02017-10-26 16:23:25 -05001717 rc = reset_rx_pools(adapter);
1718 if (rc)
1719 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001720
John Allenc26eba02017-10-26 16:23:25 -05001721 if (reset_state == VNIC_CLOSED)
1722 return 0;
1723 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001724 }
1725
Nathan Fontenoted651a12017-05-03 14:04:38 -04001726 rc = __ibmvnic_open(netdev);
1727 if (rc) {
1728 if (list_empty(&adapter->rwi_list))
1729 adapter->state = VNIC_CLOSED;
1730 else
1731 adapter->state = reset_state;
1732
1733 return 0;
1734 }
1735
Nathan Fontenoted651a12017-05-03 14:04:38 -04001736 /* kick napi */
1737 for (i = 0; i < adapter->req_rx_queues; i++)
1738 napi_schedule(&adapter->napi[i]);
1739
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001740 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1741 netdev_notify_peers(netdev);
1742
Thomas Falconcc85c022018-02-13 15:32:50 -06001743 netif_carrier_on(netdev);
1744
Nathan Fontenoted651a12017-05-03 14:04:38 -04001745 return 0;
1746}
1747
1748static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1749{
1750 struct ibmvnic_rwi *rwi;
1751
1752 mutex_lock(&adapter->rwi_lock);
1753
1754 if (!list_empty(&adapter->rwi_list)) {
1755 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1756 list);
1757 list_del(&rwi->list);
1758 } else {
1759 rwi = NULL;
1760 }
1761
1762 mutex_unlock(&adapter->rwi_lock);
1763 return rwi;
1764}
1765
1766static void free_all_rwi(struct ibmvnic_adapter *adapter)
1767{
1768 struct ibmvnic_rwi *rwi;
1769
1770 rwi = get_next_rwi(adapter);
1771 while (rwi) {
1772 kfree(rwi);
1773 rwi = get_next_rwi(adapter);
1774 }
1775}
1776
1777static void __ibmvnic_reset(struct work_struct *work)
1778{
1779 struct ibmvnic_rwi *rwi;
1780 struct ibmvnic_adapter *adapter;
1781 struct net_device *netdev;
1782 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001783 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001784
1785 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1786 netdev = adapter->netdev;
1787
1788 mutex_lock(&adapter->reset_lock);
1789 adapter->resetting = true;
1790 reset_state = adapter->state;
1791
1792 rwi = get_next_rwi(adapter);
1793 while (rwi) {
1794 rc = do_reset(adapter, rwi, reset_state);
1795 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001796 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001797 break;
1798
1799 rwi = get_next_rwi(adapter);
1800 }
1801
John Allenc26eba02017-10-26 16:23:25 -05001802 if (adapter->wait_for_reset) {
1803 adapter->wait_for_reset = false;
1804 adapter->reset_done_rc = rc;
1805 complete(&adapter->reset_done);
1806 }
1807
Nathan Fontenoted651a12017-05-03 14:04:38 -04001808 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001809 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001810 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001811 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001812 return;
1813 }
1814
1815 adapter->resetting = false;
1816 mutex_unlock(&adapter->reset_lock);
1817}
1818
1819static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1820 enum ibmvnic_reset_reason reason)
1821{
1822 struct ibmvnic_rwi *rwi, *tmp;
1823 struct net_device *netdev = adapter->netdev;
1824 struct list_head *entry;
1825
1826 if (adapter->state == VNIC_REMOVING ||
1827 adapter->state == VNIC_REMOVED) {
1828 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1829 return;
1830 }
1831
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001832 if (adapter->state == VNIC_PROBING) {
1833 netdev_warn(netdev, "Adapter reset during probe\n");
1834 adapter->init_done_rc = EAGAIN;
1835 return;
1836 }
1837
Nathan Fontenoted651a12017-05-03 14:04:38 -04001838 mutex_lock(&adapter->rwi_lock);
1839
1840 list_for_each(entry, &adapter->rwi_list) {
1841 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1842 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001843 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001844 mutex_unlock(&adapter->rwi_lock);
1845 return;
1846 }
1847 }
1848
1849 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1850 if (!rwi) {
1851 mutex_unlock(&adapter->rwi_lock);
1852 ibmvnic_close(netdev);
1853 return;
1854 }
1855
1856 rwi->reset_reason = reason;
1857 list_add_tail(&rwi->list, &adapter->rwi_list);
1858 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001859
1860 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001861 schedule_work(&adapter->ibmvnic_reset);
1862}
1863
Thomas Falcon032c5e82015-12-21 11:26:06 -06001864static void ibmvnic_tx_timeout(struct net_device *dev)
1865{
1866 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867
Nathan Fontenoted651a12017-05-03 14:04:38 -04001868 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001869}
1870
1871static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1872 struct ibmvnic_rx_buff *rx_buff)
1873{
1874 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1875
1876 rx_buff->skb = NULL;
1877
1878 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1879 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1880
1881 atomic_dec(&pool->available);
1882}
1883
1884static int ibmvnic_poll(struct napi_struct *napi, int budget)
1885{
1886 struct net_device *netdev = napi->dev;
1887 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1888 int scrq_num = (int)(napi - adapter->napi);
1889 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001890
Thomas Falcon032c5e82015-12-21 11:26:06 -06001891restart_poll:
1892 while (frames_processed < budget) {
1893 struct sk_buff *skb;
1894 struct ibmvnic_rx_buff *rx_buff;
1895 union sub_crq *next;
1896 u32 length;
1897 u16 offset;
1898 u8 flags = 0;
1899
John Allen34686562018-02-06 16:21:49 -06001900 if (unlikely(adapter->resetting &&
1901 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001902 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1903 napi_complete_done(napi, frames_processed);
1904 return frames_processed;
1905 }
1906
Thomas Falcon032c5e82015-12-21 11:26:06 -06001907 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1908 break;
1909 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1910 rx_buff =
1911 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1912 rx_comp.correlator);
1913 /* do error checking */
1914 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001915 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1916 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001917 /* free the entry */
1918 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001919 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001920 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001921 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001922 }
1923
1924 length = be32_to_cpu(next->rx_comp.len);
1925 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1926 flags = next->rx_comp.flags;
1927 skb = rx_buff->skb;
1928 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1929 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001930
1931 /* VLAN Header has been stripped by the system firmware and
1932 * needs to be inserted by the driver
1933 */
1934 if (adapter->rx_vlan_header_insertion &&
1935 (flags & IBMVNIC_VLAN_STRIPPED))
1936 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1937 ntohs(next->rx_comp.vlan_tci));
1938
Thomas Falcon032c5e82015-12-21 11:26:06 -06001939 /* free the entry */
1940 next->rx_comp.first = 0;
1941 remove_buff_from_pool(adapter, rx_buff);
1942
1943 skb_put(skb, length);
1944 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001945 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001946
1947 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1948 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1949 skb->ip_summed = CHECKSUM_UNNECESSARY;
1950 }
1951
1952 length = skb->len;
1953 napi_gro_receive(napi, skb); /* send it up */
1954 netdev->stats.rx_packets++;
1955 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001956 adapter->rx_stats_buffers[scrq_num].packets++;
1957 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001958 frames_processed++;
1959 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001960
1961 if (adapter->state != VNIC_CLOSING)
1962 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001963
1964 if (frames_processed < budget) {
1965 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001966 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001967 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1968 napi_reschedule(napi)) {
1969 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1970 goto restart_poll;
1971 }
1972 }
1973 return frames_processed;
1974}
1975
1976#ifdef CONFIG_NET_POLL_CONTROLLER
1977static void ibmvnic_netpoll_controller(struct net_device *dev)
1978{
1979 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1980 int i;
1981
1982 replenish_pools(netdev_priv(dev));
1983 for (i = 0; i < adapter->req_rx_queues; i++)
1984 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1985 adapter->rx_scrq[i]);
1986}
1987#endif
1988
John Allenc26eba02017-10-26 16:23:25 -05001989static int wait_for_reset(struct ibmvnic_adapter *adapter)
1990{
1991 adapter->fallback.mtu = adapter->req_mtu;
1992 adapter->fallback.rx_queues = adapter->req_rx_queues;
1993 adapter->fallback.tx_queues = adapter->req_tx_queues;
1994 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1995 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1996
1997 init_completion(&adapter->reset_done);
1998 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1999 adapter->wait_for_reset = true;
2000 wait_for_completion(&adapter->reset_done);
2001
2002 if (adapter->reset_done_rc) {
2003 adapter->desired.mtu = adapter->fallback.mtu;
2004 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2005 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2006 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2007 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2008
2009 init_completion(&adapter->reset_done);
2010 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2011 wait_for_completion(&adapter->reset_done);
2012 }
2013 adapter->wait_for_reset = false;
2014
2015 return adapter->reset_done_rc;
2016}
2017
John Allen3a807b72017-06-06 16:55:52 -05002018static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2019{
John Allenc26eba02017-10-26 16:23:25 -05002020 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2021
2022 adapter->desired.mtu = new_mtu + ETH_HLEN;
2023
2024 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002025}
2026
Thomas Falcon032c5e82015-12-21 11:26:06 -06002027static const struct net_device_ops ibmvnic_netdev_ops = {
2028 .ndo_open = ibmvnic_open,
2029 .ndo_stop = ibmvnic_close,
2030 .ndo_start_xmit = ibmvnic_xmit,
2031 .ndo_set_rx_mode = ibmvnic_set_multi,
2032 .ndo_set_mac_address = ibmvnic_set_mac,
2033 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002034 .ndo_tx_timeout = ibmvnic_tx_timeout,
2035#ifdef CONFIG_NET_POLL_CONTROLLER
2036 .ndo_poll_controller = ibmvnic_netpoll_controller,
2037#endif
John Allen3a807b72017-06-06 16:55:52 -05002038 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002039};
2040
2041/* ethtool functions */
2042
Philippe Reynes8a433792017-01-07 22:37:29 +01002043static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2044 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002045{
Philippe Reynes8a433792017-01-07 22:37:29 +01002046 u32 supported, advertising;
2047
2048 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002049 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002050 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002051 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002052 cmd->base.speed = SPEED_1000;
2053 cmd->base.duplex = DUPLEX_FULL;
2054 cmd->base.port = PORT_FIBRE;
2055 cmd->base.phy_address = 0;
2056 cmd->base.autoneg = AUTONEG_ENABLE;
2057
2058 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2059 supported);
2060 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2061 advertising);
2062
Thomas Falcon032c5e82015-12-21 11:26:06 -06002063 return 0;
2064}
2065
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002066static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002067 struct ethtool_drvinfo *info)
2068{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002069 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2070
Thomas Falcon032c5e82015-12-21 11:26:06 -06002071 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2072 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002073 strlcpy(info->fw_version, adapter->fw_version,
2074 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002075}
2076
2077static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2078{
2079 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2080
2081 return adapter->msg_enable;
2082}
2083
2084static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2085{
2086 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2087
2088 adapter->msg_enable = data;
2089}
2090
2091static u32 ibmvnic_get_link(struct net_device *netdev)
2092{
2093 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2094
2095 /* Don't need to send a query because we request a logical link up at
2096 * init and then we wait for link state indications
2097 */
2098 return adapter->logical_link_state;
2099}
2100
2101static void ibmvnic_get_ringparam(struct net_device *netdev,
2102 struct ethtool_ringparam *ring)
2103{
John Allenbc131b32017-08-02 16:46:30 -05002104 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2105
2106 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2107 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002108 ring->rx_mini_max_pending = 0;
2109 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002110 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2111 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002112 ring->rx_mini_pending = 0;
2113 ring->rx_jumbo_pending = 0;
2114}
2115
John Allenc26eba02017-10-26 16:23:25 -05002116static int ibmvnic_set_ringparam(struct net_device *netdev,
2117 struct ethtool_ringparam *ring)
2118{
2119 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2120
2121 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2122 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2123 netdev_err(netdev, "Invalid request.\n");
2124 netdev_err(netdev, "Max tx buffers = %llu\n",
2125 adapter->max_rx_add_entries_per_subcrq);
2126 netdev_err(netdev, "Max rx buffers = %llu\n",
2127 adapter->max_tx_entries_per_subcrq);
2128 return -EINVAL;
2129 }
2130
2131 adapter->desired.rx_entries = ring->rx_pending;
2132 adapter->desired.tx_entries = ring->tx_pending;
2133
2134 return wait_for_reset(adapter);
2135}
2136
John Allenc2dbeb62017-08-02 16:47:17 -05002137static void ibmvnic_get_channels(struct net_device *netdev,
2138 struct ethtool_channels *channels)
2139{
2140 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2141
2142 channels->max_rx = adapter->max_rx_queues;
2143 channels->max_tx = adapter->max_tx_queues;
2144 channels->max_other = 0;
2145 channels->max_combined = 0;
2146 channels->rx_count = adapter->req_rx_queues;
2147 channels->tx_count = adapter->req_tx_queues;
2148 channels->other_count = 0;
2149 channels->combined_count = 0;
2150}
2151
John Allenc26eba02017-10-26 16:23:25 -05002152static int ibmvnic_set_channels(struct net_device *netdev,
2153 struct ethtool_channels *channels)
2154{
2155 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2156
2157 adapter->desired.rx_queues = channels->rx_count;
2158 adapter->desired.tx_queues = channels->tx_count;
2159
2160 return wait_for_reset(adapter);
2161}
2162
Thomas Falcon032c5e82015-12-21 11:26:06 -06002163static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2164{
John Allen3d52b592017-08-02 16:44:14 -05002165 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002166 int i;
2167
2168 if (stringset != ETH_SS_STATS)
2169 return;
2170
2171 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2172 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002173
2174 for (i = 0; i < adapter->req_tx_queues; i++) {
2175 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2176 data += ETH_GSTRING_LEN;
2177
2178 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2179 data += ETH_GSTRING_LEN;
2180
2181 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2182 data += ETH_GSTRING_LEN;
2183 }
2184
2185 for (i = 0; i < adapter->req_rx_queues; i++) {
2186 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2187 data += ETH_GSTRING_LEN;
2188
2189 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2190 data += ETH_GSTRING_LEN;
2191
2192 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2193 data += ETH_GSTRING_LEN;
2194 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002195}
2196
2197static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2198{
John Allen3d52b592017-08-02 16:44:14 -05002199 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2200
Thomas Falcon032c5e82015-12-21 11:26:06 -06002201 switch (sset) {
2202 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002203 return ARRAY_SIZE(ibmvnic_stats) +
2204 adapter->req_tx_queues * NUM_TX_STATS +
2205 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002206 default:
2207 return -EOPNOTSUPP;
2208 }
2209}
2210
2211static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2212 struct ethtool_stats *stats, u64 *data)
2213{
2214 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2215 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002216 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002217
2218 memset(&crq, 0, sizeof(crq));
2219 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2220 crq.request_statistics.cmd = REQUEST_STATISTICS;
2221 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2222 crq.request_statistics.len =
2223 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002224
2225 /* Wait for data to be written */
2226 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002227 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002228 wait_for_completion(&adapter->stats_done);
2229
2230 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002231 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2232 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002233
2234 for (j = 0; j < adapter->req_tx_queues; j++) {
2235 data[i] = adapter->tx_stats_buffers[j].packets;
2236 i++;
2237 data[i] = adapter->tx_stats_buffers[j].bytes;
2238 i++;
2239 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2240 i++;
2241 }
2242
2243 for (j = 0; j < adapter->req_rx_queues; j++) {
2244 data[i] = adapter->rx_stats_buffers[j].packets;
2245 i++;
2246 data[i] = adapter->rx_stats_buffers[j].bytes;
2247 i++;
2248 data[i] = adapter->rx_stats_buffers[j].interrupts;
2249 i++;
2250 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002251}
2252
2253static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002254 .get_drvinfo = ibmvnic_get_drvinfo,
2255 .get_msglevel = ibmvnic_get_msglevel,
2256 .set_msglevel = ibmvnic_set_msglevel,
2257 .get_link = ibmvnic_get_link,
2258 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002259 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002260 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002261 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002262 .get_strings = ibmvnic_get_strings,
2263 .get_sset_count = ibmvnic_get_sset_count,
2264 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002265 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002266};
2267
2268/* Routines for managing CRQs/sCRQs */
2269
Nathan Fontenot57a49432017-05-26 10:31:12 -04002270static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2271 struct ibmvnic_sub_crq_queue *scrq)
2272{
2273 int rc;
2274
2275 if (scrq->irq) {
2276 free_irq(scrq->irq, scrq);
2277 irq_dispose_mapping(scrq->irq);
2278 scrq->irq = 0;
2279 }
2280
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002281 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002282 scrq->cur = 0;
2283
2284 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2285 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2286 return rc;
2287}
2288
2289static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2290{
2291 int i, rc;
2292
2293 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002294 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002295 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2296 if (rc)
2297 return rc;
2298 }
2299
2300 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002301 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002302 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2303 if (rc)
2304 return rc;
2305 }
2306
Nathan Fontenot57a49432017-05-26 10:31:12 -04002307 return rc;
2308}
2309
Thomas Falcon032c5e82015-12-21 11:26:06 -06002310static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002311 struct ibmvnic_sub_crq_queue *scrq,
2312 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002313{
2314 struct device *dev = &adapter->vdev->dev;
2315 long rc;
2316
2317 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2318
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002319 if (do_h_free) {
2320 /* Close the sub-crqs */
2321 do {
2322 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2323 adapter->vdev->unit_address,
2324 scrq->crq_num);
2325 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002326
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002327 if (rc) {
2328 netdev_err(adapter->netdev,
2329 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2330 scrq->crq_num, rc);
2331 }
Thomas Falconffa73852017-04-19 13:44:29 -04002332 }
2333
Thomas Falcon032c5e82015-12-21 11:26:06 -06002334 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2335 DMA_BIDIRECTIONAL);
2336 free_pages((unsigned long)scrq->msgs, 2);
2337 kfree(scrq);
2338}
2339
2340static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2341 *adapter)
2342{
2343 struct device *dev = &adapter->vdev->dev;
2344 struct ibmvnic_sub_crq_queue *scrq;
2345 int rc;
2346
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002347 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002348 if (!scrq)
2349 return NULL;
2350
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002351 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002352 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002353 if (!scrq->msgs) {
2354 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2355 goto zero_page_failed;
2356 }
2357
2358 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2359 DMA_BIDIRECTIONAL);
2360 if (dma_mapping_error(dev, scrq->msg_token)) {
2361 dev_warn(dev, "Couldn't map crq queue messages page\n");
2362 goto map_failed;
2363 }
2364
2365 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2366 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2367
2368 if (rc == H_RESOURCE)
2369 rc = ibmvnic_reset_crq(adapter);
2370
2371 if (rc == H_CLOSED) {
2372 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2373 } else if (rc) {
2374 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2375 goto reg_failed;
2376 }
2377
Thomas Falcon032c5e82015-12-21 11:26:06 -06002378 scrq->adapter = adapter;
2379 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002380 spin_lock_init(&scrq->lock);
2381
2382 netdev_dbg(adapter->netdev,
2383 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2384 scrq->crq_num, scrq->hw_irq, scrq->irq);
2385
2386 return scrq;
2387
Thomas Falcon032c5e82015-12-21 11:26:06 -06002388reg_failed:
2389 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2390 DMA_BIDIRECTIONAL);
2391map_failed:
2392 free_pages((unsigned long)scrq->msgs, 2);
2393zero_page_failed:
2394 kfree(scrq);
2395
2396 return NULL;
2397}
2398
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002399static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002400{
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002401 u64 num_tx_scrqs, num_rx_scrqs;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002402 int i;
2403
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002404 if (adapter->state == VNIC_PROBED) {
2405 num_tx_scrqs = adapter->req_tx_queues;
2406 num_rx_scrqs = adapter->req_rx_queues;
2407 } else {
2408 num_tx_scrqs = adapter->num_active_tx_scrqs;
2409 num_rx_scrqs = adapter->num_active_rx_scrqs;
2410 }
2411
Thomas Falcon032c5e82015-12-21 11:26:06 -06002412 if (adapter->tx_scrq) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002413 for (i = 0; i < num_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002414 if (!adapter->tx_scrq[i])
2415 continue;
2416
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002417 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2418 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002419 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002420 free_irq(adapter->tx_scrq[i]->irq,
2421 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002422 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002423 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002424 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002425
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002426 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2427 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002428 }
2429
Nathan Fontenot9501df32017-03-15 23:38:07 -04002430 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002431 adapter->tx_scrq = NULL;
2432 }
2433
2434 if (adapter->rx_scrq) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002435 for (i = 0; i < num_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002436 if (!adapter->rx_scrq[i])
2437 continue;
2438
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002439 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2440 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002441 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002442 free_irq(adapter->rx_scrq[i]->irq,
2443 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002444 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002445 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002447
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002448 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2449 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002450 }
2451
Nathan Fontenot9501df32017-03-15 23:38:07 -04002452 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002453 adapter->rx_scrq = NULL;
2454 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002455}
2456
2457static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2458 struct ibmvnic_sub_crq_queue *scrq)
2459{
2460 struct device *dev = &adapter->vdev->dev;
2461 unsigned long rc;
2462
2463 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2464 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2465 if (rc)
2466 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2467 scrq->hw_irq, rc);
2468 return rc;
2469}
2470
2471static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2472 struct ibmvnic_sub_crq_queue *scrq)
2473{
2474 struct device *dev = &adapter->vdev->dev;
2475 unsigned long rc;
2476
2477 if (scrq->hw_irq > 0x100000000ULL) {
2478 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2479 return 1;
2480 }
2481
2482 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2483 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2484 if (rc)
2485 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2486 scrq->hw_irq, rc);
2487 return rc;
2488}
2489
2490static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2491 struct ibmvnic_sub_crq_queue *scrq)
2492{
2493 struct device *dev = &adapter->vdev->dev;
2494 struct ibmvnic_tx_buff *txbuff;
2495 union sub_crq *next;
2496 int index;
2497 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002498 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002499
2500restart_loop:
2501 while (pending_scrq(adapter, scrq)) {
2502 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002503 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002504
2505 next = ibmvnic_next_scrq(adapter, scrq);
2506 for (i = 0; i < next->tx_comp.num_comps; i++) {
2507 if (next->tx_comp.rcs[i]) {
2508 dev_err(dev, "tx error %x\n",
2509 next->tx_comp.rcs[i]);
2510 continue;
2511 }
2512 index = be32_to_cpu(next->tx_comp.correlators[i]);
2513 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2514
2515 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2516 if (!txbuff->data_dma[j])
2517 continue;
2518
2519 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002521 /* if sub_crq was sent indirectly */
2522 first = txbuff->indir_arr[0].generic.first;
2523 if (first == IBMVNIC_CRQ_CMD) {
2524 dma_unmap_single(dev, txbuff->indir_dma,
2525 sizeof(txbuff->indir_arr),
2526 DMA_TO_DEVICE);
2527 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002528
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002529 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002530 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002531 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002532 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533
Thomas Falconffc385b2018-02-18 10:08:41 -06002534 num_entries += txbuff->num_entries;
2535
Thomas Falcon032c5e82015-12-21 11:26:06 -06002536 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2537 producer_index] = index;
2538 adapter->tx_pool[pool].producer_index =
2539 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002540 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002541 }
2542 /* remove tx_comp scrq*/
2543 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002544
Thomas Falconffc385b2018-02-18 10:08:41 -06002545 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002546 (adapter->req_tx_entries_per_subcrq / 2) &&
2547 __netif_subqueue_stopped(adapter->netdev,
2548 scrq->pool_index)) {
2549 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2550 netdev_info(adapter->netdev, "Started queue %d\n",
2551 scrq->pool_index);
2552 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002553 }
2554
2555 enable_scrq_irq(adapter, scrq);
2556
2557 if (pending_scrq(adapter, scrq)) {
2558 disable_scrq_irq(adapter, scrq);
2559 goto restart_loop;
2560 }
2561
2562 return 0;
2563}
2564
2565static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2566{
2567 struct ibmvnic_sub_crq_queue *scrq = instance;
2568 struct ibmvnic_adapter *adapter = scrq->adapter;
2569
2570 disable_scrq_irq(adapter, scrq);
2571 ibmvnic_complete_tx(adapter, scrq);
2572
2573 return IRQ_HANDLED;
2574}
2575
2576static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2577{
2578 struct ibmvnic_sub_crq_queue *scrq = instance;
2579 struct ibmvnic_adapter *adapter = scrq->adapter;
2580
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002581 /* When booting a kdump kernel we can hit pending interrupts
2582 * prior to completing driver initialization.
2583 */
2584 if (unlikely(adapter->state != VNIC_OPEN))
2585 return IRQ_NONE;
2586
John Allen3d52b592017-08-02 16:44:14 -05002587 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2588
Thomas Falcon032c5e82015-12-21 11:26:06 -06002589 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2590 disable_scrq_irq(adapter, scrq);
2591 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2592 }
2593
2594 return IRQ_HANDLED;
2595}
2596
Thomas Falconea22d512016-07-06 15:35:17 -05002597static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2598{
2599 struct device *dev = &adapter->vdev->dev;
2600 struct ibmvnic_sub_crq_queue *scrq;
2601 int i = 0, j = 0;
2602 int rc = 0;
2603
2604 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002605 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2606 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002607 scrq = adapter->tx_scrq[i];
2608 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2609
Michael Ellerman99c17902016-09-10 19:59:05 +10002610 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002611 rc = -EINVAL;
2612 dev_err(dev, "Error mapping irq\n");
2613 goto req_tx_irq_failed;
2614 }
2615
2616 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2617 0, "ibmvnic_tx", scrq);
2618
2619 if (rc) {
2620 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2621 scrq->irq, rc);
2622 irq_dispose_mapping(scrq->irq);
2623 goto req_rx_irq_failed;
2624 }
2625 }
2626
2627 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002628 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2629 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002630 scrq = adapter->rx_scrq[i];
2631 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002632 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002633 rc = -EINVAL;
2634 dev_err(dev, "Error mapping irq\n");
2635 goto req_rx_irq_failed;
2636 }
2637 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2638 0, "ibmvnic_rx", scrq);
2639 if (rc) {
2640 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2641 scrq->irq, rc);
2642 irq_dispose_mapping(scrq->irq);
2643 goto req_rx_irq_failed;
2644 }
2645 }
2646 return rc;
2647
2648req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002649 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002650 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2651 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002652 }
Thomas Falconea22d512016-07-06 15:35:17 -05002653 i = adapter->req_tx_queues;
2654req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002655 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002656 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2657 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002658 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002659 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002660 return rc;
2661}
2662
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002663static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002664{
2665 struct device *dev = &adapter->vdev->dev;
2666 struct ibmvnic_sub_crq_queue **allqueues;
2667 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002668 int total_queues;
2669 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002670 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002671
Thomas Falcon032c5e82015-12-21 11:26:06 -06002672 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2673
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002674 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002675 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002676 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002677
2678 for (i = 0; i < total_queues; i++) {
2679 allqueues[i] = init_sub_crq_queue(adapter);
2680 if (!allqueues[i]) {
2681 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2682 break;
2683 }
2684 registered_queues++;
2685 }
2686
2687 /* Make sure we were able to register the minimum number of queues */
2688 if (registered_queues <
2689 adapter->min_tx_queues + adapter->min_rx_queues) {
2690 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2691 goto tx_failed;
2692 }
2693
2694 /* Distribute the failed allocated queues*/
2695 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2696 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2697 switch (i % 3) {
2698 case 0:
2699 if (adapter->req_rx_queues > adapter->min_rx_queues)
2700 adapter->req_rx_queues--;
2701 else
2702 more++;
2703 break;
2704 case 1:
2705 if (adapter->req_tx_queues > adapter->min_tx_queues)
2706 adapter->req_tx_queues--;
2707 else
2708 more++;
2709 break;
2710 }
2711 }
2712
2713 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002714 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715 if (!adapter->tx_scrq)
2716 goto tx_failed;
2717
2718 for (i = 0; i < adapter->req_tx_queues; i++) {
2719 adapter->tx_scrq[i] = allqueues[i];
2720 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002721 }
2722
2723 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002724 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 if (!adapter->rx_scrq)
2726 goto rx_failed;
2727
2728 for (i = 0; i < adapter->req_rx_queues; i++) {
2729 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2730 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 }
2732
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002733 kfree(allqueues);
2734 return 0;
2735
2736rx_failed:
2737 kfree(adapter->tx_scrq);
2738 adapter->tx_scrq = NULL;
2739tx_failed:
2740 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002741 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002742 kfree(allqueues);
2743 return -1;
2744}
2745
2746static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2747{
2748 struct device *dev = &adapter->vdev->dev;
2749 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002750 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002751
2752 if (!retry) {
2753 /* Sub-CRQ entries are 32 byte long */
2754 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2755
2756 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2757 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2758 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2759 return;
2760 }
2761
John Allenc26eba02017-10-26 16:23:25 -05002762 if (adapter->desired.mtu)
2763 adapter->req_mtu = adapter->desired.mtu;
2764 else
2765 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002766
John Allenc26eba02017-10-26 16:23:25 -05002767 if (!adapter->desired.tx_entries)
2768 adapter->desired.tx_entries =
2769 adapter->max_tx_entries_per_subcrq;
2770 if (!adapter->desired.rx_entries)
2771 adapter->desired.rx_entries =
2772 adapter->max_rx_add_entries_per_subcrq;
2773
2774 max_entries = IBMVNIC_MAX_LTB_SIZE /
2775 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2776
2777 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2778 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2779 adapter->desired.tx_entries = max_entries;
2780 }
2781
2782 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2783 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2784 adapter->desired.rx_entries = max_entries;
2785 }
2786
2787 if (adapter->desired.tx_entries)
2788 adapter->req_tx_entries_per_subcrq =
2789 adapter->desired.tx_entries;
2790 else
2791 adapter->req_tx_entries_per_subcrq =
2792 adapter->max_tx_entries_per_subcrq;
2793
2794 if (adapter->desired.rx_entries)
2795 adapter->req_rx_add_entries_per_subcrq =
2796 adapter->desired.rx_entries;
2797 else
2798 adapter->req_rx_add_entries_per_subcrq =
2799 adapter->max_rx_add_entries_per_subcrq;
2800
2801 if (adapter->desired.tx_queues)
2802 adapter->req_tx_queues =
2803 adapter->desired.tx_queues;
2804 else
2805 adapter->req_tx_queues =
2806 adapter->opt_tx_comp_sub_queues;
2807
2808 if (adapter->desired.rx_queues)
2809 adapter->req_rx_queues =
2810 adapter->desired.rx_queues;
2811 else
2812 adapter->req_rx_queues =
2813 adapter->opt_rx_comp_queues;
2814
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002815 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002816 }
2817
Thomas Falcon032c5e82015-12-21 11:26:06 -06002818 memset(&crq, 0, sizeof(crq));
2819 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2820 crq.request_capability.cmd = REQUEST_CAPABILITY;
2821
2822 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002823 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002824 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002825 ibmvnic_send_crq(adapter, &crq);
2826
2827 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002828 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002829 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002830 ibmvnic_send_crq(adapter, &crq);
2831
2832 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002833 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002834 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002835 ibmvnic_send_crq(adapter, &crq);
2836
2837 crq.request_capability.capability =
2838 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2839 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002840 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002841 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002842 ibmvnic_send_crq(adapter, &crq);
2843
2844 crq.request_capability.capability =
2845 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2846 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002847 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002848 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002849 ibmvnic_send_crq(adapter, &crq);
2850
2851 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002852 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002853 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002854 ibmvnic_send_crq(adapter, &crq);
2855
2856 if (adapter->netdev->flags & IFF_PROMISC) {
2857 if (adapter->promisc_supported) {
2858 crq.request_capability.capability =
2859 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002860 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002861 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002862 ibmvnic_send_crq(adapter, &crq);
2863 }
2864 } else {
2865 crq.request_capability.capability =
2866 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002867 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002868 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869 ibmvnic_send_crq(adapter, &crq);
2870 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002871}
2872
2873static int pending_scrq(struct ibmvnic_adapter *adapter,
2874 struct ibmvnic_sub_crq_queue *scrq)
2875{
2876 union sub_crq *entry = &scrq->msgs[scrq->cur];
2877
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002878 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002879 return 1;
2880 else
2881 return 0;
2882}
2883
2884static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2885 struct ibmvnic_sub_crq_queue *scrq)
2886{
2887 union sub_crq *entry;
2888 unsigned long flags;
2889
2890 spin_lock_irqsave(&scrq->lock, flags);
2891 entry = &scrq->msgs[scrq->cur];
2892 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2893 if (++scrq->cur == scrq->size)
2894 scrq->cur = 0;
2895 } else {
2896 entry = NULL;
2897 }
2898 spin_unlock_irqrestore(&scrq->lock, flags);
2899
2900 return entry;
2901}
2902
2903static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2904{
2905 struct ibmvnic_crq_queue *queue = &adapter->crq;
2906 union ibmvnic_crq *crq;
2907
2908 crq = &queue->msgs[queue->cur];
2909 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2910 if (++queue->cur == queue->size)
2911 queue->cur = 0;
2912 } else {
2913 crq = NULL;
2914 }
2915
2916 return crq;
2917}
2918
2919static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2920 union sub_crq *sub_crq)
2921{
2922 unsigned int ua = adapter->vdev->unit_address;
2923 struct device *dev = &adapter->vdev->dev;
2924 u64 *u64_crq = (u64 *)sub_crq;
2925 int rc;
2926
2927 netdev_dbg(adapter->netdev,
2928 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2929 (unsigned long int)cpu_to_be64(remote_handle),
2930 (unsigned long int)cpu_to_be64(u64_crq[0]),
2931 (unsigned long int)cpu_to_be64(u64_crq[1]),
2932 (unsigned long int)cpu_to_be64(u64_crq[2]),
2933 (unsigned long int)cpu_to_be64(u64_crq[3]));
2934
2935 /* Make sure the hypervisor sees the complete request */
2936 mb();
2937
2938 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2939 cpu_to_be64(remote_handle),
2940 cpu_to_be64(u64_crq[0]),
2941 cpu_to_be64(u64_crq[1]),
2942 cpu_to_be64(u64_crq[2]),
2943 cpu_to_be64(u64_crq[3]));
2944
2945 if (rc) {
2946 if (rc == H_CLOSED)
2947 dev_warn(dev, "CRQ Queue closed\n");
2948 dev_err(dev, "Send error (rc=%d)\n", rc);
2949 }
2950
2951 return rc;
2952}
2953
Thomas Falconad7775d2016-04-01 17:20:34 -05002954static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2955 u64 remote_handle, u64 ioba, u64 num_entries)
2956{
2957 unsigned int ua = adapter->vdev->unit_address;
2958 struct device *dev = &adapter->vdev->dev;
2959 int rc;
2960
2961 /* Make sure the hypervisor sees the complete request */
2962 mb();
2963 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2964 cpu_to_be64(remote_handle),
2965 ioba, num_entries);
2966
2967 if (rc) {
2968 if (rc == H_CLOSED)
2969 dev_warn(dev, "CRQ Queue closed\n");
2970 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2971 }
2972
2973 return rc;
2974}
2975
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2977 union ibmvnic_crq *crq)
2978{
2979 unsigned int ua = adapter->vdev->unit_address;
2980 struct device *dev = &adapter->vdev->dev;
2981 u64 *u64_crq = (u64 *)crq;
2982 int rc;
2983
2984 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2985 (unsigned long int)cpu_to_be64(u64_crq[0]),
2986 (unsigned long int)cpu_to_be64(u64_crq[1]));
2987
2988 /* Make sure the hypervisor sees the complete request */
2989 mb();
2990
2991 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2992 cpu_to_be64(u64_crq[0]),
2993 cpu_to_be64(u64_crq[1]));
2994
2995 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002996 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002997 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002998 if (adapter->resetting)
2999 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3000 }
3001
Thomas Falcon032c5e82015-12-21 11:26:06 -06003002 dev_warn(dev, "Send error (rc=%d)\n", rc);
3003 }
3004
3005 return rc;
3006}
3007
3008static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3009{
3010 union ibmvnic_crq crq;
3011
3012 memset(&crq, 0, sizeof(crq));
3013 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3014 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3015 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3016
3017 return ibmvnic_send_crq(adapter, &crq);
3018}
3019
Thomas Falcon032c5e82015-12-21 11:26:06 -06003020static int send_version_xchg(struct ibmvnic_adapter *adapter)
3021{
3022 union ibmvnic_crq crq;
3023
3024 memset(&crq, 0, sizeof(crq));
3025 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3026 crq.version_exchange.cmd = VERSION_EXCHANGE;
3027 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3028
3029 return ibmvnic_send_crq(adapter, &crq);
3030}
3031
Nathan Fontenot37798d02017-11-08 11:23:56 -06003032struct vnic_login_client_data {
3033 u8 type;
3034 __be16 len;
3035 char name;
3036} __packed;
3037
3038static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3039{
3040 int len;
3041
3042 /* Calculate the amount of buffer space needed for the
3043 * vnic client data in the login buffer. There are four entries,
3044 * OS name, LPAR name, device name, and a null last entry.
3045 */
3046 len = 4 * sizeof(struct vnic_login_client_data);
3047 len += 6; /* "Linux" plus NULL */
3048 len += strlen(utsname()->nodename) + 1;
3049 len += strlen(adapter->netdev->name) + 1;
3050
3051 return len;
3052}
3053
3054static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3055 struct vnic_login_client_data *vlcd)
3056{
3057 const char *os_name = "Linux";
3058 int len;
3059
3060 /* Type 1 - LPAR OS */
3061 vlcd->type = 1;
3062 len = strlen(os_name) + 1;
3063 vlcd->len = cpu_to_be16(len);
3064 strncpy(&vlcd->name, os_name, len);
3065 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3066
3067 /* Type 2 - LPAR name */
3068 vlcd->type = 2;
3069 len = strlen(utsname()->nodename) + 1;
3070 vlcd->len = cpu_to_be16(len);
3071 strncpy(&vlcd->name, utsname()->nodename, len);
3072 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3073
3074 /* Type 3 - device name */
3075 vlcd->type = 3;
3076 len = strlen(adapter->netdev->name) + 1;
3077 vlcd->len = cpu_to_be16(len);
3078 strncpy(&vlcd->name, adapter->netdev->name, len);
3079}
3080
Thomas Falcon032c5e82015-12-21 11:26:06 -06003081static void send_login(struct ibmvnic_adapter *adapter)
3082{
3083 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3084 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003085 struct device *dev = &adapter->vdev->dev;
3086 dma_addr_t rsp_buffer_token;
3087 dma_addr_t buffer_token;
3088 size_t rsp_buffer_size;
3089 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003090 size_t buffer_size;
3091 __be64 *tx_list_p;
3092 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003093 int client_data_len;
3094 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003095 int i;
3096
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003097 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003098 client_data_len = vnic_client_data_len(adapter);
3099
Thomas Falcon032c5e82015-12-21 11:26:06 -06003100 buffer_size =
3101 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003102 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3103 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003104
Nathan Fontenot37798d02017-11-08 11:23:56 -06003105 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003106 if (!login_buffer)
3107 goto buf_alloc_failed;
3108
3109 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3110 DMA_TO_DEVICE);
3111 if (dma_mapping_error(dev, buffer_token)) {
3112 dev_err(dev, "Couldn't map login buffer\n");
3113 goto buf_map_failed;
3114 }
3115
John Allen498cd8e2016-04-06 11:49:55 -05003116 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3117 sizeof(u64) * adapter->req_tx_queues +
3118 sizeof(u64) * adapter->req_rx_queues +
3119 sizeof(u64) * adapter->req_rx_queues +
3120 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003121
3122 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3123 if (!login_rsp_buffer)
3124 goto buf_rsp_alloc_failed;
3125
3126 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3127 rsp_buffer_size, DMA_FROM_DEVICE);
3128 if (dma_mapping_error(dev, rsp_buffer_token)) {
3129 dev_err(dev, "Couldn't map login rsp buffer\n");
3130 goto buf_rsp_map_failed;
3131 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003132
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 adapter->login_buf = login_buffer;
3134 adapter->login_buf_token = buffer_token;
3135 adapter->login_buf_sz = buffer_size;
3136 adapter->login_rsp_buf = login_rsp_buffer;
3137 adapter->login_rsp_buf_token = rsp_buffer_token;
3138 adapter->login_rsp_buf_sz = rsp_buffer_size;
3139
3140 login_buffer->len = cpu_to_be32(buffer_size);
3141 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3142 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3143 login_buffer->off_txcomp_subcrqs =
3144 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3145 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3146 login_buffer->off_rxcomp_subcrqs =
3147 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3148 sizeof(u64) * adapter->req_tx_queues);
3149 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3150 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3151
3152 tx_list_p = (__be64 *)((char *)login_buffer +
3153 sizeof(struct ibmvnic_login_buffer));
3154 rx_list_p = (__be64 *)((char *)login_buffer +
3155 sizeof(struct ibmvnic_login_buffer) +
3156 sizeof(u64) * adapter->req_tx_queues);
3157
3158 for (i = 0; i < adapter->req_tx_queues; i++) {
3159 if (adapter->tx_scrq[i]) {
3160 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3161 crq_num);
3162 }
3163 }
3164
3165 for (i = 0; i < adapter->req_rx_queues; i++) {
3166 if (adapter->rx_scrq[i]) {
3167 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3168 crq_num);
3169 }
3170 }
3171
Nathan Fontenot37798d02017-11-08 11:23:56 -06003172 /* Insert vNIC login client data */
3173 vlcd = (struct vnic_login_client_data *)
3174 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3175 login_buffer->client_data_offset =
3176 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3177 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3178
3179 vnic_add_client_data(adapter, vlcd);
3180
Thomas Falcon032c5e82015-12-21 11:26:06 -06003181 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3182 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3183 netdev_dbg(adapter->netdev, "%016lx\n",
3184 ((unsigned long int *)(adapter->login_buf))[i]);
3185 }
3186
3187 memset(&crq, 0, sizeof(crq));
3188 crq.login.first = IBMVNIC_CRQ_CMD;
3189 crq.login.cmd = LOGIN;
3190 crq.login.ioba = cpu_to_be32(buffer_token);
3191 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003192 ibmvnic_send_crq(adapter, &crq);
3193
3194 return;
3195
Thomas Falcon032c5e82015-12-21 11:26:06 -06003196buf_rsp_map_failed:
3197 kfree(login_rsp_buffer);
3198buf_rsp_alloc_failed:
3199 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3200buf_map_failed:
3201 kfree(login_buffer);
3202buf_alloc_failed:
3203 return;
3204}
3205
3206static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3207 u32 len, u8 map_id)
3208{
3209 union ibmvnic_crq crq;
3210
3211 memset(&crq, 0, sizeof(crq));
3212 crq.request_map.first = IBMVNIC_CRQ_CMD;
3213 crq.request_map.cmd = REQUEST_MAP;
3214 crq.request_map.map_id = map_id;
3215 crq.request_map.ioba = cpu_to_be32(addr);
3216 crq.request_map.len = cpu_to_be32(len);
3217 ibmvnic_send_crq(adapter, &crq);
3218}
3219
3220static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3221{
3222 union ibmvnic_crq crq;
3223
3224 memset(&crq, 0, sizeof(crq));
3225 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3226 crq.request_unmap.cmd = REQUEST_UNMAP;
3227 crq.request_unmap.map_id = map_id;
3228 ibmvnic_send_crq(adapter, &crq);
3229}
3230
3231static void send_map_query(struct ibmvnic_adapter *adapter)
3232{
3233 union ibmvnic_crq crq;
3234
3235 memset(&crq, 0, sizeof(crq));
3236 crq.query_map.first = IBMVNIC_CRQ_CMD;
3237 crq.query_map.cmd = QUERY_MAP;
3238 ibmvnic_send_crq(adapter, &crq);
3239}
3240
3241/* Send a series of CRQs requesting various capabilities of the VNIC server */
3242static void send_cap_queries(struct ibmvnic_adapter *adapter)
3243{
3244 union ibmvnic_crq crq;
3245
Thomas Falcon901e0402017-02-15 12:17:59 -06003246 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003247 memset(&crq, 0, sizeof(crq));
3248 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3249 crq.query_capability.cmd = QUERY_CAPABILITY;
3250
3251 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003252 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003253 ibmvnic_send_crq(adapter, &crq);
3254
3255 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003256 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003257 ibmvnic_send_crq(adapter, &crq);
3258
3259 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003260 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003261 ibmvnic_send_crq(adapter, &crq);
3262
3263 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003264 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003265 ibmvnic_send_crq(adapter, &crq);
3266
3267 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003268 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003269 ibmvnic_send_crq(adapter, &crq);
3270
3271 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003272 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003273 ibmvnic_send_crq(adapter, &crq);
3274
3275 crq.query_capability.capability =
3276 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
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_RX_ADD_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(MAX_TX_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_RX_ADD_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 = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003296 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 ibmvnic_send_crq(adapter, &crq);
3298
3299 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003300 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 ibmvnic_send_crq(adapter, &crq);
3302
3303 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003304 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003305 ibmvnic_send_crq(adapter, &crq);
3306
3307 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003308 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003309 ibmvnic_send_crq(adapter, &crq);
3310
3311 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003312 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 ibmvnic_send_crq(adapter, &crq);
3314
3315 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003316 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003317 ibmvnic_send_crq(adapter, &crq);
3318
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003319 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3320 atomic_inc(&adapter->running_cap_crqs);
3321 ibmvnic_send_crq(adapter, &crq);
3322
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003324 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003325 ibmvnic_send_crq(adapter, &crq);
3326
3327 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003328 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003329 ibmvnic_send_crq(adapter, &crq);
3330
3331 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003332 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333 ibmvnic_send_crq(adapter, &crq);
3334
3335 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003336 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337 ibmvnic_send_crq(adapter, &crq);
3338
3339 crq.query_capability.capability =
3340 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
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_TX_ENTRIES_PER_SUBCRQ);
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_RXBA_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 = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003355 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003356 ibmvnic_send_crq(adapter, &crq);
3357}
3358
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003359static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3360 struct ibmvnic_adapter *adapter)
3361{
3362 struct device *dev = &adapter->vdev->dev;
3363
3364 if (crq->get_vpd_size_rsp.rc.code) {
3365 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3366 crq->get_vpd_size_rsp.rc.code);
3367 complete(&adapter->fw_done);
3368 return;
3369 }
3370
3371 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3372 complete(&adapter->fw_done);
3373}
3374
3375static void handle_vpd_rsp(union ibmvnic_crq *crq,
3376 struct ibmvnic_adapter *adapter)
3377{
3378 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003379 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003380 u8 fw_level_len = 0;
3381
3382 memset(adapter->fw_version, 0, 32);
3383
3384 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3385 DMA_FROM_DEVICE);
3386
3387 if (crq->get_vpd_rsp.rc.code) {
3388 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3389 crq->get_vpd_rsp.rc.code);
3390 goto complete;
3391 }
3392
3393 /* get the position of the firmware version info
3394 * located after the ASCII 'RM' substring in the buffer
3395 */
3396 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3397 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003398 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 -02003399 goto complete;
3400 }
3401
3402 /* get length of firmware level ASCII substring */
3403 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3404 fw_level_len = *(substr + 2);
3405 } else {
3406 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3407 goto complete;
3408 }
3409
3410 /* copy firmware version string from vpd into adapter */
3411 if ((substr + 3 + fw_level_len) <
3412 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003413 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003414 } else {
3415 dev_info(dev, "FW substr extrapolated VPD buff\n");
3416 }
3417
3418complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003419 if (adapter->fw_version[0] == '\0')
3420 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003421 complete(&adapter->fw_done);
3422}
3423
Thomas Falcon032c5e82015-12-21 11:26:06 -06003424static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3425{
3426 struct device *dev = &adapter->vdev->dev;
3427 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3428 union ibmvnic_crq crq;
3429 int i;
3430
3431 dma_unmap_single(dev, adapter->ip_offload_tok,
3432 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3433
3434 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3435 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3436 netdev_dbg(adapter->netdev, "%016lx\n",
3437 ((unsigned long int *)(buf))[i]);
3438
3439 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3440 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3441 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3442 buf->tcp_ipv4_chksum);
3443 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3444 buf->tcp_ipv6_chksum);
3445 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3446 buf->udp_ipv4_chksum);
3447 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3448 buf->udp_ipv6_chksum);
3449 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3450 buf->large_tx_ipv4);
3451 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3452 buf->large_tx_ipv6);
3453 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3454 buf->large_rx_ipv4);
3455 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3456 buf->large_rx_ipv6);
3457 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3458 buf->max_ipv4_header_size);
3459 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3460 buf->max_ipv6_header_size);
3461 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3462 buf->max_tcp_header_size);
3463 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3464 buf->max_udp_header_size);
3465 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3466 buf->max_large_tx_size);
3467 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3468 buf->max_large_rx_size);
3469 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3470 buf->ipv6_extension_header);
3471 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3472 buf->tcp_pseudosum_req);
3473 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3474 buf->num_ipv6_ext_headers);
3475 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3476 buf->off_ipv6_ext_headers);
3477
3478 adapter->ip_offload_ctrl_tok =
3479 dma_map_single(dev, &adapter->ip_offload_ctrl,
3480 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3481
3482 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3483 dev_err(dev, "Couldn't map ip offload control buffer\n");
3484 return;
3485 }
3486
Thomas Falconf6897942018-01-18 19:05:01 -06003487 adapter->ip_offload_ctrl.len =
3488 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003489 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003490 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3491 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003492 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3493 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3494 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3495 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003496 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3497 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003498
Thomas Falconfdb06102017-10-17 12:36:55 -05003499 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003500 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3501 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3502
Thomas Falcon15482052017-10-17 12:36:54 -05003503 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003504
3505 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3506 adapter->netdev->features |= NETIF_F_IP_CSUM;
3507
3508 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3509 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3510
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003511 if ((adapter->netdev->features &
3512 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3513 adapter->netdev->features |= NETIF_F_RXCSUM;
3514
Thomas Falconfdb06102017-10-17 12:36:55 -05003515 if (buf->large_tx_ipv4)
3516 adapter->netdev->features |= NETIF_F_TSO;
3517 if (buf->large_tx_ipv6)
3518 adapter->netdev->features |= NETIF_F_TSO6;
3519
Thomas Falconaa0bf852017-10-17 12:36:56 -05003520 adapter->netdev->hw_features |= adapter->netdev->features;
3521
Thomas Falcon032c5e82015-12-21 11:26:06 -06003522 memset(&crq, 0, sizeof(crq));
3523 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3524 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3525 crq.control_ip_offload.len =
3526 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3527 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3528 ibmvnic_send_crq(adapter, &crq);
3529}
3530
3531static void handle_error_info_rsp(union ibmvnic_crq *crq,
3532 struct ibmvnic_adapter *adapter)
3533{
3534 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003535 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003536 unsigned long flags;
3537 bool found = false;
3538 int i;
3539
3540 if (!crq->request_error_rsp.rc.code) {
3541 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3542 crq->request_error_rsp.rc.code);
3543 return;
3544 }
3545
3546 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003547 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003548 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3549 found = true;
3550 list_del(&error_buff->list);
3551 break;
3552 }
3553 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3554
3555 if (!found) {
3556 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003557 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003558 return;
3559 }
3560
3561 dev_err(dev, "Detailed info for error id %x:",
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
3564 for (i = 0; i < error_buff->len; i++) {
3565 pr_cont("%02x", (int)error_buff->buff[i]);
3566 if (i % 8 == 7)
3567 pr_cont(" ");
3568 }
3569 pr_cont("\n");
3570
3571 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3572 DMA_FROM_DEVICE);
3573 kfree(error_buff->buff);
3574 kfree(error_buff);
3575}
3576
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003577static void request_error_information(struct ibmvnic_adapter *adapter,
3578 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003579{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003580 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003581 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003582 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003583 unsigned long timeout = msecs_to_jiffies(30000);
3584 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003585 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003586 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003587
3588 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3589 if (!error_buff)
3590 return;
3591
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003592 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003593 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3594 if (!error_buff->buff) {
3595 kfree(error_buff);
3596 return;
3597 }
3598
3599 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3600 DMA_FROM_DEVICE);
3601 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003602 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003603 kfree(error_buff->buff);
3604 kfree(error_buff);
3605 return;
3606 }
3607
Thomas Falcon032c5e82015-12-21 11:26:06 -06003608 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003609 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003610
3611 spin_lock_irqsave(&adapter->error_list_lock, flags);
3612 list_add_tail(&error_buff->list, &adapter->errors);
3613 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3614
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003615 memset(&crq, 0, sizeof(crq));
3616 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3617 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3618 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3619 crq.request_error_info.len = cpu_to_be32(detail_len);
3620 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3621
3622 rc = ibmvnic_send_crq(adapter, &crq);
3623 if (rc) {
3624 netdev_err(netdev, "failed to request error information\n");
3625 goto err_info_fail;
3626 }
3627
3628 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3629 netdev_err(netdev, "timeout waiting for error information\n");
3630 goto err_info_fail;
3631 }
3632
3633 return;
3634
3635err_info_fail:
3636 spin_lock_irqsave(&adapter->error_list_lock, flags);
3637 list_del(&error_buff->list);
3638 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3639
3640 kfree(error_buff->buff);
3641 kfree(error_buff);
3642}
3643
3644static void handle_error_indication(union ibmvnic_crq *crq,
3645 struct ibmvnic_adapter *adapter)
3646{
3647 struct device *dev = &adapter->vdev->dev;
3648
3649 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3650 crq->error_indication.flags
3651 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3652 be32_to_cpu(crq->error_indication.error_id),
3653 be16_to_cpu(crq->error_indication.error_cause));
3654
3655 if (be32_to_cpu(crq->error_indication.error_id))
3656 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003657
3658 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3659 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003660 else
3661 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003662}
3663
Thomas Falconf8136142018-01-29 13:45:05 -06003664static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3665 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003666{
3667 struct net_device *netdev = adapter->netdev;
3668 struct device *dev = &adapter->vdev->dev;
3669 long rc;
3670
3671 rc = crq->change_mac_addr_rsp.rc.code;
3672 if (rc) {
3673 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003674 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003675 }
3676 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3677 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003678out:
3679 complete(&adapter->fw_done);
3680 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003681}
3682
3683static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3684 struct ibmvnic_adapter *adapter)
3685{
3686 struct device *dev = &adapter->vdev->dev;
3687 u64 *req_value;
3688 char *name;
3689
Thomas Falcon901e0402017-02-15 12:17:59 -06003690 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003691 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3692 case REQ_TX_QUEUES:
3693 req_value = &adapter->req_tx_queues;
3694 name = "tx";
3695 break;
3696 case REQ_RX_QUEUES:
3697 req_value = &adapter->req_rx_queues;
3698 name = "rx";
3699 break;
3700 case REQ_RX_ADD_QUEUES:
3701 req_value = &adapter->req_rx_add_queues;
3702 name = "rx_add";
3703 break;
3704 case REQ_TX_ENTRIES_PER_SUBCRQ:
3705 req_value = &adapter->req_tx_entries_per_subcrq;
3706 name = "tx_entries_per_subcrq";
3707 break;
3708 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3709 req_value = &adapter->req_rx_add_entries_per_subcrq;
3710 name = "rx_add_entries_per_subcrq";
3711 break;
3712 case REQ_MTU:
3713 req_value = &adapter->req_mtu;
3714 name = "mtu";
3715 break;
3716 case PROMISC_REQUESTED:
3717 req_value = &adapter->promisc;
3718 name = "promisc";
3719 break;
3720 default:
3721 dev_err(dev, "Got invalid cap request rsp %d\n",
3722 crq->request_capability.capability);
3723 return;
3724 }
3725
3726 switch (crq->request_capability_rsp.rc.code) {
3727 case SUCCESS:
3728 break;
3729 case PARTIALSUCCESS:
3730 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3731 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003732 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003733 number), name);
John Allene7913802018-01-18 16:27:12 -06003734
3735 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3736 REQ_MTU) {
3737 pr_err("mtu of %llu is not supported. Reverting.\n",
3738 *req_value);
3739 *req_value = adapter->fallback.mtu;
3740 } else {
3741 *req_value =
3742 be64_to_cpu(crq->request_capability_rsp.number);
3743 }
3744
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003745 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003746 return;
3747 default:
3748 dev_err(dev, "Error %d in request cap rsp\n",
3749 crq->request_capability_rsp.rc.code);
3750 return;
3751 }
3752
3753 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003754 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003755 union ibmvnic_crq newcrq;
3756 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3757 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3758 &adapter->ip_offload_buf;
3759
Thomas Falcon249168a2017-02-15 12:18:00 -06003760 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003761 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3762 buf_sz,
3763 DMA_FROM_DEVICE);
3764
3765 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3766 if (!firmware_has_feature(FW_FEATURE_CMO))
3767 dev_err(dev, "Couldn't map offload buffer\n");
3768 return;
3769 }
3770
3771 memset(&newcrq, 0, sizeof(newcrq));
3772 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3773 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3774 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3775 newcrq.query_ip_offload.ioba =
3776 cpu_to_be32(adapter->ip_offload_tok);
3777
3778 ibmvnic_send_crq(adapter, &newcrq);
3779 }
3780}
3781
3782static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3783 struct ibmvnic_adapter *adapter)
3784{
3785 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003786 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003787 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3788 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003789 int i;
3790
3791 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3792 DMA_BIDIRECTIONAL);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003793 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003794 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3795 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3796
John Allen498cd8e2016-04-06 11:49:55 -05003797 /* If the number of queues requested can't be allocated by the
3798 * server, the login response will return with code 1. We will need
3799 * to resend the login buffer with fewer queues requested.
3800 */
3801 if (login_rsp_crq->generic.rc.code) {
3802 adapter->renegotiate = true;
3803 complete(&adapter->init_done);
3804 return 0;
3805 }
3806
John Allenc26eba02017-10-26 16:23:25 -05003807 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3808
Thomas Falcon032c5e82015-12-21 11:26:06 -06003809 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3810 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3811 netdev_dbg(adapter->netdev, "%016lx\n",
3812 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3813 }
3814
3815 /* Sanity checks */
3816 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3817 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3818 adapter->req_rx_add_queues !=
3819 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3820 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3821 ibmvnic_remove(adapter->vdev);
3822 return -EIO;
3823 }
3824 complete(&adapter->init_done);
3825
Thomas Falcon032c5e82015-12-21 11:26:06 -06003826 return 0;
3827}
3828
Thomas Falcon032c5e82015-12-21 11:26:06 -06003829static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3830 struct ibmvnic_adapter *adapter)
3831{
3832 struct device *dev = &adapter->vdev->dev;
3833 long rc;
3834
3835 rc = crq->request_unmap_rsp.rc.code;
3836 if (rc)
3837 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3838}
3839
3840static void handle_query_map_rsp(union ibmvnic_crq *crq,
3841 struct ibmvnic_adapter *adapter)
3842{
3843 struct net_device *netdev = adapter->netdev;
3844 struct device *dev = &adapter->vdev->dev;
3845 long rc;
3846
3847 rc = crq->query_map_rsp.rc.code;
3848 if (rc) {
3849 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3850 return;
3851 }
3852 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3853 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3854 crq->query_map_rsp.free_pages);
3855}
3856
3857static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3858 struct ibmvnic_adapter *adapter)
3859{
3860 struct net_device *netdev = adapter->netdev;
3861 struct device *dev = &adapter->vdev->dev;
3862 long rc;
3863
Thomas Falcon901e0402017-02-15 12:17:59 -06003864 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003865 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003866 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003867 rc = crq->query_capability.rc.code;
3868 if (rc) {
3869 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3870 goto out;
3871 }
3872
3873 switch (be16_to_cpu(crq->query_capability.capability)) {
3874 case MIN_TX_QUEUES:
3875 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003876 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003877 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3878 adapter->min_tx_queues);
3879 break;
3880 case MIN_RX_QUEUES:
3881 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003882 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003883 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3884 adapter->min_rx_queues);
3885 break;
3886 case MIN_RX_ADD_QUEUES:
3887 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003888 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003889 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3890 adapter->min_rx_add_queues);
3891 break;
3892 case MAX_TX_QUEUES:
3893 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003894 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003895 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3896 adapter->max_tx_queues);
3897 break;
3898 case MAX_RX_QUEUES:
3899 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003900 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003901 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3902 adapter->max_rx_queues);
3903 break;
3904 case MAX_RX_ADD_QUEUES:
3905 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003906 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003907 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3908 adapter->max_rx_add_queues);
3909 break;
3910 case MIN_TX_ENTRIES_PER_SUBCRQ:
3911 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003912 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003913 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3914 adapter->min_tx_entries_per_subcrq);
3915 break;
3916 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3917 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003918 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003919 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3920 adapter->min_rx_add_entries_per_subcrq);
3921 break;
3922 case MAX_TX_ENTRIES_PER_SUBCRQ:
3923 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003924 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003925 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3926 adapter->max_tx_entries_per_subcrq);
3927 break;
3928 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3929 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003930 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3932 adapter->max_rx_add_entries_per_subcrq);
3933 break;
3934 case TCP_IP_OFFLOAD:
3935 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003936 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003937 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3938 adapter->tcp_ip_offload);
3939 break;
3940 case PROMISC_SUPPORTED:
3941 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003942 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003943 netdev_dbg(netdev, "promisc_supported = %lld\n",
3944 adapter->promisc_supported);
3945 break;
3946 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003947 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003948 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003949 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3950 break;
3951 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003952 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003953 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003954 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3955 break;
3956 case MAX_MULTICAST_FILTERS:
3957 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003958 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003959 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3960 adapter->max_multicast_filters);
3961 break;
3962 case VLAN_HEADER_INSERTION:
3963 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003964 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 if (adapter->vlan_header_insertion)
3966 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3967 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3968 adapter->vlan_header_insertion);
3969 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003970 case RX_VLAN_HEADER_INSERTION:
3971 adapter->rx_vlan_header_insertion =
3972 be64_to_cpu(crq->query_capability.number);
3973 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3974 adapter->rx_vlan_header_insertion);
3975 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003976 case MAX_TX_SG_ENTRIES:
3977 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003978 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003979 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3980 adapter->max_tx_sg_entries);
3981 break;
3982 case RX_SG_SUPPORTED:
3983 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003984 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3986 adapter->rx_sg_supported);
3987 break;
3988 case OPT_TX_COMP_SUB_QUEUES:
3989 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003990 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003991 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3992 adapter->opt_tx_comp_sub_queues);
3993 break;
3994 case OPT_RX_COMP_QUEUES:
3995 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003996 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003997 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3998 adapter->opt_rx_comp_queues);
3999 break;
4000 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4001 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06004002 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4004 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4005 break;
4006 case OPT_TX_ENTRIES_PER_SUBCRQ:
4007 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004008 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4010 adapter->opt_tx_entries_per_subcrq);
4011 break;
4012 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4013 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06004014 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004015 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4016 adapter->opt_rxba_entries_per_subcrq);
4017 break;
4018 case TX_RX_DESC_REQ:
4019 adapter->tx_rx_desc_req = crq->query_capability.number;
4020 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4021 adapter->tx_rx_desc_req);
4022 break;
4023
4024 default:
4025 netdev_err(netdev, "Got invalid cap rsp %d\n",
4026 crq->query_capability.capability);
4027 }
4028
4029out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004030 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4031 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004032 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004033 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004034}
4035
Thomas Falcon032c5e82015-12-21 11:26:06 -06004036static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4037 struct ibmvnic_adapter *adapter)
4038{
4039 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4040 struct net_device *netdev = adapter->netdev;
4041 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004042 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004043 long rc;
4044
4045 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004046 (unsigned long int)cpu_to_be64(u64_crq[0]),
4047 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004048 switch (gen_crq->first) {
4049 case IBMVNIC_CRQ_INIT_RSP:
4050 switch (gen_crq->cmd) {
4051 case IBMVNIC_CRQ_INIT:
4052 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004053 adapter->from_passive_init = true;
4054 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004055 break;
4056 case IBMVNIC_CRQ_INIT_COMPLETE:
4057 dev_info(dev, "Partner initialization complete\n");
4058 send_version_xchg(adapter);
4059 break;
4060 default:
4061 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4062 }
4063 return;
4064 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004065 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004066 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004067 dev_info(dev, "Migrated, re-enabling adapter\n");
4068 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004069 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4070 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004071 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004072 } else {
4073 /* The adapter lost the connection */
4074 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4075 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004076 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004077 }
4078 return;
4079 case IBMVNIC_CRQ_CMD_RSP:
4080 break;
4081 default:
4082 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4083 gen_crq->first);
4084 return;
4085 }
4086
4087 switch (gen_crq->cmd) {
4088 case VERSION_EXCHANGE_RSP:
4089 rc = crq->version_exchange_rsp.rc.code;
4090 if (rc) {
4091 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4092 break;
4093 }
4094 dev_info(dev, "Partner protocol version is %d\n",
4095 crq->version_exchange_rsp.version);
4096 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4097 ibmvnic_version)
4098 ibmvnic_version =
4099 be16_to_cpu(crq->version_exchange_rsp.version);
4100 send_cap_queries(adapter);
4101 break;
4102 case QUERY_CAPABILITY_RSP:
4103 handle_query_cap_rsp(crq, adapter);
4104 break;
4105 case QUERY_MAP_RSP:
4106 handle_query_map_rsp(crq, adapter);
4107 break;
4108 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004109 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4110 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004111 break;
4112 case REQUEST_UNMAP_RSP:
4113 handle_request_unmap_rsp(crq, adapter);
4114 break;
4115 case REQUEST_CAPABILITY_RSP:
4116 handle_request_cap_rsp(crq, adapter);
4117 break;
4118 case LOGIN_RSP:
4119 netdev_dbg(netdev, "Got Login Response\n");
4120 handle_login_rsp(crq, adapter);
4121 break;
4122 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004123 netdev_dbg(netdev,
4124 "Got Logical Link State Response, state: %d rc: %d\n",
4125 crq->logical_link_state_rsp.link_state,
4126 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004127 adapter->logical_link_state =
4128 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004129 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4130 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004131 break;
4132 case LINK_STATE_INDICATION:
4133 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4134 adapter->phys_link_state =
4135 crq->link_state_indication.phys_link_state;
4136 adapter->logical_link_state =
4137 crq->link_state_indication.logical_link_state;
4138 break;
4139 case CHANGE_MAC_ADDR_RSP:
4140 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004141 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004142 break;
4143 case ERROR_INDICATION:
4144 netdev_dbg(netdev, "Got Error Indication\n");
4145 handle_error_indication(crq, adapter);
4146 break;
4147 case REQUEST_ERROR_RSP:
4148 netdev_dbg(netdev, "Got Error Detail Response\n");
4149 handle_error_info_rsp(crq, adapter);
4150 break;
4151 case REQUEST_STATISTICS_RSP:
4152 netdev_dbg(netdev, "Got Statistics Response\n");
4153 complete(&adapter->stats_done);
4154 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004155 case QUERY_IP_OFFLOAD_RSP:
4156 netdev_dbg(netdev, "Got Query IP offload Response\n");
4157 handle_query_ip_offload_rsp(adapter);
4158 break;
4159 case MULTICAST_CTRL_RSP:
4160 netdev_dbg(netdev, "Got multicast control Response\n");
4161 break;
4162 case CONTROL_IP_OFFLOAD_RSP:
4163 netdev_dbg(netdev, "Got Control IP offload Response\n");
4164 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4165 sizeof(adapter->ip_offload_ctrl),
4166 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004167 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004168 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004169 case COLLECT_FW_TRACE_RSP:
4170 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4171 complete(&adapter->fw_done);
4172 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004173 case GET_VPD_SIZE_RSP:
4174 handle_vpd_size_rsp(crq, adapter);
4175 break;
4176 case GET_VPD_RSP:
4177 handle_vpd_rsp(crq, adapter);
4178 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179 default:
4180 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4181 gen_crq->cmd);
4182 }
4183}
4184
4185static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4186{
4187 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004188
Thomas Falcon6c267b32017-02-15 12:17:58 -06004189 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004190 return IRQ_HANDLED;
4191}
4192
4193static void ibmvnic_tasklet(void *data)
4194{
4195 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004196 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004197 union ibmvnic_crq *crq;
4198 unsigned long flags;
4199 bool done = false;
4200
4201 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004202 while (!done) {
4203 /* Pull all the valid messages off the CRQ */
4204 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4205 ibmvnic_handle_crq(crq, adapter);
4206 crq->generic.first = 0;
4207 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004208
4209 /* remain in tasklet until all
4210 * capabilities responses are received
4211 */
4212 if (!adapter->wait_capability)
4213 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004214 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004215 /* if capabilities CRQ's were sent in this tasklet, the following
4216 * tasklet must wait until all responses are received
4217 */
4218 if (atomic_read(&adapter->running_cap_crqs) != 0)
4219 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004220 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004221}
4222
4223static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4224{
4225 struct vio_dev *vdev = adapter->vdev;
4226 int rc;
4227
4228 do {
4229 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4230 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4231
4232 if (rc)
4233 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4234
4235 return rc;
4236}
4237
4238static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4239{
4240 struct ibmvnic_crq_queue *crq = &adapter->crq;
4241 struct device *dev = &adapter->vdev->dev;
4242 struct vio_dev *vdev = adapter->vdev;
4243 int rc;
4244
4245 /* Close the CRQ */
4246 do {
4247 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4248 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4249
4250 /* Clean out the queue */
4251 memset(crq->msgs, 0, PAGE_SIZE);
4252 crq->cur = 0;
4253
4254 /* And re-open it again */
4255 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4256 crq->msg_token, PAGE_SIZE);
4257
4258 if (rc == H_CLOSED)
4259 /* Adapter is good, but other end is not ready */
4260 dev_warn(dev, "Partner adapter not ready\n");
4261 else if (rc != 0)
4262 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4263
4264 return rc;
4265}
4266
Nathan Fontenotf9928872017-03-30 02:48:54 -04004267static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004268{
4269 struct ibmvnic_crq_queue *crq = &adapter->crq;
4270 struct vio_dev *vdev = adapter->vdev;
4271 long rc;
4272
Nathan Fontenotf9928872017-03-30 02:48:54 -04004273 if (!crq->msgs)
4274 return;
4275
Thomas Falcon032c5e82015-12-21 11:26:06 -06004276 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4277 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004278 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004279 do {
4280 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4281 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4282
4283 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4284 DMA_BIDIRECTIONAL);
4285 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004286 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004287}
4288
Nathan Fontenotf9928872017-03-30 02:48:54 -04004289static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004290{
4291 struct ibmvnic_crq_queue *crq = &adapter->crq;
4292 struct device *dev = &adapter->vdev->dev;
4293 struct vio_dev *vdev = adapter->vdev;
4294 int rc, retrc = -ENOMEM;
4295
Nathan Fontenotf9928872017-03-30 02:48:54 -04004296 if (crq->msgs)
4297 return 0;
4298
Thomas Falcon032c5e82015-12-21 11:26:06 -06004299 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4300 /* Should we allocate more than one page? */
4301
4302 if (!crq->msgs)
4303 return -ENOMEM;
4304
4305 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4306 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4307 DMA_BIDIRECTIONAL);
4308 if (dma_mapping_error(dev, crq->msg_token))
4309 goto map_failed;
4310
4311 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4312 crq->msg_token, PAGE_SIZE);
4313
4314 if (rc == H_RESOURCE)
4315 /* maybe kexecing and resource is busy. try a reset */
4316 rc = ibmvnic_reset_crq(adapter);
4317 retrc = rc;
4318
4319 if (rc == H_CLOSED) {
4320 dev_warn(dev, "Partner adapter not ready\n");
4321 } else if (rc) {
4322 dev_warn(dev, "Error %d opening adapter\n", rc);
4323 goto reg_crq_failed;
4324 }
4325
4326 retrc = 0;
4327
Thomas Falcon6c267b32017-02-15 12:17:58 -06004328 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4329 (unsigned long)adapter);
4330
Thomas Falcon032c5e82015-12-21 11:26:06 -06004331 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4332 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4333 adapter);
4334 if (rc) {
4335 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4336 vdev->irq, rc);
4337 goto req_irq_failed;
4338 }
4339
4340 rc = vio_enable_interrupts(vdev);
4341 if (rc) {
4342 dev_err(dev, "Error %d enabling interrupts\n", rc);
4343 goto req_irq_failed;
4344 }
4345
4346 crq->cur = 0;
4347 spin_lock_init(&crq->lock);
4348
4349 return retrc;
4350
4351req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004352 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004353 do {
4354 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4355 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4356reg_crq_failed:
4357 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4358map_failed:
4359 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004360 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004361 return retrc;
4362}
4363
John Allenf6ef6402017-03-17 17:13:42 -05004364static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4365{
4366 struct device *dev = &adapter->vdev->dev;
4367 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004368 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004369 int rc;
4370
John Allenc26eba02017-10-26 16:23:25 -05004371 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004372 rc = ibmvnic_reset_crq(adapter);
4373 if (!rc)
4374 rc = vio_enable_interrupts(adapter->vdev);
4375 } else {
4376 rc = init_crq_queue(adapter);
4377 }
4378
John Allenf6ef6402017-03-17 17:13:42 -05004379 if (rc) {
4380 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4381 return rc;
4382 }
4383
John Allen017892c12017-05-26 10:30:19 -04004384 adapter->from_passive_init = false;
4385
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004386 old_num_rx_queues = adapter->req_rx_queues;
4387 old_num_tx_queues = adapter->req_tx_queues;
4388
John Allenf6ef6402017-03-17 17:13:42 -05004389 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004390 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004391 ibmvnic_send_crq_init(adapter);
4392 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4393 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004394 return -1;
4395 }
4396
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004397 if (adapter->init_done_rc) {
4398 release_crq_queue(adapter);
4399 return adapter->init_done_rc;
4400 }
4401
John Allen017892c12017-05-26 10:30:19 -04004402 if (adapter->from_passive_init) {
4403 adapter->state = VNIC_OPEN;
4404 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004405 return -1;
4406 }
4407
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004408 if (adapter->resetting && !adapter->wait_for_reset) {
4409 if (adapter->req_rx_queues != old_num_rx_queues ||
4410 adapter->req_tx_queues != old_num_tx_queues) {
4411 release_sub_crqs(adapter, 0);
4412 rc = init_sub_crqs(adapter);
4413 } else {
4414 rc = reset_sub_crq_queues(adapter);
4415 }
4416 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004417 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004418 }
4419
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004420 if (rc) {
4421 dev_err(dev, "Initialization of sub crqs failed\n");
4422 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004423 return rc;
4424 }
4425
4426 rc = init_sub_crq_irqs(adapter);
4427 if (rc) {
4428 dev_err(dev, "Failed to initialize sub crq irqs\n");
4429 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004430 }
4431
4432 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004433}
4434
Thomas Falcon40c9db82017-06-12 12:35:04 -05004435static struct device_attribute dev_attr_failover;
4436
Thomas Falcon032c5e82015-12-21 11:26:06 -06004437static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4438{
4439 struct ibmvnic_adapter *adapter;
4440 struct net_device *netdev;
4441 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004442 int rc;
4443
4444 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4445 dev->unit_address);
4446
4447 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4448 VETH_MAC_ADDR, NULL);
4449 if (!mac_addr_p) {
4450 dev_err(&dev->dev,
4451 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4452 __FILE__, __LINE__);
4453 return 0;
4454 }
4455
4456 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004457 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004458 if (!netdev)
4459 return -ENOMEM;
4460
4461 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004462 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004463 dev_set_drvdata(&dev->dev, netdev);
4464 adapter->vdev = dev;
4465 adapter->netdev = netdev;
4466
4467 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4468 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4469 netdev->irq = dev->irq;
4470 netdev->netdev_ops = &ibmvnic_netdev_ops;
4471 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4472 SET_NETDEV_DEV(netdev, &dev->dev);
4473
4474 spin_lock_init(&adapter->stats_lock);
4475
Thomas Falcon032c5e82015-12-21 11:26:06 -06004476 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004477 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004478
Nathan Fontenoted651a12017-05-03 14:04:38 -04004479 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4480 INIT_LIST_HEAD(&adapter->rwi_list);
4481 mutex_init(&adapter->reset_lock);
4482 mutex_init(&adapter->rwi_lock);
4483 adapter->resetting = false;
4484
John Allenc26eba02017-10-26 16:23:25 -05004485 adapter->mac_change_pending = false;
4486
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004487 do {
4488 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004489 if (rc && rc != EAGAIN)
4490 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004491 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004492
Thomas Falconf39f0d12017-02-14 10:22:59 -06004493 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004494 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4495 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004496
Thomas Falcon40c9db82017-06-12 12:35:04 -05004497 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004498 if (rc)
4499 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004500
Mick Tarsele876a8a2017-09-28 13:53:18 -07004501 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004502 rc = register_netdev(netdev);
4503 if (rc) {
4504 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004505 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004506 }
4507 dev_info(&dev->dev, "ibmvnic registered\n");
4508
Nathan Fontenot90c80142017-05-03 14:04:32 -04004509 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004510
4511 adapter->wait_for_reset = false;
4512
Thomas Falcon032c5e82015-12-21 11:26:06 -06004513 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004514
4515ibmvnic_register_fail:
4516 device_remove_file(&dev->dev, &dev_attr_failover);
4517
4518ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004519 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004520 release_crq_queue(adapter);
4521 free_netdev(netdev);
4522
4523 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004524}
4525
4526static int ibmvnic_remove(struct vio_dev *dev)
4527{
4528 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004529 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004530
Nathan Fontenot90c80142017-05-03 14:04:32 -04004531 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004532 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004533 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004534
4535 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004536 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004537 release_crq_queue(adapter);
4538
Nathan Fontenot90c80142017-05-03 14:04:32 -04004539 adapter->state = VNIC_REMOVED;
4540
Nathan Fontenoted651a12017-05-03 14:04:38 -04004541 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004542 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004543 free_netdev(netdev);
4544 dev_set_drvdata(&dev->dev, NULL);
4545
4546 return 0;
4547}
4548
Thomas Falcon40c9db82017-06-12 12:35:04 -05004549static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4550 const char *buf, size_t count)
4551{
4552 struct net_device *netdev = dev_get_drvdata(dev);
4553 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4554 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4555 __be64 session_token;
4556 long rc;
4557
4558 if (!sysfs_streq(buf, "1"))
4559 return -EINVAL;
4560
4561 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4562 H_GET_SESSION_TOKEN, 0, 0, 0);
4563 if (rc) {
4564 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4565 rc);
4566 return -EINVAL;
4567 }
4568
4569 session_token = (__be64)retbuf[0];
4570 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4571 be64_to_cpu(session_token));
4572 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4573 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4574 if (rc) {
4575 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4576 rc);
4577 return -EINVAL;
4578 }
4579
4580 return count;
4581}
4582
Joe Perches6cbaefb2017-12-19 10:15:09 -08004583static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004584
Thomas Falcon032c5e82015-12-21 11:26:06 -06004585static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4586{
4587 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4588 struct ibmvnic_adapter *adapter;
4589 struct iommu_table *tbl;
4590 unsigned long ret = 0;
4591 int i;
4592
4593 tbl = get_iommu_table_base(&vdev->dev);
4594
4595 /* netdev inits at probe time along with the structures we need below*/
4596 if (!netdev)
4597 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4598
4599 adapter = netdev_priv(netdev);
4600
4601 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004602 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4603
4604 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4605 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4606
4607 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4608 i++)
4609 ret += adapter->rx_pool[i].size *
4610 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4611
4612 return ret;
4613}
4614
4615static int ibmvnic_resume(struct device *dev)
4616{
4617 struct net_device *netdev = dev_get_drvdata(dev);
4618 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004619
John Allencb89ba22017-06-19 11:27:53 -05004620 if (adapter->state != VNIC_OPEN)
4621 return 0;
4622
John Allena2488782017-07-24 13:26:06 -05004623 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004624
4625 return 0;
4626}
4627
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304628static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004629 {"network", "IBM,vnic"},
4630 {"", "" }
4631};
4632MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4633
4634static const struct dev_pm_ops ibmvnic_pm_ops = {
4635 .resume = ibmvnic_resume
4636};
4637
4638static struct vio_driver ibmvnic_driver = {
4639 .id_table = ibmvnic_device_table,
4640 .probe = ibmvnic_probe,
4641 .remove = ibmvnic_remove,
4642 .get_desired_dma = ibmvnic_get_desired_dma,
4643 .name = ibmvnic_driver_name,
4644 .pm = &ibmvnic_pm_ops,
4645};
4646
4647/* module functions */
4648static int __init ibmvnic_module_init(void)
4649{
4650 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4651 IBMVNIC_DRIVER_VERSION);
4652
4653 return vio_register_driver(&ibmvnic_driver);
4654}
4655
4656static void __exit ibmvnic_module_exit(void)
4657{
4658 vio_unregister_driver(&ibmvnic_driver);
4659}
4660
4661module_init(ibmvnic_module_init);
4662module_exit(ibmvnic_module_exit);