blob: 33cfd277f0080edfba78284df4770b5a75856ce6 [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 =
364 kcalloc(adapter->req_tx_queues,
365 sizeof(struct ibmvnic_tx_queue_stats),
366 GFP_KERNEL);
367 if (!adapter->tx_stats_buffers)
368 return -ENOMEM;
369
370 adapter->rx_stats_buffers =
371 kcalloc(adapter->req_rx_queues,
372 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
John Allena57a5d22017-03-17 17:13:41 -0500733static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600734{
735 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500736 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600737 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500738 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600739
John Allenbd0b6722017-03-17 17:13:40 -0500740 do {
741 if (adapter->renegotiate) {
742 adapter->renegotiate = false;
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -0600743 release_sub_crqs(adapter, 1);
John Allenbd0b6722017-03-17 17:13:40 -0500744
745 reinit_completion(&adapter->init_done);
746 send_cap_queries(adapter);
747 if (!wait_for_completion_timeout(&adapter->init_done,
748 timeout)) {
749 dev_err(dev, "Capabilities query timeout\n");
750 return -1;
751 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500752 rc = init_sub_crqs(adapter);
753 if (rc) {
754 dev_err(dev,
755 "Initialization of SCRQ's failed\n");
756 return -1;
757 }
758 rc = init_sub_crq_irqs(adapter);
759 if (rc) {
760 dev_err(dev,
761 "Initialization of SCRQ's irqs failed\n");
762 return -1;
763 }
John Allenbd0b6722017-03-17 17:13:40 -0500764 }
765
766 reinit_completion(&adapter->init_done);
767 send_login(adapter);
768 if (!wait_for_completion_timeout(&adapter->init_done,
769 timeout)) {
770 dev_err(dev, "Login timeout\n");
771 return -1;
772 }
773 } while (adapter->renegotiate);
774
Thomas Falcon3d166132018-01-10 19:39:52 -0600775 /* handle pending MAC address changes after successful login */
776 if (adapter->mac_change_pending) {
777 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
778 adapter->mac_change_pending = false;
779 }
780
John Allena57a5d22017-03-17 17:13:41 -0500781 return 0;
782}
783
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600784static void release_login_buffer(struct ibmvnic_adapter *adapter)
785{
786 kfree(adapter->login_buf);
787 adapter->login_buf = NULL;
788}
789
790static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
791{
792 kfree(adapter->login_rsp_buf);
793 adapter->login_rsp_buf = NULL;
794}
795
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400796static void release_resources(struct ibmvnic_adapter *adapter)
797{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400798 int i;
799
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200800 release_vpd_data(adapter);
801
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400802 release_tx_pools(adapter);
803 release_rx_pools(adapter);
804
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400805 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500806 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400807 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400808
809 if (adapter->napi) {
810 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500811 if (&adapter->napi[i]) {
812 netdev_dbg(adapter->netdev,
813 "Releasing napi[%d]\n", i);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400814 netif_napi_del(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500815 }
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400816 }
817 }
Thomas Falcon6e4842dd2018-02-13 18:23:41 -0600818 kfree(adapter->napi);
819 adapter->napi = NULL;
Thomas Falcon34f0f4e2018-02-13 18:23:40 -0600820
821 release_login_rsp_buffer(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400822}
823
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400824static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
825{
826 struct net_device *netdev = adapter->netdev;
827 unsigned long timeout = msecs_to_jiffies(30000);
828 union ibmvnic_crq crq;
829 bool resend;
830 int rc;
831
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500832 netdev_dbg(netdev, "setting link state %d\n", link_state);
833
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400834 memset(&crq, 0, sizeof(crq));
835 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
836 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
837 crq.logical_link_state.link_state = link_state;
838
839 do {
840 resend = false;
841
842 reinit_completion(&adapter->init_done);
843 rc = ibmvnic_send_crq(adapter, &crq);
844 if (rc) {
845 netdev_err(netdev, "Failed to set link state\n");
846 return rc;
847 }
848
849 if (!wait_for_completion_timeout(&adapter->init_done,
850 timeout)) {
851 netdev_err(netdev, "timeout setting link state\n");
852 return -1;
853 }
854
855 if (adapter->init_done_rc == 1) {
856 /* Partuial success, delay and re-send */
857 mdelay(1000);
858 resend = true;
859 }
860 } while (resend);
861
862 return 0;
863}
864
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400865static int set_real_num_queues(struct net_device *netdev)
866{
867 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
868 int rc;
869
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500870 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
871 adapter->req_tx_queues, adapter->req_rx_queues);
872
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400873 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
874 if (rc) {
875 netdev_err(netdev, "failed to set the number of tx queues\n");
876 return rc;
877 }
878
879 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
880 if (rc)
881 netdev_err(netdev, "failed to set the number of rx queues\n");
882
883 return rc;
884}
885
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200886static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
887{
888 struct device *dev = &adapter->vdev->dev;
889 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200890 int len = 0;
891
892 if (adapter->vpd->buff)
893 len = adapter->vpd->len;
894
John Allen69d08dc2018-01-18 16:27:58 -0600895 init_completion(&adapter->fw_done);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200896 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
897 crq.get_vpd_size.cmd = GET_VPD_SIZE;
898 ibmvnic_send_crq(adapter, &crq);
899 wait_for_completion(&adapter->fw_done);
900
901 if (!adapter->vpd->len)
902 return -ENODATA;
903
904 if (!adapter->vpd->buff)
905 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
906 else if (adapter->vpd->len != len)
907 adapter->vpd->buff =
908 krealloc(adapter->vpd->buff,
909 adapter->vpd->len, GFP_KERNEL);
910
911 if (!adapter->vpd->buff) {
912 dev_err(dev, "Could allocate VPD buffer\n");
913 return -ENOMEM;
914 }
915
916 adapter->vpd->dma_addr =
917 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
918 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200919 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200920 dev_err(dev, "Could not map VPD buffer\n");
921 kfree(adapter->vpd->buff);
Thomas Falconb0992ec2018-02-06 17:25:23 -0600922 adapter->vpd->buff = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200923 return -ENOMEM;
924 }
925
926 reinit_completion(&adapter->fw_done);
927 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
928 crq.get_vpd.cmd = GET_VPD;
929 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
930 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
931 ibmvnic_send_crq(adapter, &crq);
932 wait_for_completion(&adapter->fw_done);
933
934 return 0;
935}
936
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400937static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500938{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400939 struct net_device *netdev = adapter->netdev;
940 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500941
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400942 rc = set_real_num_queues(netdev);
943 if (rc)
944 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500945
John Allen3d52b592017-08-02 16:44:14 -0500946 rc = init_stats_buffers(adapter);
947 if (rc)
948 return rc;
949
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400950 rc = init_stats_token(adapter);
951 if (rc)
952 return rc;
953
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200954 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
955 if (!adapter->vpd)
956 return -ENOMEM;
957
John Allen69d08dc2018-01-18 16:27:58 -0600958 /* Vital Product Data (VPD) */
959 rc = ibmvnic_get_vpd(adapter);
960 if (rc) {
961 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
962 return rc;
963 }
964
Thomas Falcon032c5e82015-12-21 11:26:06 -0600965 adapter->map_id = 1;
966 adapter->napi = kcalloc(adapter->req_rx_queues,
967 sizeof(struct napi_struct), GFP_KERNEL);
968 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400969 return -ENOMEM;
970
Thomas Falcon032c5e82015-12-21 11:26:06 -0600971 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500972 netdev_dbg(netdev, "Adding napi[%d]\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600973 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
974 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600975 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600976
Thomas Falcon032c5e82015-12-21 11:26:06 -0600977 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400978
979 rc = init_rx_pools(netdev);
980 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400981 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600982
Nathan Fontenotc657e322017-03-30 02:49:06 -0400983 rc = init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -0600984
985 adapter->num_active_tx_scrqs = adapter->req_tx_queues;
986 adapter->num_active_rx_scrqs = adapter->req_rx_queues;
987
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400988 return rc;
989}
990
Nathan Fontenoted651a12017-05-03 14:04:38 -0400991static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400992{
993 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400994 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400995 int i, rc;
996
Nathan Fontenot90c80142017-05-03 14:04:32 -0400997 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600998 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400999 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001000
Thomas Falcon032c5e82015-12-21 11:26:06 -06001001 /* We're ready to receive frames, enable the sub-crq interrupts and
1002 * set the logical link state to up
1003 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001004 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001005 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001006 if (prev_state == VNIC_CLOSED)
1007 enable_irq(adapter->rx_scrq[i]->irq);
1008 else
1009 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1010 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001011
Nathan Fontenoted651a12017-05-03 14:04:38 -04001012 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001013 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001014 if (prev_state == VNIC_CLOSED)
1015 enable_irq(adapter->tx_scrq[i]->irq);
1016 else
1017 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1018 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001019
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001020 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001021 if (rc) {
1022 for (i = 0; i < adapter->req_rx_queues; i++)
1023 napi_disable(&adapter->napi[i]);
1024 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001025 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001026 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001027
Nathan Fontenoted651a12017-05-03 14:04:38 -04001028 netif_tx_start_all_queues(netdev);
1029
1030 if (prev_state == VNIC_CLOSED) {
1031 for (i = 0; i < adapter->req_rx_queues; i++)
1032 napi_schedule(&adapter->napi[i]);
1033 }
1034
1035 adapter->state = VNIC_OPEN;
1036 return rc;
1037}
1038
1039static int ibmvnic_open(struct net_device *netdev)
1040{
1041 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allen69d08dc2018-01-18 16:27:58 -06001042 int rc;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001043
1044 mutex_lock(&adapter->reset_lock);
1045
1046 if (adapter->state != VNIC_CLOSED) {
1047 rc = ibmvnic_login(netdev);
1048 if (rc) {
1049 mutex_unlock(&adapter->reset_lock);
1050 return rc;
1051 }
1052
1053 rc = init_resources(adapter);
1054 if (rc) {
1055 netdev_err(netdev, "failed to initialize resources\n");
1056 release_resources(adapter);
1057 mutex_unlock(&adapter->reset_lock);
1058 return rc;
1059 }
1060 }
1061
1062 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001063 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001064
Nathan Fontenoted651a12017-05-03 14:04:38 -04001065 mutex_unlock(&adapter->reset_lock);
1066
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001067 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001068}
1069
Thomas Falcond0869c02018-02-13 18:23:43 -06001070static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1071{
1072 struct ibmvnic_rx_pool *rx_pool;
1073 u64 rx_entries;
1074 int rx_scrqs;
1075 int i, j;
1076
1077 if (!adapter->rx_pool)
1078 return;
1079
1080 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1081 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1082
1083 /* Free any remaining skbs in the rx buffer pools */
1084 for (i = 0; i < rx_scrqs; i++) {
1085 rx_pool = &adapter->rx_pool[i];
1086 if (!rx_pool)
1087 continue;
1088
1089 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1090 for (j = 0; j < rx_entries; j++) {
1091 if (rx_pool->rx_buff[j].skb) {
1092 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
1093 rx_pool->rx_buff[j].skb = NULL;
1094 }
1095 }
1096 }
1097}
1098
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001099static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1100{
1101 struct ibmvnic_tx_pool *tx_pool;
1102 u64 tx_entries;
1103 int tx_scrqs;
1104 int i, j;
1105
1106 if (!adapter->tx_pool)
1107 return;
1108
1109 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1110 tx_entries = adapter->req_tx_entries_per_subcrq;
1111
1112 /* Free any remaining skbs in the tx buffer pools */
1113 for (i = 0; i < tx_scrqs; i++) {
1114 tx_pool = &adapter->tx_pool[i];
1115 if (!tx_pool)
1116 continue;
1117
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001118 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001119 for (j = 0; j < tx_entries; j++) {
1120 if (tx_pool->tx_buff[j].skb) {
1121 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1122 tx_pool->tx_buff[j].skb = NULL;
1123 }
1124 }
1125 }
1126}
1127
Nathan Fontenoted651a12017-05-03 14:04:38 -04001128static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001129{
1130 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001131 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -05001132 int i;
1133
Nathan Fontenot90c80142017-05-03 14:04:32 -04001134 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001135
1136 /* ensure that transmissions are stopped if called by do_reset */
1137 if (adapter->resetting)
1138 netif_tx_disable(netdev);
1139 else
1140 netif_tx_stop_all_queues(netdev);
1141
John Allend944c3d62017-05-26 10:30:13 -04001142 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001143
1144 if (adapter->tx_scrq) {
1145 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001146 if (adapter->tx_scrq[i]->irq) {
1147 netdev_dbg(adapter->netdev,
1148 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001149 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001150 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001151 }
1152
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001153 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001154 if (rc)
1155 return rc;
1156
1157 if (adapter->rx_scrq) {
1158 for (i = 0; i < adapter->req_rx_queues; i++) {
1159 int retries = 10;
1160
1161 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1162 retries--;
1163 mdelay(100);
1164
1165 if (retries == 0)
1166 break;
1167 }
1168
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001169 if (adapter->rx_scrq[i]->irq) {
1170 netdev_dbg(adapter->netdev,
1171 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001172 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001173 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001174 }
1175 }
Thomas Falcond0869c02018-02-13 18:23:43 -06001176 clean_rx_pools(adapter);
Thomas Falcon10f76212017-05-26 10:30:31 -04001177 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001178 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001179 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001180}
1181
Nathan Fontenoted651a12017-05-03 14:04:38 -04001182static int ibmvnic_close(struct net_device *netdev)
1183{
1184 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1185 int rc;
1186
1187 mutex_lock(&adapter->reset_lock);
1188 rc = __ibmvnic_close(netdev);
1189 mutex_unlock(&adapter->reset_lock);
1190
1191 return rc;
1192}
1193
Thomas Falconad7775d2016-04-01 17:20:34 -05001194/**
1195 * build_hdr_data - creates L2/L3/L4 header data buffer
1196 * @hdr_field - bitfield determining needed headers
1197 * @skb - socket buffer
1198 * @hdr_len - array of header lengths
1199 * @tot_len - total length of data
1200 *
1201 * Reads hdr_field to determine which headers are needed by firmware.
1202 * Builds a buffer containing these headers. Saves individual header
1203 * lengths and total buffer length to be used to build descriptors.
1204 */
1205static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1206 int *hdr_len, u8 *hdr_data)
1207{
1208 int len = 0;
1209 u8 *hdr;
1210
1211 hdr_len[0] = sizeof(struct ethhdr);
1212
1213 if (skb->protocol == htons(ETH_P_IP)) {
1214 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1215 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1216 hdr_len[2] = tcp_hdrlen(skb);
1217 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1218 hdr_len[2] = sizeof(struct udphdr);
1219 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1220 hdr_len[1] = sizeof(struct ipv6hdr);
1221 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1222 hdr_len[2] = tcp_hdrlen(skb);
1223 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1224 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001225 } else if (skb->protocol == htons(ETH_P_ARP)) {
1226 hdr_len[1] = arp_hdr_len(skb->dev);
1227 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001228 }
1229
1230 memset(hdr_data, 0, 120);
1231 if ((hdr_field >> 6) & 1) {
1232 hdr = skb_mac_header(skb);
1233 memcpy(hdr_data, hdr, hdr_len[0]);
1234 len += hdr_len[0];
1235 }
1236
1237 if ((hdr_field >> 5) & 1) {
1238 hdr = skb_network_header(skb);
1239 memcpy(hdr_data + len, hdr, hdr_len[1]);
1240 len += hdr_len[1];
1241 }
1242
1243 if ((hdr_field >> 4) & 1) {
1244 hdr = skb_transport_header(skb);
1245 memcpy(hdr_data + len, hdr, hdr_len[2]);
1246 len += hdr_len[2];
1247 }
1248 return len;
1249}
1250
1251/**
1252 * create_hdr_descs - create header and header extension descriptors
1253 * @hdr_field - bitfield determining needed headers
1254 * @data - buffer containing header data
1255 * @len - length of data buffer
1256 * @hdr_len - array of individual header lengths
1257 * @scrq_arr - descriptor array
1258 *
1259 * Creates header and, if needed, header extension descriptors and
1260 * places them in a descriptor array, scrq_arr
1261 */
1262
Thomas Falcon2de09682017-10-16 10:02:11 -05001263static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1264 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001265{
1266 union sub_crq hdr_desc;
1267 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001268 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001269 u8 *data, *cur;
1270 int tmp;
1271
1272 while (tmp_len > 0) {
1273 cur = hdr_data + len - tmp_len;
1274
1275 memset(&hdr_desc, 0, sizeof(hdr_desc));
1276 if (cur != hdr_data) {
1277 data = hdr_desc.hdr_ext.data;
1278 tmp = tmp_len > 29 ? 29 : tmp_len;
1279 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1280 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1281 hdr_desc.hdr_ext.len = tmp;
1282 } else {
1283 data = hdr_desc.hdr.data;
1284 tmp = tmp_len > 24 ? 24 : tmp_len;
1285 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1286 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1287 hdr_desc.hdr.len = tmp;
1288 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1289 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1290 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1291 hdr_desc.hdr.flag = hdr_field << 1;
1292 }
1293 memcpy(data, cur, tmp);
1294 tmp_len -= tmp;
1295 *scrq_arr = hdr_desc;
1296 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001297 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001298 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001299
1300 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001301}
1302
1303/**
1304 * build_hdr_descs_arr - build a header descriptor array
1305 * @skb - socket buffer
1306 * @num_entries - number of descriptors to be sent
1307 * @subcrq - first TX descriptor
1308 * @hdr_field - bit field determining which headers will be sent
1309 *
1310 * This function will build a TX descriptor array with applicable
1311 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1312 */
1313
1314static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1315 int *num_entries, u8 hdr_field)
1316{
1317 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001318 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001319 u8 *hdr_data = txbuff->hdr_data;
1320
1321 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1322 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001323 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001324 txbuff->indir_arr + 1);
1325}
1326
Thomas Falcon032c5e82015-12-21 11:26:06 -06001327static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1328{
1329 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1330 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001331 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001332 struct device *dev = &adapter->vdev->dev;
1333 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001334 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001335 struct ibmvnic_tx_pool *tx_pool;
1336 unsigned int tx_send_failed = 0;
1337 unsigned int tx_map_failed = 0;
1338 unsigned int tx_dropped = 0;
1339 unsigned int tx_packets = 0;
1340 unsigned int tx_bytes = 0;
1341 dma_addr_t data_dma_addr;
1342 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001343 unsigned long lpar_rc;
1344 union sub_crq tx_crq;
1345 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001346 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001347 unsigned char *dst;
1348 u64 *handle_array;
1349 int index = 0;
Thomas Falcona0dca102018-01-18 19:29:48 -06001350 u8 proto = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001351 int ret = 0;
1352
Nathan Fontenoted651a12017-05-03 14:04:38 -04001353 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001354 if (!netif_subqueue_stopped(netdev, skb))
1355 netif_stop_subqueue(netdev, queue_num);
1356 dev_kfree_skb_any(skb);
1357
Thomas Falcon032c5e82015-12-21 11:26:06 -06001358 tx_send_failed++;
1359 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001360 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001361 goto out;
1362 }
1363
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001364 tx_pool = &adapter->tx_pool[queue_num];
1365 tx_scrq = adapter->tx_scrq[queue_num];
1366 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1367 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1368 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1369
Thomas Falcon032c5e82015-12-21 11:26:06 -06001370 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001371
1372 if (skb_is_gso(skb)) {
1373 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1374 dst = tx_pool->tso_ltb.buff + offset;
1375 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1376 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1377 tx_pool->tso_index++;
1378 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1379 tx_pool->tso_index = 0;
1380 } else {
1381 offset = index * adapter->req_mtu;
1382 dst = tx_pool->long_term_buff.buff + offset;
1383 memset(dst, 0, adapter->req_mtu);
1384 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1385 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001386
Thomas Falcon15482052017-10-17 12:36:54 -05001387 if (skb_shinfo(skb)->nr_frags) {
1388 int cur, i;
1389
1390 /* Copy the head */
1391 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1392 cur = skb_headlen(skb);
1393
1394 /* Copy the frags */
1395 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1396 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1397
1398 memcpy(dst + cur,
1399 page_address(skb_frag_page(frag)) +
1400 frag->page_offset, skb_frag_size(frag));
1401 cur += skb_frag_size(frag);
1402 }
1403 } else {
1404 skb_copy_from_linear_data(skb, dst, skb->len);
1405 }
1406
Thomas Falcon032c5e82015-12-21 11:26:06 -06001407 tx_pool->consumer_index =
1408 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001409 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001410
1411 tx_buff = &tx_pool->tx_buff[index];
1412 tx_buff->skb = skb;
1413 tx_buff->data_dma[0] = data_dma_addr;
1414 tx_buff->data_len[0] = skb->len;
1415 tx_buff->index = index;
1416 tx_buff->pool_index = queue_num;
1417 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001418
1419 memset(&tx_crq, 0, sizeof(tx_crq));
1420 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1421 tx_crq.v1.type = IBMVNIC_TX_DESC;
1422 tx_crq.v1.n_crq_elem = 1;
1423 tx_crq.v1.n_sge = 1;
1424 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1425 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001426 if (skb_is_gso(skb))
1427 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1428 else
1429 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001430 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1431 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1432
1433 if (adapter->vlan_header_insertion) {
1434 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1435 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1436 }
1437
1438 if (skb->protocol == htons(ETH_P_IP)) {
Thomas Falcona0dca102018-01-18 19:29:48 -06001439 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1440 proto = ip_hdr(skb)->protocol;
1441 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1442 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1443 proto = ipv6_hdr(skb)->nexthdr;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001444 }
1445
Thomas Falcona0dca102018-01-18 19:29:48 -06001446 if (proto == IPPROTO_TCP)
1447 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1448 else if (proto == IPPROTO_UDP)
1449 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1450
Thomas Falconad7775d2016-04-01 17:20:34 -05001451 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001452 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001453 hdrs += 2;
1454 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001455 if (skb_is_gso(skb)) {
1456 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1457 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1458 hdrs += 2;
1459 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001460 /* determine if l2/3/4 headers are sent to firmware */
John Allen2fa56a42018-02-09 13:19:46 -06001461 if ((*hdrs >> 7) & 1) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001462 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1463 tx_crq.v1.n_crq_elem = num_entries;
Thomas Falconffc385b2018-02-18 10:08:41 -06001464 tx_buff->num_entries = num_entries;
Thomas Falconad7775d2016-04-01 17:20:34 -05001465 tx_buff->indir_arr[0] = tx_crq;
1466 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1467 sizeof(tx_buff->indir_arr),
1468 DMA_TO_DEVICE);
1469 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001470 dev_kfree_skb_any(skb);
1471 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001472 if (!firmware_has_feature(FW_FEATURE_CMO))
1473 dev_err(dev, "tx: unable to map descriptor array\n");
1474 tx_map_failed++;
1475 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001476 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001477 goto out;
1478 }
John Allen498cd8e2016-04-06 11:49:55 -05001479 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001480 (u64)tx_buff->indir_dma,
1481 (u64)num_entries);
1482 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001483 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1484 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001485 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001486 if (lpar_rc != H_SUCCESS) {
1487 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1488
1489 if (tx_pool->consumer_index == 0)
1490 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001491 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001492 else
1493 tx_pool->consumer_index--;
1494
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001495 dev_kfree_skb_any(skb);
1496 tx_buff->skb = NULL;
1497
Thomas Falconb8c80b82017-05-26 10:30:42 -04001498 if (lpar_rc == H_CLOSED) {
1499 /* Disable TX and report carrier off if queue is closed.
1500 * Firmware guarantees that a signal will be sent to the
1501 * driver, triggering a reset or some other action.
1502 */
1503 netif_tx_stop_all_queues(netdev);
1504 netif_carrier_off(netdev);
1505 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001506
Thomas Falcon032c5e82015-12-21 11:26:06 -06001507 tx_send_failed++;
1508 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001509 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001510 goto out;
1511 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001512
Thomas Falconffc385b2018-02-18 10:08:41 -06001513 if (atomic_add_return(num_entries, &tx_scrq->used)
Brian King58c8c0c2017-04-19 13:44:47 -04001514 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001515 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1516 netif_stop_subqueue(netdev, queue_num);
1517 }
1518
Thomas Falcon032c5e82015-12-21 11:26:06 -06001519 tx_packets++;
1520 tx_bytes += skb->len;
1521 txq->trans_start = jiffies;
1522 ret = NETDEV_TX_OK;
1523
1524out:
1525 netdev->stats.tx_dropped += tx_dropped;
1526 netdev->stats.tx_bytes += tx_bytes;
1527 netdev->stats.tx_packets += tx_packets;
1528 adapter->tx_send_failed += tx_send_failed;
1529 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001530 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1531 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1532 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001533
1534 return ret;
1535}
1536
1537static void ibmvnic_set_multi(struct net_device *netdev)
1538{
1539 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1540 struct netdev_hw_addr *ha;
1541 union ibmvnic_crq crq;
1542
1543 memset(&crq, 0, sizeof(crq));
1544 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1545 crq.request_capability.cmd = REQUEST_CAPABILITY;
1546
1547 if (netdev->flags & IFF_PROMISC) {
1548 if (!adapter->promisc_supported)
1549 return;
1550 } else {
1551 if (netdev->flags & IFF_ALLMULTI) {
1552 /* Accept all multicast */
1553 memset(&crq, 0, sizeof(crq));
1554 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1555 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1556 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1557 ibmvnic_send_crq(adapter, &crq);
1558 } else if (netdev_mc_empty(netdev)) {
1559 /* Reject all multicast */
1560 memset(&crq, 0, sizeof(crq));
1561 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1562 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1563 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1564 ibmvnic_send_crq(adapter, &crq);
1565 } else {
1566 /* Accept one or more multicast(s) */
1567 netdev_for_each_mc_addr(ha, netdev) {
1568 memset(&crq, 0, sizeof(crq));
1569 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1570 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1571 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1572 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1573 ha->addr);
1574 ibmvnic_send_crq(adapter, &crq);
1575 }
1576 }
1577 }
1578}
1579
John Allenc26eba02017-10-26 16:23:25 -05001580static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001581{
1582 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1583 struct sockaddr *addr = p;
1584 union ibmvnic_crq crq;
1585
1586 if (!is_valid_ether_addr(addr->sa_data))
1587 return -EADDRNOTAVAIL;
1588
1589 memset(&crq, 0, sizeof(crq));
1590 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1591 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1592 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
Thomas Falconf8136142018-01-29 13:45:05 -06001593
1594 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001595 ibmvnic_send_crq(adapter, &crq);
Thomas Falconf8136142018-01-29 13:45:05 -06001596 wait_for_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001597 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
Thomas Falconf8136142018-01-29 13:45:05 -06001598 return adapter->fw_done_rc ? -EIO : 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001599}
1600
John Allenc26eba02017-10-26 16:23:25 -05001601static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1602{
1603 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1604 struct sockaddr *addr = p;
Thomas Falconf8136142018-01-29 13:45:05 -06001605 int rc;
John Allenc26eba02017-10-26 16:23:25 -05001606
Thomas Falcon3d166132018-01-10 19:39:52 -06001607 if (adapter->state == VNIC_PROBED) {
John Allenc26eba02017-10-26 16:23:25 -05001608 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1609 adapter->mac_change_pending = true;
1610 return 0;
1611 }
1612
Thomas Falconf8136142018-01-29 13:45:05 -06001613 rc = __ibmvnic_set_mac(netdev, addr);
John Allenc26eba02017-10-26 16:23:25 -05001614
Thomas Falconf8136142018-01-29 13:45:05 -06001615 return rc;
John Allenc26eba02017-10-26 16:23:25 -05001616}
1617
Nathan Fontenoted651a12017-05-03 14:04:38 -04001618/**
1619 * do_reset returns zero if we are able to keep processing reset events, or
1620 * non-zero if we hit a fatal error and must halt.
1621 */
1622static int do_reset(struct ibmvnic_adapter *adapter,
1623 struct ibmvnic_rwi *rwi, u32 reset_state)
1624{
John Allen896d8692018-01-18 16:26:31 -06001625 u64 old_num_rx_queues, old_num_tx_queues;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001626 struct net_device *netdev = adapter->netdev;
1627 int i, rc;
1628
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001629 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1630 rwi->reset_reason);
1631
Nathan Fontenoted651a12017-05-03 14:04:38 -04001632 netif_carrier_off(netdev);
1633 adapter->reset_reason = rwi->reset_reason;
1634
John Allen896d8692018-01-18 16:26:31 -06001635 old_num_rx_queues = adapter->req_rx_queues;
1636 old_num_tx_queues = adapter->req_tx_queues;
1637
Nathan Fontenoted651a12017-05-03 14:04:38 -04001638 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1639 rc = ibmvnic_reenable_crq_queue(adapter);
1640 if (rc)
1641 return 0;
1642 }
1643
1644 rc = __ibmvnic_close(netdev);
1645 if (rc)
1646 return rc;
1647
John Allenc26eba02017-10-26 16:23:25 -05001648 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1649 adapter->wait_for_reset) {
1650 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06001651 release_sub_crqs(adapter, 1);
John Allenc26eba02017-10-26 16:23:25 -05001652 release_crq_queue(adapter);
1653 }
1654
John Allen8cb31cf2017-05-26 10:30:37 -04001655 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1656 /* remove the closed state so when we call open it appears
1657 * we are coming from the probed state.
1658 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001659 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001660
John Allen8cb31cf2017-05-26 10:30:37 -04001661 rc = ibmvnic_init(adapter);
1662 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001663 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001664
1665 /* If the adapter was in PROBE state prior to the reset,
1666 * exit here.
1667 */
1668 if (reset_state == VNIC_PROBED)
1669 return 0;
1670
1671 rc = ibmvnic_login(netdev);
1672 if (rc) {
1673 adapter->state = VNIC_PROBED;
1674 return 0;
1675 }
1676
John Allenc26eba02017-10-26 16:23:25 -05001677 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1678 adapter->wait_for_reset) {
1679 rc = init_resources(adapter);
1680 if (rc)
1681 return rc;
John Allen896d8692018-01-18 16:26:31 -06001682 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1683 adapter->req_tx_queues != old_num_tx_queues) {
Thomas Falconfaefaa92018-02-09 11:41:09 -06001684 adapter->map_id = 1;
John Allen896d8692018-01-18 16:26:31 -06001685 release_rx_pools(adapter);
1686 release_tx_pools(adapter);
1687 init_rx_pools(netdev);
1688 init_tx_pools(netdev);
Nathan Fontenotd9043c12018-02-19 13:30:14 -06001689
1690 adapter->num_active_tx_scrqs = adapter->req_tx_queues;
1691 adapter->num_active_rx_scrqs = adapter->req_rx_queues;
John Allenc26eba02017-10-26 16:23:25 -05001692 } else {
1693 rc = reset_tx_pools(adapter);
1694 if (rc)
1695 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001696
John Allenc26eba02017-10-26 16:23:25 -05001697 rc = reset_rx_pools(adapter);
1698 if (rc)
1699 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001700
John Allenc26eba02017-10-26 16:23:25 -05001701 if (reset_state == VNIC_CLOSED)
1702 return 0;
1703 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001704 }
1705
Nathan Fontenoted651a12017-05-03 14:04:38 -04001706 rc = __ibmvnic_open(netdev);
1707 if (rc) {
1708 if (list_empty(&adapter->rwi_list))
1709 adapter->state = VNIC_CLOSED;
1710 else
1711 adapter->state = reset_state;
1712
1713 return 0;
1714 }
1715
Nathan Fontenoted651a12017-05-03 14:04:38 -04001716 /* kick napi */
1717 for (i = 0; i < adapter->req_rx_queues; i++)
1718 napi_schedule(&adapter->napi[i]);
1719
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001720 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1721 netdev_notify_peers(netdev);
1722
Thomas Falconcc85c022018-02-13 15:32:50 -06001723 netif_carrier_on(netdev);
1724
Nathan Fontenoted651a12017-05-03 14:04:38 -04001725 return 0;
1726}
1727
1728static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1729{
1730 struct ibmvnic_rwi *rwi;
1731
1732 mutex_lock(&adapter->rwi_lock);
1733
1734 if (!list_empty(&adapter->rwi_list)) {
1735 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1736 list);
1737 list_del(&rwi->list);
1738 } else {
1739 rwi = NULL;
1740 }
1741
1742 mutex_unlock(&adapter->rwi_lock);
1743 return rwi;
1744}
1745
1746static void free_all_rwi(struct ibmvnic_adapter *adapter)
1747{
1748 struct ibmvnic_rwi *rwi;
1749
1750 rwi = get_next_rwi(adapter);
1751 while (rwi) {
1752 kfree(rwi);
1753 rwi = get_next_rwi(adapter);
1754 }
1755}
1756
1757static void __ibmvnic_reset(struct work_struct *work)
1758{
1759 struct ibmvnic_rwi *rwi;
1760 struct ibmvnic_adapter *adapter;
1761 struct net_device *netdev;
1762 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001763 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001764
1765 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1766 netdev = adapter->netdev;
1767
1768 mutex_lock(&adapter->reset_lock);
1769 adapter->resetting = true;
1770 reset_state = adapter->state;
1771
1772 rwi = get_next_rwi(adapter);
1773 while (rwi) {
1774 rc = do_reset(adapter, rwi, reset_state);
1775 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001776 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001777 break;
1778
1779 rwi = get_next_rwi(adapter);
1780 }
1781
John Allenc26eba02017-10-26 16:23:25 -05001782 if (adapter->wait_for_reset) {
1783 adapter->wait_for_reset = false;
1784 adapter->reset_done_rc = rc;
1785 complete(&adapter->reset_done);
1786 }
1787
Nathan Fontenoted651a12017-05-03 14:04:38 -04001788 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001789 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001790 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001791 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001792 return;
1793 }
1794
1795 adapter->resetting = false;
1796 mutex_unlock(&adapter->reset_lock);
1797}
1798
1799static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1800 enum ibmvnic_reset_reason reason)
1801{
1802 struct ibmvnic_rwi *rwi, *tmp;
1803 struct net_device *netdev = adapter->netdev;
1804 struct list_head *entry;
1805
1806 if (adapter->state == VNIC_REMOVING ||
1807 adapter->state == VNIC_REMOVED) {
1808 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1809 return;
1810 }
1811
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001812 if (adapter->state == VNIC_PROBING) {
1813 netdev_warn(netdev, "Adapter reset during probe\n");
1814 adapter->init_done_rc = EAGAIN;
1815 return;
1816 }
1817
Nathan Fontenoted651a12017-05-03 14:04:38 -04001818 mutex_lock(&adapter->rwi_lock);
1819
1820 list_for_each(entry, &adapter->rwi_list) {
1821 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1822 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001823 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001824 mutex_unlock(&adapter->rwi_lock);
1825 return;
1826 }
1827 }
1828
1829 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1830 if (!rwi) {
1831 mutex_unlock(&adapter->rwi_lock);
1832 ibmvnic_close(netdev);
1833 return;
1834 }
1835
1836 rwi->reset_reason = reason;
1837 list_add_tail(&rwi->list, &adapter->rwi_list);
1838 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001839
1840 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001841 schedule_work(&adapter->ibmvnic_reset);
1842}
1843
Thomas Falcon032c5e82015-12-21 11:26:06 -06001844static void ibmvnic_tx_timeout(struct net_device *dev)
1845{
1846 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001847
Nathan Fontenoted651a12017-05-03 14:04:38 -04001848 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001849}
1850
1851static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1852 struct ibmvnic_rx_buff *rx_buff)
1853{
1854 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1855
1856 rx_buff->skb = NULL;
1857
1858 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1859 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1860
1861 atomic_dec(&pool->available);
1862}
1863
1864static int ibmvnic_poll(struct napi_struct *napi, int budget)
1865{
1866 struct net_device *netdev = napi->dev;
1867 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1868 int scrq_num = (int)(napi - adapter->napi);
1869 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001870
Thomas Falcon032c5e82015-12-21 11:26:06 -06001871restart_poll:
1872 while (frames_processed < budget) {
1873 struct sk_buff *skb;
1874 struct ibmvnic_rx_buff *rx_buff;
1875 union sub_crq *next;
1876 u32 length;
1877 u16 offset;
1878 u8 flags = 0;
1879
John Allen34686562018-02-06 16:21:49 -06001880 if (unlikely(adapter->resetting &&
1881 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
Thomas Falcon21ecba62017-06-14 23:50:09 -05001882 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1883 napi_complete_done(napi, frames_processed);
1884 return frames_processed;
1885 }
1886
Thomas Falcon032c5e82015-12-21 11:26:06 -06001887 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1888 break;
1889 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1890 rx_buff =
1891 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1892 rx_comp.correlator);
1893 /* do error checking */
1894 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001895 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1896 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001897 /* free the entry */
1898 next->rx_comp.first = 0;
Thomas Falcon4b9b0f02018-02-13 18:23:42 -06001899 dev_kfree_skb_any(rx_buff->skb);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001900 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001901 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001902 }
1903
1904 length = be32_to_cpu(next->rx_comp.len);
1905 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1906 flags = next->rx_comp.flags;
1907 skb = rx_buff->skb;
1908 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1909 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001910
1911 /* VLAN Header has been stripped by the system firmware and
1912 * needs to be inserted by the driver
1913 */
1914 if (adapter->rx_vlan_header_insertion &&
1915 (flags & IBMVNIC_VLAN_STRIPPED))
1916 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1917 ntohs(next->rx_comp.vlan_tci));
1918
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919 /* free the entry */
1920 next->rx_comp.first = 0;
1921 remove_buff_from_pool(adapter, rx_buff);
1922
1923 skb_put(skb, length);
1924 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001925 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001926
1927 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1928 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1929 skb->ip_summed = CHECKSUM_UNNECESSARY;
1930 }
1931
1932 length = skb->len;
1933 napi_gro_receive(napi, skb); /* send it up */
1934 netdev->stats.rx_packets++;
1935 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001936 adapter->rx_stats_buffers[scrq_num].packets++;
1937 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001938 frames_processed++;
1939 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001940
1941 if (adapter->state != VNIC_CLOSING)
1942 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001943
1944 if (frames_processed < budget) {
1945 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001946 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001947 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1948 napi_reschedule(napi)) {
1949 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1950 goto restart_poll;
1951 }
1952 }
1953 return frames_processed;
1954}
1955
1956#ifdef CONFIG_NET_POLL_CONTROLLER
1957static void ibmvnic_netpoll_controller(struct net_device *dev)
1958{
1959 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1960 int i;
1961
1962 replenish_pools(netdev_priv(dev));
1963 for (i = 0; i < adapter->req_rx_queues; i++)
1964 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1965 adapter->rx_scrq[i]);
1966}
1967#endif
1968
John Allenc26eba02017-10-26 16:23:25 -05001969static int wait_for_reset(struct ibmvnic_adapter *adapter)
1970{
1971 adapter->fallback.mtu = adapter->req_mtu;
1972 adapter->fallback.rx_queues = adapter->req_rx_queues;
1973 adapter->fallback.tx_queues = adapter->req_tx_queues;
1974 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1975 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1976
1977 init_completion(&adapter->reset_done);
1978 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1979 adapter->wait_for_reset = true;
1980 wait_for_completion(&adapter->reset_done);
1981
1982 if (adapter->reset_done_rc) {
1983 adapter->desired.mtu = adapter->fallback.mtu;
1984 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1985 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1986 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1987 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1988
1989 init_completion(&adapter->reset_done);
1990 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1991 wait_for_completion(&adapter->reset_done);
1992 }
1993 adapter->wait_for_reset = false;
1994
1995 return adapter->reset_done_rc;
1996}
1997
John Allen3a807b72017-06-06 16:55:52 -05001998static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1999{
John Allenc26eba02017-10-26 16:23:25 -05002000 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2001
2002 adapter->desired.mtu = new_mtu + ETH_HLEN;
2003
2004 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05002005}
2006
Thomas Falcon032c5e82015-12-21 11:26:06 -06002007static const struct net_device_ops ibmvnic_netdev_ops = {
2008 .ndo_open = ibmvnic_open,
2009 .ndo_stop = ibmvnic_close,
2010 .ndo_start_xmit = ibmvnic_xmit,
2011 .ndo_set_rx_mode = ibmvnic_set_multi,
2012 .ndo_set_mac_address = ibmvnic_set_mac,
2013 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002014 .ndo_tx_timeout = ibmvnic_tx_timeout,
2015#ifdef CONFIG_NET_POLL_CONTROLLER
2016 .ndo_poll_controller = ibmvnic_netpoll_controller,
2017#endif
John Allen3a807b72017-06-06 16:55:52 -05002018 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002019};
2020
2021/* ethtool functions */
2022
Philippe Reynes8a433792017-01-07 22:37:29 +01002023static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2024 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002025{
Philippe Reynes8a433792017-01-07 22:37:29 +01002026 u32 supported, advertising;
2027
2028 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002029 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002030 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06002031 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01002032 cmd->base.speed = SPEED_1000;
2033 cmd->base.duplex = DUPLEX_FULL;
2034 cmd->base.port = PORT_FIBRE;
2035 cmd->base.phy_address = 0;
2036 cmd->base.autoneg = AUTONEG_ENABLE;
2037
2038 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2039 supported);
2040 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2041 advertising);
2042
Thomas Falcon032c5e82015-12-21 11:26:06 -06002043 return 0;
2044}
2045
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002046static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002047 struct ethtool_drvinfo *info)
2048{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002049 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2050
Thomas Falcon032c5e82015-12-21 11:26:06 -06002051 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2052 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02002053 strlcpy(info->fw_version, adapter->fw_version,
2054 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055}
2056
2057static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2058{
2059 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2060
2061 return adapter->msg_enable;
2062}
2063
2064static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2065{
2066 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2067
2068 adapter->msg_enable = data;
2069}
2070
2071static u32 ibmvnic_get_link(struct net_device *netdev)
2072{
2073 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2074
2075 /* Don't need to send a query because we request a logical link up at
2076 * init and then we wait for link state indications
2077 */
2078 return adapter->logical_link_state;
2079}
2080
2081static void ibmvnic_get_ringparam(struct net_device *netdev,
2082 struct ethtool_ringparam *ring)
2083{
John Allenbc131b32017-08-02 16:46:30 -05002084 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2085
2086 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2087 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002088 ring->rx_mini_max_pending = 0;
2089 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002090 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2091 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002092 ring->rx_mini_pending = 0;
2093 ring->rx_jumbo_pending = 0;
2094}
2095
John Allenc26eba02017-10-26 16:23:25 -05002096static int ibmvnic_set_ringparam(struct net_device *netdev,
2097 struct ethtool_ringparam *ring)
2098{
2099 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2100
2101 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2102 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2103 netdev_err(netdev, "Invalid request.\n");
2104 netdev_err(netdev, "Max tx buffers = %llu\n",
2105 adapter->max_rx_add_entries_per_subcrq);
2106 netdev_err(netdev, "Max rx buffers = %llu\n",
2107 adapter->max_tx_entries_per_subcrq);
2108 return -EINVAL;
2109 }
2110
2111 adapter->desired.rx_entries = ring->rx_pending;
2112 adapter->desired.tx_entries = ring->tx_pending;
2113
2114 return wait_for_reset(adapter);
2115}
2116
John Allenc2dbeb62017-08-02 16:47:17 -05002117static void ibmvnic_get_channels(struct net_device *netdev,
2118 struct ethtool_channels *channels)
2119{
2120 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2121
2122 channels->max_rx = adapter->max_rx_queues;
2123 channels->max_tx = adapter->max_tx_queues;
2124 channels->max_other = 0;
2125 channels->max_combined = 0;
2126 channels->rx_count = adapter->req_rx_queues;
2127 channels->tx_count = adapter->req_tx_queues;
2128 channels->other_count = 0;
2129 channels->combined_count = 0;
2130}
2131
John Allenc26eba02017-10-26 16:23:25 -05002132static int ibmvnic_set_channels(struct net_device *netdev,
2133 struct ethtool_channels *channels)
2134{
2135 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2136
2137 adapter->desired.rx_queues = channels->rx_count;
2138 adapter->desired.tx_queues = channels->tx_count;
2139
2140 return wait_for_reset(adapter);
2141}
2142
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2144{
John Allen3d52b592017-08-02 16:44:14 -05002145 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002146 int i;
2147
2148 if (stringset != ETH_SS_STATS)
2149 return;
2150
2151 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2152 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002153
2154 for (i = 0; i < adapter->req_tx_queues; i++) {
2155 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2156 data += ETH_GSTRING_LEN;
2157
2158 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2159 data += ETH_GSTRING_LEN;
2160
2161 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2162 data += ETH_GSTRING_LEN;
2163 }
2164
2165 for (i = 0; i < adapter->req_rx_queues; i++) {
2166 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2167 data += ETH_GSTRING_LEN;
2168
2169 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2170 data += ETH_GSTRING_LEN;
2171
2172 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2173 data += ETH_GSTRING_LEN;
2174 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002175}
2176
2177static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2178{
John Allen3d52b592017-08-02 16:44:14 -05002179 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2180
Thomas Falcon032c5e82015-12-21 11:26:06 -06002181 switch (sset) {
2182 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002183 return ARRAY_SIZE(ibmvnic_stats) +
2184 adapter->req_tx_queues * NUM_TX_STATS +
2185 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002186 default:
2187 return -EOPNOTSUPP;
2188 }
2189}
2190
2191static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2192 struct ethtool_stats *stats, u64 *data)
2193{
2194 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2195 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002196 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002197
2198 memset(&crq, 0, sizeof(crq));
2199 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2200 crq.request_statistics.cmd = REQUEST_STATISTICS;
2201 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2202 crq.request_statistics.len =
2203 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002204
2205 /* Wait for data to be written */
2206 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002207 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002208 wait_for_completion(&adapter->stats_done);
2209
2210 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002211 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2212 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002213
2214 for (j = 0; j < adapter->req_tx_queues; j++) {
2215 data[i] = adapter->tx_stats_buffers[j].packets;
2216 i++;
2217 data[i] = adapter->tx_stats_buffers[j].bytes;
2218 i++;
2219 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2220 i++;
2221 }
2222
2223 for (j = 0; j < adapter->req_rx_queues; j++) {
2224 data[i] = adapter->rx_stats_buffers[j].packets;
2225 i++;
2226 data[i] = adapter->rx_stats_buffers[j].bytes;
2227 i++;
2228 data[i] = adapter->rx_stats_buffers[j].interrupts;
2229 i++;
2230 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002231}
2232
2233static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002234 .get_drvinfo = ibmvnic_get_drvinfo,
2235 .get_msglevel = ibmvnic_get_msglevel,
2236 .set_msglevel = ibmvnic_set_msglevel,
2237 .get_link = ibmvnic_get_link,
2238 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002239 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002240 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002241 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002242 .get_strings = ibmvnic_get_strings,
2243 .get_sset_count = ibmvnic_get_sset_count,
2244 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002245 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002246};
2247
2248/* Routines for managing CRQs/sCRQs */
2249
Nathan Fontenot57a49432017-05-26 10:31:12 -04002250static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2251 struct ibmvnic_sub_crq_queue *scrq)
2252{
2253 int rc;
2254
2255 if (scrq->irq) {
2256 free_irq(scrq->irq, scrq);
2257 irq_dispose_mapping(scrq->irq);
2258 scrq->irq = 0;
2259 }
2260
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002261 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002262 scrq->cur = 0;
2263
2264 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2265 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2266 return rc;
2267}
2268
2269static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2270{
2271 int i, rc;
2272
2273 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002274 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002275 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2276 if (rc)
2277 return rc;
2278 }
2279
2280 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002281 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002282 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2283 if (rc)
2284 return rc;
2285 }
2286
Nathan Fontenot57a49432017-05-26 10:31:12 -04002287 return rc;
2288}
2289
Thomas Falcon032c5e82015-12-21 11:26:06 -06002290static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002291 struct ibmvnic_sub_crq_queue *scrq,
2292 bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002293{
2294 struct device *dev = &adapter->vdev->dev;
2295 long rc;
2296
2297 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2298
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002299 if (do_h_free) {
2300 /* Close the sub-crqs */
2301 do {
2302 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2303 adapter->vdev->unit_address,
2304 scrq->crq_num);
2305 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002306
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002307 if (rc) {
2308 netdev_err(adapter->netdev,
2309 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2310 scrq->crq_num, rc);
2311 }
Thomas Falconffa73852017-04-19 13:44:29 -04002312 }
2313
Thomas Falcon032c5e82015-12-21 11:26:06 -06002314 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2315 DMA_BIDIRECTIONAL);
2316 free_pages((unsigned long)scrq->msgs, 2);
2317 kfree(scrq);
2318}
2319
2320static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2321 *adapter)
2322{
2323 struct device *dev = &adapter->vdev->dev;
2324 struct ibmvnic_sub_crq_queue *scrq;
2325 int rc;
2326
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002327 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328 if (!scrq)
2329 return NULL;
2330
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002331 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002332 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002333 if (!scrq->msgs) {
2334 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2335 goto zero_page_failed;
2336 }
2337
2338 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2339 DMA_BIDIRECTIONAL);
2340 if (dma_mapping_error(dev, scrq->msg_token)) {
2341 dev_warn(dev, "Couldn't map crq queue messages page\n");
2342 goto map_failed;
2343 }
2344
2345 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2346 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2347
2348 if (rc == H_RESOURCE)
2349 rc = ibmvnic_reset_crq(adapter);
2350
2351 if (rc == H_CLOSED) {
2352 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2353 } else if (rc) {
2354 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2355 goto reg_failed;
2356 }
2357
Thomas Falcon032c5e82015-12-21 11:26:06 -06002358 scrq->adapter = adapter;
2359 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002360 spin_lock_init(&scrq->lock);
2361
2362 netdev_dbg(adapter->netdev,
2363 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2364 scrq->crq_num, scrq->hw_irq, scrq->irq);
2365
2366 return scrq;
2367
Thomas Falcon032c5e82015-12-21 11:26:06 -06002368reg_failed:
2369 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2370 DMA_BIDIRECTIONAL);
2371map_failed:
2372 free_pages((unsigned long)scrq->msgs, 2);
2373zero_page_failed:
2374 kfree(scrq);
2375
2376 return NULL;
2377}
2378
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002379static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002380{
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002381 u64 num_tx_scrqs, num_rx_scrqs;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002382 int i;
2383
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002384 if (adapter->state == VNIC_PROBED) {
2385 num_tx_scrqs = adapter->req_tx_queues;
2386 num_rx_scrqs = adapter->req_rx_queues;
2387 } else {
2388 num_tx_scrqs = adapter->num_active_tx_scrqs;
2389 num_rx_scrqs = adapter->num_active_rx_scrqs;
2390 }
2391
Thomas Falcon032c5e82015-12-21 11:26:06 -06002392 if (adapter->tx_scrq) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002393 for (i = 0; i < num_tx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002394 if (!adapter->tx_scrq[i])
2395 continue;
2396
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002397 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2398 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002399 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002400 free_irq(adapter->tx_scrq[i]->irq,
2401 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002402 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002403 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002404 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002405
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002406 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2407 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002408 }
2409
Nathan Fontenot9501df32017-03-15 23:38:07 -04002410 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002411 adapter->tx_scrq = NULL;
2412 }
2413
2414 if (adapter->rx_scrq) {
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002415 for (i = 0; i < num_rx_scrqs; i++) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002416 if (!adapter->rx_scrq[i])
2417 continue;
2418
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002419 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2420 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002421 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002422 free_irq(adapter->rx_scrq[i]->irq,
2423 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002424 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002425 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002426 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002427
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002428 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2429 do_h_free);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002430 }
2431
Nathan Fontenot9501df32017-03-15 23:38:07 -04002432 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002433 adapter->rx_scrq = NULL;
2434 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002435}
2436
2437static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2438 struct ibmvnic_sub_crq_queue *scrq)
2439{
2440 struct device *dev = &adapter->vdev->dev;
2441 unsigned long rc;
2442
2443 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2444 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2445 if (rc)
2446 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2447 scrq->hw_irq, rc);
2448 return rc;
2449}
2450
2451static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2452 struct ibmvnic_sub_crq_queue *scrq)
2453{
2454 struct device *dev = &adapter->vdev->dev;
2455 unsigned long rc;
2456
2457 if (scrq->hw_irq > 0x100000000ULL) {
2458 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2459 return 1;
2460 }
2461
2462 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2463 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2464 if (rc)
2465 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2466 scrq->hw_irq, rc);
2467 return rc;
2468}
2469
2470static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2471 struct ibmvnic_sub_crq_queue *scrq)
2472{
2473 struct device *dev = &adapter->vdev->dev;
2474 struct ibmvnic_tx_buff *txbuff;
2475 union sub_crq *next;
2476 int index;
2477 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002478 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002479
2480restart_loop:
2481 while (pending_scrq(adapter, scrq)) {
2482 unsigned int pool = scrq->pool_index;
Thomas Falconffc385b2018-02-18 10:08:41 -06002483 int num_entries = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002484
2485 next = ibmvnic_next_scrq(adapter, scrq);
2486 for (i = 0; i < next->tx_comp.num_comps; i++) {
2487 if (next->tx_comp.rcs[i]) {
2488 dev_err(dev, "tx error %x\n",
2489 next->tx_comp.rcs[i]);
2490 continue;
2491 }
2492 index = be32_to_cpu(next->tx_comp.correlators[i]);
2493 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2494
2495 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2496 if (!txbuff->data_dma[j])
2497 continue;
2498
2499 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002500 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002501 /* if sub_crq was sent indirectly */
2502 first = txbuff->indir_arr[0].generic.first;
2503 if (first == IBMVNIC_CRQ_CMD) {
2504 dma_unmap_single(dev, txbuff->indir_dma,
2505 sizeof(txbuff->indir_arr),
2506 DMA_TO_DEVICE);
2507 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002508
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002509 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002510 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002511 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002512 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002513
Thomas Falconffc385b2018-02-18 10:08:41 -06002514 num_entries += txbuff->num_entries;
2515
Thomas Falcon032c5e82015-12-21 11:26:06 -06002516 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2517 producer_index] = index;
2518 adapter->tx_pool[pool].producer_index =
2519 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002520 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002521 }
2522 /* remove tx_comp scrq*/
2523 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002524
Thomas Falconffc385b2018-02-18 10:08:41 -06002525 if (atomic_sub_return(num_entries, &scrq->used) <=
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002526 (adapter->req_tx_entries_per_subcrq / 2) &&
2527 __netif_subqueue_stopped(adapter->netdev,
2528 scrq->pool_index)) {
2529 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2530 netdev_info(adapter->netdev, "Started queue %d\n",
2531 scrq->pool_index);
2532 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533 }
2534
2535 enable_scrq_irq(adapter, scrq);
2536
2537 if (pending_scrq(adapter, scrq)) {
2538 disable_scrq_irq(adapter, scrq);
2539 goto restart_loop;
2540 }
2541
2542 return 0;
2543}
2544
2545static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2546{
2547 struct ibmvnic_sub_crq_queue *scrq = instance;
2548 struct ibmvnic_adapter *adapter = scrq->adapter;
2549
2550 disable_scrq_irq(adapter, scrq);
2551 ibmvnic_complete_tx(adapter, scrq);
2552
2553 return IRQ_HANDLED;
2554}
2555
2556static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2557{
2558 struct ibmvnic_sub_crq_queue *scrq = instance;
2559 struct ibmvnic_adapter *adapter = scrq->adapter;
2560
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002561 /* When booting a kdump kernel we can hit pending interrupts
2562 * prior to completing driver initialization.
2563 */
2564 if (unlikely(adapter->state != VNIC_OPEN))
2565 return IRQ_NONE;
2566
John Allen3d52b592017-08-02 16:44:14 -05002567 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2568
Thomas Falcon032c5e82015-12-21 11:26:06 -06002569 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2570 disable_scrq_irq(adapter, scrq);
2571 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2572 }
2573
2574 return IRQ_HANDLED;
2575}
2576
Thomas Falconea22d512016-07-06 15:35:17 -05002577static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2578{
2579 struct device *dev = &adapter->vdev->dev;
2580 struct ibmvnic_sub_crq_queue *scrq;
2581 int i = 0, j = 0;
2582 int rc = 0;
2583
2584 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002585 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2586 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002587 scrq = adapter->tx_scrq[i];
2588 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2589
Michael Ellerman99c17902016-09-10 19:59:05 +10002590 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002591 rc = -EINVAL;
2592 dev_err(dev, "Error mapping irq\n");
2593 goto req_tx_irq_failed;
2594 }
2595
2596 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2597 0, "ibmvnic_tx", scrq);
2598
2599 if (rc) {
2600 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2601 scrq->irq, rc);
2602 irq_dispose_mapping(scrq->irq);
2603 goto req_rx_irq_failed;
2604 }
2605 }
2606
2607 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002608 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2609 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002610 scrq = adapter->rx_scrq[i];
2611 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002612 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002613 rc = -EINVAL;
2614 dev_err(dev, "Error mapping irq\n");
2615 goto req_rx_irq_failed;
2616 }
2617 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2618 0, "ibmvnic_rx", scrq);
2619 if (rc) {
2620 dev_err(dev, "Couldn't register rx 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 return rc;
2627
2628req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002629 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002630 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2631 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002632 }
Thomas Falconea22d512016-07-06 15:35:17 -05002633 i = adapter->req_tx_queues;
2634req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002635 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002636 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2637 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002638 }
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002639 release_sub_crqs(adapter, 1);
Thomas Falconea22d512016-07-06 15:35:17 -05002640 return rc;
2641}
2642
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002643static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002644{
2645 struct device *dev = &adapter->vdev->dev;
2646 struct ibmvnic_sub_crq_queue **allqueues;
2647 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002648 int total_queues;
2649 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002650 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002651
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2653
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002654 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002655 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002656 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002657
2658 for (i = 0; i < total_queues; i++) {
2659 allqueues[i] = init_sub_crq_queue(adapter);
2660 if (!allqueues[i]) {
2661 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2662 break;
2663 }
2664 registered_queues++;
2665 }
2666
2667 /* Make sure we were able to register the minimum number of queues */
2668 if (registered_queues <
2669 adapter->min_tx_queues + adapter->min_rx_queues) {
2670 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2671 goto tx_failed;
2672 }
2673
2674 /* Distribute the failed allocated queues*/
2675 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2676 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2677 switch (i % 3) {
2678 case 0:
2679 if (adapter->req_rx_queues > adapter->min_rx_queues)
2680 adapter->req_rx_queues--;
2681 else
2682 more++;
2683 break;
2684 case 1:
2685 if (adapter->req_tx_queues > adapter->min_tx_queues)
2686 adapter->req_tx_queues--;
2687 else
2688 more++;
2689 break;
2690 }
2691 }
2692
2693 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002694 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 if (!adapter->tx_scrq)
2696 goto tx_failed;
2697
2698 for (i = 0; i < adapter->req_tx_queues; i++) {
2699 adapter->tx_scrq[i] = allqueues[i];
2700 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002701 }
2702
2703 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002704 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002705 if (!adapter->rx_scrq)
2706 goto rx_failed;
2707
2708 for (i = 0; i < adapter->req_rx_queues; i++) {
2709 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2710 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002711 }
2712
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002713 kfree(allqueues);
2714 return 0;
2715
2716rx_failed:
2717 kfree(adapter->tx_scrq);
2718 adapter->tx_scrq = NULL;
2719tx_failed:
2720 for (i = 0; i < registered_queues; i++)
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06002721 release_sub_crq_queue(adapter, allqueues[i], 1);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002722 kfree(allqueues);
2723 return -1;
2724}
2725
2726static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2727{
2728 struct device *dev = &adapter->vdev->dev;
2729 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002730 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002731
2732 if (!retry) {
2733 /* Sub-CRQ entries are 32 byte long */
2734 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2735
2736 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2737 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2738 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2739 return;
2740 }
2741
John Allenc26eba02017-10-26 16:23:25 -05002742 if (adapter->desired.mtu)
2743 adapter->req_mtu = adapter->desired.mtu;
2744 else
2745 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002746
John Allenc26eba02017-10-26 16:23:25 -05002747 if (!adapter->desired.tx_entries)
2748 adapter->desired.tx_entries =
2749 adapter->max_tx_entries_per_subcrq;
2750 if (!adapter->desired.rx_entries)
2751 adapter->desired.rx_entries =
2752 adapter->max_rx_add_entries_per_subcrq;
2753
2754 max_entries = IBMVNIC_MAX_LTB_SIZE /
2755 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2756
2757 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2758 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2759 adapter->desired.tx_entries = max_entries;
2760 }
2761
2762 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2763 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2764 adapter->desired.rx_entries = max_entries;
2765 }
2766
2767 if (adapter->desired.tx_entries)
2768 adapter->req_tx_entries_per_subcrq =
2769 adapter->desired.tx_entries;
2770 else
2771 adapter->req_tx_entries_per_subcrq =
2772 adapter->max_tx_entries_per_subcrq;
2773
2774 if (adapter->desired.rx_entries)
2775 adapter->req_rx_add_entries_per_subcrq =
2776 adapter->desired.rx_entries;
2777 else
2778 adapter->req_rx_add_entries_per_subcrq =
2779 adapter->max_rx_add_entries_per_subcrq;
2780
2781 if (adapter->desired.tx_queues)
2782 adapter->req_tx_queues =
2783 adapter->desired.tx_queues;
2784 else
2785 adapter->req_tx_queues =
2786 adapter->opt_tx_comp_sub_queues;
2787
2788 if (adapter->desired.rx_queues)
2789 adapter->req_rx_queues =
2790 adapter->desired.rx_queues;
2791 else
2792 adapter->req_rx_queues =
2793 adapter->opt_rx_comp_queues;
2794
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002795 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002796 }
2797
Thomas Falcon032c5e82015-12-21 11:26:06 -06002798 memset(&crq, 0, sizeof(crq));
2799 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2800 crq.request_capability.cmd = REQUEST_CAPABILITY;
2801
2802 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002803 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002804 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 ibmvnic_send_crq(adapter, &crq);
2806
2807 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002808 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002809 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002810 ibmvnic_send_crq(adapter, &crq);
2811
2812 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002813 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002814 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002815 ibmvnic_send_crq(adapter, &crq);
2816
2817 crq.request_capability.capability =
2818 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2819 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002820 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002821 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002822 ibmvnic_send_crq(adapter, &crq);
2823
2824 crq.request_capability.capability =
2825 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2826 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002827 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002828 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002829 ibmvnic_send_crq(adapter, &crq);
2830
2831 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002832 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002833 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834 ibmvnic_send_crq(adapter, &crq);
2835
2836 if (adapter->netdev->flags & IFF_PROMISC) {
2837 if (adapter->promisc_supported) {
2838 crq.request_capability.capability =
2839 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002840 crq.request_capability.number = cpu_to_be64(1);
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 } else {
2845 crq.request_capability.capability =
2846 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002847 crq.request_capability.number = cpu_to_be64(0);
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 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002851}
2852
2853static int pending_scrq(struct ibmvnic_adapter *adapter,
2854 struct ibmvnic_sub_crq_queue *scrq)
2855{
2856 union sub_crq *entry = &scrq->msgs[scrq->cur];
2857
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002858 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002859 return 1;
2860 else
2861 return 0;
2862}
2863
2864static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2865 struct ibmvnic_sub_crq_queue *scrq)
2866{
2867 union sub_crq *entry;
2868 unsigned long flags;
2869
2870 spin_lock_irqsave(&scrq->lock, flags);
2871 entry = &scrq->msgs[scrq->cur];
2872 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2873 if (++scrq->cur == scrq->size)
2874 scrq->cur = 0;
2875 } else {
2876 entry = NULL;
2877 }
2878 spin_unlock_irqrestore(&scrq->lock, flags);
2879
2880 return entry;
2881}
2882
2883static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2884{
2885 struct ibmvnic_crq_queue *queue = &adapter->crq;
2886 union ibmvnic_crq *crq;
2887
2888 crq = &queue->msgs[queue->cur];
2889 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2890 if (++queue->cur == queue->size)
2891 queue->cur = 0;
2892 } else {
2893 crq = NULL;
2894 }
2895
2896 return crq;
2897}
2898
2899static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2900 union sub_crq *sub_crq)
2901{
2902 unsigned int ua = adapter->vdev->unit_address;
2903 struct device *dev = &adapter->vdev->dev;
2904 u64 *u64_crq = (u64 *)sub_crq;
2905 int rc;
2906
2907 netdev_dbg(adapter->netdev,
2908 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2909 (unsigned long int)cpu_to_be64(remote_handle),
2910 (unsigned long int)cpu_to_be64(u64_crq[0]),
2911 (unsigned long int)cpu_to_be64(u64_crq[1]),
2912 (unsigned long int)cpu_to_be64(u64_crq[2]),
2913 (unsigned long int)cpu_to_be64(u64_crq[3]));
2914
2915 /* Make sure the hypervisor sees the complete request */
2916 mb();
2917
2918 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2919 cpu_to_be64(remote_handle),
2920 cpu_to_be64(u64_crq[0]),
2921 cpu_to_be64(u64_crq[1]),
2922 cpu_to_be64(u64_crq[2]),
2923 cpu_to_be64(u64_crq[3]));
2924
2925 if (rc) {
2926 if (rc == H_CLOSED)
2927 dev_warn(dev, "CRQ Queue closed\n");
2928 dev_err(dev, "Send error (rc=%d)\n", rc);
2929 }
2930
2931 return rc;
2932}
2933
Thomas Falconad7775d2016-04-01 17:20:34 -05002934static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2935 u64 remote_handle, u64 ioba, u64 num_entries)
2936{
2937 unsigned int ua = adapter->vdev->unit_address;
2938 struct device *dev = &adapter->vdev->dev;
2939 int rc;
2940
2941 /* Make sure the hypervisor sees the complete request */
2942 mb();
2943 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2944 cpu_to_be64(remote_handle),
2945 ioba, num_entries);
2946
2947 if (rc) {
2948 if (rc == H_CLOSED)
2949 dev_warn(dev, "CRQ Queue closed\n");
2950 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2951 }
2952
2953 return rc;
2954}
2955
Thomas Falcon032c5e82015-12-21 11:26:06 -06002956static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2957 union ibmvnic_crq *crq)
2958{
2959 unsigned int ua = adapter->vdev->unit_address;
2960 struct device *dev = &adapter->vdev->dev;
2961 u64 *u64_crq = (u64 *)crq;
2962 int rc;
2963
2964 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2965 (unsigned long int)cpu_to_be64(u64_crq[0]),
2966 (unsigned long int)cpu_to_be64(u64_crq[1]));
2967
2968 /* Make sure the hypervisor sees the complete request */
2969 mb();
2970
2971 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2972 cpu_to_be64(u64_crq[0]),
2973 cpu_to_be64(u64_crq[1]));
2974
2975 if (rc) {
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002976 if (rc == H_CLOSED) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002977 dev_warn(dev, "CRQ Queue closed\n");
Nathan Fontenotec95dff2018-02-07 13:00:24 -06002978 if (adapter->resetting)
2979 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2980 }
2981
Thomas Falcon032c5e82015-12-21 11:26:06 -06002982 dev_warn(dev, "Send error (rc=%d)\n", rc);
2983 }
2984
2985 return rc;
2986}
2987
2988static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2989{
2990 union ibmvnic_crq crq;
2991
2992 memset(&crq, 0, sizeof(crq));
2993 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2994 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2995 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2996
2997 return ibmvnic_send_crq(adapter, &crq);
2998}
2999
Thomas Falcon032c5e82015-12-21 11:26:06 -06003000static int send_version_xchg(struct ibmvnic_adapter *adapter)
3001{
3002 union ibmvnic_crq crq;
3003
3004 memset(&crq, 0, sizeof(crq));
3005 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3006 crq.version_exchange.cmd = VERSION_EXCHANGE;
3007 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3008
3009 return ibmvnic_send_crq(adapter, &crq);
3010}
3011
Nathan Fontenot37798d02017-11-08 11:23:56 -06003012struct vnic_login_client_data {
3013 u8 type;
3014 __be16 len;
3015 char name;
3016} __packed;
3017
3018static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3019{
3020 int len;
3021
3022 /* Calculate the amount of buffer space needed for the
3023 * vnic client data in the login buffer. There are four entries,
3024 * OS name, LPAR name, device name, and a null last entry.
3025 */
3026 len = 4 * sizeof(struct vnic_login_client_data);
3027 len += 6; /* "Linux" plus NULL */
3028 len += strlen(utsname()->nodename) + 1;
3029 len += strlen(adapter->netdev->name) + 1;
3030
3031 return len;
3032}
3033
3034static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3035 struct vnic_login_client_data *vlcd)
3036{
3037 const char *os_name = "Linux";
3038 int len;
3039
3040 /* Type 1 - LPAR OS */
3041 vlcd->type = 1;
3042 len = strlen(os_name) + 1;
3043 vlcd->len = cpu_to_be16(len);
3044 strncpy(&vlcd->name, os_name, len);
3045 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3046
3047 /* Type 2 - LPAR name */
3048 vlcd->type = 2;
3049 len = strlen(utsname()->nodename) + 1;
3050 vlcd->len = cpu_to_be16(len);
3051 strncpy(&vlcd->name, utsname()->nodename, len);
3052 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3053
3054 /* Type 3 - device name */
3055 vlcd->type = 3;
3056 len = strlen(adapter->netdev->name) + 1;
3057 vlcd->len = cpu_to_be16(len);
3058 strncpy(&vlcd->name, adapter->netdev->name, len);
3059}
3060
Thomas Falcon032c5e82015-12-21 11:26:06 -06003061static void send_login(struct ibmvnic_adapter *adapter)
3062{
3063 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3064 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003065 struct device *dev = &adapter->vdev->dev;
3066 dma_addr_t rsp_buffer_token;
3067 dma_addr_t buffer_token;
3068 size_t rsp_buffer_size;
3069 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003070 size_t buffer_size;
3071 __be64 *tx_list_p;
3072 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06003073 int client_data_len;
3074 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 int i;
3076
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003077 release_login_rsp_buffer(adapter);
Nathan Fontenot37798d02017-11-08 11:23:56 -06003078 client_data_len = vnic_client_data_len(adapter);
3079
Thomas Falcon032c5e82015-12-21 11:26:06 -06003080 buffer_size =
3081 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06003082 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3083 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003084
Nathan Fontenot37798d02017-11-08 11:23:56 -06003085 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003086 if (!login_buffer)
3087 goto buf_alloc_failed;
3088
3089 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3090 DMA_TO_DEVICE);
3091 if (dma_mapping_error(dev, buffer_token)) {
3092 dev_err(dev, "Couldn't map login buffer\n");
3093 goto buf_map_failed;
3094 }
3095
John Allen498cd8e2016-04-06 11:49:55 -05003096 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3097 sizeof(u64) * adapter->req_tx_queues +
3098 sizeof(u64) * adapter->req_rx_queues +
3099 sizeof(u64) * adapter->req_rx_queues +
3100 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003101
3102 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3103 if (!login_rsp_buffer)
3104 goto buf_rsp_alloc_failed;
3105
3106 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3107 rsp_buffer_size, DMA_FROM_DEVICE);
3108 if (dma_mapping_error(dev, rsp_buffer_token)) {
3109 dev_err(dev, "Couldn't map login rsp buffer\n");
3110 goto buf_rsp_map_failed;
3111 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003112
Thomas Falcon032c5e82015-12-21 11:26:06 -06003113 adapter->login_buf = login_buffer;
3114 adapter->login_buf_token = buffer_token;
3115 adapter->login_buf_sz = buffer_size;
3116 adapter->login_rsp_buf = login_rsp_buffer;
3117 adapter->login_rsp_buf_token = rsp_buffer_token;
3118 adapter->login_rsp_buf_sz = rsp_buffer_size;
3119
3120 login_buffer->len = cpu_to_be32(buffer_size);
3121 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3122 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3123 login_buffer->off_txcomp_subcrqs =
3124 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3125 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3126 login_buffer->off_rxcomp_subcrqs =
3127 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3128 sizeof(u64) * adapter->req_tx_queues);
3129 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3130 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3131
3132 tx_list_p = (__be64 *)((char *)login_buffer +
3133 sizeof(struct ibmvnic_login_buffer));
3134 rx_list_p = (__be64 *)((char *)login_buffer +
3135 sizeof(struct ibmvnic_login_buffer) +
3136 sizeof(u64) * adapter->req_tx_queues);
3137
3138 for (i = 0; i < adapter->req_tx_queues; i++) {
3139 if (adapter->tx_scrq[i]) {
3140 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3141 crq_num);
3142 }
3143 }
3144
3145 for (i = 0; i < adapter->req_rx_queues; i++) {
3146 if (adapter->rx_scrq[i]) {
3147 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3148 crq_num);
3149 }
3150 }
3151
Nathan Fontenot37798d02017-11-08 11:23:56 -06003152 /* Insert vNIC login client data */
3153 vlcd = (struct vnic_login_client_data *)
3154 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3155 login_buffer->client_data_offset =
3156 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3157 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3158
3159 vnic_add_client_data(adapter, vlcd);
3160
Thomas Falcon032c5e82015-12-21 11:26:06 -06003161 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3162 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3163 netdev_dbg(adapter->netdev, "%016lx\n",
3164 ((unsigned long int *)(adapter->login_buf))[i]);
3165 }
3166
3167 memset(&crq, 0, sizeof(crq));
3168 crq.login.first = IBMVNIC_CRQ_CMD;
3169 crq.login.cmd = LOGIN;
3170 crq.login.ioba = cpu_to_be32(buffer_token);
3171 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003172 ibmvnic_send_crq(adapter, &crq);
3173
3174 return;
3175
Thomas Falcon032c5e82015-12-21 11:26:06 -06003176buf_rsp_map_failed:
3177 kfree(login_rsp_buffer);
3178buf_rsp_alloc_failed:
3179 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3180buf_map_failed:
3181 kfree(login_buffer);
3182buf_alloc_failed:
3183 return;
3184}
3185
3186static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3187 u32 len, u8 map_id)
3188{
3189 union ibmvnic_crq crq;
3190
3191 memset(&crq, 0, sizeof(crq));
3192 crq.request_map.first = IBMVNIC_CRQ_CMD;
3193 crq.request_map.cmd = REQUEST_MAP;
3194 crq.request_map.map_id = map_id;
3195 crq.request_map.ioba = cpu_to_be32(addr);
3196 crq.request_map.len = cpu_to_be32(len);
3197 ibmvnic_send_crq(adapter, &crq);
3198}
3199
3200static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3201{
3202 union ibmvnic_crq crq;
3203
3204 memset(&crq, 0, sizeof(crq));
3205 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3206 crq.request_unmap.cmd = REQUEST_UNMAP;
3207 crq.request_unmap.map_id = map_id;
3208 ibmvnic_send_crq(adapter, &crq);
3209}
3210
3211static void send_map_query(struct ibmvnic_adapter *adapter)
3212{
3213 union ibmvnic_crq crq;
3214
3215 memset(&crq, 0, sizeof(crq));
3216 crq.query_map.first = IBMVNIC_CRQ_CMD;
3217 crq.query_map.cmd = QUERY_MAP;
3218 ibmvnic_send_crq(adapter, &crq);
3219}
3220
3221/* Send a series of CRQs requesting various capabilities of the VNIC server */
3222static void send_cap_queries(struct ibmvnic_adapter *adapter)
3223{
3224 union ibmvnic_crq crq;
3225
Thomas Falcon901e0402017-02-15 12:17:59 -06003226 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003227 memset(&crq, 0, sizeof(crq));
3228 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3229 crq.query_capability.cmd = QUERY_CAPABILITY;
3230
3231 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003232 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003233 ibmvnic_send_crq(adapter, &crq);
3234
3235 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003236 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003237 ibmvnic_send_crq(adapter, &crq);
3238
3239 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003240 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003241 ibmvnic_send_crq(adapter, &crq);
3242
3243 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003244 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003245 ibmvnic_send_crq(adapter, &crq);
3246
3247 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003248 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003249 ibmvnic_send_crq(adapter, &crq);
3250
3251 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_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 =
3256 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003257 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003258 ibmvnic_send_crq(adapter, &crq);
3259
3260 crq.query_capability.capability =
3261 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003262 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003263 ibmvnic_send_crq(adapter, &crq);
3264
3265 crq.query_capability.capability =
3266 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003267 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003268 ibmvnic_send_crq(adapter, &crq);
3269
3270 crq.query_capability.capability =
3271 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
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 = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003276 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003277 ibmvnic_send_crq(adapter, &crq);
3278
3279 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003280 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003281 ibmvnic_send_crq(adapter, &crq);
3282
3283 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003284 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003285 ibmvnic_send_crq(adapter, &crq);
3286
3287 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003288 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003289 ibmvnic_send_crq(adapter, &crq);
3290
3291 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003299 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3300 atomic_inc(&adapter->running_cap_crqs);
3301 ibmvnic_send_crq(adapter, &crq);
3302
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
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(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003312 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313 ibmvnic_send_crq(adapter, &crq);
3314
3315 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003316 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003317 ibmvnic_send_crq(adapter, &crq);
3318
3319 crq.query_capability.capability =
3320 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003321 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 ibmvnic_send_crq(adapter, &crq);
3323
3324 crq.query_capability.capability =
3325 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003326 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003327 ibmvnic_send_crq(adapter, &crq);
3328
3329 crq.query_capability.capability =
3330 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003331 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003332 ibmvnic_send_crq(adapter, &crq);
3333
3334 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003335 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003336 ibmvnic_send_crq(adapter, &crq);
3337}
3338
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003339static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3340 struct ibmvnic_adapter *adapter)
3341{
3342 struct device *dev = &adapter->vdev->dev;
3343
3344 if (crq->get_vpd_size_rsp.rc.code) {
3345 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3346 crq->get_vpd_size_rsp.rc.code);
3347 complete(&adapter->fw_done);
3348 return;
3349 }
3350
3351 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3352 complete(&adapter->fw_done);
3353}
3354
3355static void handle_vpd_rsp(union ibmvnic_crq *crq,
3356 struct ibmvnic_adapter *adapter)
3357{
3358 struct device *dev = &adapter->vdev->dev;
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003359 unsigned char *substr = NULL;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003360 u8 fw_level_len = 0;
3361
3362 memset(adapter->fw_version, 0, 32);
3363
3364 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3365 DMA_FROM_DEVICE);
3366
3367 if (crq->get_vpd_rsp.rc.code) {
3368 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3369 crq->get_vpd_rsp.rc.code);
3370 goto complete;
3371 }
3372
3373 /* get the position of the firmware version info
3374 * located after the ASCII 'RM' substring in the buffer
3375 */
3376 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3377 if (!substr) {
Desnes Augusto Nunes do Rosarioa1073112018-02-01 16:04:30 -02003378 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 -02003379 goto complete;
3380 }
3381
3382 /* get length of firmware level ASCII substring */
3383 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3384 fw_level_len = *(substr + 2);
3385 } else {
3386 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3387 goto complete;
3388 }
3389
3390 /* copy firmware version string from vpd into adapter */
3391 if ((substr + 3 + fw_level_len) <
3392 (adapter->vpd->buff + adapter->vpd->len)) {
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003393 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003394 } else {
3395 dev_info(dev, "FW substr extrapolated VPD buff\n");
3396 }
3397
3398complete:
Desnes Augusto Nunes do Rosario21a25452018-02-05 14:33:55 -02003399 if (adapter->fw_version[0] == '\0')
3400 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003401 complete(&adapter->fw_done);
3402}
3403
Thomas Falcon032c5e82015-12-21 11:26:06 -06003404static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3405{
3406 struct device *dev = &adapter->vdev->dev;
3407 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3408 union ibmvnic_crq crq;
3409 int i;
3410
3411 dma_unmap_single(dev, adapter->ip_offload_tok,
3412 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3413
3414 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3415 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3416 netdev_dbg(adapter->netdev, "%016lx\n",
3417 ((unsigned long int *)(buf))[i]);
3418
3419 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3420 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3421 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3422 buf->tcp_ipv4_chksum);
3423 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3424 buf->tcp_ipv6_chksum);
3425 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3426 buf->udp_ipv4_chksum);
3427 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3428 buf->udp_ipv6_chksum);
3429 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3430 buf->large_tx_ipv4);
3431 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3432 buf->large_tx_ipv6);
3433 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3434 buf->large_rx_ipv4);
3435 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3436 buf->large_rx_ipv6);
3437 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3438 buf->max_ipv4_header_size);
3439 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3440 buf->max_ipv6_header_size);
3441 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3442 buf->max_tcp_header_size);
3443 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3444 buf->max_udp_header_size);
3445 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3446 buf->max_large_tx_size);
3447 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3448 buf->max_large_rx_size);
3449 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3450 buf->ipv6_extension_header);
3451 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3452 buf->tcp_pseudosum_req);
3453 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3454 buf->num_ipv6_ext_headers);
3455 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3456 buf->off_ipv6_ext_headers);
3457
3458 adapter->ip_offload_ctrl_tok =
3459 dma_map_single(dev, &adapter->ip_offload_ctrl,
3460 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3461
3462 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3463 dev_err(dev, "Couldn't map ip offload control buffer\n");
3464 return;
3465 }
3466
Thomas Falconf6897942018-01-18 19:05:01 -06003467 adapter->ip_offload_ctrl.len =
3468 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003469 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
Thomas Falconf6897942018-01-18 19:05:01 -06003470 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3471 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003472 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3473 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3474 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3475 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003476 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3477 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003478
Thomas Falconfdb06102017-10-17 12:36:55 -05003479 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003480 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3481 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3482
Thomas Falcon15482052017-10-17 12:36:54 -05003483 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003484
3485 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3486 adapter->netdev->features |= NETIF_F_IP_CSUM;
3487
3488 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3489 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3490
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003491 if ((adapter->netdev->features &
3492 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3493 adapter->netdev->features |= NETIF_F_RXCSUM;
3494
Thomas Falconfdb06102017-10-17 12:36:55 -05003495 if (buf->large_tx_ipv4)
3496 adapter->netdev->features |= NETIF_F_TSO;
3497 if (buf->large_tx_ipv6)
3498 adapter->netdev->features |= NETIF_F_TSO6;
3499
Thomas Falconaa0bf852017-10-17 12:36:56 -05003500 adapter->netdev->hw_features |= adapter->netdev->features;
3501
Thomas Falcon032c5e82015-12-21 11:26:06 -06003502 memset(&crq, 0, sizeof(crq));
3503 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3504 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3505 crq.control_ip_offload.len =
3506 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3507 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3508 ibmvnic_send_crq(adapter, &crq);
3509}
3510
3511static void handle_error_info_rsp(union ibmvnic_crq *crq,
3512 struct ibmvnic_adapter *adapter)
3513{
3514 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003515 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003516 unsigned long flags;
3517 bool found = false;
3518 int i;
3519
3520 if (!crq->request_error_rsp.rc.code) {
3521 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3522 crq->request_error_rsp.rc.code);
3523 return;
3524 }
3525
3526 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003527 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003528 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3529 found = true;
3530 list_del(&error_buff->list);
3531 break;
3532 }
3533 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3534
3535 if (!found) {
3536 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003537 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003538 return;
3539 }
3540
3541 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003542 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003543
3544 for (i = 0; i < error_buff->len; i++) {
3545 pr_cont("%02x", (int)error_buff->buff[i]);
3546 if (i % 8 == 7)
3547 pr_cont(" ");
3548 }
3549 pr_cont("\n");
3550
3551 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3552 DMA_FROM_DEVICE);
3553 kfree(error_buff->buff);
3554 kfree(error_buff);
3555}
3556
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003557static void request_error_information(struct ibmvnic_adapter *adapter,
3558 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003559{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003560 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003561 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003562 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003563 unsigned long timeout = msecs_to_jiffies(30000);
3564 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003565 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003566 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003567
3568 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3569 if (!error_buff)
3570 return;
3571
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003572 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003573 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3574 if (!error_buff->buff) {
3575 kfree(error_buff);
3576 return;
3577 }
3578
3579 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3580 DMA_FROM_DEVICE);
3581 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003582 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003583 kfree(error_buff->buff);
3584 kfree(error_buff);
3585 return;
3586 }
3587
Thomas Falcon032c5e82015-12-21 11:26:06 -06003588 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003589 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003590
3591 spin_lock_irqsave(&adapter->error_list_lock, flags);
3592 list_add_tail(&error_buff->list, &adapter->errors);
3593 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3594
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003595 memset(&crq, 0, sizeof(crq));
3596 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3597 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3598 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3599 crq.request_error_info.len = cpu_to_be32(detail_len);
3600 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3601
3602 rc = ibmvnic_send_crq(adapter, &crq);
3603 if (rc) {
3604 netdev_err(netdev, "failed to request error information\n");
3605 goto err_info_fail;
3606 }
3607
3608 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3609 netdev_err(netdev, "timeout waiting for error information\n");
3610 goto err_info_fail;
3611 }
3612
3613 return;
3614
3615err_info_fail:
3616 spin_lock_irqsave(&adapter->error_list_lock, flags);
3617 list_del(&error_buff->list);
3618 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3619
3620 kfree(error_buff->buff);
3621 kfree(error_buff);
3622}
3623
3624static void handle_error_indication(union ibmvnic_crq *crq,
3625 struct ibmvnic_adapter *adapter)
3626{
3627 struct device *dev = &adapter->vdev->dev;
3628
3629 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3630 crq->error_indication.flags
3631 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3632 be32_to_cpu(crq->error_indication.error_id),
3633 be16_to_cpu(crq->error_indication.error_cause));
3634
3635 if (be32_to_cpu(crq->error_indication.error_id))
3636 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003637
3638 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3639 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003640 else
3641 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003642}
3643
Thomas Falconf8136142018-01-29 13:45:05 -06003644static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3645 struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003646{
3647 struct net_device *netdev = adapter->netdev;
3648 struct device *dev = &adapter->vdev->dev;
3649 long rc;
3650
3651 rc = crq->change_mac_addr_rsp.rc.code;
3652 if (rc) {
3653 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
Thomas Falconf8136142018-01-29 13:45:05 -06003654 goto out;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003655 }
3656 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3657 ETH_ALEN);
Thomas Falconf8136142018-01-29 13:45:05 -06003658out:
3659 complete(&adapter->fw_done);
3660 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003661}
3662
3663static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3664 struct ibmvnic_adapter *adapter)
3665{
3666 struct device *dev = &adapter->vdev->dev;
3667 u64 *req_value;
3668 char *name;
3669
Thomas Falcon901e0402017-02-15 12:17:59 -06003670 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003671 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3672 case REQ_TX_QUEUES:
3673 req_value = &adapter->req_tx_queues;
3674 name = "tx";
3675 break;
3676 case REQ_RX_QUEUES:
3677 req_value = &adapter->req_rx_queues;
3678 name = "rx";
3679 break;
3680 case REQ_RX_ADD_QUEUES:
3681 req_value = &adapter->req_rx_add_queues;
3682 name = "rx_add";
3683 break;
3684 case REQ_TX_ENTRIES_PER_SUBCRQ:
3685 req_value = &adapter->req_tx_entries_per_subcrq;
3686 name = "tx_entries_per_subcrq";
3687 break;
3688 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3689 req_value = &adapter->req_rx_add_entries_per_subcrq;
3690 name = "rx_add_entries_per_subcrq";
3691 break;
3692 case REQ_MTU:
3693 req_value = &adapter->req_mtu;
3694 name = "mtu";
3695 break;
3696 case PROMISC_REQUESTED:
3697 req_value = &adapter->promisc;
3698 name = "promisc";
3699 break;
3700 default:
3701 dev_err(dev, "Got invalid cap request rsp %d\n",
3702 crq->request_capability.capability);
3703 return;
3704 }
3705
3706 switch (crq->request_capability_rsp.rc.code) {
3707 case SUCCESS:
3708 break;
3709 case PARTIALSUCCESS:
3710 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3711 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003712 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003713 number), name);
John Allene7913802018-01-18 16:27:12 -06003714
3715 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3716 REQ_MTU) {
3717 pr_err("mtu of %llu is not supported. Reverting.\n",
3718 *req_value);
3719 *req_value = adapter->fallback.mtu;
3720 } else {
3721 *req_value =
3722 be64_to_cpu(crq->request_capability_rsp.number);
3723 }
3724
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003725 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003726 return;
3727 default:
3728 dev_err(dev, "Error %d in request cap rsp\n",
3729 crq->request_capability_rsp.rc.code);
3730 return;
3731 }
3732
3733 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003734 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003735 union ibmvnic_crq newcrq;
3736 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3737 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3738 &adapter->ip_offload_buf;
3739
Thomas Falcon249168a2017-02-15 12:18:00 -06003740 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003741 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3742 buf_sz,
3743 DMA_FROM_DEVICE);
3744
3745 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3746 if (!firmware_has_feature(FW_FEATURE_CMO))
3747 dev_err(dev, "Couldn't map offload buffer\n");
3748 return;
3749 }
3750
3751 memset(&newcrq, 0, sizeof(newcrq));
3752 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3753 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3754 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3755 newcrq.query_ip_offload.ioba =
3756 cpu_to_be32(adapter->ip_offload_tok);
3757
3758 ibmvnic_send_crq(adapter, &newcrq);
3759 }
3760}
3761
3762static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3763 struct ibmvnic_adapter *adapter)
3764{
3765 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003766 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003767 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3768 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003769 int i;
3770
3771 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3772 DMA_BIDIRECTIONAL);
Thomas Falcon34f0f4e2018-02-13 18:23:40 -06003773 release_login_buffer(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003774 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3775 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3776
John Allen498cd8e2016-04-06 11:49:55 -05003777 /* If the number of queues requested can't be allocated by the
3778 * server, the login response will return with code 1. We will need
3779 * to resend the login buffer with fewer queues requested.
3780 */
3781 if (login_rsp_crq->generic.rc.code) {
3782 adapter->renegotiate = true;
3783 complete(&adapter->init_done);
3784 return 0;
3785 }
3786
John Allenc26eba02017-10-26 16:23:25 -05003787 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3788
Thomas Falcon032c5e82015-12-21 11:26:06 -06003789 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3790 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3791 netdev_dbg(adapter->netdev, "%016lx\n",
3792 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3793 }
3794
3795 /* Sanity checks */
3796 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3797 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3798 adapter->req_rx_add_queues !=
3799 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3800 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3801 ibmvnic_remove(adapter->vdev);
3802 return -EIO;
3803 }
3804 complete(&adapter->init_done);
3805
Thomas Falcon032c5e82015-12-21 11:26:06 -06003806 return 0;
3807}
3808
Thomas Falcon032c5e82015-12-21 11:26:06 -06003809static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3810 struct ibmvnic_adapter *adapter)
3811{
3812 struct device *dev = &adapter->vdev->dev;
3813 long rc;
3814
3815 rc = crq->request_unmap_rsp.rc.code;
3816 if (rc)
3817 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3818}
3819
3820static void handle_query_map_rsp(union ibmvnic_crq *crq,
3821 struct ibmvnic_adapter *adapter)
3822{
3823 struct net_device *netdev = adapter->netdev;
3824 struct device *dev = &adapter->vdev->dev;
3825 long rc;
3826
3827 rc = crq->query_map_rsp.rc.code;
3828 if (rc) {
3829 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3830 return;
3831 }
3832 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3833 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3834 crq->query_map_rsp.free_pages);
3835}
3836
3837static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3838 struct ibmvnic_adapter *adapter)
3839{
3840 struct net_device *netdev = adapter->netdev;
3841 struct device *dev = &adapter->vdev->dev;
3842 long rc;
3843
Thomas Falcon901e0402017-02-15 12:17:59 -06003844 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003845 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003846 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003847 rc = crq->query_capability.rc.code;
3848 if (rc) {
3849 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3850 goto out;
3851 }
3852
3853 switch (be16_to_cpu(crq->query_capability.capability)) {
3854 case MIN_TX_QUEUES:
3855 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003856 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003857 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3858 adapter->min_tx_queues);
3859 break;
3860 case MIN_RX_QUEUES:
3861 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003862 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003863 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3864 adapter->min_rx_queues);
3865 break;
3866 case MIN_RX_ADD_QUEUES:
3867 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003868 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003869 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3870 adapter->min_rx_add_queues);
3871 break;
3872 case MAX_TX_QUEUES:
3873 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003874 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003875 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3876 adapter->max_tx_queues);
3877 break;
3878 case MAX_RX_QUEUES:
3879 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003880 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003881 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3882 adapter->max_rx_queues);
3883 break;
3884 case MAX_RX_ADD_QUEUES:
3885 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003886 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003887 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3888 adapter->max_rx_add_queues);
3889 break;
3890 case MIN_TX_ENTRIES_PER_SUBCRQ:
3891 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003892 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003893 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3894 adapter->min_tx_entries_per_subcrq);
3895 break;
3896 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3897 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003898 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003899 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3900 adapter->min_rx_add_entries_per_subcrq);
3901 break;
3902 case MAX_TX_ENTRIES_PER_SUBCRQ:
3903 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003904 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003905 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3906 adapter->max_tx_entries_per_subcrq);
3907 break;
3908 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3909 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003910 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003911 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3912 adapter->max_rx_add_entries_per_subcrq);
3913 break;
3914 case TCP_IP_OFFLOAD:
3915 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003916 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003917 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3918 adapter->tcp_ip_offload);
3919 break;
3920 case PROMISC_SUPPORTED:
3921 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003922 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003923 netdev_dbg(netdev, "promisc_supported = %lld\n",
3924 adapter->promisc_supported);
3925 break;
3926 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003927 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003928 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003929 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3930 break;
3931 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003932 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003933 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003934 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3935 break;
3936 case MAX_MULTICAST_FILTERS:
3937 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003938 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003939 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3940 adapter->max_multicast_filters);
3941 break;
3942 case VLAN_HEADER_INSERTION:
3943 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003944 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003945 if (adapter->vlan_header_insertion)
3946 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3947 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3948 adapter->vlan_header_insertion);
3949 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003950 case RX_VLAN_HEADER_INSERTION:
3951 adapter->rx_vlan_header_insertion =
3952 be64_to_cpu(crq->query_capability.number);
3953 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3954 adapter->rx_vlan_header_insertion);
3955 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003956 case MAX_TX_SG_ENTRIES:
3957 adapter->max_tx_sg_entries =
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_tx_sg_entries = %lld\n",
3960 adapter->max_tx_sg_entries);
3961 break;
3962 case RX_SG_SUPPORTED:
3963 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003964 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3966 adapter->rx_sg_supported);
3967 break;
3968 case OPT_TX_COMP_SUB_QUEUES:
3969 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003970 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003971 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3972 adapter->opt_tx_comp_sub_queues);
3973 break;
3974 case OPT_RX_COMP_QUEUES:
3975 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003976 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003977 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3978 adapter->opt_rx_comp_queues);
3979 break;
3980 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3981 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003982 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003983 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3984 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3985 break;
3986 case OPT_TX_ENTRIES_PER_SUBCRQ:
3987 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003988 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003989 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3990 adapter->opt_tx_entries_per_subcrq);
3991 break;
3992 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3993 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003994 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003995 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3996 adapter->opt_rxba_entries_per_subcrq);
3997 break;
3998 case TX_RX_DESC_REQ:
3999 adapter->tx_rx_desc_req = crq->query_capability.number;
4000 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4001 adapter->tx_rx_desc_req);
4002 break;
4003
4004 default:
4005 netdev_err(netdev, "Got invalid cap rsp %d\n",
4006 crq->query_capability.capability);
4007 }
4008
4009out:
Thomas Falcon249168a2017-02-15 12:18:00 -06004010 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4011 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04004012 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06004013 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06004014}
4015
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4017 struct ibmvnic_adapter *adapter)
4018{
4019 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4020 struct net_device *netdev = adapter->netdev;
4021 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004022 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004023 long rc;
4024
4025 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04004026 (unsigned long int)cpu_to_be64(u64_crq[0]),
4027 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06004028 switch (gen_crq->first) {
4029 case IBMVNIC_CRQ_INIT_RSP:
4030 switch (gen_crq->cmd) {
4031 case IBMVNIC_CRQ_INIT:
4032 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04004033 adapter->from_passive_init = true;
4034 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004035 break;
4036 case IBMVNIC_CRQ_INIT_COMPLETE:
4037 dev_info(dev, "Partner initialization complete\n");
4038 send_version_xchg(adapter);
4039 break;
4040 default:
4041 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4042 }
4043 return;
4044 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04004045 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004046 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04004047 dev_info(dev, "Migrated, re-enabling adapter\n");
4048 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05004049 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4050 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04004051 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004052 } else {
4053 /* The adapter lost the connection */
4054 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4055 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004056 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004057 }
4058 return;
4059 case IBMVNIC_CRQ_CMD_RSP:
4060 break;
4061 default:
4062 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4063 gen_crq->first);
4064 return;
4065 }
4066
4067 switch (gen_crq->cmd) {
4068 case VERSION_EXCHANGE_RSP:
4069 rc = crq->version_exchange_rsp.rc.code;
4070 if (rc) {
4071 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4072 break;
4073 }
4074 dev_info(dev, "Partner protocol version is %d\n",
4075 crq->version_exchange_rsp.version);
4076 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4077 ibmvnic_version)
4078 ibmvnic_version =
4079 be16_to_cpu(crq->version_exchange_rsp.version);
4080 send_cap_queries(adapter);
4081 break;
4082 case QUERY_CAPABILITY_RSP:
4083 handle_query_cap_rsp(crq, adapter);
4084 break;
4085 case QUERY_MAP_RSP:
4086 handle_query_map_rsp(crq, adapter);
4087 break;
4088 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05004089 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4090 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004091 break;
4092 case REQUEST_UNMAP_RSP:
4093 handle_request_unmap_rsp(crq, adapter);
4094 break;
4095 case REQUEST_CAPABILITY_RSP:
4096 handle_request_cap_rsp(crq, adapter);
4097 break;
4098 case LOGIN_RSP:
4099 netdev_dbg(netdev, "Got Login Response\n");
4100 handle_login_rsp(crq, adapter);
4101 break;
4102 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004103 netdev_dbg(netdev,
4104 "Got Logical Link State Response, state: %d rc: %d\n",
4105 crq->logical_link_state_rsp.link_state,
4106 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004107 adapter->logical_link_state =
4108 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04004109 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4110 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004111 break;
4112 case LINK_STATE_INDICATION:
4113 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4114 adapter->phys_link_state =
4115 crq->link_state_indication.phys_link_state;
4116 adapter->logical_link_state =
4117 crq->link_state_indication.logical_link_state;
4118 break;
4119 case CHANGE_MAC_ADDR_RSP:
4120 netdev_dbg(netdev, "Got MAC address change Response\n");
Thomas Falconf8136142018-01-29 13:45:05 -06004121 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004122 break;
4123 case ERROR_INDICATION:
4124 netdev_dbg(netdev, "Got Error Indication\n");
4125 handle_error_indication(crq, adapter);
4126 break;
4127 case REQUEST_ERROR_RSP:
4128 netdev_dbg(netdev, "Got Error Detail Response\n");
4129 handle_error_info_rsp(crq, adapter);
4130 break;
4131 case REQUEST_STATISTICS_RSP:
4132 netdev_dbg(netdev, "Got Statistics Response\n");
4133 complete(&adapter->stats_done);
4134 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004135 case QUERY_IP_OFFLOAD_RSP:
4136 netdev_dbg(netdev, "Got Query IP offload Response\n");
4137 handle_query_ip_offload_rsp(adapter);
4138 break;
4139 case MULTICAST_CTRL_RSP:
4140 netdev_dbg(netdev, "Got multicast control Response\n");
4141 break;
4142 case CONTROL_IP_OFFLOAD_RSP:
4143 netdev_dbg(netdev, "Got Control IP offload Response\n");
4144 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4145 sizeof(adapter->ip_offload_ctrl),
4146 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004147 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004148 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004149 case COLLECT_FW_TRACE_RSP:
4150 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4151 complete(&adapter->fw_done);
4152 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004153 case GET_VPD_SIZE_RSP:
4154 handle_vpd_size_rsp(crq, adapter);
4155 break;
4156 case GET_VPD_RSP:
4157 handle_vpd_rsp(crq, adapter);
4158 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004159 default:
4160 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4161 gen_crq->cmd);
4162 }
4163}
4164
4165static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4166{
4167 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004168
Thomas Falcon6c267b32017-02-15 12:17:58 -06004169 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004170 return IRQ_HANDLED;
4171}
4172
4173static void ibmvnic_tasklet(void *data)
4174{
4175 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004176 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004177 union ibmvnic_crq *crq;
4178 unsigned long flags;
4179 bool done = false;
4180
4181 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004182 while (!done) {
4183 /* Pull all the valid messages off the CRQ */
4184 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4185 ibmvnic_handle_crq(crq, adapter);
4186 crq->generic.first = 0;
4187 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004188
4189 /* remain in tasklet until all
4190 * capabilities responses are received
4191 */
4192 if (!adapter->wait_capability)
4193 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004194 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004195 /* if capabilities CRQ's were sent in this tasklet, the following
4196 * tasklet must wait until all responses are received
4197 */
4198 if (atomic_read(&adapter->running_cap_crqs) != 0)
4199 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004200 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004201}
4202
4203static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4204{
4205 struct vio_dev *vdev = adapter->vdev;
4206 int rc;
4207
4208 do {
4209 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4210 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4211
4212 if (rc)
4213 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4214
4215 return rc;
4216}
4217
4218static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4219{
4220 struct ibmvnic_crq_queue *crq = &adapter->crq;
4221 struct device *dev = &adapter->vdev->dev;
4222 struct vio_dev *vdev = adapter->vdev;
4223 int rc;
4224
4225 /* Close the CRQ */
4226 do {
4227 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4228 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4229
4230 /* Clean out the queue */
4231 memset(crq->msgs, 0, PAGE_SIZE);
4232 crq->cur = 0;
4233
4234 /* And re-open it again */
4235 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4236 crq->msg_token, PAGE_SIZE);
4237
4238 if (rc == H_CLOSED)
4239 /* Adapter is good, but other end is not ready */
4240 dev_warn(dev, "Partner adapter not ready\n");
4241 else if (rc != 0)
4242 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4243
4244 return rc;
4245}
4246
Nathan Fontenotf9928872017-03-30 02:48:54 -04004247static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004248{
4249 struct ibmvnic_crq_queue *crq = &adapter->crq;
4250 struct vio_dev *vdev = adapter->vdev;
4251 long rc;
4252
Nathan Fontenotf9928872017-03-30 02:48:54 -04004253 if (!crq->msgs)
4254 return;
4255
Thomas Falcon032c5e82015-12-21 11:26:06 -06004256 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4257 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004258 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004259 do {
4260 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4261 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4262
4263 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4264 DMA_BIDIRECTIONAL);
4265 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004266 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004267}
4268
Nathan Fontenotf9928872017-03-30 02:48:54 -04004269static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004270{
4271 struct ibmvnic_crq_queue *crq = &adapter->crq;
4272 struct device *dev = &adapter->vdev->dev;
4273 struct vio_dev *vdev = adapter->vdev;
4274 int rc, retrc = -ENOMEM;
4275
Nathan Fontenotf9928872017-03-30 02:48:54 -04004276 if (crq->msgs)
4277 return 0;
4278
Thomas Falcon032c5e82015-12-21 11:26:06 -06004279 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4280 /* Should we allocate more than one page? */
4281
4282 if (!crq->msgs)
4283 return -ENOMEM;
4284
4285 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4286 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4287 DMA_BIDIRECTIONAL);
4288 if (dma_mapping_error(dev, crq->msg_token))
4289 goto map_failed;
4290
4291 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4292 crq->msg_token, PAGE_SIZE);
4293
4294 if (rc == H_RESOURCE)
4295 /* maybe kexecing and resource is busy. try a reset */
4296 rc = ibmvnic_reset_crq(adapter);
4297 retrc = rc;
4298
4299 if (rc == H_CLOSED) {
4300 dev_warn(dev, "Partner adapter not ready\n");
4301 } else if (rc) {
4302 dev_warn(dev, "Error %d opening adapter\n", rc);
4303 goto reg_crq_failed;
4304 }
4305
4306 retrc = 0;
4307
Thomas Falcon6c267b32017-02-15 12:17:58 -06004308 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4309 (unsigned long)adapter);
4310
Thomas Falcon032c5e82015-12-21 11:26:06 -06004311 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4312 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4313 adapter);
4314 if (rc) {
4315 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4316 vdev->irq, rc);
4317 goto req_irq_failed;
4318 }
4319
4320 rc = vio_enable_interrupts(vdev);
4321 if (rc) {
4322 dev_err(dev, "Error %d enabling interrupts\n", rc);
4323 goto req_irq_failed;
4324 }
4325
4326 crq->cur = 0;
4327 spin_lock_init(&crq->lock);
4328
4329 return retrc;
4330
4331req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004332 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004333 do {
4334 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4335 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4336reg_crq_failed:
4337 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4338map_failed:
4339 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004340 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004341 return retrc;
4342}
4343
John Allenf6ef6402017-03-17 17:13:42 -05004344static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4345{
4346 struct device *dev = &adapter->vdev->dev;
4347 unsigned long timeout = msecs_to_jiffies(30000);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004348 u64 old_num_rx_queues, old_num_tx_queues;
John Allenf6ef6402017-03-17 17:13:42 -05004349 int rc;
4350
John Allenc26eba02017-10-26 16:23:25 -05004351 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004352 rc = ibmvnic_reset_crq(adapter);
4353 if (!rc)
4354 rc = vio_enable_interrupts(adapter->vdev);
4355 } else {
4356 rc = init_crq_queue(adapter);
4357 }
4358
John Allenf6ef6402017-03-17 17:13:42 -05004359 if (rc) {
4360 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4361 return rc;
4362 }
4363
John Allen017892c12017-05-26 10:30:19 -04004364 adapter->from_passive_init = false;
4365
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004366 old_num_rx_queues = adapter->req_rx_queues;
4367 old_num_tx_queues = adapter->req_tx_queues;
4368
John Allenf6ef6402017-03-17 17:13:42 -05004369 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004370 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004371 ibmvnic_send_crq_init(adapter);
4372 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4373 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004374 return -1;
4375 }
4376
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004377 if (adapter->init_done_rc) {
4378 release_crq_queue(adapter);
4379 return adapter->init_done_rc;
4380 }
4381
John Allen017892c12017-05-26 10:30:19 -04004382 if (adapter->from_passive_init) {
4383 adapter->state = VNIC_OPEN;
4384 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004385 return -1;
4386 }
4387
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004388 if (adapter->resetting && !adapter->wait_for_reset) {
4389 if (adapter->req_rx_queues != old_num_rx_queues ||
4390 adapter->req_tx_queues != old_num_tx_queues) {
4391 release_sub_crqs(adapter, 0);
4392 rc = init_sub_crqs(adapter);
4393 } else {
4394 rc = reset_sub_crq_queues(adapter);
4395 }
4396 } else {
Nathan Fontenot57a49432017-05-26 10:31:12 -04004397 rc = init_sub_crqs(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004398 }
4399
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004400 if (rc) {
4401 dev_err(dev, "Initialization of sub crqs failed\n");
4402 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004403 return rc;
4404 }
4405
4406 rc = init_sub_crq_irqs(adapter);
4407 if (rc) {
4408 dev_err(dev, "Failed to initialize sub crq irqs\n");
4409 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004410 }
4411
4412 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004413}
4414
Thomas Falcon40c9db82017-06-12 12:35:04 -05004415static struct device_attribute dev_attr_failover;
4416
Thomas Falcon032c5e82015-12-21 11:26:06 -06004417static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4418{
4419 struct ibmvnic_adapter *adapter;
4420 struct net_device *netdev;
4421 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004422 int rc;
4423
4424 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4425 dev->unit_address);
4426
4427 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4428 VETH_MAC_ADDR, NULL);
4429 if (!mac_addr_p) {
4430 dev_err(&dev->dev,
4431 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4432 __FILE__, __LINE__);
4433 return 0;
4434 }
4435
4436 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004437 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004438 if (!netdev)
4439 return -ENOMEM;
4440
4441 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004442 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004443 dev_set_drvdata(&dev->dev, netdev);
4444 adapter->vdev = dev;
4445 adapter->netdev = netdev;
4446
4447 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4448 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4449 netdev->irq = dev->irq;
4450 netdev->netdev_ops = &ibmvnic_netdev_ops;
4451 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4452 SET_NETDEV_DEV(netdev, &dev->dev);
4453
4454 spin_lock_init(&adapter->stats_lock);
4455
Thomas Falcon032c5e82015-12-21 11:26:06 -06004456 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004457 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004458
Nathan Fontenoted651a12017-05-03 14:04:38 -04004459 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4460 INIT_LIST_HEAD(&adapter->rwi_list);
4461 mutex_init(&adapter->reset_lock);
4462 mutex_init(&adapter->rwi_lock);
4463 adapter->resetting = false;
4464
John Allenc26eba02017-10-26 16:23:25 -05004465 adapter->mac_change_pending = false;
4466
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004467 do {
4468 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004469 if (rc && rc != EAGAIN)
4470 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004471 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004472
Thomas Falconf39f0d12017-02-14 10:22:59 -06004473 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004474 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4475 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004476
Thomas Falcon40c9db82017-06-12 12:35:04 -05004477 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004478 if (rc)
4479 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004480
Mick Tarsele876a8a2017-09-28 13:53:18 -07004481 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004482 rc = register_netdev(netdev);
4483 if (rc) {
4484 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004485 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004486 }
4487 dev_info(&dev->dev, "ibmvnic registered\n");
4488
Nathan Fontenot90c80142017-05-03 14:04:32 -04004489 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004490
4491 adapter->wait_for_reset = false;
4492
Thomas Falcon032c5e82015-12-21 11:26:06 -06004493 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004494
4495ibmvnic_register_fail:
4496 device_remove_file(&dev->dev, &dev_attr_failover);
4497
4498ibmvnic_init_fail:
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004499 release_sub_crqs(adapter, 1);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004500 release_crq_queue(adapter);
4501 free_netdev(netdev);
4502
4503 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004504}
4505
4506static int ibmvnic_remove(struct vio_dev *dev)
4507{
4508 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004509 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004510
Nathan Fontenot90c80142017-05-03 14:04:32 -04004511 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004512 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004513 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004514
4515 release_resources(adapter);
Nathan Fontenotd7c0ef32018-02-19 13:30:31 -06004516 release_sub_crqs(adapter, 1);
Nathan Fontenot37489052017-04-19 13:45:04 -04004517 release_crq_queue(adapter);
4518
Nathan Fontenot90c80142017-05-03 14:04:32 -04004519 adapter->state = VNIC_REMOVED;
4520
Nathan Fontenoted651a12017-05-03 14:04:38 -04004521 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004522 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004523 free_netdev(netdev);
4524 dev_set_drvdata(&dev->dev, NULL);
4525
4526 return 0;
4527}
4528
Thomas Falcon40c9db82017-06-12 12:35:04 -05004529static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4530 const char *buf, size_t count)
4531{
4532 struct net_device *netdev = dev_get_drvdata(dev);
4533 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4534 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4535 __be64 session_token;
4536 long rc;
4537
4538 if (!sysfs_streq(buf, "1"))
4539 return -EINVAL;
4540
4541 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4542 H_GET_SESSION_TOKEN, 0, 0, 0);
4543 if (rc) {
4544 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4545 rc);
4546 return -EINVAL;
4547 }
4548
4549 session_token = (__be64)retbuf[0];
4550 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4551 be64_to_cpu(session_token));
4552 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4553 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4554 if (rc) {
4555 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4556 rc);
4557 return -EINVAL;
4558 }
4559
4560 return count;
4561}
4562
Joe Perches6cbaefb2017-12-19 10:15:09 -08004563static DEVICE_ATTR_WO(failover);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004564
Thomas Falcon032c5e82015-12-21 11:26:06 -06004565static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4566{
4567 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4568 struct ibmvnic_adapter *adapter;
4569 struct iommu_table *tbl;
4570 unsigned long ret = 0;
4571 int i;
4572
4573 tbl = get_iommu_table_base(&vdev->dev);
4574
4575 /* netdev inits at probe time along with the structures we need below*/
4576 if (!netdev)
4577 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4578
4579 adapter = netdev_priv(netdev);
4580
4581 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004582 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4583
4584 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4585 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4586
4587 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4588 i++)
4589 ret += adapter->rx_pool[i].size *
4590 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4591
4592 return ret;
4593}
4594
4595static int ibmvnic_resume(struct device *dev)
4596{
4597 struct net_device *netdev = dev_get_drvdata(dev);
4598 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004599
John Allencb89ba22017-06-19 11:27:53 -05004600 if (adapter->state != VNIC_OPEN)
4601 return 0;
4602
John Allena2488782017-07-24 13:26:06 -05004603 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004604
4605 return 0;
4606}
4607
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304608static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004609 {"network", "IBM,vnic"},
4610 {"", "" }
4611};
4612MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4613
4614static const struct dev_pm_ops ibmvnic_pm_ops = {
4615 .resume = ibmvnic_resume
4616};
4617
4618static struct vio_driver ibmvnic_driver = {
4619 .id_table = ibmvnic_device_table,
4620 .probe = ibmvnic_probe,
4621 .remove = ibmvnic_remove,
4622 .get_desired_dma = ibmvnic_get_desired_dma,
4623 .name = ibmvnic_driver_name,
4624 .pm = &ibmvnic_pm_ops,
4625};
4626
4627/* module functions */
4628static int __init ibmvnic_module_init(void)
4629{
4630 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4631 IBMVNIC_DRIVER_VERSION);
4632
4633 return vio_register_driver(&ibmvnic_driver);
4634}
4635
4636static void __exit ibmvnic_module_exit(void)
4637{
4638 vio_unregister_driver(&ibmvnic_driver);
4639}
4640
4641module_init(ibmvnic_module_init);
4642module_exit(ibmvnic_module_exit);