blob: 461014b7ccddeeab98440e73c11d635c3d8fb44f [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 *);
93static void release_sub_crqs(struct ibmvnic_adapter *);
94static 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);
357}
358
359static int init_stats_buffers(struct ibmvnic_adapter *adapter)
360{
361 adapter->tx_stats_buffers =
362 kcalloc(adapter->req_tx_queues,
363 sizeof(struct ibmvnic_tx_queue_stats),
364 GFP_KERNEL);
365 if (!adapter->tx_stats_buffers)
366 return -ENOMEM;
367
368 adapter->rx_stats_buffers =
369 kcalloc(adapter->req_rx_queues,
370 sizeof(struct ibmvnic_rx_queue_stats),
371 GFP_KERNEL);
372 if (!adapter->rx_stats_buffers)
373 return -ENOMEM;
374
375 return 0;
376}
377
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400378static void release_stats_token(struct ibmvnic_adapter *adapter)
379{
380 struct device *dev = &adapter->vdev->dev;
381
382 if (!adapter->stats_token)
383 return;
384
385 dma_unmap_single(dev, adapter->stats_token,
386 sizeof(struct ibmvnic_statistics),
387 DMA_FROM_DEVICE);
388 adapter->stats_token = 0;
389}
390
391static int init_stats_token(struct ibmvnic_adapter *adapter)
392{
393 struct device *dev = &adapter->vdev->dev;
394 dma_addr_t stok;
395
396 stok = dma_map_single(dev, &adapter->stats,
397 sizeof(struct ibmvnic_statistics),
398 DMA_FROM_DEVICE);
399 if (dma_mapping_error(dev, stok)) {
400 dev_err(dev, "Couldn't map stats buffer\n");
401 return -1;
402 }
403
404 adapter->stats_token = stok;
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500405 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400406 return 0;
407}
408
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400409static int reset_rx_pools(struct ibmvnic_adapter *adapter)
410{
411 struct ibmvnic_rx_pool *rx_pool;
412 int rx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500413 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400414
415 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
416 for (i = 0; i < rx_scrqs; i++) {
417 rx_pool = &adapter->rx_pool[i];
418
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500419 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
420
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500421 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
422 if (rc)
423 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400424
425 for (j = 0; j < rx_pool->size; j++)
426 rx_pool->free_map[j] = j;
427
428 memset(rx_pool->rx_buff, 0,
429 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
430
431 atomic_set(&rx_pool->available, 0);
432 rx_pool->next_alloc = 0;
433 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500434 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400435 }
436
437 return 0;
438}
439
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400440static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600441{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400442 struct ibmvnic_rx_pool *rx_pool;
443 int rx_scrqs;
444 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600445
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400446 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600447 return;
448
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400449 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
450 for (i = 0; i < rx_scrqs; i++) {
451 rx_pool = &adapter->rx_pool[i];
452
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500453 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
454
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400455 kfree(rx_pool->free_map);
456 free_long_term_buff(adapter, &rx_pool->long_term_buff);
457
458 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400459 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400460
461 for (j = 0; j < rx_pool->size; j++) {
462 if (rx_pool->rx_buff[j].skb) {
463 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
464 rx_pool->rx_buff[i].skb = NULL;
465 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600466 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400467
468 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600469 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400470
471 kfree(adapter->rx_pool);
472 adapter->rx_pool = NULL;
473}
474
475static int init_rx_pools(struct net_device *netdev)
476{
477 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
478 struct device *dev = &adapter->vdev->dev;
479 struct ibmvnic_rx_pool *rx_pool;
480 int rxadd_subcrqs;
481 u64 *size_array;
482 int i, j;
483
484 rxadd_subcrqs =
485 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
486 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
487 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
488
489 adapter->rx_pool = kcalloc(rxadd_subcrqs,
490 sizeof(struct ibmvnic_rx_pool),
491 GFP_KERNEL);
492 if (!adapter->rx_pool) {
493 dev_err(dev, "Failed to allocate rx pools\n");
494 return -1;
495 }
496
497 for (i = 0; i < rxadd_subcrqs; i++) {
498 rx_pool = &adapter->rx_pool[i];
499
500 netdev_dbg(adapter->netdev,
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500501 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400502 i, adapter->req_rx_add_entries_per_subcrq,
503 be64_to_cpu(size_array[i]));
504
505 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
506 rx_pool->index = i;
507 rx_pool->buff_size = be64_to_cpu(size_array[i]);
508 rx_pool->active = 1;
509
510 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
511 GFP_KERNEL);
512 if (!rx_pool->free_map) {
513 release_rx_pools(adapter);
514 return -1;
515 }
516
517 rx_pool->rx_buff = kcalloc(rx_pool->size,
518 sizeof(struct ibmvnic_rx_buff),
519 GFP_KERNEL);
520 if (!rx_pool->rx_buff) {
521 dev_err(dev, "Couldn't alloc rx buffers\n");
522 release_rx_pools(adapter);
523 return -1;
524 }
525
526 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
527 rx_pool->size * rx_pool->buff_size)) {
528 release_rx_pools(adapter);
529 return -1;
530 }
531
532 for (j = 0; j < rx_pool->size; ++j)
533 rx_pool->free_map[j] = j;
534
535 atomic_set(&rx_pool->available, 0);
536 rx_pool->next_alloc = 0;
537 rx_pool->next_free = 0;
538 }
539
540 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600541}
542
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400543static int reset_tx_pools(struct ibmvnic_adapter *adapter)
544{
545 struct ibmvnic_tx_pool *tx_pool;
546 int tx_scrqs;
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500547 int i, j, rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400548
549 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
550 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500551 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
552
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400553 tx_pool = &adapter->tx_pool[i];
554
Thomas Falconf3be0cb2017-06-21 14:53:01 -0500555 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
556 if (rc)
557 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400558
Thomas Falconfdb06102017-10-17 12:36:55 -0500559 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
560 if (rc)
561 return rc;
562
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400563 memset(tx_pool->tx_buff, 0,
564 adapter->req_tx_entries_per_subcrq *
565 sizeof(struct ibmvnic_tx_buff));
566
567 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
568 tx_pool->free_map[j] = j;
569
570 tx_pool->consumer_index = 0;
571 tx_pool->producer_index = 0;
Thomas Falconfdb06102017-10-17 12:36:55 -0500572 tx_pool->tso_index = 0;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400573 }
574
575 return 0;
576}
577
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200578static void release_vpd_data(struct ibmvnic_adapter *adapter)
579{
580 if (!adapter->vpd)
581 return;
582
583 kfree(adapter->vpd->buff);
584 kfree(adapter->vpd);
585}
586
Nathan Fontenotc657e322017-03-30 02:49:06 -0400587static void release_tx_pools(struct ibmvnic_adapter *adapter)
588{
589 struct ibmvnic_tx_pool *tx_pool;
590 int i, tx_scrqs;
591
592 if (!adapter->tx_pool)
593 return;
594
595 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
596 for (i = 0; i < tx_scrqs; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500597 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400598 tx_pool = &adapter->tx_pool[i];
599 kfree(tx_pool->tx_buff);
600 free_long_term_buff(adapter, &tx_pool->long_term_buff);
Thomas Falconfdb06102017-10-17 12:36:55 -0500601 free_long_term_buff(adapter, &tx_pool->tso_ltb);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400602 kfree(tx_pool->free_map);
603 }
604
605 kfree(adapter->tx_pool);
606 adapter->tx_pool = NULL;
607}
608
609static int init_tx_pools(struct net_device *netdev)
610{
611 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
612 struct device *dev = &adapter->vdev->dev;
613 struct ibmvnic_tx_pool *tx_pool;
614 int tx_subcrqs;
615 int i, j;
616
617 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
618 adapter->tx_pool = kcalloc(tx_subcrqs,
619 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
620 if (!adapter->tx_pool)
621 return -1;
622
623 for (i = 0; i < tx_subcrqs; i++) {
624 tx_pool = &adapter->tx_pool[i];
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500625
626 netdev_dbg(adapter->netdev,
627 "Initializing tx_pool[%d], %lld buffs\n",
628 i, adapter->req_tx_entries_per_subcrq);
629
Nathan Fontenotc657e322017-03-30 02:49:06 -0400630 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
631 sizeof(struct ibmvnic_tx_buff),
632 GFP_KERNEL);
633 if (!tx_pool->tx_buff) {
634 dev_err(dev, "tx pool buffer allocation failed\n");
635 release_tx_pools(adapter);
636 return -1;
637 }
638
639 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
640 adapter->req_tx_entries_per_subcrq *
641 adapter->req_mtu)) {
642 release_tx_pools(adapter);
643 return -1;
644 }
645
Thomas Falconfdb06102017-10-17 12:36:55 -0500646 /* alloc TSO ltb */
647 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
648 IBMVNIC_TSO_BUFS *
649 IBMVNIC_TSO_BUF_SZ)) {
650 release_tx_pools(adapter);
651 return -1;
652 }
653
654 tx_pool->tso_index = 0;
655
Nathan Fontenotc657e322017-03-30 02:49:06 -0400656 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
657 sizeof(int), GFP_KERNEL);
658 if (!tx_pool->free_map) {
659 release_tx_pools(adapter);
660 return -1;
661 }
662
663 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
664 tx_pool->free_map[j] = j;
665
666 tx_pool->consumer_index = 0;
667 tx_pool->producer_index = 0;
668 }
669
670 return 0;
671}
672
Nathan Fontenot661a2622017-04-19 13:44:58 -0400673static void release_error_buffers(struct ibmvnic_adapter *adapter)
674{
675 struct device *dev = &adapter->vdev->dev;
676 struct ibmvnic_error_buff *error_buff, *tmp;
677 unsigned long flags;
678
679 spin_lock_irqsave(&adapter->error_list_lock, flags);
680 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
681 list_del(&error_buff->list);
682 dma_unmap_single(dev, error_buff->dma, error_buff->len,
683 DMA_FROM_DEVICE);
684 kfree(error_buff->buff);
685 kfree(error_buff);
686 }
687 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
688}
689
John Allend944c3d62017-05-26 10:30:13 -0400690static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
691{
692 int i;
693
694 if (adapter->napi_enabled)
695 return;
696
697 for (i = 0; i < adapter->req_rx_queues; i++)
698 napi_enable(&adapter->napi[i]);
699
700 adapter->napi_enabled = true;
701}
702
703static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
704{
705 int i;
706
707 if (!adapter->napi_enabled)
708 return;
709
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500710 for (i = 0; i < adapter->req_rx_queues; i++) {
711 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
John Allend944c3d62017-05-26 10:30:13 -0400712 napi_disable(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500713 }
John Allend944c3d62017-05-26 10:30:13 -0400714
715 adapter->napi_enabled = false;
716}
717
John Allena57a5d22017-03-17 17:13:41 -0500718static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600719{
720 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500721 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600722 struct device *dev = &adapter->vdev->dev;
Thomas Falcon4d96f122017-08-01 15:04:36 -0500723 int rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600724
John Allenbd0b6722017-03-17 17:13:40 -0500725 do {
726 if (adapter->renegotiate) {
727 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400728 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500729
730 reinit_completion(&adapter->init_done);
731 send_cap_queries(adapter);
732 if (!wait_for_completion_timeout(&adapter->init_done,
733 timeout)) {
734 dev_err(dev, "Capabilities query timeout\n");
735 return -1;
736 }
Thomas Falcon4d96f122017-08-01 15:04:36 -0500737 rc = init_sub_crqs(adapter);
738 if (rc) {
739 dev_err(dev,
740 "Initialization of SCRQ's failed\n");
741 return -1;
742 }
743 rc = init_sub_crq_irqs(adapter);
744 if (rc) {
745 dev_err(dev,
746 "Initialization of SCRQ's irqs failed\n");
747 return -1;
748 }
John Allenbd0b6722017-03-17 17:13:40 -0500749 }
750
751 reinit_completion(&adapter->init_done);
752 send_login(adapter);
753 if (!wait_for_completion_timeout(&adapter->init_done,
754 timeout)) {
755 dev_err(dev, "Login timeout\n");
756 return -1;
757 }
758 } while (adapter->renegotiate);
759
John Allena57a5d22017-03-17 17:13:41 -0500760 return 0;
761}
762
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400763static void release_resources(struct ibmvnic_adapter *adapter)
764{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400765 int i;
766
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200767 release_vpd_data(adapter);
768
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400769 release_tx_pools(adapter);
770 release_rx_pools(adapter);
771
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400772 release_stats_token(adapter);
John Allen3d52b592017-08-02 16:44:14 -0500773 release_stats_buffers(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400774 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400775
776 if (adapter->napi) {
777 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500778 if (&adapter->napi[i]) {
779 netdev_dbg(adapter->netdev,
780 "Releasing napi[%d]\n", i);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400781 netif_napi_del(&adapter->napi[i]);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500782 }
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400783 }
784 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400785}
786
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400787static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
788{
789 struct net_device *netdev = adapter->netdev;
790 unsigned long timeout = msecs_to_jiffies(30000);
791 union ibmvnic_crq crq;
792 bool resend;
793 int rc;
794
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500795 netdev_dbg(netdev, "setting link state %d\n", link_state);
796
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400797 memset(&crq, 0, sizeof(crq));
798 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
799 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
800 crq.logical_link_state.link_state = link_state;
801
802 do {
803 resend = false;
804
805 reinit_completion(&adapter->init_done);
806 rc = ibmvnic_send_crq(adapter, &crq);
807 if (rc) {
808 netdev_err(netdev, "Failed to set link state\n");
809 return rc;
810 }
811
812 if (!wait_for_completion_timeout(&adapter->init_done,
813 timeout)) {
814 netdev_err(netdev, "timeout setting link state\n");
815 return -1;
816 }
817
818 if (adapter->init_done_rc == 1) {
819 /* Partuial success, delay and re-send */
820 mdelay(1000);
821 resend = true;
822 }
823 } while (resend);
824
825 return 0;
826}
827
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400828static int set_real_num_queues(struct net_device *netdev)
829{
830 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
831 int rc;
832
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500833 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
834 adapter->req_tx_queues, adapter->req_rx_queues);
835
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400836 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
837 if (rc) {
838 netdev_err(netdev, "failed to set the number of tx queues\n");
839 return rc;
840 }
841
842 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
843 if (rc)
844 netdev_err(netdev, "failed to set the number of rx queues\n");
845
846 return rc;
847}
848
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200849static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
850{
851 struct device *dev = &adapter->vdev->dev;
852 union ibmvnic_crq crq;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200853 int len = 0;
854
855 if (adapter->vpd->buff)
856 len = adapter->vpd->len;
857
858 reinit_completion(&adapter->fw_done);
859 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
860 crq.get_vpd_size.cmd = GET_VPD_SIZE;
861 ibmvnic_send_crq(adapter, &crq);
862 wait_for_completion(&adapter->fw_done);
863
864 if (!adapter->vpd->len)
865 return -ENODATA;
866
867 if (!adapter->vpd->buff)
868 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
869 else if (adapter->vpd->len != len)
870 adapter->vpd->buff =
871 krealloc(adapter->vpd->buff,
872 adapter->vpd->len, GFP_KERNEL);
873
874 if (!adapter->vpd->buff) {
875 dev_err(dev, "Could allocate VPD buffer\n");
876 return -ENOMEM;
877 }
878
879 adapter->vpd->dma_addr =
880 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
881 DMA_FROM_DEVICE);
Desnes Augusto Nunes do Rosariof7431062017-11-17 09:09:04 -0200882 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200883 dev_err(dev, "Could not map VPD buffer\n");
884 kfree(adapter->vpd->buff);
885 return -ENOMEM;
886 }
887
888 reinit_completion(&adapter->fw_done);
889 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
890 crq.get_vpd.cmd = GET_VPD;
891 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
892 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
893 ibmvnic_send_crq(adapter, &crq);
894 wait_for_completion(&adapter->fw_done);
895
896 return 0;
897}
898
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400899static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500900{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400901 struct net_device *netdev = adapter->netdev;
902 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500903
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400904 rc = set_real_num_queues(netdev);
905 if (rc)
906 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500907
John Allen3d52b592017-08-02 16:44:14 -0500908 rc = init_stats_buffers(adapter);
909 if (rc)
910 return rc;
911
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400912 rc = init_stats_token(adapter);
913 if (rc)
914 return rc;
915
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200916 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
917 if (!adapter->vpd)
918 return -ENOMEM;
919
Thomas Falcon032c5e82015-12-21 11:26:06 -0600920 adapter->map_id = 1;
921 adapter->napi = kcalloc(adapter->req_rx_queues,
922 sizeof(struct napi_struct), GFP_KERNEL);
923 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400924 return -ENOMEM;
925
Thomas Falcon032c5e82015-12-21 11:26:06 -0600926 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500927 netdev_dbg(netdev, "Adding napi[%d]\n", i);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600928 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
929 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600930 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600931
Thomas Falcon032c5e82015-12-21 11:26:06 -0600932 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400933
934 rc = init_rx_pools(netdev);
935 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400936 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600937
Nathan Fontenotc657e322017-03-30 02:49:06 -0400938 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400939 return rc;
940}
941
Nathan Fontenoted651a12017-05-03 14:04:38 -0400942static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400943{
944 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400945 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400946 int i, rc;
947
Nathan Fontenot90c80142017-05-03 14:04:32 -0400948 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600949 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400950 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400951
Thomas Falcon032c5e82015-12-21 11:26:06 -0600952 /* We're ready to receive frames, enable the sub-crq interrupts and
953 * set the logical link state to up
954 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400955 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500956 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400957 if (prev_state == VNIC_CLOSED)
958 enable_irq(adapter->rx_scrq[i]->irq);
959 else
960 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
961 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600962
Nathan Fontenoted651a12017-05-03 14:04:38 -0400963 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -0500964 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400965 if (prev_state == VNIC_CLOSED)
966 enable_irq(adapter->tx_scrq[i]->irq);
967 else
968 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
969 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600970
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400971 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400972 if (rc) {
973 for (i = 0; i < adapter->req_rx_queues; i++)
974 napi_disable(&adapter->napi[i]);
975 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400976 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400977 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600978
Nathan Fontenoted651a12017-05-03 14:04:38 -0400979 netif_tx_start_all_queues(netdev);
980
981 if (prev_state == VNIC_CLOSED) {
982 for (i = 0; i < adapter->req_rx_queues; i++)
983 napi_schedule(&adapter->napi[i]);
984 }
985
986 adapter->state = VNIC_OPEN;
987 return rc;
988}
989
990static int ibmvnic_open(struct net_device *netdev)
991{
992 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -0200993 int rc, vpd;
Nathan Fontenoted651a12017-05-03 14:04:38 -0400994
995 mutex_lock(&adapter->reset_lock);
996
John Allenc26eba02017-10-26 16:23:25 -0500997 if (adapter->mac_change_pending) {
998 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
999 adapter->mac_change_pending = false;
1000 }
1001
Nathan Fontenoted651a12017-05-03 14:04:38 -04001002 if (adapter->state != VNIC_CLOSED) {
1003 rc = ibmvnic_login(netdev);
1004 if (rc) {
1005 mutex_unlock(&adapter->reset_lock);
1006 return rc;
1007 }
1008
1009 rc = init_resources(adapter);
1010 if (rc) {
1011 netdev_err(netdev, "failed to initialize resources\n");
1012 release_resources(adapter);
1013 mutex_unlock(&adapter->reset_lock);
1014 return rc;
1015 }
1016 }
1017
1018 rc = __ibmvnic_open(netdev);
Mick Tarsele876a8a2017-09-28 13:53:18 -07001019 netif_carrier_on(netdev);
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001020
1021 /* Vital Product Data (VPD) */
1022 vpd = ibmvnic_get_vpd(adapter);
1023 if (vpd)
1024 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1025
Nathan Fontenoted651a12017-05-03 14:04:38 -04001026 mutex_unlock(&adapter->reset_lock);
1027
Nathan Fontenotbfc32f22017-05-03 14:04:26 -04001028 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001029}
1030
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001031static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1032{
1033 struct ibmvnic_tx_pool *tx_pool;
1034 u64 tx_entries;
1035 int tx_scrqs;
1036 int i, j;
1037
1038 if (!adapter->tx_pool)
1039 return;
1040
1041 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1042 tx_entries = adapter->req_tx_entries_per_subcrq;
1043
1044 /* Free any remaining skbs in the tx buffer pools */
1045 for (i = 0; i < tx_scrqs; i++) {
1046 tx_pool = &adapter->tx_pool[i];
1047 if (!tx_pool)
1048 continue;
1049
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001050 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
Nathan Fontenotb41b83e2017-05-03 14:04:56 -04001051 for (j = 0; j < tx_entries; j++) {
1052 if (tx_pool->tx_buff[j].skb) {
1053 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1054 tx_pool->tx_buff[j].skb = NULL;
1055 }
1056 }
1057 }
1058}
1059
Nathan Fontenoted651a12017-05-03 14:04:38 -04001060static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -05001061{
1062 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001063 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -05001064 int i;
1065
Nathan Fontenot90c80142017-05-03 14:04:32 -04001066 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -05001067
1068 /* ensure that transmissions are stopped if called by do_reset */
1069 if (adapter->resetting)
1070 netif_tx_disable(netdev);
1071 else
1072 netif_tx_stop_all_queues(netdev);
1073
John Allend944c3d62017-05-26 10:30:13 -04001074 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001075
1076 if (adapter->tx_scrq) {
1077 for (i = 0; i < adapter->req_tx_queues; i++)
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001078 if (adapter->tx_scrq[i]->irq) {
1079 netdev_dbg(adapter->netdev,
1080 "Disabling tx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001081 disable_irq(adapter->tx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001082 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001083 }
1084
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001085 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001086 if (rc)
1087 return rc;
1088
1089 if (adapter->rx_scrq) {
1090 for (i = 0; i < adapter->req_rx_queues; i++) {
1091 int retries = 10;
1092
1093 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1094 retries--;
1095 mdelay(100);
1096
1097 if (retries == 0)
1098 break;
1099 }
1100
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001101 if (adapter->rx_scrq[i]->irq) {
1102 netdev_dbg(adapter->netdev,
1103 "Disabling rx_scrq[%d] irq\n", i);
Nathan Fontenot46293b92017-05-03 14:05:02 -04001104 disable_irq(adapter->rx_scrq[i]->irq);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001105 }
Nathan Fontenot46293b92017-05-03 14:05:02 -04001106 }
1107 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001108
Thomas Falcon10f76212017-05-26 10:30:31 -04001109 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -04001110 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04001111 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001112}
1113
Nathan Fontenoted651a12017-05-03 14:04:38 -04001114static int ibmvnic_close(struct net_device *netdev)
1115{
1116 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1117 int rc;
1118
1119 mutex_lock(&adapter->reset_lock);
1120 rc = __ibmvnic_close(netdev);
1121 mutex_unlock(&adapter->reset_lock);
1122
1123 return rc;
1124}
1125
Thomas Falconad7775d2016-04-01 17:20:34 -05001126/**
1127 * build_hdr_data - creates L2/L3/L4 header data buffer
1128 * @hdr_field - bitfield determining needed headers
1129 * @skb - socket buffer
1130 * @hdr_len - array of header lengths
1131 * @tot_len - total length of data
1132 *
1133 * Reads hdr_field to determine which headers are needed by firmware.
1134 * Builds a buffer containing these headers. Saves individual header
1135 * lengths and total buffer length to be used to build descriptors.
1136 */
1137static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1138 int *hdr_len, u8 *hdr_data)
1139{
1140 int len = 0;
1141 u8 *hdr;
1142
1143 hdr_len[0] = sizeof(struct ethhdr);
1144
1145 if (skb->protocol == htons(ETH_P_IP)) {
1146 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1147 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1148 hdr_len[2] = tcp_hdrlen(skb);
1149 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1150 hdr_len[2] = sizeof(struct udphdr);
1151 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1152 hdr_len[1] = sizeof(struct ipv6hdr);
1153 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1154 hdr_len[2] = tcp_hdrlen(skb);
1155 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1156 hdr_len[2] = sizeof(struct udphdr);
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001157 } else if (skb->protocol == htons(ETH_P_ARP)) {
1158 hdr_len[1] = arp_hdr_len(skb->dev);
1159 hdr_len[2] = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001160 }
1161
1162 memset(hdr_data, 0, 120);
1163 if ((hdr_field >> 6) & 1) {
1164 hdr = skb_mac_header(skb);
1165 memcpy(hdr_data, hdr, hdr_len[0]);
1166 len += hdr_len[0];
1167 }
1168
1169 if ((hdr_field >> 5) & 1) {
1170 hdr = skb_network_header(skb);
1171 memcpy(hdr_data + len, hdr, hdr_len[1]);
1172 len += hdr_len[1];
1173 }
1174
1175 if ((hdr_field >> 4) & 1) {
1176 hdr = skb_transport_header(skb);
1177 memcpy(hdr_data + len, hdr, hdr_len[2]);
1178 len += hdr_len[2];
1179 }
1180 return len;
1181}
1182
1183/**
1184 * create_hdr_descs - create header and header extension descriptors
1185 * @hdr_field - bitfield determining needed headers
1186 * @data - buffer containing header data
1187 * @len - length of data buffer
1188 * @hdr_len - array of individual header lengths
1189 * @scrq_arr - descriptor array
1190 *
1191 * Creates header and, if needed, header extension descriptors and
1192 * places them in a descriptor array, scrq_arr
1193 */
1194
Thomas Falcon2de09682017-10-16 10:02:11 -05001195static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1196 union sub_crq *scrq_arr)
Thomas Falconad7775d2016-04-01 17:20:34 -05001197{
1198 union sub_crq hdr_desc;
1199 int tmp_len = len;
Thomas Falcon2de09682017-10-16 10:02:11 -05001200 int num_descs = 0;
Thomas Falconad7775d2016-04-01 17:20:34 -05001201 u8 *data, *cur;
1202 int tmp;
1203
1204 while (tmp_len > 0) {
1205 cur = hdr_data + len - tmp_len;
1206
1207 memset(&hdr_desc, 0, sizeof(hdr_desc));
1208 if (cur != hdr_data) {
1209 data = hdr_desc.hdr_ext.data;
1210 tmp = tmp_len > 29 ? 29 : tmp_len;
1211 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1212 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1213 hdr_desc.hdr_ext.len = tmp;
1214 } else {
1215 data = hdr_desc.hdr.data;
1216 tmp = tmp_len > 24 ? 24 : tmp_len;
1217 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1218 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1219 hdr_desc.hdr.len = tmp;
1220 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1221 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1222 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1223 hdr_desc.hdr.flag = hdr_field << 1;
1224 }
1225 memcpy(data, cur, tmp);
1226 tmp_len -= tmp;
1227 *scrq_arr = hdr_desc;
1228 scrq_arr++;
Thomas Falcon2de09682017-10-16 10:02:11 -05001229 num_descs++;
Thomas Falconad7775d2016-04-01 17:20:34 -05001230 }
Thomas Falcon2de09682017-10-16 10:02:11 -05001231
1232 return num_descs;
Thomas Falconad7775d2016-04-01 17:20:34 -05001233}
1234
1235/**
1236 * build_hdr_descs_arr - build a header descriptor array
1237 * @skb - socket buffer
1238 * @num_entries - number of descriptors to be sent
1239 * @subcrq - first TX descriptor
1240 * @hdr_field - bit field determining which headers will be sent
1241 *
1242 * This function will build a TX descriptor array with applicable
1243 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1244 */
1245
1246static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1247 int *num_entries, u8 hdr_field)
1248{
1249 int hdr_len[3] = {0, 0, 0};
Thomas Falcon2de09682017-10-16 10:02:11 -05001250 int tot_len;
Thomas Falconad7775d2016-04-01 17:20:34 -05001251 u8 *hdr_data = txbuff->hdr_data;
1252
1253 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1254 txbuff->hdr_data);
Thomas Falcon2de09682017-10-16 10:02:11 -05001255 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
Thomas Falconad7775d2016-04-01 17:20:34 -05001256 txbuff->indir_arr + 1);
1257}
1258
Thomas Falcon032c5e82015-12-21 11:26:06 -06001259static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1260{
1261 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1262 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001263 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001264 struct device *dev = &adapter->vdev->dev;
1265 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001266 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001267 struct ibmvnic_tx_pool *tx_pool;
1268 unsigned int tx_send_failed = 0;
1269 unsigned int tx_map_failed = 0;
1270 unsigned int tx_dropped = 0;
1271 unsigned int tx_packets = 0;
1272 unsigned int tx_bytes = 0;
1273 dma_addr_t data_dma_addr;
1274 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001275 unsigned long lpar_rc;
1276 union sub_crq tx_crq;
1277 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001278 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001279 unsigned char *dst;
1280 u64 *handle_array;
1281 int index = 0;
1282 int ret = 0;
1283
Nathan Fontenoted651a12017-05-03 14:04:38 -04001284 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001285 if (!netif_subqueue_stopped(netdev, skb))
1286 netif_stop_subqueue(netdev, queue_num);
1287 dev_kfree_skb_any(skb);
1288
Thomas Falcon032c5e82015-12-21 11:26:06 -06001289 tx_send_failed++;
1290 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001291 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001292 goto out;
1293 }
1294
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001295 tx_pool = &adapter->tx_pool[queue_num];
1296 tx_scrq = adapter->tx_scrq[queue_num];
1297 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1298 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1299 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1300
Thomas Falcon032c5e82015-12-21 11:26:06 -06001301 index = tx_pool->free_map[tx_pool->consumer_index];
Thomas Falconfdb06102017-10-17 12:36:55 -05001302
1303 if (skb_is_gso(skb)) {
1304 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1305 dst = tx_pool->tso_ltb.buff + offset;
1306 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1307 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1308 tx_pool->tso_index++;
1309 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1310 tx_pool->tso_index = 0;
1311 } else {
1312 offset = index * adapter->req_mtu;
1313 dst = tx_pool->long_term_buff.buff + offset;
1314 memset(dst, 0, adapter->req_mtu);
1315 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1316 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001317
Thomas Falcon15482052017-10-17 12:36:54 -05001318 if (skb_shinfo(skb)->nr_frags) {
1319 int cur, i;
1320
1321 /* Copy the head */
1322 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1323 cur = skb_headlen(skb);
1324
1325 /* Copy the frags */
1326 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1327 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1328
1329 memcpy(dst + cur,
1330 page_address(skb_frag_page(frag)) +
1331 frag->page_offset, skb_frag_size(frag));
1332 cur += skb_frag_size(frag);
1333 }
1334 } else {
1335 skb_copy_from_linear_data(skb, dst, skb->len);
1336 }
1337
Thomas Falcon032c5e82015-12-21 11:26:06 -06001338 tx_pool->consumer_index =
1339 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001340 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001341
1342 tx_buff = &tx_pool->tx_buff[index];
1343 tx_buff->skb = skb;
1344 tx_buff->data_dma[0] = data_dma_addr;
1345 tx_buff->data_len[0] = skb->len;
1346 tx_buff->index = index;
1347 tx_buff->pool_index = queue_num;
1348 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001349
1350 memset(&tx_crq, 0, sizeof(tx_crq));
1351 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1352 tx_crq.v1.type = IBMVNIC_TX_DESC;
1353 tx_crq.v1.n_crq_elem = 1;
1354 tx_crq.v1.n_sge = 1;
1355 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1356 tx_crq.v1.correlator = cpu_to_be32(index);
Thomas Falconfdb06102017-10-17 12:36:55 -05001357 if (skb_is_gso(skb))
1358 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1359 else
1360 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001361 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1362 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1363
1364 if (adapter->vlan_header_insertion) {
1365 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1366 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1367 }
1368
1369 if (skb->protocol == htons(ETH_P_IP)) {
1370 if (ip_hdr(skb)->version == 4)
1371 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1372 else if (ip_hdr(skb)->version == 6)
1373 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1374
1375 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1376 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1377 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1378 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1379 }
1380
Thomas Falconad7775d2016-04-01 17:20:34 -05001381 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001382 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001383 hdrs += 2;
1384 }
Thomas Falconfdb06102017-10-17 12:36:55 -05001385 if (skb_is_gso(skb)) {
1386 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1387 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1388 hdrs += 2;
1389 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001390 /* determine if l2/3/4 headers are sent to firmware */
1391 if ((*hdrs >> 7) & 1 &&
1392 (skb->protocol == htons(ETH_P_IP) ||
Thomas Falcon4eb50ce2017-12-18 12:52:40 -06001393 skb->protocol == htons(ETH_P_IPV6) ||
1394 skb->protocol == htons(ETH_P_ARP))) {
Thomas Falconad7775d2016-04-01 17:20:34 -05001395 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1396 tx_crq.v1.n_crq_elem = num_entries;
1397 tx_buff->indir_arr[0] = tx_crq;
1398 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1399 sizeof(tx_buff->indir_arr),
1400 DMA_TO_DEVICE);
1401 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001402 dev_kfree_skb_any(skb);
1403 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001404 if (!firmware_has_feature(FW_FEATURE_CMO))
1405 dev_err(dev, "tx: unable to map descriptor array\n");
1406 tx_map_failed++;
1407 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001408 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001409 goto out;
1410 }
John Allen498cd8e2016-04-06 11:49:55 -05001411 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001412 (u64)tx_buff->indir_dma,
1413 (u64)num_entries);
1414 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001415 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1416 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001417 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001418 if (lpar_rc != H_SUCCESS) {
1419 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1420
1421 if (tx_pool->consumer_index == 0)
1422 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001423 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001424 else
1425 tx_pool->consumer_index--;
1426
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001427 dev_kfree_skb_any(skb);
1428 tx_buff->skb = NULL;
1429
Thomas Falconb8c80b82017-05-26 10:30:42 -04001430 if (lpar_rc == H_CLOSED) {
1431 /* Disable TX and report carrier off if queue is closed.
1432 * Firmware guarantees that a signal will be sent to the
1433 * driver, triggering a reset or some other action.
1434 */
1435 netif_tx_stop_all_queues(netdev);
1436 netif_carrier_off(netdev);
1437 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001438
Thomas Falcon032c5e82015-12-21 11:26:06 -06001439 tx_send_failed++;
1440 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001441 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001442 goto out;
1443 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001444
Brian King58c8c0c2017-04-19 13:44:47 -04001445 if (atomic_inc_return(&tx_scrq->used)
1446 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001447 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1448 netif_stop_subqueue(netdev, queue_num);
1449 }
1450
Thomas Falcon032c5e82015-12-21 11:26:06 -06001451 tx_packets++;
1452 tx_bytes += skb->len;
1453 txq->trans_start = jiffies;
1454 ret = NETDEV_TX_OK;
1455
1456out:
1457 netdev->stats.tx_dropped += tx_dropped;
1458 netdev->stats.tx_bytes += tx_bytes;
1459 netdev->stats.tx_packets += tx_packets;
1460 adapter->tx_send_failed += tx_send_failed;
1461 adapter->tx_map_failed += tx_map_failed;
John Allen3d52b592017-08-02 16:44:14 -05001462 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1463 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1464 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001465
1466 return ret;
1467}
1468
1469static void ibmvnic_set_multi(struct net_device *netdev)
1470{
1471 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1472 struct netdev_hw_addr *ha;
1473 union ibmvnic_crq crq;
1474
1475 memset(&crq, 0, sizeof(crq));
1476 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1477 crq.request_capability.cmd = REQUEST_CAPABILITY;
1478
1479 if (netdev->flags & IFF_PROMISC) {
1480 if (!adapter->promisc_supported)
1481 return;
1482 } else {
1483 if (netdev->flags & IFF_ALLMULTI) {
1484 /* Accept all multicast */
1485 memset(&crq, 0, sizeof(crq));
1486 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1487 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1488 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1489 ibmvnic_send_crq(adapter, &crq);
1490 } else if (netdev_mc_empty(netdev)) {
1491 /* Reject all multicast */
1492 memset(&crq, 0, sizeof(crq));
1493 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1494 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1495 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1496 ibmvnic_send_crq(adapter, &crq);
1497 } else {
1498 /* Accept one or more multicast(s) */
1499 netdev_for_each_mc_addr(ha, netdev) {
1500 memset(&crq, 0, sizeof(crq));
1501 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1502 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1503 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1504 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1505 ha->addr);
1506 ibmvnic_send_crq(adapter, &crq);
1507 }
1508 }
1509 }
1510}
1511
John Allenc26eba02017-10-26 16:23:25 -05001512static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001513{
1514 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1515 struct sockaddr *addr = p;
1516 union ibmvnic_crq crq;
1517
1518 if (!is_valid_ether_addr(addr->sa_data))
1519 return -EADDRNOTAVAIL;
1520
1521 memset(&crq, 0, sizeof(crq));
1522 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1523 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1524 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1525 ibmvnic_send_crq(adapter, &crq);
1526 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1527 return 0;
1528}
1529
John Allenc26eba02017-10-26 16:23:25 -05001530static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1531{
1532 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1533 struct sockaddr *addr = p;
1534
1535 if (adapter->state != VNIC_OPEN) {
1536 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1537 adapter->mac_change_pending = true;
1538 return 0;
1539 }
1540
1541 __ibmvnic_set_mac(netdev, addr);
1542
1543 return 0;
1544}
1545
Nathan Fontenoted651a12017-05-03 14:04:38 -04001546/**
1547 * do_reset returns zero if we are able to keep processing reset events, or
1548 * non-zero if we hit a fatal error and must halt.
1549 */
1550static int do_reset(struct ibmvnic_adapter *adapter,
1551 struct ibmvnic_rwi *rwi, u32 reset_state)
1552{
1553 struct net_device *netdev = adapter->netdev;
1554 int i, rc;
1555
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001556 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1557 rwi->reset_reason);
1558
Nathan Fontenoted651a12017-05-03 14:04:38 -04001559 netif_carrier_off(netdev);
1560 adapter->reset_reason = rwi->reset_reason;
1561
1562 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1563 rc = ibmvnic_reenable_crq_queue(adapter);
1564 if (rc)
1565 return 0;
1566 }
1567
1568 rc = __ibmvnic_close(netdev);
1569 if (rc)
1570 return rc;
1571
John Allenc26eba02017-10-26 16:23:25 -05001572 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1573 adapter->wait_for_reset) {
1574 release_resources(adapter);
1575 release_sub_crqs(adapter);
1576 release_crq_queue(adapter);
1577 }
1578
John Allen8cb31cf2017-05-26 10:30:37 -04001579 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1580 /* remove the closed state so when we call open it appears
1581 * we are coming from the probed state.
1582 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001583 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001584
John Allen8cb31cf2017-05-26 10:30:37 -04001585 rc = ibmvnic_init(adapter);
1586 if (rc)
John Allen2a1bf512017-10-26 16:24:15 -05001587 return IBMVNIC_INIT_FAILED;
John Allen8cb31cf2017-05-26 10:30:37 -04001588
1589 /* If the adapter was in PROBE state prior to the reset,
1590 * exit here.
1591 */
1592 if (reset_state == VNIC_PROBED)
1593 return 0;
1594
1595 rc = ibmvnic_login(netdev);
1596 if (rc) {
1597 adapter->state = VNIC_PROBED;
1598 return 0;
1599 }
1600
John Allenc26eba02017-10-26 16:23:25 -05001601 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1602 adapter->wait_for_reset) {
1603 rc = init_resources(adapter);
1604 if (rc)
1605 return rc;
1606 } else {
1607 rc = reset_tx_pools(adapter);
1608 if (rc)
1609 return rc;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001610
John Allenc26eba02017-10-26 16:23:25 -05001611 rc = reset_rx_pools(adapter);
1612 if (rc)
1613 return rc;
John Allen8cb31cf2017-05-26 10:30:37 -04001614
John Allenc26eba02017-10-26 16:23:25 -05001615 if (reset_state == VNIC_CLOSED)
1616 return 0;
1617 }
Nathan Fontenoted651a12017-05-03 14:04:38 -04001618 }
1619
Nathan Fontenoted651a12017-05-03 14:04:38 -04001620 rc = __ibmvnic_open(netdev);
1621 if (rc) {
1622 if (list_empty(&adapter->rwi_list))
1623 adapter->state = VNIC_CLOSED;
1624 else
1625 adapter->state = reset_state;
1626
1627 return 0;
1628 }
1629
1630 netif_carrier_on(netdev);
1631
1632 /* kick napi */
1633 for (i = 0; i < adapter->req_rx_queues; i++)
1634 napi_schedule(&adapter->napi[i]);
1635
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001636 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1637 netdev_notify_peers(netdev);
1638
Nathan Fontenoted651a12017-05-03 14:04:38 -04001639 return 0;
1640}
1641
1642static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1643{
1644 struct ibmvnic_rwi *rwi;
1645
1646 mutex_lock(&adapter->rwi_lock);
1647
1648 if (!list_empty(&adapter->rwi_list)) {
1649 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1650 list);
1651 list_del(&rwi->list);
1652 } else {
1653 rwi = NULL;
1654 }
1655
1656 mutex_unlock(&adapter->rwi_lock);
1657 return rwi;
1658}
1659
1660static void free_all_rwi(struct ibmvnic_adapter *adapter)
1661{
1662 struct ibmvnic_rwi *rwi;
1663
1664 rwi = get_next_rwi(adapter);
1665 while (rwi) {
1666 kfree(rwi);
1667 rwi = get_next_rwi(adapter);
1668 }
1669}
1670
1671static void __ibmvnic_reset(struct work_struct *work)
1672{
1673 struct ibmvnic_rwi *rwi;
1674 struct ibmvnic_adapter *adapter;
1675 struct net_device *netdev;
1676 u32 reset_state;
John Allenc26eba02017-10-26 16:23:25 -05001677 int rc = 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001678
1679 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1680 netdev = adapter->netdev;
1681
1682 mutex_lock(&adapter->reset_lock);
1683 adapter->resetting = true;
1684 reset_state = adapter->state;
1685
1686 rwi = get_next_rwi(adapter);
1687 while (rwi) {
1688 rc = do_reset(adapter, rwi, reset_state);
1689 kfree(rwi);
John Allen2a1bf512017-10-26 16:24:15 -05001690 if (rc && rc != IBMVNIC_INIT_FAILED)
Nathan Fontenoted651a12017-05-03 14:04:38 -04001691 break;
1692
1693 rwi = get_next_rwi(adapter);
1694 }
1695
John Allenc26eba02017-10-26 16:23:25 -05001696 if (adapter->wait_for_reset) {
1697 adapter->wait_for_reset = false;
1698 adapter->reset_done_rc = rc;
1699 complete(&adapter->reset_done);
1700 }
1701
Nathan Fontenoted651a12017-05-03 14:04:38 -04001702 if (rc) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001703 netdev_dbg(adapter->netdev, "Reset failed\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001704 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001705 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001706 return;
1707 }
1708
1709 adapter->resetting = false;
1710 mutex_unlock(&adapter->reset_lock);
1711}
1712
1713static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1714 enum ibmvnic_reset_reason reason)
1715{
1716 struct ibmvnic_rwi *rwi, *tmp;
1717 struct net_device *netdev = adapter->netdev;
1718 struct list_head *entry;
1719
1720 if (adapter->state == VNIC_REMOVING ||
1721 adapter->state == VNIC_REMOVED) {
1722 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1723 return;
1724 }
1725
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001726 if (adapter->state == VNIC_PROBING) {
1727 netdev_warn(netdev, "Adapter reset during probe\n");
1728 adapter->init_done_rc = EAGAIN;
1729 return;
1730 }
1731
Nathan Fontenoted651a12017-05-03 14:04:38 -04001732 mutex_lock(&adapter->rwi_lock);
1733
1734 list_for_each(entry, &adapter->rwi_list) {
1735 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1736 if (tmp->reset_reason == reason) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001737 netdev_dbg(netdev, "Skipping matching reset\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04001738 mutex_unlock(&adapter->rwi_lock);
1739 return;
1740 }
1741 }
1742
1743 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1744 if (!rwi) {
1745 mutex_unlock(&adapter->rwi_lock);
1746 ibmvnic_close(netdev);
1747 return;
1748 }
1749
1750 rwi->reset_reason = reason;
1751 list_add_tail(&rwi->list, &adapter->rwi_list);
1752 mutex_unlock(&adapter->rwi_lock);
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05001753
1754 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001755 schedule_work(&adapter->ibmvnic_reset);
1756}
1757
Thomas Falcon032c5e82015-12-21 11:26:06 -06001758static void ibmvnic_tx_timeout(struct net_device *dev)
1759{
1760 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001761
Nathan Fontenoted651a12017-05-03 14:04:38 -04001762 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001763}
1764
1765static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1766 struct ibmvnic_rx_buff *rx_buff)
1767{
1768 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1769
1770 rx_buff->skb = NULL;
1771
1772 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1773 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1774
1775 atomic_dec(&pool->available);
1776}
1777
1778static int ibmvnic_poll(struct napi_struct *napi, int budget)
1779{
1780 struct net_device *netdev = napi->dev;
1781 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1782 int scrq_num = (int)(napi - adapter->napi);
1783 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001784
Thomas Falcon032c5e82015-12-21 11:26:06 -06001785restart_poll:
1786 while (frames_processed < budget) {
1787 struct sk_buff *skb;
1788 struct ibmvnic_rx_buff *rx_buff;
1789 union sub_crq *next;
1790 u32 length;
1791 u16 offset;
1792 u8 flags = 0;
1793
Thomas Falcon21ecba62017-06-14 23:50:09 -05001794 if (unlikely(adapter->resetting)) {
1795 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1796 napi_complete_done(napi, frames_processed);
1797 return frames_processed;
1798 }
1799
Thomas Falcon032c5e82015-12-21 11:26:06 -06001800 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1801 break;
1802 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1803 rx_buff =
1804 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1805 rx_comp.correlator);
1806 /* do error checking */
1807 if (next->rx_comp.rc) {
John Allene1cea2e2017-08-07 15:42:30 -05001808 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1809 be16_to_cpu(next->rx_comp.rc));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001810 /* free the entry */
1811 next->rx_comp.first = 0;
1812 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001813 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001814 }
1815
1816 length = be32_to_cpu(next->rx_comp.len);
1817 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1818 flags = next->rx_comp.flags;
1819 skb = rx_buff->skb;
1820 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1821 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001822
1823 /* VLAN Header has been stripped by the system firmware and
1824 * needs to be inserted by the driver
1825 */
1826 if (adapter->rx_vlan_header_insertion &&
1827 (flags & IBMVNIC_VLAN_STRIPPED))
1828 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1829 ntohs(next->rx_comp.vlan_tci));
1830
Thomas Falcon032c5e82015-12-21 11:26:06 -06001831 /* free the entry */
1832 next->rx_comp.first = 0;
1833 remove_buff_from_pool(adapter, rx_buff);
1834
1835 skb_put(skb, length);
1836 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001837 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001838
1839 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1840 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1841 skb->ip_summed = CHECKSUM_UNNECESSARY;
1842 }
1843
1844 length = skb->len;
1845 napi_gro_receive(napi, skb); /* send it up */
1846 netdev->stats.rx_packets++;
1847 netdev->stats.rx_bytes += length;
John Allen3d52b592017-08-02 16:44:14 -05001848 adapter->rx_stats_buffers[scrq_num].packets++;
1849 adapter->rx_stats_buffers[scrq_num].bytes += length;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001850 frames_processed++;
1851 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001852
1853 if (adapter->state != VNIC_CLOSING)
1854 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001855
1856 if (frames_processed < budget) {
1857 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001858 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001859 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1860 napi_reschedule(napi)) {
1861 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1862 goto restart_poll;
1863 }
1864 }
1865 return frames_processed;
1866}
1867
1868#ifdef CONFIG_NET_POLL_CONTROLLER
1869static void ibmvnic_netpoll_controller(struct net_device *dev)
1870{
1871 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1872 int i;
1873
1874 replenish_pools(netdev_priv(dev));
1875 for (i = 0; i < adapter->req_rx_queues; i++)
1876 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1877 adapter->rx_scrq[i]);
1878}
1879#endif
1880
John Allenc26eba02017-10-26 16:23:25 -05001881static int wait_for_reset(struct ibmvnic_adapter *adapter)
1882{
1883 adapter->fallback.mtu = adapter->req_mtu;
1884 adapter->fallback.rx_queues = adapter->req_rx_queues;
1885 adapter->fallback.tx_queues = adapter->req_tx_queues;
1886 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1887 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1888
1889 init_completion(&adapter->reset_done);
1890 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1891 adapter->wait_for_reset = true;
1892 wait_for_completion(&adapter->reset_done);
1893
1894 if (adapter->reset_done_rc) {
1895 adapter->desired.mtu = adapter->fallback.mtu;
1896 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1897 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1898 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1899 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1900
1901 init_completion(&adapter->reset_done);
1902 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1903 wait_for_completion(&adapter->reset_done);
1904 }
1905 adapter->wait_for_reset = false;
1906
1907 return adapter->reset_done_rc;
1908}
1909
John Allen3a807b72017-06-06 16:55:52 -05001910static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1911{
John Allenc26eba02017-10-26 16:23:25 -05001912 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1913
1914 adapter->desired.mtu = new_mtu + ETH_HLEN;
1915
1916 return wait_for_reset(adapter);
John Allen3a807b72017-06-06 16:55:52 -05001917}
1918
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919static const struct net_device_ops ibmvnic_netdev_ops = {
1920 .ndo_open = ibmvnic_open,
1921 .ndo_stop = ibmvnic_close,
1922 .ndo_start_xmit = ibmvnic_xmit,
1923 .ndo_set_rx_mode = ibmvnic_set_multi,
1924 .ndo_set_mac_address = ibmvnic_set_mac,
1925 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001926 .ndo_tx_timeout = ibmvnic_tx_timeout,
1927#ifdef CONFIG_NET_POLL_CONTROLLER
1928 .ndo_poll_controller = ibmvnic_netpoll_controller,
1929#endif
John Allen3a807b72017-06-06 16:55:52 -05001930 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001931};
1932
1933/* ethtool functions */
1934
Philippe Reynes8a433792017-01-07 22:37:29 +01001935static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1936 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001937{
Philippe Reynes8a433792017-01-07 22:37:29 +01001938 u32 supported, advertising;
1939
1940 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001941 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001942 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001943 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001944 cmd->base.speed = SPEED_1000;
1945 cmd->base.duplex = DUPLEX_FULL;
1946 cmd->base.port = PORT_FIBRE;
1947 cmd->base.phy_address = 0;
1948 cmd->base.autoneg = AUTONEG_ENABLE;
1949
1950 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1951 supported);
1952 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1953 advertising);
1954
Thomas Falcon032c5e82015-12-21 11:26:06 -06001955 return 0;
1956}
1957
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001958static void ibmvnic_get_drvinfo(struct net_device *netdev,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001959 struct ethtool_drvinfo *info)
1960{
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001961 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1962
Thomas Falcon032c5e82015-12-21 11:26:06 -06001963 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1964 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02001965 strlcpy(info->fw_version, adapter->fw_version,
1966 sizeof(info->fw_version));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001967}
1968
1969static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1970{
1971 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1972
1973 return adapter->msg_enable;
1974}
1975
1976static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1977{
1978 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1979
1980 adapter->msg_enable = data;
1981}
1982
1983static u32 ibmvnic_get_link(struct net_device *netdev)
1984{
1985 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1986
1987 /* Don't need to send a query because we request a logical link up at
1988 * init and then we wait for link state indications
1989 */
1990 return adapter->logical_link_state;
1991}
1992
1993static void ibmvnic_get_ringparam(struct net_device *netdev,
1994 struct ethtool_ringparam *ring)
1995{
John Allenbc131b32017-08-02 16:46:30 -05001996 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1997
1998 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1999 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002000 ring->rx_mini_max_pending = 0;
2001 ring->rx_jumbo_max_pending = 0;
John Allenbc131b32017-08-02 16:46:30 -05002002 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2003 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002004 ring->rx_mini_pending = 0;
2005 ring->rx_jumbo_pending = 0;
2006}
2007
John Allenc26eba02017-10-26 16:23:25 -05002008static int ibmvnic_set_ringparam(struct net_device *netdev,
2009 struct ethtool_ringparam *ring)
2010{
2011 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2012
2013 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2014 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2015 netdev_err(netdev, "Invalid request.\n");
2016 netdev_err(netdev, "Max tx buffers = %llu\n",
2017 adapter->max_rx_add_entries_per_subcrq);
2018 netdev_err(netdev, "Max rx buffers = %llu\n",
2019 adapter->max_tx_entries_per_subcrq);
2020 return -EINVAL;
2021 }
2022
2023 adapter->desired.rx_entries = ring->rx_pending;
2024 adapter->desired.tx_entries = ring->tx_pending;
2025
2026 return wait_for_reset(adapter);
2027}
2028
John Allenc2dbeb62017-08-02 16:47:17 -05002029static void ibmvnic_get_channels(struct net_device *netdev,
2030 struct ethtool_channels *channels)
2031{
2032 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2033
2034 channels->max_rx = adapter->max_rx_queues;
2035 channels->max_tx = adapter->max_tx_queues;
2036 channels->max_other = 0;
2037 channels->max_combined = 0;
2038 channels->rx_count = adapter->req_rx_queues;
2039 channels->tx_count = adapter->req_tx_queues;
2040 channels->other_count = 0;
2041 channels->combined_count = 0;
2042}
2043
John Allenc26eba02017-10-26 16:23:25 -05002044static int ibmvnic_set_channels(struct net_device *netdev,
2045 struct ethtool_channels *channels)
2046{
2047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2048
2049 adapter->desired.rx_queues = channels->rx_count;
2050 adapter->desired.tx_queues = channels->tx_count;
2051
2052 return wait_for_reset(adapter);
2053}
2054
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2056{
John Allen3d52b592017-08-02 16:44:14 -05002057 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002058 int i;
2059
2060 if (stringset != ETH_SS_STATS)
2061 return;
2062
2063 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2064 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
John Allen3d52b592017-08-02 16:44:14 -05002065
2066 for (i = 0; i < adapter->req_tx_queues; i++) {
2067 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2068 data += ETH_GSTRING_LEN;
2069
2070 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2071 data += ETH_GSTRING_LEN;
2072
2073 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2074 data += ETH_GSTRING_LEN;
2075 }
2076
2077 for (i = 0; i < adapter->req_rx_queues; i++) {
2078 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2079 data += ETH_GSTRING_LEN;
2080
2081 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2082 data += ETH_GSTRING_LEN;
2083
2084 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2085 data += ETH_GSTRING_LEN;
2086 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002087}
2088
2089static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2090{
John Allen3d52b592017-08-02 16:44:14 -05002091 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2092
Thomas Falcon032c5e82015-12-21 11:26:06 -06002093 switch (sset) {
2094 case ETH_SS_STATS:
John Allen3d52b592017-08-02 16:44:14 -05002095 return ARRAY_SIZE(ibmvnic_stats) +
2096 adapter->req_tx_queues * NUM_TX_STATS +
2097 adapter->req_rx_queues * NUM_RX_STATS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002098 default:
2099 return -EOPNOTSUPP;
2100 }
2101}
2102
2103static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2104 struct ethtool_stats *stats, u64 *data)
2105{
2106 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2107 union ibmvnic_crq crq;
John Allen3d52b592017-08-02 16:44:14 -05002108 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002109
2110 memset(&crq, 0, sizeof(crq));
2111 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2112 crq.request_statistics.cmd = REQUEST_STATISTICS;
2113 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2114 crq.request_statistics.len =
2115 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002116
2117 /* Wait for data to be written */
2118 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05002119 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002120 wait_for_completion(&adapter->stats_done);
2121
2122 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
John Allen52da5c12017-08-02 16:45:28 -05002123 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2124 ibmvnic_stats[i].offset));
John Allen3d52b592017-08-02 16:44:14 -05002125
2126 for (j = 0; j < adapter->req_tx_queues; j++) {
2127 data[i] = adapter->tx_stats_buffers[j].packets;
2128 i++;
2129 data[i] = adapter->tx_stats_buffers[j].bytes;
2130 i++;
2131 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2132 i++;
2133 }
2134
2135 for (j = 0; j < adapter->req_rx_queues; j++) {
2136 data[i] = adapter->rx_stats_buffers[j].packets;
2137 i++;
2138 data[i] = adapter->rx_stats_buffers[j].bytes;
2139 i++;
2140 data[i] = adapter->rx_stats_buffers[j].interrupts;
2141 i++;
2142 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143}
2144
2145static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002146 .get_drvinfo = ibmvnic_get_drvinfo,
2147 .get_msglevel = ibmvnic_get_msglevel,
2148 .set_msglevel = ibmvnic_set_msglevel,
2149 .get_link = ibmvnic_get_link,
2150 .get_ringparam = ibmvnic_get_ringparam,
John Allenc26eba02017-10-26 16:23:25 -05002151 .set_ringparam = ibmvnic_set_ringparam,
John Allenc2dbeb62017-08-02 16:47:17 -05002152 .get_channels = ibmvnic_get_channels,
John Allenc26eba02017-10-26 16:23:25 -05002153 .set_channels = ibmvnic_set_channels,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002154 .get_strings = ibmvnic_get_strings,
2155 .get_sset_count = ibmvnic_get_sset_count,
2156 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01002157 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06002158};
2159
2160/* Routines for managing CRQs/sCRQs */
2161
Nathan Fontenot57a49432017-05-26 10:31:12 -04002162static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2163 struct ibmvnic_sub_crq_queue *scrq)
2164{
2165 int rc;
2166
2167 if (scrq->irq) {
2168 free_irq(scrq->irq, scrq);
2169 irq_dispose_mapping(scrq->irq);
2170 scrq->irq = 0;
2171 }
2172
Thomas Falconc8b2ad02017-06-14 23:50:07 -05002173 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002174 scrq->cur = 0;
2175
2176 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2177 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2178 return rc;
2179}
2180
2181static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2182{
2183 int i, rc;
2184
2185 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002186 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002187 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2188 if (rc)
2189 return rc;
2190 }
2191
2192 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002193 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
Nathan Fontenot57a49432017-05-26 10:31:12 -04002194 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2195 if (rc)
2196 return rc;
2197 }
2198
Nathan Fontenot57a49432017-05-26 10:31:12 -04002199 return rc;
2200}
2201
Thomas Falcon032c5e82015-12-21 11:26:06 -06002202static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2203 struct ibmvnic_sub_crq_queue *scrq)
2204{
2205 struct device *dev = &adapter->vdev->dev;
2206 long rc;
2207
2208 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2209
2210 /* Close the sub-crqs */
2211 do {
2212 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2213 adapter->vdev->unit_address,
2214 scrq->crq_num);
2215 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2216
Thomas Falconffa73852017-04-19 13:44:29 -04002217 if (rc) {
2218 netdev_err(adapter->netdev,
2219 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2220 scrq->crq_num, rc);
2221 }
2222
Thomas Falcon032c5e82015-12-21 11:26:06 -06002223 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2224 DMA_BIDIRECTIONAL);
2225 free_pages((unsigned long)scrq->msgs, 2);
2226 kfree(scrq);
2227}
2228
2229static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2230 *adapter)
2231{
2232 struct device *dev = &adapter->vdev->dev;
2233 struct ibmvnic_sub_crq_queue *scrq;
2234 int rc;
2235
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002236 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002237 if (!scrq)
2238 return NULL;
2239
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04002240 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002241 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002242 if (!scrq->msgs) {
2243 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2244 goto zero_page_failed;
2245 }
2246
2247 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2248 DMA_BIDIRECTIONAL);
2249 if (dma_mapping_error(dev, scrq->msg_token)) {
2250 dev_warn(dev, "Couldn't map crq queue messages page\n");
2251 goto map_failed;
2252 }
2253
2254 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2255 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2256
2257 if (rc == H_RESOURCE)
2258 rc = ibmvnic_reset_crq(adapter);
2259
2260 if (rc == H_CLOSED) {
2261 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2262 } else if (rc) {
2263 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2264 goto reg_failed;
2265 }
2266
Thomas Falcon032c5e82015-12-21 11:26:06 -06002267 scrq->adapter = adapter;
2268 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002269 spin_lock_init(&scrq->lock);
2270
2271 netdev_dbg(adapter->netdev,
2272 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2273 scrq->crq_num, scrq->hw_irq, scrq->irq);
2274
2275 return scrq;
2276
Thomas Falcon032c5e82015-12-21 11:26:06 -06002277reg_failed:
2278 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2279 DMA_BIDIRECTIONAL);
2280map_failed:
2281 free_pages((unsigned long)scrq->msgs, 2);
2282zero_page_failed:
2283 kfree(scrq);
2284
2285 return NULL;
2286}
2287
2288static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2289{
2290 int i;
2291
2292 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002293 for (i = 0; i < adapter->req_tx_queues; i++) {
2294 if (!adapter->tx_scrq[i])
2295 continue;
2296
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002297 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2298 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002299 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002300 free_irq(adapter->tx_scrq[i]->irq,
2301 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002302 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002303 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002304 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002305
2306 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2307 }
2308
Nathan Fontenot9501df32017-03-15 23:38:07 -04002309 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002310 adapter->tx_scrq = NULL;
2311 }
2312
2313 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04002314 for (i = 0; i < adapter->req_rx_queues; i++) {
2315 if (!adapter->rx_scrq[i])
2316 continue;
2317
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002318 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2319 i);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002320 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002321 free_irq(adapter->rx_scrq[i]->irq,
2322 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05002323 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002324 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002325 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002326
2327 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2328 }
2329
Nathan Fontenot9501df32017-03-15 23:38:07 -04002330 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002331 adapter->rx_scrq = NULL;
2332 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002333}
2334
2335static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2336 struct ibmvnic_sub_crq_queue *scrq)
2337{
2338 struct device *dev = &adapter->vdev->dev;
2339 unsigned long rc;
2340
2341 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2342 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2343 if (rc)
2344 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2345 scrq->hw_irq, rc);
2346 return rc;
2347}
2348
2349static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2350 struct ibmvnic_sub_crq_queue *scrq)
2351{
2352 struct device *dev = &adapter->vdev->dev;
2353 unsigned long rc;
2354
2355 if (scrq->hw_irq > 0x100000000ULL) {
2356 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2357 return 1;
2358 }
2359
2360 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2361 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2362 if (rc)
2363 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2364 scrq->hw_irq, rc);
2365 return rc;
2366}
2367
2368static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2369 struct ibmvnic_sub_crq_queue *scrq)
2370{
2371 struct device *dev = &adapter->vdev->dev;
2372 struct ibmvnic_tx_buff *txbuff;
2373 union sub_crq *next;
2374 int index;
2375 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05002376 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002377
2378restart_loop:
2379 while (pending_scrq(adapter, scrq)) {
2380 unsigned int pool = scrq->pool_index;
2381
2382 next = ibmvnic_next_scrq(adapter, scrq);
2383 for (i = 0; i < next->tx_comp.num_comps; i++) {
2384 if (next->tx_comp.rcs[i]) {
2385 dev_err(dev, "tx error %x\n",
2386 next->tx_comp.rcs[i]);
2387 continue;
2388 }
2389 index = be32_to_cpu(next->tx_comp.correlators[i]);
2390 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2391
2392 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2393 if (!txbuff->data_dma[j])
2394 continue;
2395
2396 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002397 }
Thomas Falconad7775d2016-04-01 17:20:34 -05002398 /* if sub_crq was sent indirectly */
2399 first = txbuff->indir_arr[0].generic.first;
2400 if (first == IBMVNIC_CRQ_CMD) {
2401 dma_unmap_single(dev, txbuff->indir_dma,
2402 sizeof(txbuff->indir_arr),
2403 DMA_TO_DEVICE);
2404 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002405
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002406 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002407 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002408 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06002409 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002410
2411 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2412 producer_index] = index;
2413 adapter->tx_pool[pool].producer_index =
2414 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002415 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 }
2417 /* remove tx_comp scrq*/
2418 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002419
2420 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2421 (adapter->req_tx_entries_per_subcrq / 2) &&
2422 __netif_subqueue_stopped(adapter->netdev,
2423 scrq->pool_index)) {
2424 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2425 netdev_info(adapter->netdev, "Started queue %d\n",
2426 scrq->pool_index);
2427 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002428 }
2429
2430 enable_scrq_irq(adapter, scrq);
2431
2432 if (pending_scrq(adapter, scrq)) {
2433 disable_scrq_irq(adapter, scrq);
2434 goto restart_loop;
2435 }
2436
2437 return 0;
2438}
2439
2440static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2441{
2442 struct ibmvnic_sub_crq_queue *scrq = instance;
2443 struct ibmvnic_adapter *adapter = scrq->adapter;
2444
2445 disable_scrq_irq(adapter, scrq);
2446 ibmvnic_complete_tx(adapter, scrq);
2447
2448 return IRQ_HANDLED;
2449}
2450
2451static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2452{
2453 struct ibmvnic_sub_crq_queue *scrq = instance;
2454 struct ibmvnic_adapter *adapter = scrq->adapter;
2455
Nathan Fontenot09fb35e2018-01-10 10:40:09 -06002456 /* When booting a kdump kernel we can hit pending interrupts
2457 * prior to completing driver initialization.
2458 */
2459 if (unlikely(adapter->state != VNIC_OPEN))
2460 return IRQ_NONE;
2461
John Allen3d52b592017-08-02 16:44:14 -05002462 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2463
Thomas Falcon032c5e82015-12-21 11:26:06 -06002464 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2465 disable_scrq_irq(adapter, scrq);
2466 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2467 }
2468
2469 return IRQ_HANDLED;
2470}
2471
Thomas Falconea22d512016-07-06 15:35:17 -05002472static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2473{
2474 struct device *dev = &adapter->vdev->dev;
2475 struct ibmvnic_sub_crq_queue *scrq;
2476 int i = 0, j = 0;
2477 int rc = 0;
2478
2479 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002480 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2481 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002482 scrq = adapter->tx_scrq[i];
2483 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2484
Michael Ellerman99c17902016-09-10 19:59:05 +10002485 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002486 rc = -EINVAL;
2487 dev_err(dev, "Error mapping irq\n");
2488 goto req_tx_irq_failed;
2489 }
2490
2491 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2492 0, "ibmvnic_tx", scrq);
2493
2494 if (rc) {
2495 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2496 scrq->irq, rc);
2497 irq_dispose_mapping(scrq->irq);
2498 goto req_rx_irq_failed;
2499 }
2500 }
2501
2502 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002503 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2504 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002505 scrq = adapter->rx_scrq[i];
2506 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002507 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002508 rc = -EINVAL;
2509 dev_err(dev, "Error mapping irq\n");
2510 goto req_rx_irq_failed;
2511 }
2512 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2513 0, "ibmvnic_rx", scrq);
2514 if (rc) {
2515 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2516 scrq->irq, rc);
2517 irq_dispose_mapping(scrq->irq);
2518 goto req_rx_irq_failed;
2519 }
2520 }
2521 return rc;
2522
2523req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002524 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002525 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2526 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002527 }
Thomas Falconea22d512016-07-06 15:35:17 -05002528 i = adapter->req_tx_queues;
2529req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002530 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002531 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2532 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002533 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002534 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002535 return rc;
2536}
2537
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002538static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002539{
2540 struct device *dev = &adapter->vdev->dev;
2541 struct ibmvnic_sub_crq_queue **allqueues;
2542 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002543 int total_queues;
2544 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002545 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546
Thomas Falcon032c5e82015-12-21 11:26:06 -06002547 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2548
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002549 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002550 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002551 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002552
2553 for (i = 0; i < total_queues; i++) {
2554 allqueues[i] = init_sub_crq_queue(adapter);
2555 if (!allqueues[i]) {
2556 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2557 break;
2558 }
2559 registered_queues++;
2560 }
2561
2562 /* Make sure we were able to register the minimum number of queues */
2563 if (registered_queues <
2564 adapter->min_tx_queues + adapter->min_rx_queues) {
2565 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2566 goto tx_failed;
2567 }
2568
2569 /* Distribute the failed allocated queues*/
2570 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2571 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2572 switch (i % 3) {
2573 case 0:
2574 if (adapter->req_rx_queues > adapter->min_rx_queues)
2575 adapter->req_rx_queues--;
2576 else
2577 more++;
2578 break;
2579 case 1:
2580 if (adapter->req_tx_queues > adapter->min_tx_queues)
2581 adapter->req_tx_queues--;
2582 else
2583 more++;
2584 break;
2585 }
2586 }
2587
2588 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002589 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002590 if (!adapter->tx_scrq)
2591 goto tx_failed;
2592
2593 for (i = 0; i < adapter->req_tx_queues; i++) {
2594 adapter->tx_scrq[i] = allqueues[i];
2595 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002596 }
2597
2598 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002599 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002600 if (!adapter->rx_scrq)
2601 goto rx_failed;
2602
2603 for (i = 0; i < adapter->req_rx_queues; i++) {
2604 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2605 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002606 }
2607
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002608 kfree(allqueues);
2609 return 0;
2610
2611rx_failed:
2612 kfree(adapter->tx_scrq);
2613 adapter->tx_scrq = NULL;
2614tx_failed:
2615 for (i = 0; i < registered_queues; i++)
2616 release_sub_crq_queue(adapter, allqueues[i]);
2617 kfree(allqueues);
2618 return -1;
2619}
2620
2621static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2622{
2623 struct device *dev = &adapter->vdev->dev;
2624 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002625 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002626
2627 if (!retry) {
2628 /* Sub-CRQ entries are 32 byte long */
2629 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2630
2631 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2632 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2633 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2634 return;
2635 }
2636
John Allenc26eba02017-10-26 16:23:25 -05002637 if (adapter->desired.mtu)
2638 adapter->req_mtu = adapter->desired.mtu;
2639 else
2640 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002641
John Allenc26eba02017-10-26 16:23:25 -05002642 if (!adapter->desired.tx_entries)
2643 adapter->desired.tx_entries =
2644 adapter->max_tx_entries_per_subcrq;
2645 if (!adapter->desired.rx_entries)
2646 adapter->desired.rx_entries =
2647 adapter->max_rx_add_entries_per_subcrq;
2648
2649 max_entries = IBMVNIC_MAX_LTB_SIZE /
2650 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2651
2652 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2653 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2654 adapter->desired.tx_entries = max_entries;
2655 }
2656
2657 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2658 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2659 adapter->desired.rx_entries = max_entries;
2660 }
2661
2662 if (adapter->desired.tx_entries)
2663 adapter->req_tx_entries_per_subcrq =
2664 adapter->desired.tx_entries;
2665 else
2666 adapter->req_tx_entries_per_subcrq =
2667 adapter->max_tx_entries_per_subcrq;
2668
2669 if (adapter->desired.rx_entries)
2670 adapter->req_rx_add_entries_per_subcrq =
2671 adapter->desired.rx_entries;
2672 else
2673 adapter->req_rx_add_entries_per_subcrq =
2674 adapter->max_rx_add_entries_per_subcrq;
2675
2676 if (adapter->desired.tx_queues)
2677 adapter->req_tx_queues =
2678 adapter->desired.tx_queues;
2679 else
2680 adapter->req_tx_queues =
2681 adapter->opt_tx_comp_sub_queues;
2682
2683 if (adapter->desired.rx_queues)
2684 adapter->req_rx_queues =
2685 adapter->desired.rx_queues;
2686 else
2687 adapter->req_rx_queues =
2688 adapter->opt_rx_comp_queues;
2689
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002690 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002691 }
2692
Thomas Falcon032c5e82015-12-21 11:26:06 -06002693 memset(&crq, 0, sizeof(crq));
2694 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2695 crq.request_capability.cmd = REQUEST_CAPABILITY;
2696
2697 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002698 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002699 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700 ibmvnic_send_crq(adapter, &crq);
2701
2702 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002703 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002704 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002705 ibmvnic_send_crq(adapter, &crq);
2706
2707 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002708 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002709 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002710 ibmvnic_send_crq(adapter, &crq);
2711
2712 crq.request_capability.capability =
2713 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2714 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002715 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002716 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002717 ibmvnic_send_crq(adapter, &crq);
2718
2719 crq.request_capability.capability =
2720 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2721 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002722 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002723 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 ibmvnic_send_crq(adapter, &crq);
2725
2726 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002727 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002728 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002729 ibmvnic_send_crq(adapter, &crq);
2730
2731 if (adapter->netdev->flags & IFF_PROMISC) {
2732 if (adapter->promisc_supported) {
2733 crq.request_capability.capability =
2734 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002735 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002736 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002737 ibmvnic_send_crq(adapter, &crq);
2738 }
2739 } else {
2740 crq.request_capability.capability =
2741 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002742 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002743 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744 ibmvnic_send_crq(adapter, &crq);
2745 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002746}
2747
2748static int pending_scrq(struct ibmvnic_adapter *adapter,
2749 struct ibmvnic_sub_crq_queue *scrq)
2750{
2751 union sub_crq *entry = &scrq->msgs[scrq->cur];
2752
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002753 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002754 return 1;
2755 else
2756 return 0;
2757}
2758
2759static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2760 struct ibmvnic_sub_crq_queue *scrq)
2761{
2762 union sub_crq *entry;
2763 unsigned long flags;
2764
2765 spin_lock_irqsave(&scrq->lock, flags);
2766 entry = &scrq->msgs[scrq->cur];
2767 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2768 if (++scrq->cur == scrq->size)
2769 scrq->cur = 0;
2770 } else {
2771 entry = NULL;
2772 }
2773 spin_unlock_irqrestore(&scrq->lock, flags);
2774
2775 return entry;
2776}
2777
2778static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2779{
2780 struct ibmvnic_crq_queue *queue = &adapter->crq;
2781 union ibmvnic_crq *crq;
2782
2783 crq = &queue->msgs[queue->cur];
2784 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2785 if (++queue->cur == queue->size)
2786 queue->cur = 0;
2787 } else {
2788 crq = NULL;
2789 }
2790
2791 return crq;
2792}
2793
2794static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2795 union sub_crq *sub_crq)
2796{
2797 unsigned int ua = adapter->vdev->unit_address;
2798 struct device *dev = &adapter->vdev->dev;
2799 u64 *u64_crq = (u64 *)sub_crq;
2800 int rc;
2801
2802 netdev_dbg(adapter->netdev,
2803 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2804 (unsigned long int)cpu_to_be64(remote_handle),
2805 (unsigned long int)cpu_to_be64(u64_crq[0]),
2806 (unsigned long int)cpu_to_be64(u64_crq[1]),
2807 (unsigned long int)cpu_to_be64(u64_crq[2]),
2808 (unsigned long int)cpu_to_be64(u64_crq[3]));
2809
2810 /* Make sure the hypervisor sees the complete request */
2811 mb();
2812
2813 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2814 cpu_to_be64(remote_handle),
2815 cpu_to_be64(u64_crq[0]),
2816 cpu_to_be64(u64_crq[1]),
2817 cpu_to_be64(u64_crq[2]),
2818 cpu_to_be64(u64_crq[3]));
2819
2820 if (rc) {
2821 if (rc == H_CLOSED)
2822 dev_warn(dev, "CRQ Queue closed\n");
2823 dev_err(dev, "Send error (rc=%d)\n", rc);
2824 }
2825
2826 return rc;
2827}
2828
Thomas Falconad7775d2016-04-01 17:20:34 -05002829static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2830 u64 remote_handle, u64 ioba, u64 num_entries)
2831{
2832 unsigned int ua = adapter->vdev->unit_address;
2833 struct device *dev = &adapter->vdev->dev;
2834 int rc;
2835
2836 /* Make sure the hypervisor sees the complete request */
2837 mb();
2838 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2839 cpu_to_be64(remote_handle),
2840 ioba, num_entries);
2841
2842 if (rc) {
2843 if (rc == H_CLOSED)
2844 dev_warn(dev, "CRQ Queue closed\n");
2845 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2846 }
2847
2848 return rc;
2849}
2850
Thomas Falcon032c5e82015-12-21 11:26:06 -06002851static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2852 union ibmvnic_crq *crq)
2853{
2854 unsigned int ua = adapter->vdev->unit_address;
2855 struct device *dev = &adapter->vdev->dev;
2856 u64 *u64_crq = (u64 *)crq;
2857 int rc;
2858
2859 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2860 (unsigned long int)cpu_to_be64(u64_crq[0]),
2861 (unsigned long int)cpu_to_be64(u64_crq[1]));
2862
2863 /* Make sure the hypervisor sees the complete request */
2864 mb();
2865
2866 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2867 cpu_to_be64(u64_crq[0]),
2868 cpu_to_be64(u64_crq[1]));
2869
2870 if (rc) {
2871 if (rc == H_CLOSED)
2872 dev_warn(dev, "CRQ Queue closed\n");
2873 dev_warn(dev, "Send error (rc=%d)\n", rc);
2874 }
2875
2876 return rc;
2877}
2878
2879static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2880{
2881 union ibmvnic_crq crq;
2882
2883 memset(&crq, 0, sizeof(crq));
2884 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2885 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2886 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2887
2888 return ibmvnic_send_crq(adapter, &crq);
2889}
2890
Thomas Falcon032c5e82015-12-21 11:26:06 -06002891static int send_version_xchg(struct ibmvnic_adapter *adapter)
2892{
2893 union ibmvnic_crq crq;
2894
2895 memset(&crq, 0, sizeof(crq));
2896 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2897 crq.version_exchange.cmd = VERSION_EXCHANGE;
2898 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2899
2900 return ibmvnic_send_crq(adapter, &crq);
2901}
2902
Nathan Fontenot37798d02017-11-08 11:23:56 -06002903struct vnic_login_client_data {
2904 u8 type;
2905 __be16 len;
2906 char name;
2907} __packed;
2908
2909static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2910{
2911 int len;
2912
2913 /* Calculate the amount of buffer space needed for the
2914 * vnic client data in the login buffer. There are four entries,
2915 * OS name, LPAR name, device name, and a null last entry.
2916 */
2917 len = 4 * sizeof(struct vnic_login_client_data);
2918 len += 6; /* "Linux" plus NULL */
2919 len += strlen(utsname()->nodename) + 1;
2920 len += strlen(adapter->netdev->name) + 1;
2921
2922 return len;
2923}
2924
2925static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2926 struct vnic_login_client_data *vlcd)
2927{
2928 const char *os_name = "Linux";
2929 int len;
2930
2931 /* Type 1 - LPAR OS */
2932 vlcd->type = 1;
2933 len = strlen(os_name) + 1;
2934 vlcd->len = cpu_to_be16(len);
2935 strncpy(&vlcd->name, os_name, len);
2936 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2937
2938 /* Type 2 - LPAR name */
2939 vlcd->type = 2;
2940 len = strlen(utsname()->nodename) + 1;
2941 vlcd->len = cpu_to_be16(len);
2942 strncpy(&vlcd->name, utsname()->nodename, len);
2943 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2944
2945 /* Type 3 - device name */
2946 vlcd->type = 3;
2947 len = strlen(adapter->netdev->name) + 1;
2948 vlcd->len = cpu_to_be16(len);
2949 strncpy(&vlcd->name, adapter->netdev->name, len);
2950}
2951
Thomas Falcon032c5e82015-12-21 11:26:06 -06002952static void send_login(struct ibmvnic_adapter *adapter)
2953{
2954 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2955 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002956 struct device *dev = &adapter->vdev->dev;
2957 dma_addr_t rsp_buffer_token;
2958 dma_addr_t buffer_token;
2959 size_t rsp_buffer_size;
2960 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002961 size_t buffer_size;
2962 __be64 *tx_list_p;
2963 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06002964 int client_data_len;
2965 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002966 int i;
2967
Nathan Fontenot37798d02017-11-08 11:23:56 -06002968 client_data_len = vnic_client_data_len(adapter);
2969
Thomas Falcon032c5e82015-12-21 11:26:06 -06002970 buffer_size =
2971 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06002972 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
2973 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002974
Nathan Fontenot37798d02017-11-08 11:23:56 -06002975 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 if (!login_buffer)
2977 goto buf_alloc_failed;
2978
2979 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2980 DMA_TO_DEVICE);
2981 if (dma_mapping_error(dev, buffer_token)) {
2982 dev_err(dev, "Couldn't map login buffer\n");
2983 goto buf_map_failed;
2984 }
2985
John Allen498cd8e2016-04-06 11:49:55 -05002986 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2987 sizeof(u64) * adapter->req_tx_queues +
2988 sizeof(u64) * adapter->req_rx_queues +
2989 sizeof(u64) * adapter->req_rx_queues +
2990 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002991
2992 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2993 if (!login_rsp_buffer)
2994 goto buf_rsp_alloc_failed;
2995
2996 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2997 rsp_buffer_size, DMA_FROM_DEVICE);
2998 if (dma_mapping_error(dev, rsp_buffer_token)) {
2999 dev_err(dev, "Couldn't map login rsp buffer\n");
3000 goto buf_rsp_map_failed;
3001 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04003002
Thomas Falcon032c5e82015-12-21 11:26:06 -06003003 adapter->login_buf = login_buffer;
3004 adapter->login_buf_token = buffer_token;
3005 adapter->login_buf_sz = buffer_size;
3006 adapter->login_rsp_buf = login_rsp_buffer;
3007 adapter->login_rsp_buf_token = rsp_buffer_token;
3008 adapter->login_rsp_buf_sz = rsp_buffer_size;
3009
3010 login_buffer->len = cpu_to_be32(buffer_size);
3011 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3012 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3013 login_buffer->off_txcomp_subcrqs =
3014 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3015 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3016 login_buffer->off_rxcomp_subcrqs =
3017 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3018 sizeof(u64) * adapter->req_tx_queues);
3019 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3020 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3021
3022 tx_list_p = (__be64 *)((char *)login_buffer +
3023 sizeof(struct ibmvnic_login_buffer));
3024 rx_list_p = (__be64 *)((char *)login_buffer +
3025 sizeof(struct ibmvnic_login_buffer) +
3026 sizeof(u64) * adapter->req_tx_queues);
3027
3028 for (i = 0; i < adapter->req_tx_queues; i++) {
3029 if (adapter->tx_scrq[i]) {
3030 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3031 crq_num);
3032 }
3033 }
3034
3035 for (i = 0; i < adapter->req_rx_queues; i++) {
3036 if (adapter->rx_scrq[i]) {
3037 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3038 crq_num);
3039 }
3040 }
3041
Nathan Fontenot37798d02017-11-08 11:23:56 -06003042 /* Insert vNIC login client data */
3043 vlcd = (struct vnic_login_client_data *)
3044 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3045 login_buffer->client_data_offset =
3046 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3047 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3048
3049 vnic_add_client_data(adapter, vlcd);
3050
Thomas Falcon032c5e82015-12-21 11:26:06 -06003051 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3052 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3053 netdev_dbg(adapter->netdev, "%016lx\n",
3054 ((unsigned long int *)(adapter->login_buf))[i]);
3055 }
3056
3057 memset(&crq, 0, sizeof(crq));
3058 crq.login.first = IBMVNIC_CRQ_CMD;
3059 crq.login.cmd = LOGIN;
3060 crq.login.ioba = cpu_to_be32(buffer_token);
3061 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003062 ibmvnic_send_crq(adapter, &crq);
3063
3064 return;
3065
Thomas Falcon032c5e82015-12-21 11:26:06 -06003066buf_rsp_map_failed:
3067 kfree(login_rsp_buffer);
3068buf_rsp_alloc_failed:
3069 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3070buf_map_failed:
3071 kfree(login_buffer);
3072buf_alloc_failed:
3073 return;
3074}
3075
3076static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3077 u32 len, u8 map_id)
3078{
3079 union ibmvnic_crq crq;
3080
3081 memset(&crq, 0, sizeof(crq));
3082 crq.request_map.first = IBMVNIC_CRQ_CMD;
3083 crq.request_map.cmd = REQUEST_MAP;
3084 crq.request_map.map_id = map_id;
3085 crq.request_map.ioba = cpu_to_be32(addr);
3086 crq.request_map.len = cpu_to_be32(len);
3087 ibmvnic_send_crq(adapter, &crq);
3088}
3089
3090static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3091{
3092 union ibmvnic_crq crq;
3093
3094 memset(&crq, 0, sizeof(crq));
3095 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3096 crq.request_unmap.cmd = REQUEST_UNMAP;
3097 crq.request_unmap.map_id = map_id;
3098 ibmvnic_send_crq(adapter, &crq);
3099}
3100
3101static void send_map_query(struct ibmvnic_adapter *adapter)
3102{
3103 union ibmvnic_crq crq;
3104
3105 memset(&crq, 0, sizeof(crq));
3106 crq.query_map.first = IBMVNIC_CRQ_CMD;
3107 crq.query_map.cmd = QUERY_MAP;
3108 ibmvnic_send_crq(adapter, &crq);
3109}
3110
3111/* Send a series of CRQs requesting various capabilities of the VNIC server */
3112static void send_cap_queries(struct ibmvnic_adapter *adapter)
3113{
3114 union ibmvnic_crq crq;
3115
Thomas Falcon901e0402017-02-15 12:17:59 -06003116 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003117 memset(&crq, 0, sizeof(crq));
3118 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3119 crq.query_capability.cmd = QUERY_CAPABILITY;
3120
3121 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003122 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003123 ibmvnic_send_crq(adapter, &crq);
3124
3125 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003126 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127 ibmvnic_send_crq(adapter, &crq);
3128
3129 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003130 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003131 ibmvnic_send_crq(adapter, &crq);
3132
3133 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003134 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135 ibmvnic_send_crq(adapter, &crq);
3136
3137 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003138 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003139 ibmvnic_send_crq(adapter, &crq);
3140
3141 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003142 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003143 ibmvnic_send_crq(adapter, &crq);
3144
3145 crq.query_capability.capability =
3146 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003147 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003148 ibmvnic_send_crq(adapter, &crq);
3149
3150 crq.query_capability.capability =
3151 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003152 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003153 ibmvnic_send_crq(adapter, &crq);
3154
3155 crq.query_capability.capability =
3156 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003157 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003158 ibmvnic_send_crq(adapter, &crq);
3159
3160 crq.query_capability.capability =
3161 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003162 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003163 ibmvnic_send_crq(adapter, &crq);
3164
3165 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003166 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003167 ibmvnic_send_crq(adapter, &crq);
3168
3169 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003170 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171 ibmvnic_send_crq(adapter, &crq);
3172
3173 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003174 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003175 ibmvnic_send_crq(adapter, &crq);
3176
3177 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003178 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003179 ibmvnic_send_crq(adapter, &crq);
3180
3181 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003182 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003183 ibmvnic_send_crq(adapter, &crq);
3184
3185 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003186 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 ibmvnic_send_crq(adapter, &crq);
3188
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003189 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3190 atomic_inc(&adapter->running_cap_crqs);
3191 ibmvnic_send_crq(adapter, &crq);
3192
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003194 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003195 ibmvnic_send_crq(adapter, &crq);
3196
3197 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003198 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003199 ibmvnic_send_crq(adapter, &crq);
3200
3201 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003202 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003203 ibmvnic_send_crq(adapter, &crq);
3204
3205 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003206 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003207 ibmvnic_send_crq(adapter, &crq);
3208
3209 crq.query_capability.capability =
3210 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003211 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003212 ibmvnic_send_crq(adapter, &crq);
3213
3214 crq.query_capability.capability =
3215 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003216 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003217 ibmvnic_send_crq(adapter, &crq);
3218
3219 crq.query_capability.capability =
3220 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003221 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003222 ibmvnic_send_crq(adapter, &crq);
3223
3224 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003225 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003226 ibmvnic_send_crq(adapter, &crq);
3227}
3228
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003229static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3230 struct ibmvnic_adapter *adapter)
3231{
3232 struct device *dev = &adapter->vdev->dev;
3233
3234 if (crq->get_vpd_size_rsp.rc.code) {
3235 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3236 crq->get_vpd_size_rsp.rc.code);
3237 complete(&adapter->fw_done);
3238 return;
3239 }
3240
3241 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3242 complete(&adapter->fw_done);
3243}
3244
3245static void handle_vpd_rsp(union ibmvnic_crq *crq,
3246 struct ibmvnic_adapter *adapter)
3247{
3248 struct device *dev = &adapter->vdev->dev;
3249 unsigned char *substr = NULL, *ptr = NULL;
3250 u8 fw_level_len = 0;
3251
3252 memset(adapter->fw_version, 0, 32);
3253
3254 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3255 DMA_FROM_DEVICE);
3256
3257 if (crq->get_vpd_rsp.rc.code) {
3258 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3259 crq->get_vpd_rsp.rc.code);
3260 goto complete;
3261 }
3262
3263 /* get the position of the firmware version info
3264 * located after the ASCII 'RM' substring in the buffer
3265 */
3266 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3267 if (!substr) {
3268 dev_info(dev, "No FW level provided by VPD\n");
3269 goto complete;
3270 }
3271
3272 /* get length of firmware level ASCII substring */
3273 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3274 fw_level_len = *(substr + 2);
3275 } else {
3276 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3277 goto complete;
3278 }
3279
3280 /* copy firmware version string from vpd into adapter */
3281 if ((substr + 3 + fw_level_len) <
3282 (adapter->vpd->buff + adapter->vpd->len)) {
3283 ptr = strncpy((char *)adapter->fw_version,
3284 substr + 3, fw_level_len);
3285
3286 if (!ptr)
3287 dev_err(dev, "Failed to isolate FW level string\n");
3288 } else {
3289 dev_info(dev, "FW substr extrapolated VPD buff\n");
3290 }
3291
3292complete:
3293 complete(&adapter->fw_done);
3294}
3295
Thomas Falcon032c5e82015-12-21 11:26:06 -06003296static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3297{
3298 struct device *dev = &adapter->vdev->dev;
3299 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3300 union ibmvnic_crq crq;
3301 int i;
3302
3303 dma_unmap_single(dev, adapter->ip_offload_tok,
3304 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3305
3306 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3307 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3308 netdev_dbg(adapter->netdev, "%016lx\n",
3309 ((unsigned long int *)(buf))[i]);
3310
3311 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3312 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3313 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3314 buf->tcp_ipv4_chksum);
3315 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3316 buf->tcp_ipv6_chksum);
3317 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3318 buf->udp_ipv4_chksum);
3319 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3320 buf->udp_ipv6_chksum);
3321 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3322 buf->large_tx_ipv4);
3323 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3324 buf->large_tx_ipv6);
3325 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3326 buf->large_rx_ipv4);
3327 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3328 buf->large_rx_ipv6);
3329 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3330 buf->max_ipv4_header_size);
3331 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3332 buf->max_ipv6_header_size);
3333 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3334 buf->max_tcp_header_size);
3335 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3336 buf->max_udp_header_size);
3337 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3338 buf->max_large_tx_size);
3339 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3340 buf->max_large_rx_size);
3341 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3342 buf->ipv6_extension_header);
3343 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3344 buf->tcp_pseudosum_req);
3345 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3346 buf->num_ipv6_ext_headers);
3347 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3348 buf->off_ipv6_ext_headers);
3349
3350 adapter->ip_offload_ctrl_tok =
3351 dma_map_single(dev, &adapter->ip_offload_ctrl,
3352 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3353
3354 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3355 dev_err(dev, "Couldn't map ip offload control buffer\n");
3356 return;
3357 }
3358
3359 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3360 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3361 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3362 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3363 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003364 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3365 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003366
Thomas Falconfdb06102017-10-17 12:36:55 -05003367 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003368 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3369 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3370
Thomas Falcon15482052017-10-17 12:36:54 -05003371 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003372
3373 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3374 adapter->netdev->features |= NETIF_F_IP_CSUM;
3375
3376 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3377 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3378
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003379 if ((adapter->netdev->features &
3380 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3381 adapter->netdev->features |= NETIF_F_RXCSUM;
3382
Thomas Falconfdb06102017-10-17 12:36:55 -05003383 if (buf->large_tx_ipv4)
3384 adapter->netdev->features |= NETIF_F_TSO;
3385 if (buf->large_tx_ipv6)
3386 adapter->netdev->features |= NETIF_F_TSO6;
3387
Thomas Falconaa0bf852017-10-17 12:36:56 -05003388 adapter->netdev->hw_features |= adapter->netdev->features;
3389
Thomas Falcon032c5e82015-12-21 11:26:06 -06003390 memset(&crq, 0, sizeof(crq));
3391 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3392 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3393 crq.control_ip_offload.len =
3394 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3395 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3396 ibmvnic_send_crq(adapter, &crq);
3397}
3398
3399static void handle_error_info_rsp(union ibmvnic_crq *crq,
3400 struct ibmvnic_adapter *adapter)
3401{
3402 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003403 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003404 unsigned long flags;
3405 bool found = false;
3406 int i;
3407
3408 if (!crq->request_error_rsp.rc.code) {
3409 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3410 crq->request_error_rsp.rc.code);
3411 return;
3412 }
3413
3414 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003415 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003416 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3417 found = true;
3418 list_del(&error_buff->list);
3419 break;
3420 }
3421 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3422
3423 if (!found) {
3424 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003425 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003426 return;
3427 }
3428
3429 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003430 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003431
3432 for (i = 0; i < error_buff->len; i++) {
3433 pr_cont("%02x", (int)error_buff->buff[i]);
3434 if (i % 8 == 7)
3435 pr_cont(" ");
3436 }
3437 pr_cont("\n");
3438
3439 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3440 DMA_FROM_DEVICE);
3441 kfree(error_buff->buff);
3442 kfree(error_buff);
3443}
3444
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003445static void request_error_information(struct ibmvnic_adapter *adapter,
3446 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003448 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003449 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003450 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003451 unsigned long timeout = msecs_to_jiffies(30000);
3452 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003453 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003454 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003455
3456 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3457 if (!error_buff)
3458 return;
3459
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003460 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003461 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3462 if (!error_buff->buff) {
3463 kfree(error_buff);
3464 return;
3465 }
3466
3467 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3468 DMA_FROM_DEVICE);
3469 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003470 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003471 kfree(error_buff->buff);
3472 kfree(error_buff);
3473 return;
3474 }
3475
Thomas Falcon032c5e82015-12-21 11:26:06 -06003476 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003477 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003478
3479 spin_lock_irqsave(&adapter->error_list_lock, flags);
3480 list_add_tail(&error_buff->list, &adapter->errors);
3481 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3482
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003483 memset(&crq, 0, sizeof(crq));
3484 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3485 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3486 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3487 crq.request_error_info.len = cpu_to_be32(detail_len);
3488 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3489
3490 rc = ibmvnic_send_crq(adapter, &crq);
3491 if (rc) {
3492 netdev_err(netdev, "failed to request error information\n");
3493 goto err_info_fail;
3494 }
3495
3496 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3497 netdev_err(netdev, "timeout waiting for error information\n");
3498 goto err_info_fail;
3499 }
3500
3501 return;
3502
3503err_info_fail:
3504 spin_lock_irqsave(&adapter->error_list_lock, flags);
3505 list_del(&error_buff->list);
3506 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3507
3508 kfree(error_buff->buff);
3509 kfree(error_buff);
3510}
3511
3512static void handle_error_indication(union ibmvnic_crq *crq,
3513 struct ibmvnic_adapter *adapter)
3514{
3515 struct device *dev = &adapter->vdev->dev;
3516
3517 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3518 crq->error_indication.flags
3519 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3520 be32_to_cpu(crq->error_indication.error_id),
3521 be16_to_cpu(crq->error_indication.error_cause));
3522
3523 if (be32_to_cpu(crq->error_indication.error_id))
3524 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003525
3526 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3527 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003528 else
3529 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003530}
3531
3532static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3533 struct ibmvnic_adapter *adapter)
3534{
3535 struct net_device *netdev = adapter->netdev;
3536 struct device *dev = &adapter->vdev->dev;
3537 long rc;
3538
3539 rc = crq->change_mac_addr_rsp.rc.code;
3540 if (rc) {
3541 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3542 return;
3543 }
3544 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3545 ETH_ALEN);
3546}
3547
3548static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3549 struct ibmvnic_adapter *adapter)
3550{
3551 struct device *dev = &adapter->vdev->dev;
3552 u64 *req_value;
3553 char *name;
3554
Thomas Falcon901e0402017-02-15 12:17:59 -06003555 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003556 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3557 case REQ_TX_QUEUES:
3558 req_value = &adapter->req_tx_queues;
3559 name = "tx";
3560 break;
3561 case REQ_RX_QUEUES:
3562 req_value = &adapter->req_rx_queues;
3563 name = "rx";
3564 break;
3565 case REQ_RX_ADD_QUEUES:
3566 req_value = &adapter->req_rx_add_queues;
3567 name = "rx_add";
3568 break;
3569 case REQ_TX_ENTRIES_PER_SUBCRQ:
3570 req_value = &adapter->req_tx_entries_per_subcrq;
3571 name = "tx_entries_per_subcrq";
3572 break;
3573 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3574 req_value = &adapter->req_rx_add_entries_per_subcrq;
3575 name = "rx_add_entries_per_subcrq";
3576 break;
3577 case REQ_MTU:
3578 req_value = &adapter->req_mtu;
3579 name = "mtu";
3580 break;
3581 case PROMISC_REQUESTED:
3582 req_value = &adapter->promisc;
3583 name = "promisc";
3584 break;
3585 default:
3586 dev_err(dev, "Got invalid cap request rsp %d\n",
3587 crq->request_capability.capability);
3588 return;
3589 }
3590
3591 switch (crq->request_capability_rsp.rc.code) {
3592 case SUCCESS:
3593 break;
3594 case PARTIALSUCCESS:
3595 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3596 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003597 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003598 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003599 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003600 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003601 return;
3602 default:
3603 dev_err(dev, "Error %d in request cap rsp\n",
3604 crq->request_capability_rsp.rc.code);
3605 return;
3606 }
3607
3608 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003609 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003610 union ibmvnic_crq newcrq;
3611 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3612 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3613 &adapter->ip_offload_buf;
3614
Thomas Falcon249168a2017-02-15 12:18:00 -06003615 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003616 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3617 buf_sz,
3618 DMA_FROM_DEVICE);
3619
3620 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3621 if (!firmware_has_feature(FW_FEATURE_CMO))
3622 dev_err(dev, "Couldn't map offload buffer\n");
3623 return;
3624 }
3625
3626 memset(&newcrq, 0, sizeof(newcrq));
3627 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3628 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3629 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3630 newcrq.query_ip_offload.ioba =
3631 cpu_to_be32(adapter->ip_offload_tok);
3632
3633 ibmvnic_send_crq(adapter, &newcrq);
3634 }
3635}
3636
3637static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3638 struct ibmvnic_adapter *adapter)
3639{
3640 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003641 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003642 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3643 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003644 int i;
3645
3646 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3647 DMA_BIDIRECTIONAL);
3648 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3649 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3650
John Allen498cd8e2016-04-06 11:49:55 -05003651 /* If the number of queues requested can't be allocated by the
3652 * server, the login response will return with code 1. We will need
3653 * to resend the login buffer with fewer queues requested.
3654 */
3655 if (login_rsp_crq->generic.rc.code) {
3656 adapter->renegotiate = true;
3657 complete(&adapter->init_done);
3658 return 0;
3659 }
3660
John Allenc26eba02017-10-26 16:23:25 -05003661 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3662
Thomas Falcon032c5e82015-12-21 11:26:06 -06003663 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3664 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3665 netdev_dbg(adapter->netdev, "%016lx\n",
3666 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3667 }
3668
3669 /* Sanity checks */
3670 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3671 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3672 adapter->req_rx_add_queues !=
3673 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3674 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3675 ibmvnic_remove(adapter->vdev);
3676 return -EIO;
3677 }
3678 complete(&adapter->init_done);
3679
Thomas Falcon032c5e82015-12-21 11:26:06 -06003680 return 0;
3681}
3682
Thomas Falcon032c5e82015-12-21 11:26:06 -06003683static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3684 struct ibmvnic_adapter *adapter)
3685{
3686 struct device *dev = &adapter->vdev->dev;
3687 long rc;
3688
3689 rc = crq->request_unmap_rsp.rc.code;
3690 if (rc)
3691 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3692}
3693
3694static void handle_query_map_rsp(union ibmvnic_crq *crq,
3695 struct ibmvnic_adapter *adapter)
3696{
3697 struct net_device *netdev = adapter->netdev;
3698 struct device *dev = &adapter->vdev->dev;
3699 long rc;
3700
3701 rc = crq->query_map_rsp.rc.code;
3702 if (rc) {
3703 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3704 return;
3705 }
3706 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3707 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3708 crq->query_map_rsp.free_pages);
3709}
3710
3711static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3712 struct ibmvnic_adapter *adapter)
3713{
3714 struct net_device *netdev = adapter->netdev;
3715 struct device *dev = &adapter->vdev->dev;
3716 long rc;
3717
Thomas Falcon901e0402017-02-15 12:17:59 -06003718 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003719 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003720 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003721 rc = crq->query_capability.rc.code;
3722 if (rc) {
3723 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3724 goto out;
3725 }
3726
3727 switch (be16_to_cpu(crq->query_capability.capability)) {
3728 case MIN_TX_QUEUES:
3729 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003730 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003731 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3732 adapter->min_tx_queues);
3733 break;
3734 case MIN_RX_QUEUES:
3735 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003736 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003737 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3738 adapter->min_rx_queues);
3739 break;
3740 case MIN_RX_ADD_QUEUES:
3741 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003742 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003743 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3744 adapter->min_rx_add_queues);
3745 break;
3746 case MAX_TX_QUEUES:
3747 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003748 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003749 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3750 adapter->max_tx_queues);
3751 break;
3752 case MAX_RX_QUEUES:
3753 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003754 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003755 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3756 adapter->max_rx_queues);
3757 break;
3758 case MAX_RX_ADD_QUEUES:
3759 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003760 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003761 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3762 adapter->max_rx_add_queues);
3763 break;
3764 case MIN_TX_ENTRIES_PER_SUBCRQ:
3765 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003766 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003767 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3768 adapter->min_tx_entries_per_subcrq);
3769 break;
3770 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3771 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003772 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003773 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3774 adapter->min_rx_add_entries_per_subcrq);
3775 break;
3776 case MAX_TX_ENTRIES_PER_SUBCRQ:
3777 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003778 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003779 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3780 adapter->max_tx_entries_per_subcrq);
3781 break;
3782 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3783 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003784 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003785 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3786 adapter->max_rx_add_entries_per_subcrq);
3787 break;
3788 case TCP_IP_OFFLOAD:
3789 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003790 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003791 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3792 adapter->tcp_ip_offload);
3793 break;
3794 case PROMISC_SUPPORTED:
3795 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003796 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003797 netdev_dbg(netdev, "promisc_supported = %lld\n",
3798 adapter->promisc_supported);
3799 break;
3800 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003801 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003802 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003803 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3804 break;
3805 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003806 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003807 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003808 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3809 break;
3810 case MAX_MULTICAST_FILTERS:
3811 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003812 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3814 adapter->max_multicast_filters);
3815 break;
3816 case VLAN_HEADER_INSERTION:
3817 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003818 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003819 if (adapter->vlan_header_insertion)
3820 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3821 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3822 adapter->vlan_header_insertion);
3823 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003824 case RX_VLAN_HEADER_INSERTION:
3825 adapter->rx_vlan_header_insertion =
3826 be64_to_cpu(crq->query_capability.number);
3827 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3828 adapter->rx_vlan_header_insertion);
3829 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003830 case MAX_TX_SG_ENTRIES:
3831 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003832 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003833 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3834 adapter->max_tx_sg_entries);
3835 break;
3836 case RX_SG_SUPPORTED:
3837 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003838 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003839 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3840 adapter->rx_sg_supported);
3841 break;
3842 case OPT_TX_COMP_SUB_QUEUES:
3843 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003844 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003845 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3846 adapter->opt_tx_comp_sub_queues);
3847 break;
3848 case OPT_RX_COMP_QUEUES:
3849 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003850 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003851 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3852 adapter->opt_rx_comp_queues);
3853 break;
3854 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3855 adapter->opt_rx_bufadd_q_per_rx_comp_q =
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, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3858 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3859 break;
3860 case OPT_TX_ENTRIES_PER_SUBCRQ:
3861 adapter->opt_tx_entries_per_subcrq =
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, "opt_tx_entries_per_subcrq = %lld\n",
3864 adapter->opt_tx_entries_per_subcrq);
3865 break;
3866 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3867 adapter->opt_rxba_entries_per_subcrq =
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, "opt_rxba_entries_per_subcrq = %lld\n",
3870 adapter->opt_rxba_entries_per_subcrq);
3871 break;
3872 case TX_RX_DESC_REQ:
3873 adapter->tx_rx_desc_req = crq->query_capability.number;
3874 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3875 adapter->tx_rx_desc_req);
3876 break;
3877
3878 default:
3879 netdev_err(netdev, "Got invalid cap rsp %d\n",
3880 crq->query_capability.capability);
3881 }
3882
3883out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003884 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3885 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003886 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003887 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003888}
3889
Thomas Falcon032c5e82015-12-21 11:26:06 -06003890static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3891 struct ibmvnic_adapter *adapter)
3892{
3893 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3894 struct net_device *netdev = adapter->netdev;
3895 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003896 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003897 long rc;
3898
3899 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003900 (unsigned long int)cpu_to_be64(u64_crq[0]),
3901 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003902 switch (gen_crq->first) {
3903 case IBMVNIC_CRQ_INIT_RSP:
3904 switch (gen_crq->cmd) {
3905 case IBMVNIC_CRQ_INIT:
3906 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003907 adapter->from_passive_init = true;
3908 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003909 break;
3910 case IBMVNIC_CRQ_INIT_COMPLETE:
3911 dev_info(dev, "Partner initialization complete\n");
3912 send_version_xchg(adapter);
3913 break;
3914 default:
3915 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3916 }
3917 return;
3918 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003919 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003921 dev_info(dev, "Migrated, re-enabling adapter\n");
3922 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003923 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3924 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003925 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003926 } else {
3927 /* The adapter lost the connection */
3928 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3929 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003930 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003931 }
3932 return;
3933 case IBMVNIC_CRQ_CMD_RSP:
3934 break;
3935 default:
3936 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3937 gen_crq->first);
3938 return;
3939 }
3940
3941 switch (gen_crq->cmd) {
3942 case VERSION_EXCHANGE_RSP:
3943 rc = crq->version_exchange_rsp.rc.code;
3944 if (rc) {
3945 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3946 break;
3947 }
3948 dev_info(dev, "Partner protocol version is %d\n",
3949 crq->version_exchange_rsp.version);
3950 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3951 ibmvnic_version)
3952 ibmvnic_version =
3953 be16_to_cpu(crq->version_exchange_rsp.version);
3954 send_cap_queries(adapter);
3955 break;
3956 case QUERY_CAPABILITY_RSP:
3957 handle_query_cap_rsp(crq, adapter);
3958 break;
3959 case QUERY_MAP_RSP:
3960 handle_query_map_rsp(crq, adapter);
3961 break;
3962 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003963 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3964 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003965 break;
3966 case REQUEST_UNMAP_RSP:
3967 handle_request_unmap_rsp(crq, adapter);
3968 break;
3969 case REQUEST_CAPABILITY_RSP:
3970 handle_request_cap_rsp(crq, adapter);
3971 break;
3972 case LOGIN_RSP:
3973 netdev_dbg(netdev, "Got Login Response\n");
3974 handle_login_rsp(crq, adapter);
3975 break;
3976 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003977 netdev_dbg(netdev,
3978 "Got Logical Link State Response, state: %d rc: %d\n",
3979 crq->logical_link_state_rsp.link_state,
3980 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003981 adapter->logical_link_state =
3982 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003983 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3984 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003985 break;
3986 case LINK_STATE_INDICATION:
3987 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3988 adapter->phys_link_state =
3989 crq->link_state_indication.phys_link_state;
3990 adapter->logical_link_state =
3991 crq->link_state_indication.logical_link_state;
3992 break;
3993 case CHANGE_MAC_ADDR_RSP:
3994 netdev_dbg(netdev, "Got MAC address change Response\n");
3995 handle_change_mac_rsp(crq, adapter);
3996 break;
3997 case ERROR_INDICATION:
3998 netdev_dbg(netdev, "Got Error Indication\n");
3999 handle_error_indication(crq, adapter);
4000 break;
4001 case REQUEST_ERROR_RSP:
4002 netdev_dbg(netdev, "Got Error Detail Response\n");
4003 handle_error_info_rsp(crq, adapter);
4004 break;
4005 case REQUEST_STATISTICS_RSP:
4006 netdev_dbg(netdev, "Got Statistics Response\n");
4007 complete(&adapter->stats_done);
4008 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004009 case QUERY_IP_OFFLOAD_RSP:
4010 netdev_dbg(netdev, "Got Query IP offload Response\n");
4011 handle_query_ip_offload_rsp(adapter);
4012 break;
4013 case MULTICAST_CTRL_RSP:
4014 netdev_dbg(netdev, "Got multicast control Response\n");
4015 break;
4016 case CONTROL_IP_OFFLOAD_RSP:
4017 netdev_dbg(netdev, "Got Control IP offload Response\n");
4018 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4019 sizeof(adapter->ip_offload_ctrl),
4020 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004021 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004022 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004023 case COLLECT_FW_TRACE_RSP:
4024 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4025 complete(&adapter->fw_done);
4026 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004027 case GET_VPD_SIZE_RSP:
4028 handle_vpd_size_rsp(crq, adapter);
4029 break;
4030 case GET_VPD_RSP:
4031 handle_vpd_rsp(crq, adapter);
4032 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004033 default:
4034 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4035 gen_crq->cmd);
4036 }
4037}
4038
4039static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4040{
4041 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004042
Thomas Falcon6c267b32017-02-15 12:17:58 -06004043 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004044 return IRQ_HANDLED;
4045}
4046
4047static void ibmvnic_tasklet(void *data)
4048{
4049 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004051 union ibmvnic_crq *crq;
4052 unsigned long flags;
4053 bool done = false;
4054
4055 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004056 while (!done) {
4057 /* Pull all the valid messages off the CRQ */
4058 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4059 ibmvnic_handle_crq(crq, adapter);
4060 crq->generic.first = 0;
4061 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004062
4063 /* remain in tasklet until all
4064 * capabilities responses are received
4065 */
4066 if (!adapter->wait_capability)
4067 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004069 /* if capabilities CRQ's were sent in this tasklet, the following
4070 * tasklet must wait until all responses are received
4071 */
4072 if (atomic_read(&adapter->running_cap_crqs) != 0)
4073 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004074 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004075}
4076
4077static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4078{
4079 struct vio_dev *vdev = adapter->vdev;
4080 int rc;
4081
4082 do {
4083 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4084 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4085
4086 if (rc)
4087 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4088
4089 return rc;
4090}
4091
4092static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4093{
4094 struct ibmvnic_crq_queue *crq = &adapter->crq;
4095 struct device *dev = &adapter->vdev->dev;
4096 struct vio_dev *vdev = adapter->vdev;
4097 int rc;
4098
4099 /* Close the CRQ */
4100 do {
4101 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4102 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4103
4104 /* Clean out the queue */
4105 memset(crq->msgs, 0, PAGE_SIZE);
4106 crq->cur = 0;
4107
4108 /* And re-open it again */
4109 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4110 crq->msg_token, PAGE_SIZE);
4111
4112 if (rc == H_CLOSED)
4113 /* Adapter is good, but other end is not ready */
4114 dev_warn(dev, "Partner adapter not ready\n");
4115 else if (rc != 0)
4116 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4117
4118 return rc;
4119}
4120
Nathan Fontenotf9928872017-03-30 02:48:54 -04004121static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004122{
4123 struct ibmvnic_crq_queue *crq = &adapter->crq;
4124 struct vio_dev *vdev = adapter->vdev;
4125 long rc;
4126
Nathan Fontenotf9928872017-03-30 02:48:54 -04004127 if (!crq->msgs)
4128 return;
4129
Thomas Falcon032c5e82015-12-21 11:26:06 -06004130 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4131 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004132 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004133 do {
4134 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4135 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4136
4137 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4138 DMA_BIDIRECTIONAL);
4139 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004140 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004141}
4142
Nathan Fontenotf9928872017-03-30 02:48:54 -04004143static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004144{
4145 struct ibmvnic_crq_queue *crq = &adapter->crq;
4146 struct device *dev = &adapter->vdev->dev;
4147 struct vio_dev *vdev = adapter->vdev;
4148 int rc, retrc = -ENOMEM;
4149
Nathan Fontenotf9928872017-03-30 02:48:54 -04004150 if (crq->msgs)
4151 return 0;
4152
Thomas Falcon032c5e82015-12-21 11:26:06 -06004153 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4154 /* Should we allocate more than one page? */
4155
4156 if (!crq->msgs)
4157 return -ENOMEM;
4158
4159 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4160 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4161 DMA_BIDIRECTIONAL);
4162 if (dma_mapping_error(dev, crq->msg_token))
4163 goto map_failed;
4164
4165 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4166 crq->msg_token, PAGE_SIZE);
4167
4168 if (rc == H_RESOURCE)
4169 /* maybe kexecing and resource is busy. try a reset */
4170 rc = ibmvnic_reset_crq(adapter);
4171 retrc = rc;
4172
4173 if (rc == H_CLOSED) {
4174 dev_warn(dev, "Partner adapter not ready\n");
4175 } else if (rc) {
4176 dev_warn(dev, "Error %d opening adapter\n", rc);
4177 goto reg_crq_failed;
4178 }
4179
4180 retrc = 0;
4181
Thomas Falcon6c267b32017-02-15 12:17:58 -06004182 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4183 (unsigned long)adapter);
4184
Thomas Falcon032c5e82015-12-21 11:26:06 -06004185 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4186 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4187 adapter);
4188 if (rc) {
4189 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4190 vdev->irq, rc);
4191 goto req_irq_failed;
4192 }
4193
4194 rc = vio_enable_interrupts(vdev);
4195 if (rc) {
4196 dev_err(dev, "Error %d enabling interrupts\n", rc);
4197 goto req_irq_failed;
4198 }
4199
4200 crq->cur = 0;
4201 spin_lock_init(&crq->lock);
4202
4203 return retrc;
4204
4205req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004206 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004207 do {
4208 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4209 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4210reg_crq_failed:
4211 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4212map_failed:
4213 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004214 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004215 return retrc;
4216}
4217
John Allenf6ef6402017-03-17 17:13:42 -05004218static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4219{
4220 struct device *dev = &adapter->vdev->dev;
4221 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05004222 int rc;
4223
John Allenc26eba02017-10-26 16:23:25 -05004224 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004225 rc = ibmvnic_reset_crq(adapter);
4226 if (!rc)
4227 rc = vio_enable_interrupts(adapter->vdev);
4228 } else {
4229 rc = init_crq_queue(adapter);
4230 }
4231
John Allenf6ef6402017-03-17 17:13:42 -05004232 if (rc) {
4233 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4234 return rc;
4235 }
4236
John Allen017892c12017-05-26 10:30:19 -04004237 adapter->from_passive_init = false;
4238
John Allenf6ef6402017-03-17 17:13:42 -05004239 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004240 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004241 ibmvnic_send_crq_init(adapter);
4242 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4243 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004244 return -1;
4245 }
4246
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004247 if (adapter->init_done_rc) {
4248 release_crq_queue(adapter);
4249 return adapter->init_done_rc;
4250 }
4251
John Allen017892c12017-05-26 10:30:19 -04004252 if (adapter->from_passive_init) {
4253 adapter->state = VNIC_OPEN;
4254 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004255 return -1;
4256 }
4257
John Allenc26eba02017-10-26 16:23:25 -05004258 if (adapter->resetting && !adapter->wait_for_reset)
Nathan Fontenot57a49432017-05-26 10:31:12 -04004259 rc = reset_sub_crq_queues(adapter);
4260 else
4261 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004262 if (rc) {
4263 dev_err(dev, "Initialization of sub crqs failed\n");
4264 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004265 return rc;
4266 }
4267
4268 rc = init_sub_crq_irqs(adapter);
4269 if (rc) {
4270 dev_err(dev, "Failed to initialize sub crq irqs\n");
4271 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004272 }
4273
4274 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004275}
4276
Thomas Falcon40c9db82017-06-12 12:35:04 -05004277static struct device_attribute dev_attr_failover;
4278
Thomas Falcon032c5e82015-12-21 11:26:06 -06004279static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4280{
4281 struct ibmvnic_adapter *adapter;
4282 struct net_device *netdev;
4283 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004284 int rc;
4285
4286 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4287 dev->unit_address);
4288
4289 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4290 VETH_MAC_ADDR, NULL);
4291 if (!mac_addr_p) {
4292 dev_err(&dev->dev,
4293 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4294 __FILE__, __LINE__);
4295 return 0;
4296 }
4297
4298 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004299 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004300 if (!netdev)
4301 return -ENOMEM;
4302
4303 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004304 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004305 dev_set_drvdata(&dev->dev, netdev);
4306 adapter->vdev = dev;
4307 adapter->netdev = netdev;
4308
4309 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4310 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4311 netdev->irq = dev->irq;
4312 netdev->netdev_ops = &ibmvnic_netdev_ops;
4313 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4314 SET_NETDEV_DEV(netdev, &dev->dev);
4315
4316 spin_lock_init(&adapter->stats_lock);
4317
Thomas Falcon032c5e82015-12-21 11:26:06 -06004318 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004319 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004320
Nathan Fontenoted651a12017-05-03 14:04:38 -04004321 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4322 INIT_LIST_HEAD(&adapter->rwi_list);
4323 mutex_init(&adapter->reset_lock);
4324 mutex_init(&adapter->rwi_lock);
4325 adapter->resetting = false;
4326
John Allenc26eba02017-10-26 16:23:25 -05004327 adapter->mac_change_pending = false;
4328
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004329 do {
4330 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004331 if (rc && rc != EAGAIN)
4332 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004333 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004334
Thomas Falconf39f0d12017-02-14 10:22:59 -06004335 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004336 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4337 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004338
Thomas Falcon40c9db82017-06-12 12:35:04 -05004339 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004340 if (rc)
4341 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004342
Mick Tarsele876a8a2017-09-28 13:53:18 -07004343 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004344 rc = register_netdev(netdev);
4345 if (rc) {
4346 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004347 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004348 }
4349 dev_info(&dev->dev, "ibmvnic registered\n");
4350
Nathan Fontenot90c80142017-05-03 14:04:32 -04004351 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004352
4353 adapter->wait_for_reset = false;
4354
Thomas Falcon032c5e82015-12-21 11:26:06 -06004355 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004356
4357ibmvnic_register_fail:
4358 device_remove_file(&dev->dev, &dev_attr_failover);
4359
4360ibmvnic_init_fail:
4361 release_sub_crqs(adapter);
4362 release_crq_queue(adapter);
4363 free_netdev(netdev);
4364
4365 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004366}
4367
4368static int ibmvnic_remove(struct vio_dev *dev)
4369{
4370 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004371 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004372
Nathan Fontenot90c80142017-05-03 14:04:32 -04004373 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004374 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004375 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004376
4377 release_resources(adapter);
4378 release_sub_crqs(adapter);
4379 release_crq_queue(adapter);
4380
Nathan Fontenot90c80142017-05-03 14:04:32 -04004381 adapter->state = VNIC_REMOVED;
4382
Nathan Fontenoted651a12017-05-03 14:04:38 -04004383 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004384 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004385 free_netdev(netdev);
4386 dev_set_drvdata(&dev->dev, NULL);
4387
4388 return 0;
4389}
4390
Thomas Falcon40c9db82017-06-12 12:35:04 -05004391static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4392 const char *buf, size_t count)
4393{
4394 struct net_device *netdev = dev_get_drvdata(dev);
4395 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4396 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4397 __be64 session_token;
4398 long rc;
4399
4400 if (!sysfs_streq(buf, "1"))
4401 return -EINVAL;
4402
4403 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4404 H_GET_SESSION_TOKEN, 0, 0, 0);
4405 if (rc) {
4406 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4407 rc);
4408 return -EINVAL;
4409 }
4410
4411 session_token = (__be64)retbuf[0];
4412 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4413 be64_to_cpu(session_token));
4414 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4415 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4416 if (rc) {
4417 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4418 rc);
4419 return -EINVAL;
4420 }
4421
4422 return count;
4423}
4424
4425static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4426
Thomas Falcon032c5e82015-12-21 11:26:06 -06004427static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4428{
4429 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4430 struct ibmvnic_adapter *adapter;
4431 struct iommu_table *tbl;
4432 unsigned long ret = 0;
4433 int i;
4434
4435 tbl = get_iommu_table_base(&vdev->dev);
4436
4437 /* netdev inits at probe time along with the structures we need below*/
4438 if (!netdev)
4439 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4440
4441 adapter = netdev_priv(netdev);
4442
4443 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004444 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4445
4446 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4447 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4448
4449 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4450 i++)
4451 ret += adapter->rx_pool[i].size *
4452 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4453
4454 return ret;
4455}
4456
4457static int ibmvnic_resume(struct device *dev)
4458{
4459 struct net_device *netdev = dev_get_drvdata(dev);
4460 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004461
John Allencb89ba22017-06-19 11:27:53 -05004462 if (adapter->state != VNIC_OPEN)
4463 return 0;
4464
John Allena2488782017-07-24 13:26:06 -05004465 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004466
4467 return 0;
4468}
4469
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304470static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004471 {"network", "IBM,vnic"},
4472 {"", "" }
4473};
4474MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4475
4476static const struct dev_pm_ops ibmvnic_pm_ops = {
4477 .resume = ibmvnic_resume
4478};
4479
4480static struct vio_driver ibmvnic_driver = {
4481 .id_table = ibmvnic_device_table,
4482 .probe = ibmvnic_probe,
4483 .remove = ibmvnic_remove,
4484 .get_desired_dma = ibmvnic_get_desired_dma,
4485 .name = ibmvnic_driver_name,
4486 .pm = &ibmvnic_pm_ops,
4487};
4488
4489/* module functions */
4490static int __init ibmvnic_module_init(void)
4491{
4492 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4493 IBMVNIC_DRIVER_VERSION);
4494
4495 return vio_register_driver(&ibmvnic_driver);
4496}
4497
4498static void __exit ibmvnic_module_exit(void)
4499{
4500 vio_unregister_driver(&ibmvnic_driver);
4501}
4502
4503module_init(ibmvnic_module_init);
4504module_exit(ibmvnic_module_exit);