blob: 6911b7cc06c527429acdf094782a23c35445c647 [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
John Allen3d52b592017-08-02 16:44:14 -05002456 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2457
Thomas Falcon032c5e82015-12-21 11:26:06 -06002458 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2459 disable_scrq_irq(adapter, scrq);
2460 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2461 }
2462
2463 return IRQ_HANDLED;
2464}
2465
Thomas Falconea22d512016-07-06 15:35:17 -05002466static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2467{
2468 struct device *dev = &adapter->vdev->dev;
2469 struct ibmvnic_sub_crq_queue *scrq;
2470 int i = 0, j = 0;
2471 int rc = 0;
2472
2473 for (i = 0; i < adapter->req_tx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002474 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2475 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002476 scrq = adapter->tx_scrq[i];
2477 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2478
Michael Ellerman99c17902016-09-10 19:59:05 +10002479 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002480 rc = -EINVAL;
2481 dev_err(dev, "Error mapping irq\n");
2482 goto req_tx_irq_failed;
2483 }
2484
2485 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2486 0, "ibmvnic_tx", scrq);
2487
2488 if (rc) {
2489 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2490 scrq->irq, rc);
2491 irq_dispose_mapping(scrq->irq);
2492 goto req_rx_irq_failed;
2493 }
2494 }
2495
2496 for (i = 0; i < adapter->req_rx_queues; i++) {
Nathan Fontenotd1cf33d2017-08-08 15:24:05 -05002497 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2498 i);
Thomas Falconea22d512016-07-06 15:35:17 -05002499 scrq = adapter->rx_scrq[i];
2500 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002501 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002502 rc = -EINVAL;
2503 dev_err(dev, "Error mapping irq\n");
2504 goto req_rx_irq_failed;
2505 }
2506 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2507 0, "ibmvnic_rx", scrq);
2508 if (rc) {
2509 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2510 scrq->irq, rc);
2511 irq_dispose_mapping(scrq->irq);
2512 goto req_rx_irq_failed;
2513 }
2514 }
2515 return rc;
2516
2517req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002518 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002519 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2520 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002521 }
Thomas Falconea22d512016-07-06 15:35:17 -05002522 i = adapter->req_tx_queues;
2523req_tx_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->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2526 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002527 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002528 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002529 return rc;
2530}
2531
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002532static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533{
2534 struct device *dev = &adapter->vdev->dev;
2535 struct ibmvnic_sub_crq_queue **allqueues;
2536 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002537 int total_queues;
2538 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002539 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002540
Thomas Falcon032c5e82015-12-21 11:26:06 -06002541 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2542
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002543 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002544 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002545 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002546
2547 for (i = 0; i < total_queues; i++) {
2548 allqueues[i] = init_sub_crq_queue(adapter);
2549 if (!allqueues[i]) {
2550 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2551 break;
2552 }
2553 registered_queues++;
2554 }
2555
2556 /* Make sure we were able to register the minimum number of queues */
2557 if (registered_queues <
2558 adapter->min_tx_queues + adapter->min_rx_queues) {
2559 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2560 goto tx_failed;
2561 }
2562
2563 /* Distribute the failed allocated queues*/
2564 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2565 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2566 switch (i % 3) {
2567 case 0:
2568 if (adapter->req_rx_queues > adapter->min_rx_queues)
2569 adapter->req_rx_queues--;
2570 else
2571 more++;
2572 break;
2573 case 1:
2574 if (adapter->req_tx_queues > adapter->min_tx_queues)
2575 adapter->req_tx_queues--;
2576 else
2577 more++;
2578 break;
2579 }
2580 }
2581
2582 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002583 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002584 if (!adapter->tx_scrq)
2585 goto tx_failed;
2586
2587 for (i = 0; i < adapter->req_tx_queues; i++) {
2588 adapter->tx_scrq[i] = allqueues[i];
2589 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002590 }
2591
2592 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002593 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002594 if (!adapter->rx_scrq)
2595 goto rx_failed;
2596
2597 for (i = 0; i < adapter->req_rx_queues; i++) {
2598 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2599 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002600 }
2601
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002602 kfree(allqueues);
2603 return 0;
2604
2605rx_failed:
2606 kfree(adapter->tx_scrq);
2607 adapter->tx_scrq = NULL;
2608tx_failed:
2609 for (i = 0; i < registered_queues; i++)
2610 release_sub_crq_queue(adapter, allqueues[i]);
2611 kfree(allqueues);
2612 return -1;
2613}
2614
2615static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2616{
2617 struct device *dev = &adapter->vdev->dev;
2618 union ibmvnic_crq crq;
John Allenc26eba02017-10-26 16:23:25 -05002619 int max_entries;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002620
2621 if (!retry) {
2622 /* Sub-CRQ entries are 32 byte long */
2623 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2624
2625 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2626 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2627 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2628 return;
2629 }
2630
John Allenc26eba02017-10-26 16:23:25 -05002631 if (adapter->desired.mtu)
2632 adapter->req_mtu = adapter->desired.mtu;
2633 else
2634 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002635
John Allenc26eba02017-10-26 16:23:25 -05002636 if (!adapter->desired.tx_entries)
2637 adapter->desired.tx_entries =
2638 adapter->max_tx_entries_per_subcrq;
2639 if (!adapter->desired.rx_entries)
2640 adapter->desired.rx_entries =
2641 adapter->max_rx_add_entries_per_subcrq;
2642
2643 max_entries = IBMVNIC_MAX_LTB_SIZE /
2644 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2645
2646 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2647 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2648 adapter->desired.tx_entries = max_entries;
2649 }
2650
2651 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2652 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2653 adapter->desired.rx_entries = max_entries;
2654 }
2655
2656 if (adapter->desired.tx_entries)
2657 adapter->req_tx_entries_per_subcrq =
2658 adapter->desired.tx_entries;
2659 else
2660 adapter->req_tx_entries_per_subcrq =
2661 adapter->max_tx_entries_per_subcrq;
2662
2663 if (adapter->desired.rx_entries)
2664 adapter->req_rx_add_entries_per_subcrq =
2665 adapter->desired.rx_entries;
2666 else
2667 adapter->req_rx_add_entries_per_subcrq =
2668 adapter->max_rx_add_entries_per_subcrq;
2669
2670 if (adapter->desired.tx_queues)
2671 adapter->req_tx_queues =
2672 adapter->desired.tx_queues;
2673 else
2674 adapter->req_tx_queues =
2675 adapter->opt_tx_comp_sub_queues;
2676
2677 if (adapter->desired.rx_queues)
2678 adapter->req_rx_queues =
2679 adapter->desired.rx_queues;
2680 else
2681 adapter->req_rx_queues =
2682 adapter->opt_rx_comp_queues;
2683
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002684 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002685 }
2686
Thomas Falcon032c5e82015-12-21 11:26:06 -06002687 memset(&crq, 0, sizeof(crq));
2688 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2689 crq.request_capability.cmd = REQUEST_CAPABILITY;
2690
2691 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002692 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002693 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002694 ibmvnic_send_crq(adapter, &crq);
2695
2696 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002697 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002698 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002699 ibmvnic_send_crq(adapter, &crq);
2700
2701 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002702 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002703 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002704 ibmvnic_send_crq(adapter, &crq);
2705
2706 crq.request_capability.capability =
2707 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2708 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002709 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002710 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002711 ibmvnic_send_crq(adapter, &crq);
2712
2713 crq.request_capability.capability =
2714 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2715 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002716 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002717 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 ibmvnic_send_crq(adapter, &crq);
2719
2720 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002721 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002722 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 ibmvnic_send_crq(adapter, &crq);
2724
2725 if (adapter->netdev->flags & IFF_PROMISC) {
2726 if (adapter->promisc_supported) {
2727 crq.request_capability.capability =
2728 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002729 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002730 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 ibmvnic_send_crq(adapter, &crq);
2732 }
2733 } else {
2734 crq.request_capability.capability =
2735 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002736 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002737 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002738 ibmvnic_send_crq(adapter, &crq);
2739 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740}
2741
2742static int pending_scrq(struct ibmvnic_adapter *adapter,
2743 struct ibmvnic_sub_crq_queue *scrq)
2744{
2745 union sub_crq *entry = &scrq->msgs[scrq->cur];
2746
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002747 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002748 return 1;
2749 else
2750 return 0;
2751}
2752
2753static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2754 struct ibmvnic_sub_crq_queue *scrq)
2755{
2756 union sub_crq *entry;
2757 unsigned long flags;
2758
2759 spin_lock_irqsave(&scrq->lock, flags);
2760 entry = &scrq->msgs[scrq->cur];
2761 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2762 if (++scrq->cur == scrq->size)
2763 scrq->cur = 0;
2764 } else {
2765 entry = NULL;
2766 }
2767 spin_unlock_irqrestore(&scrq->lock, flags);
2768
2769 return entry;
2770}
2771
2772static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2773{
2774 struct ibmvnic_crq_queue *queue = &adapter->crq;
2775 union ibmvnic_crq *crq;
2776
2777 crq = &queue->msgs[queue->cur];
2778 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2779 if (++queue->cur == queue->size)
2780 queue->cur = 0;
2781 } else {
2782 crq = NULL;
2783 }
2784
2785 return crq;
2786}
2787
2788static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2789 union sub_crq *sub_crq)
2790{
2791 unsigned int ua = adapter->vdev->unit_address;
2792 struct device *dev = &adapter->vdev->dev;
2793 u64 *u64_crq = (u64 *)sub_crq;
2794 int rc;
2795
2796 netdev_dbg(adapter->netdev,
2797 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2798 (unsigned long int)cpu_to_be64(remote_handle),
2799 (unsigned long int)cpu_to_be64(u64_crq[0]),
2800 (unsigned long int)cpu_to_be64(u64_crq[1]),
2801 (unsigned long int)cpu_to_be64(u64_crq[2]),
2802 (unsigned long int)cpu_to_be64(u64_crq[3]));
2803
2804 /* Make sure the hypervisor sees the complete request */
2805 mb();
2806
2807 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2808 cpu_to_be64(remote_handle),
2809 cpu_to_be64(u64_crq[0]),
2810 cpu_to_be64(u64_crq[1]),
2811 cpu_to_be64(u64_crq[2]),
2812 cpu_to_be64(u64_crq[3]));
2813
2814 if (rc) {
2815 if (rc == H_CLOSED)
2816 dev_warn(dev, "CRQ Queue closed\n");
2817 dev_err(dev, "Send error (rc=%d)\n", rc);
2818 }
2819
2820 return rc;
2821}
2822
Thomas Falconad7775d2016-04-01 17:20:34 -05002823static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2824 u64 remote_handle, u64 ioba, u64 num_entries)
2825{
2826 unsigned int ua = adapter->vdev->unit_address;
2827 struct device *dev = &adapter->vdev->dev;
2828 int rc;
2829
2830 /* Make sure the hypervisor sees the complete request */
2831 mb();
2832 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2833 cpu_to_be64(remote_handle),
2834 ioba, num_entries);
2835
2836 if (rc) {
2837 if (rc == H_CLOSED)
2838 dev_warn(dev, "CRQ Queue closed\n");
2839 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2840 }
2841
2842 return rc;
2843}
2844
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2846 union ibmvnic_crq *crq)
2847{
2848 unsigned int ua = adapter->vdev->unit_address;
2849 struct device *dev = &adapter->vdev->dev;
2850 u64 *u64_crq = (u64 *)crq;
2851 int rc;
2852
2853 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2854 (unsigned long int)cpu_to_be64(u64_crq[0]),
2855 (unsigned long int)cpu_to_be64(u64_crq[1]));
2856
2857 /* Make sure the hypervisor sees the complete request */
2858 mb();
2859
2860 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2861 cpu_to_be64(u64_crq[0]),
2862 cpu_to_be64(u64_crq[1]));
2863
2864 if (rc) {
2865 if (rc == H_CLOSED)
2866 dev_warn(dev, "CRQ Queue closed\n");
2867 dev_warn(dev, "Send error (rc=%d)\n", rc);
2868 }
2869
2870 return rc;
2871}
2872
2873static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2874{
2875 union ibmvnic_crq crq;
2876
2877 memset(&crq, 0, sizeof(crq));
2878 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2879 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2880 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2881
2882 return ibmvnic_send_crq(adapter, &crq);
2883}
2884
Thomas Falcon032c5e82015-12-21 11:26:06 -06002885static int send_version_xchg(struct ibmvnic_adapter *adapter)
2886{
2887 union ibmvnic_crq crq;
2888
2889 memset(&crq, 0, sizeof(crq));
2890 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2891 crq.version_exchange.cmd = VERSION_EXCHANGE;
2892 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2893
2894 return ibmvnic_send_crq(adapter, &crq);
2895}
2896
Nathan Fontenot37798d02017-11-08 11:23:56 -06002897struct vnic_login_client_data {
2898 u8 type;
2899 __be16 len;
2900 char name;
2901} __packed;
2902
2903static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2904{
2905 int len;
2906
2907 /* Calculate the amount of buffer space needed for the
2908 * vnic client data in the login buffer. There are four entries,
2909 * OS name, LPAR name, device name, and a null last entry.
2910 */
2911 len = 4 * sizeof(struct vnic_login_client_data);
2912 len += 6; /* "Linux" plus NULL */
2913 len += strlen(utsname()->nodename) + 1;
2914 len += strlen(adapter->netdev->name) + 1;
2915
2916 return len;
2917}
2918
2919static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2920 struct vnic_login_client_data *vlcd)
2921{
2922 const char *os_name = "Linux";
2923 int len;
2924
2925 /* Type 1 - LPAR OS */
2926 vlcd->type = 1;
2927 len = strlen(os_name) + 1;
2928 vlcd->len = cpu_to_be16(len);
2929 strncpy(&vlcd->name, os_name, len);
2930 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2931
2932 /* Type 2 - LPAR name */
2933 vlcd->type = 2;
2934 len = strlen(utsname()->nodename) + 1;
2935 vlcd->len = cpu_to_be16(len);
2936 strncpy(&vlcd->name, utsname()->nodename, len);
2937 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2938
2939 /* Type 3 - device name */
2940 vlcd->type = 3;
2941 len = strlen(adapter->netdev->name) + 1;
2942 vlcd->len = cpu_to_be16(len);
2943 strncpy(&vlcd->name, adapter->netdev->name, len);
2944}
2945
Thomas Falcon032c5e82015-12-21 11:26:06 -06002946static void send_login(struct ibmvnic_adapter *adapter)
2947{
2948 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2949 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002950 struct device *dev = &adapter->vdev->dev;
2951 dma_addr_t rsp_buffer_token;
2952 dma_addr_t buffer_token;
2953 size_t rsp_buffer_size;
2954 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002955 size_t buffer_size;
2956 __be64 *tx_list_p;
2957 __be64 *rx_list_p;
Nathan Fontenot37798d02017-11-08 11:23:56 -06002958 int client_data_len;
2959 struct vnic_login_client_data *vlcd;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002960 int i;
2961
Nathan Fontenot37798d02017-11-08 11:23:56 -06002962 client_data_len = vnic_client_data_len(adapter);
2963
Thomas Falcon032c5e82015-12-21 11:26:06 -06002964 buffer_size =
2965 sizeof(struct ibmvnic_login_buffer) +
Nathan Fontenot37798d02017-11-08 11:23:56 -06002966 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
2967 client_data_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002968
Nathan Fontenot37798d02017-11-08 11:23:56 -06002969 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002970 if (!login_buffer)
2971 goto buf_alloc_failed;
2972
2973 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2974 DMA_TO_DEVICE);
2975 if (dma_mapping_error(dev, buffer_token)) {
2976 dev_err(dev, "Couldn't map login buffer\n");
2977 goto buf_map_failed;
2978 }
2979
John Allen498cd8e2016-04-06 11:49:55 -05002980 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2981 sizeof(u64) * adapter->req_tx_queues +
2982 sizeof(u64) * adapter->req_rx_queues +
2983 sizeof(u64) * adapter->req_rx_queues +
2984 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002985
2986 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2987 if (!login_rsp_buffer)
2988 goto buf_rsp_alloc_failed;
2989
2990 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2991 rsp_buffer_size, DMA_FROM_DEVICE);
2992 if (dma_mapping_error(dev, rsp_buffer_token)) {
2993 dev_err(dev, "Couldn't map login rsp buffer\n");
2994 goto buf_rsp_map_failed;
2995 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002996
Thomas Falcon032c5e82015-12-21 11:26:06 -06002997 adapter->login_buf = login_buffer;
2998 adapter->login_buf_token = buffer_token;
2999 adapter->login_buf_sz = buffer_size;
3000 adapter->login_rsp_buf = login_rsp_buffer;
3001 adapter->login_rsp_buf_token = rsp_buffer_token;
3002 adapter->login_rsp_buf_sz = rsp_buffer_size;
3003
3004 login_buffer->len = cpu_to_be32(buffer_size);
3005 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3006 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3007 login_buffer->off_txcomp_subcrqs =
3008 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3009 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3010 login_buffer->off_rxcomp_subcrqs =
3011 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3012 sizeof(u64) * adapter->req_tx_queues);
3013 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3014 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3015
3016 tx_list_p = (__be64 *)((char *)login_buffer +
3017 sizeof(struct ibmvnic_login_buffer));
3018 rx_list_p = (__be64 *)((char *)login_buffer +
3019 sizeof(struct ibmvnic_login_buffer) +
3020 sizeof(u64) * adapter->req_tx_queues);
3021
3022 for (i = 0; i < adapter->req_tx_queues; i++) {
3023 if (adapter->tx_scrq[i]) {
3024 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3025 crq_num);
3026 }
3027 }
3028
3029 for (i = 0; i < adapter->req_rx_queues; i++) {
3030 if (adapter->rx_scrq[i]) {
3031 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3032 crq_num);
3033 }
3034 }
3035
Nathan Fontenot37798d02017-11-08 11:23:56 -06003036 /* Insert vNIC login client data */
3037 vlcd = (struct vnic_login_client_data *)
3038 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3039 login_buffer->client_data_offset =
3040 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3041 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3042
3043 vnic_add_client_data(adapter, vlcd);
3044
Thomas Falcon032c5e82015-12-21 11:26:06 -06003045 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3046 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3047 netdev_dbg(adapter->netdev, "%016lx\n",
3048 ((unsigned long int *)(adapter->login_buf))[i]);
3049 }
3050
3051 memset(&crq, 0, sizeof(crq));
3052 crq.login.first = IBMVNIC_CRQ_CMD;
3053 crq.login.cmd = LOGIN;
3054 crq.login.ioba = cpu_to_be32(buffer_token);
3055 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003056 ibmvnic_send_crq(adapter, &crq);
3057
3058 return;
3059
Thomas Falcon032c5e82015-12-21 11:26:06 -06003060buf_rsp_map_failed:
3061 kfree(login_rsp_buffer);
3062buf_rsp_alloc_failed:
3063 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3064buf_map_failed:
3065 kfree(login_buffer);
3066buf_alloc_failed:
3067 return;
3068}
3069
3070static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3071 u32 len, u8 map_id)
3072{
3073 union ibmvnic_crq crq;
3074
3075 memset(&crq, 0, sizeof(crq));
3076 crq.request_map.first = IBMVNIC_CRQ_CMD;
3077 crq.request_map.cmd = REQUEST_MAP;
3078 crq.request_map.map_id = map_id;
3079 crq.request_map.ioba = cpu_to_be32(addr);
3080 crq.request_map.len = cpu_to_be32(len);
3081 ibmvnic_send_crq(adapter, &crq);
3082}
3083
3084static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3085{
3086 union ibmvnic_crq crq;
3087
3088 memset(&crq, 0, sizeof(crq));
3089 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3090 crq.request_unmap.cmd = REQUEST_UNMAP;
3091 crq.request_unmap.map_id = map_id;
3092 ibmvnic_send_crq(adapter, &crq);
3093}
3094
3095static void send_map_query(struct ibmvnic_adapter *adapter)
3096{
3097 union ibmvnic_crq crq;
3098
3099 memset(&crq, 0, sizeof(crq));
3100 crq.query_map.first = IBMVNIC_CRQ_CMD;
3101 crq.query_map.cmd = QUERY_MAP;
3102 ibmvnic_send_crq(adapter, &crq);
3103}
3104
3105/* Send a series of CRQs requesting various capabilities of the VNIC server */
3106static void send_cap_queries(struct ibmvnic_adapter *adapter)
3107{
3108 union ibmvnic_crq crq;
3109
Thomas Falcon901e0402017-02-15 12:17:59 -06003110 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003111 memset(&crq, 0, sizeof(crq));
3112 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3113 crq.query_capability.cmd = QUERY_CAPABILITY;
3114
3115 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003116 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003117 ibmvnic_send_crq(adapter, &crq);
3118
3119 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003120 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003121 ibmvnic_send_crq(adapter, &crq);
3122
3123 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003124 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003125 ibmvnic_send_crq(adapter, &crq);
3126
3127 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003128 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003129 ibmvnic_send_crq(adapter, &crq);
3130
3131 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003132 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 ibmvnic_send_crq(adapter, &crq);
3134
3135 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003136 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003137 ibmvnic_send_crq(adapter, &crq);
3138
3139 crq.query_capability.capability =
3140 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003141 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003142 ibmvnic_send_crq(adapter, &crq);
3143
3144 crq.query_capability.capability =
3145 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003146 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147 ibmvnic_send_crq(adapter, &crq);
3148
3149 crq.query_capability.capability =
3150 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003151 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003152 ibmvnic_send_crq(adapter, &crq);
3153
3154 crq.query_capability.capability =
3155 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003156 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003157 ibmvnic_send_crq(adapter, &crq);
3158
3159 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06003160 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003161 ibmvnic_send_crq(adapter, &crq);
3162
3163 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003164 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003165 ibmvnic_send_crq(adapter, &crq);
3166
3167 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003168 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003169 ibmvnic_send_crq(adapter, &crq);
3170
3171 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06003172 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003173 ibmvnic_send_crq(adapter, &crq);
3174
3175 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06003176 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003177 ibmvnic_send_crq(adapter, &crq);
3178
3179 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06003180 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003181 ibmvnic_send_crq(adapter, &crq);
3182
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003183 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3184 atomic_inc(&adapter->running_cap_crqs);
3185 ibmvnic_send_crq(adapter, &crq);
3186
Thomas Falcon032c5e82015-12-21 11:26:06 -06003187 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003188 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003189 ibmvnic_send_crq(adapter, &crq);
3190
3191 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06003192 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003193 ibmvnic_send_crq(adapter, &crq);
3194
3195 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003196 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003197 ibmvnic_send_crq(adapter, &crq);
3198
3199 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06003200 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003201 ibmvnic_send_crq(adapter, &crq);
3202
3203 crq.query_capability.capability =
3204 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06003205 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003206 ibmvnic_send_crq(adapter, &crq);
3207
3208 crq.query_capability.capability =
3209 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003210 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003211 ibmvnic_send_crq(adapter, &crq);
3212
3213 crq.query_capability.capability =
3214 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003215 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003216 ibmvnic_send_crq(adapter, &crq);
3217
3218 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06003219 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003220 ibmvnic_send_crq(adapter, &crq);
3221}
3222
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02003223static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3224 struct ibmvnic_adapter *adapter)
3225{
3226 struct device *dev = &adapter->vdev->dev;
3227
3228 if (crq->get_vpd_size_rsp.rc.code) {
3229 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3230 crq->get_vpd_size_rsp.rc.code);
3231 complete(&adapter->fw_done);
3232 return;
3233 }
3234
3235 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3236 complete(&adapter->fw_done);
3237}
3238
3239static void handle_vpd_rsp(union ibmvnic_crq *crq,
3240 struct ibmvnic_adapter *adapter)
3241{
3242 struct device *dev = &adapter->vdev->dev;
3243 unsigned char *substr = NULL, *ptr = NULL;
3244 u8 fw_level_len = 0;
3245
3246 memset(adapter->fw_version, 0, 32);
3247
3248 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3249 DMA_FROM_DEVICE);
3250
3251 if (crq->get_vpd_rsp.rc.code) {
3252 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3253 crq->get_vpd_rsp.rc.code);
3254 goto complete;
3255 }
3256
3257 /* get the position of the firmware version info
3258 * located after the ASCII 'RM' substring in the buffer
3259 */
3260 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3261 if (!substr) {
3262 dev_info(dev, "No FW level provided by VPD\n");
3263 goto complete;
3264 }
3265
3266 /* get length of firmware level ASCII substring */
3267 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3268 fw_level_len = *(substr + 2);
3269 } else {
3270 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3271 goto complete;
3272 }
3273
3274 /* copy firmware version string from vpd into adapter */
3275 if ((substr + 3 + fw_level_len) <
3276 (adapter->vpd->buff + adapter->vpd->len)) {
3277 ptr = strncpy((char *)adapter->fw_version,
3278 substr + 3, fw_level_len);
3279
3280 if (!ptr)
3281 dev_err(dev, "Failed to isolate FW level string\n");
3282 } else {
3283 dev_info(dev, "FW substr extrapolated VPD buff\n");
3284 }
3285
3286complete:
3287 complete(&adapter->fw_done);
3288}
3289
Thomas Falcon032c5e82015-12-21 11:26:06 -06003290static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3291{
3292 struct device *dev = &adapter->vdev->dev;
3293 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3294 union ibmvnic_crq crq;
3295 int i;
3296
3297 dma_unmap_single(dev, adapter->ip_offload_tok,
3298 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3299
3300 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3301 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3302 netdev_dbg(adapter->netdev, "%016lx\n",
3303 ((unsigned long int *)(buf))[i]);
3304
3305 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3306 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3307 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3308 buf->tcp_ipv4_chksum);
3309 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3310 buf->tcp_ipv6_chksum);
3311 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3312 buf->udp_ipv4_chksum);
3313 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3314 buf->udp_ipv6_chksum);
3315 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3316 buf->large_tx_ipv4);
3317 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3318 buf->large_tx_ipv6);
3319 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3320 buf->large_rx_ipv4);
3321 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3322 buf->large_rx_ipv6);
3323 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3324 buf->max_ipv4_header_size);
3325 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3326 buf->max_ipv6_header_size);
3327 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3328 buf->max_tcp_header_size);
3329 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3330 buf->max_udp_header_size);
3331 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3332 buf->max_large_tx_size);
3333 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3334 buf->max_large_rx_size);
3335 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3336 buf->ipv6_extension_header);
3337 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3338 buf->tcp_pseudosum_req);
3339 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3340 buf->num_ipv6_ext_headers);
3341 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3342 buf->off_ipv6_ext_headers);
3343
3344 adapter->ip_offload_ctrl_tok =
3345 dma_map_single(dev, &adapter->ip_offload_ctrl,
3346 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3347
3348 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3349 dev_err(dev, "Couldn't map ip offload control buffer\n");
3350 return;
3351 }
3352
3353 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3354 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3355 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3356 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3357 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
Thomas Falconfdb06102017-10-17 12:36:55 -05003358 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3359 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003360
Thomas Falconfdb06102017-10-17 12:36:55 -05003361 /* large_rx disabled for now, additional features needed */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003362 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3363 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3364
Thomas Falcon15482052017-10-17 12:36:54 -05003365 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003366
3367 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3368 adapter->netdev->features |= NETIF_F_IP_CSUM;
3369
3370 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3371 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3372
Thomas Falcon9be02cd2016-04-01 17:20:35 -05003373 if ((adapter->netdev->features &
3374 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3375 adapter->netdev->features |= NETIF_F_RXCSUM;
3376
Thomas Falconfdb06102017-10-17 12:36:55 -05003377 if (buf->large_tx_ipv4)
3378 adapter->netdev->features |= NETIF_F_TSO;
3379 if (buf->large_tx_ipv6)
3380 adapter->netdev->features |= NETIF_F_TSO6;
3381
Thomas Falconaa0bf852017-10-17 12:36:56 -05003382 adapter->netdev->hw_features |= adapter->netdev->features;
3383
Thomas Falcon032c5e82015-12-21 11:26:06 -06003384 memset(&crq, 0, sizeof(crq));
3385 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3386 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3387 crq.control_ip_offload.len =
3388 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3389 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3390 ibmvnic_send_crq(adapter, &crq);
3391}
3392
3393static void handle_error_info_rsp(union ibmvnic_crq *crq,
3394 struct ibmvnic_adapter *adapter)
3395{
3396 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08003397 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003398 unsigned long flags;
3399 bool found = false;
3400 int i;
3401
3402 if (!crq->request_error_rsp.rc.code) {
3403 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3404 crq->request_error_rsp.rc.code);
3405 return;
3406 }
3407
3408 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08003409 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003410 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3411 found = true;
3412 list_del(&error_buff->list);
3413 break;
3414 }
3415 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3416
3417 if (!found) {
3418 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06003419 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003420 return;
3421 }
3422
3423 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06003424 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003425
3426 for (i = 0; i < error_buff->len; i++) {
3427 pr_cont("%02x", (int)error_buff->buff[i]);
3428 if (i % 8 == 7)
3429 pr_cont(" ");
3430 }
3431 pr_cont("\n");
3432
3433 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3434 DMA_FROM_DEVICE);
3435 kfree(error_buff->buff);
3436 kfree(error_buff);
3437}
3438
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003439static void request_error_information(struct ibmvnic_adapter *adapter,
3440 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003441{
Thomas Falcon032c5e82015-12-21 11:26:06 -06003442 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003443 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003444 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003445 unsigned long timeout = msecs_to_jiffies(30000);
3446 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003448 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003449
3450 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3451 if (!error_buff)
3452 return;
3453
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003454 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003455 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3456 if (!error_buff->buff) {
3457 kfree(error_buff);
3458 return;
3459 }
3460
3461 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3462 DMA_FROM_DEVICE);
3463 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003464 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06003465 kfree(error_buff->buff);
3466 kfree(error_buff);
3467 return;
3468 }
3469
Thomas Falcon032c5e82015-12-21 11:26:06 -06003470 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003471 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003472
3473 spin_lock_irqsave(&adapter->error_list_lock, flags);
3474 list_add_tail(&error_buff->list, &adapter->errors);
3475 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3476
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04003477 memset(&crq, 0, sizeof(crq));
3478 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3479 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3480 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3481 crq.request_error_info.len = cpu_to_be32(detail_len);
3482 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3483
3484 rc = ibmvnic_send_crq(adapter, &crq);
3485 if (rc) {
3486 netdev_err(netdev, "failed to request error information\n");
3487 goto err_info_fail;
3488 }
3489
3490 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3491 netdev_err(netdev, "timeout waiting for error information\n");
3492 goto err_info_fail;
3493 }
3494
3495 return;
3496
3497err_info_fail:
3498 spin_lock_irqsave(&adapter->error_list_lock, flags);
3499 list_del(&error_buff->list);
3500 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3501
3502 kfree(error_buff->buff);
3503 kfree(error_buff);
3504}
3505
3506static void handle_error_indication(union ibmvnic_crq *crq,
3507 struct ibmvnic_adapter *adapter)
3508{
3509 struct device *dev = &adapter->vdev->dev;
3510
3511 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3512 crq->error_indication.flags
3513 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3514 be32_to_cpu(crq->error_indication.error_id),
3515 be16_to_cpu(crq->error_indication.error_cause));
3516
3517 if (be32_to_cpu(crq->error_indication.error_id))
3518 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003519
3520 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3521 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04003522 else
3523 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003524}
3525
3526static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3527 struct ibmvnic_adapter *adapter)
3528{
3529 struct net_device *netdev = adapter->netdev;
3530 struct device *dev = &adapter->vdev->dev;
3531 long rc;
3532
3533 rc = crq->change_mac_addr_rsp.rc.code;
3534 if (rc) {
3535 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3536 return;
3537 }
3538 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3539 ETH_ALEN);
3540}
3541
3542static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3543 struct ibmvnic_adapter *adapter)
3544{
3545 struct device *dev = &adapter->vdev->dev;
3546 u64 *req_value;
3547 char *name;
3548
Thomas Falcon901e0402017-02-15 12:17:59 -06003549 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003550 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3551 case REQ_TX_QUEUES:
3552 req_value = &adapter->req_tx_queues;
3553 name = "tx";
3554 break;
3555 case REQ_RX_QUEUES:
3556 req_value = &adapter->req_rx_queues;
3557 name = "rx";
3558 break;
3559 case REQ_RX_ADD_QUEUES:
3560 req_value = &adapter->req_rx_add_queues;
3561 name = "rx_add";
3562 break;
3563 case REQ_TX_ENTRIES_PER_SUBCRQ:
3564 req_value = &adapter->req_tx_entries_per_subcrq;
3565 name = "tx_entries_per_subcrq";
3566 break;
3567 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3568 req_value = &adapter->req_rx_add_entries_per_subcrq;
3569 name = "rx_add_entries_per_subcrq";
3570 break;
3571 case REQ_MTU:
3572 req_value = &adapter->req_mtu;
3573 name = "mtu";
3574 break;
3575 case PROMISC_REQUESTED:
3576 req_value = &adapter->promisc;
3577 name = "promisc";
3578 break;
3579 default:
3580 dev_err(dev, "Got invalid cap request rsp %d\n",
3581 crq->request_capability.capability);
3582 return;
3583 }
3584
3585 switch (crq->request_capability_rsp.rc.code) {
3586 case SUCCESS:
3587 break;
3588 case PARTIALSUCCESS:
3589 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3590 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06003591 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06003592 number), name);
Thomas Falcon28f4d162017-02-15 10:32:11 -06003593 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003594 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003595 return;
3596 default:
3597 dev_err(dev, "Error %d in request cap rsp\n",
3598 crq->request_capability_rsp.rc.code);
3599 return;
3600 }
3601
3602 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003603 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003604 union ibmvnic_crq newcrq;
3605 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3606 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3607 &adapter->ip_offload_buf;
3608
Thomas Falcon249168a2017-02-15 12:18:00 -06003609 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003610 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3611 buf_sz,
3612 DMA_FROM_DEVICE);
3613
3614 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3615 if (!firmware_has_feature(FW_FEATURE_CMO))
3616 dev_err(dev, "Couldn't map offload buffer\n");
3617 return;
3618 }
3619
3620 memset(&newcrq, 0, sizeof(newcrq));
3621 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3622 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3623 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3624 newcrq.query_ip_offload.ioba =
3625 cpu_to_be32(adapter->ip_offload_tok);
3626
3627 ibmvnic_send_crq(adapter, &newcrq);
3628 }
3629}
3630
3631static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3632 struct ibmvnic_adapter *adapter)
3633{
3634 struct device *dev = &adapter->vdev->dev;
John Allenc26eba02017-10-26 16:23:25 -05003635 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003636 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3637 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003638 int i;
3639
3640 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3641 DMA_BIDIRECTIONAL);
3642 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3643 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3644
John Allen498cd8e2016-04-06 11:49:55 -05003645 /* If the number of queues requested can't be allocated by the
3646 * server, the login response will return with code 1. We will need
3647 * to resend the login buffer with fewer queues requested.
3648 */
3649 if (login_rsp_crq->generic.rc.code) {
3650 adapter->renegotiate = true;
3651 complete(&adapter->init_done);
3652 return 0;
3653 }
3654
John Allenc26eba02017-10-26 16:23:25 -05003655 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3656
Thomas Falcon032c5e82015-12-21 11:26:06 -06003657 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3658 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3659 netdev_dbg(adapter->netdev, "%016lx\n",
3660 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3661 }
3662
3663 /* Sanity checks */
3664 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3665 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3666 adapter->req_rx_add_queues !=
3667 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3668 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3669 ibmvnic_remove(adapter->vdev);
3670 return -EIO;
3671 }
3672 complete(&adapter->init_done);
3673
Thomas Falcon032c5e82015-12-21 11:26:06 -06003674 return 0;
3675}
3676
Thomas Falcon032c5e82015-12-21 11:26:06 -06003677static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3678 struct ibmvnic_adapter *adapter)
3679{
3680 struct device *dev = &adapter->vdev->dev;
3681 long rc;
3682
3683 rc = crq->request_unmap_rsp.rc.code;
3684 if (rc)
3685 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3686}
3687
3688static void handle_query_map_rsp(union ibmvnic_crq *crq,
3689 struct ibmvnic_adapter *adapter)
3690{
3691 struct net_device *netdev = adapter->netdev;
3692 struct device *dev = &adapter->vdev->dev;
3693 long rc;
3694
3695 rc = crq->query_map_rsp.rc.code;
3696 if (rc) {
3697 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3698 return;
3699 }
3700 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3701 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3702 crq->query_map_rsp.free_pages);
3703}
3704
3705static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3706 struct ibmvnic_adapter *adapter)
3707{
3708 struct net_device *netdev = adapter->netdev;
3709 struct device *dev = &adapter->vdev->dev;
3710 long rc;
3711
Thomas Falcon901e0402017-02-15 12:17:59 -06003712 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003713 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003714 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003715 rc = crq->query_capability.rc.code;
3716 if (rc) {
3717 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3718 goto out;
3719 }
3720
3721 switch (be16_to_cpu(crq->query_capability.capability)) {
3722 case MIN_TX_QUEUES:
3723 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003724 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003725 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3726 adapter->min_tx_queues);
3727 break;
3728 case MIN_RX_QUEUES:
3729 adapter->min_rx_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_rx_queues = %lld\n",
3732 adapter->min_rx_queues);
3733 break;
3734 case MIN_RX_ADD_QUEUES:
3735 adapter->min_rx_add_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_add_queues = %lld\n",
3738 adapter->min_rx_add_queues);
3739 break;
3740 case MAX_TX_QUEUES:
3741 adapter->max_tx_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, "max_tx_queues = %lld\n",
3744 adapter->max_tx_queues);
3745 break;
3746 case MAX_RX_QUEUES:
3747 adapter->max_rx_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_rx_queues = %lld\n",
3750 adapter->max_rx_queues);
3751 break;
3752 case MAX_RX_ADD_QUEUES:
3753 adapter->max_rx_add_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_add_queues = %lld\n",
3756 adapter->max_rx_add_queues);
3757 break;
3758 case MIN_TX_ENTRIES_PER_SUBCRQ:
3759 adapter->min_tx_entries_per_subcrq =
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, "min_tx_entries_per_subcrq = %lld\n",
3762 adapter->min_tx_entries_per_subcrq);
3763 break;
3764 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3765 adapter->min_rx_add_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_rx_add_entrs_per_subcrq = %lld\n",
3768 adapter->min_rx_add_entries_per_subcrq);
3769 break;
3770 case MAX_TX_ENTRIES_PER_SUBCRQ:
3771 adapter->max_tx_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, "max_tx_entries_per_subcrq = %lld\n",
3774 adapter->max_tx_entries_per_subcrq);
3775 break;
3776 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3777 adapter->max_rx_add_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_rx_add_entrs_per_subcrq = %lld\n",
3780 adapter->max_rx_add_entries_per_subcrq);
3781 break;
3782 case TCP_IP_OFFLOAD:
3783 adapter->tcp_ip_offload =
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, "tcp_ip_offload = %lld\n",
3786 adapter->tcp_ip_offload);
3787 break;
3788 case PROMISC_SUPPORTED:
3789 adapter->promisc_supported =
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, "promisc_supported = %lld\n",
3792 adapter->promisc_supported);
3793 break;
3794 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003795 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003796 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003797 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3798 break;
3799 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003800 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003801 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003802 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3803 break;
3804 case MAX_MULTICAST_FILTERS:
3805 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003806 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003807 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3808 adapter->max_multicast_filters);
3809 break;
3810 case VLAN_HEADER_INSERTION:
3811 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003812 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003813 if (adapter->vlan_header_insertion)
3814 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3815 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3816 adapter->vlan_header_insertion);
3817 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003818 case RX_VLAN_HEADER_INSERTION:
3819 adapter->rx_vlan_header_insertion =
3820 be64_to_cpu(crq->query_capability.number);
3821 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3822 adapter->rx_vlan_header_insertion);
3823 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003824 case MAX_TX_SG_ENTRIES:
3825 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003826 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003827 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3828 adapter->max_tx_sg_entries);
3829 break;
3830 case RX_SG_SUPPORTED:
3831 adapter->rx_sg_supported =
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, "rx_sg_supported = %lld\n",
3834 adapter->rx_sg_supported);
3835 break;
3836 case OPT_TX_COMP_SUB_QUEUES:
3837 adapter->opt_tx_comp_sub_queues =
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, "opt_tx_comp_sub_queues = %lld\n",
3840 adapter->opt_tx_comp_sub_queues);
3841 break;
3842 case OPT_RX_COMP_QUEUES:
3843 adapter->opt_rx_comp_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_rx_comp_queues = %lld\n",
3846 adapter->opt_rx_comp_queues);
3847 break;
3848 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3849 adapter->opt_rx_bufadd_q_per_rx_comp_q =
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_bufadd_q_per_rx_comp_q = %lld\n",
3852 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3853 break;
3854 case OPT_TX_ENTRIES_PER_SUBCRQ:
3855 adapter->opt_tx_entries_per_subcrq =
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_tx_entries_per_subcrq = %lld\n",
3858 adapter->opt_tx_entries_per_subcrq);
3859 break;
3860 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3861 adapter->opt_rxba_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_rxba_entries_per_subcrq = %lld\n",
3864 adapter->opt_rxba_entries_per_subcrq);
3865 break;
3866 case TX_RX_DESC_REQ:
3867 adapter->tx_rx_desc_req = crq->query_capability.number;
3868 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3869 adapter->tx_rx_desc_req);
3870 break;
3871
3872 default:
3873 netdev_err(netdev, "Got invalid cap rsp %d\n",
3874 crq->query_capability.capability);
3875 }
3876
3877out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003878 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3879 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003880 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003881 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003882}
3883
Thomas Falcon032c5e82015-12-21 11:26:06 -06003884static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3885 struct ibmvnic_adapter *adapter)
3886{
3887 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3888 struct net_device *netdev = adapter->netdev;
3889 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003890 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003891 long rc;
3892
3893 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003894 (unsigned long int)cpu_to_be64(u64_crq[0]),
3895 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003896 switch (gen_crq->first) {
3897 case IBMVNIC_CRQ_INIT_RSP:
3898 switch (gen_crq->cmd) {
3899 case IBMVNIC_CRQ_INIT:
3900 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003901 adapter->from_passive_init = true;
3902 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003903 break;
3904 case IBMVNIC_CRQ_INIT_COMPLETE:
3905 dev_info(dev, "Partner initialization complete\n");
3906 send_version_xchg(adapter);
3907 break;
3908 default:
3909 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3910 }
3911 return;
3912 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003913 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003914 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003915 dev_info(dev, "Migrated, re-enabling adapter\n");
3916 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003917 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3918 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003919 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003920 } else {
3921 /* The adapter lost the connection */
3922 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3923 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003924 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003925 }
3926 return;
3927 case IBMVNIC_CRQ_CMD_RSP:
3928 break;
3929 default:
3930 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3931 gen_crq->first);
3932 return;
3933 }
3934
3935 switch (gen_crq->cmd) {
3936 case VERSION_EXCHANGE_RSP:
3937 rc = crq->version_exchange_rsp.rc.code;
3938 if (rc) {
3939 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3940 break;
3941 }
3942 dev_info(dev, "Partner protocol version is %d\n",
3943 crq->version_exchange_rsp.version);
3944 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3945 ibmvnic_version)
3946 ibmvnic_version =
3947 be16_to_cpu(crq->version_exchange_rsp.version);
3948 send_cap_queries(adapter);
3949 break;
3950 case QUERY_CAPABILITY_RSP:
3951 handle_query_cap_rsp(crq, adapter);
3952 break;
3953 case QUERY_MAP_RSP:
3954 handle_query_map_rsp(crq, adapter);
3955 break;
3956 case REQUEST_MAP_RSP:
Thomas Falconf3be0cb2017-06-21 14:53:01 -05003957 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3958 complete(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003959 break;
3960 case REQUEST_UNMAP_RSP:
3961 handle_request_unmap_rsp(crq, adapter);
3962 break;
3963 case REQUEST_CAPABILITY_RSP:
3964 handle_request_cap_rsp(crq, adapter);
3965 break;
3966 case LOGIN_RSP:
3967 netdev_dbg(netdev, "Got Login Response\n");
3968 handle_login_rsp(crq, adapter);
3969 break;
3970 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003971 netdev_dbg(netdev,
3972 "Got Logical Link State Response, state: %d rc: %d\n",
3973 crq->logical_link_state_rsp.link_state,
3974 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003975 adapter->logical_link_state =
3976 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003977 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3978 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003979 break;
3980 case LINK_STATE_INDICATION:
3981 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3982 adapter->phys_link_state =
3983 crq->link_state_indication.phys_link_state;
3984 adapter->logical_link_state =
3985 crq->link_state_indication.logical_link_state;
3986 break;
3987 case CHANGE_MAC_ADDR_RSP:
3988 netdev_dbg(netdev, "Got MAC address change Response\n");
3989 handle_change_mac_rsp(crq, adapter);
3990 break;
3991 case ERROR_INDICATION:
3992 netdev_dbg(netdev, "Got Error Indication\n");
3993 handle_error_indication(crq, adapter);
3994 break;
3995 case REQUEST_ERROR_RSP:
3996 netdev_dbg(netdev, "Got Error Detail Response\n");
3997 handle_error_info_rsp(crq, adapter);
3998 break;
3999 case REQUEST_STATISTICS_RSP:
4000 netdev_dbg(netdev, "Got Statistics Response\n");
4001 complete(&adapter->stats_done);
4002 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004003 case QUERY_IP_OFFLOAD_RSP:
4004 netdev_dbg(netdev, "Got Query IP offload Response\n");
4005 handle_query_ip_offload_rsp(adapter);
4006 break;
4007 case MULTICAST_CTRL_RSP:
4008 netdev_dbg(netdev, "Got multicast control Response\n");
4009 break;
4010 case CONTROL_IP_OFFLOAD_RSP:
4011 netdev_dbg(netdev, "Got Control IP offload Response\n");
4012 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4013 sizeof(adapter->ip_offload_ctrl),
4014 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05004015 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004016 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004017 case COLLECT_FW_TRACE_RSP:
4018 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4019 complete(&adapter->fw_done);
4020 break;
Desnes Augusto Nunes do Rosario4e6759b2017-11-13 15:59:19 -02004021 case GET_VPD_SIZE_RSP:
4022 handle_vpd_size_rsp(crq, adapter);
4023 break;
4024 case GET_VPD_RSP:
4025 handle_vpd_rsp(crq, adapter);
4026 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004027 default:
4028 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4029 gen_crq->cmd);
4030 }
4031}
4032
4033static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4034{
4035 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06004036
Thomas Falcon6c267b32017-02-15 12:17:58 -06004037 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004038 return IRQ_HANDLED;
4039}
4040
4041static void ibmvnic_tasklet(void *data)
4042{
4043 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004044 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004045 union ibmvnic_crq *crq;
4046 unsigned long flags;
4047 bool done = false;
4048
4049 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004050 while (!done) {
4051 /* Pull all the valid messages off the CRQ */
4052 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4053 ibmvnic_handle_crq(crq, adapter);
4054 crq->generic.first = 0;
4055 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04004056
4057 /* remain in tasklet until all
4058 * capabilities responses are received
4059 */
4060 if (!adapter->wait_capability)
4061 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004062 }
Thomas Falcon249168a2017-02-15 12:18:00 -06004063 /* if capabilities CRQ's were sent in this tasklet, the following
4064 * tasklet must wait until all responses are received
4065 */
4066 if (atomic_read(&adapter->running_cap_crqs) != 0)
4067 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004068 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004069}
4070
4071static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4072{
4073 struct vio_dev *vdev = adapter->vdev;
4074 int rc;
4075
4076 do {
4077 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4078 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4079
4080 if (rc)
4081 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4082
4083 return rc;
4084}
4085
4086static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4087{
4088 struct ibmvnic_crq_queue *crq = &adapter->crq;
4089 struct device *dev = &adapter->vdev->dev;
4090 struct vio_dev *vdev = adapter->vdev;
4091 int rc;
4092
4093 /* Close the CRQ */
4094 do {
4095 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4096 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4097
4098 /* Clean out the queue */
4099 memset(crq->msgs, 0, PAGE_SIZE);
4100 crq->cur = 0;
4101
4102 /* And re-open it again */
4103 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4104 crq->msg_token, PAGE_SIZE);
4105
4106 if (rc == H_CLOSED)
4107 /* Adapter is good, but other end is not ready */
4108 dev_warn(dev, "Partner adapter not ready\n");
4109 else if (rc != 0)
4110 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4111
4112 return rc;
4113}
4114
Nathan Fontenotf9928872017-03-30 02:48:54 -04004115static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004116{
4117 struct ibmvnic_crq_queue *crq = &adapter->crq;
4118 struct vio_dev *vdev = adapter->vdev;
4119 long rc;
4120
Nathan Fontenotf9928872017-03-30 02:48:54 -04004121 if (!crq->msgs)
4122 return;
4123
Thomas Falcon032c5e82015-12-21 11:26:06 -06004124 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4125 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06004126 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004127 do {
4128 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4129 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4130
4131 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4132 DMA_BIDIRECTIONAL);
4133 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004134 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004135}
4136
Nathan Fontenotf9928872017-03-30 02:48:54 -04004137static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06004138{
4139 struct ibmvnic_crq_queue *crq = &adapter->crq;
4140 struct device *dev = &adapter->vdev->dev;
4141 struct vio_dev *vdev = adapter->vdev;
4142 int rc, retrc = -ENOMEM;
4143
Nathan Fontenotf9928872017-03-30 02:48:54 -04004144 if (crq->msgs)
4145 return 0;
4146
Thomas Falcon032c5e82015-12-21 11:26:06 -06004147 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4148 /* Should we allocate more than one page? */
4149
4150 if (!crq->msgs)
4151 return -ENOMEM;
4152
4153 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4154 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4155 DMA_BIDIRECTIONAL);
4156 if (dma_mapping_error(dev, crq->msg_token))
4157 goto map_failed;
4158
4159 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4160 crq->msg_token, PAGE_SIZE);
4161
4162 if (rc == H_RESOURCE)
4163 /* maybe kexecing and resource is busy. try a reset */
4164 rc = ibmvnic_reset_crq(adapter);
4165 retrc = rc;
4166
4167 if (rc == H_CLOSED) {
4168 dev_warn(dev, "Partner adapter not ready\n");
4169 } else if (rc) {
4170 dev_warn(dev, "Error %d opening adapter\n", rc);
4171 goto reg_crq_failed;
4172 }
4173
4174 retrc = 0;
4175
Thomas Falcon6c267b32017-02-15 12:17:58 -06004176 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4177 (unsigned long)adapter);
4178
Thomas Falcon032c5e82015-12-21 11:26:06 -06004179 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4180 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4181 adapter);
4182 if (rc) {
4183 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4184 vdev->irq, rc);
4185 goto req_irq_failed;
4186 }
4187
4188 rc = vio_enable_interrupts(vdev);
4189 if (rc) {
4190 dev_err(dev, "Error %d enabling interrupts\n", rc);
4191 goto req_irq_failed;
4192 }
4193
4194 crq->cur = 0;
4195 spin_lock_init(&crq->lock);
4196
4197 return retrc;
4198
4199req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06004200 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004201 do {
4202 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4203 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4204reg_crq_failed:
4205 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4206map_failed:
4207 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04004208 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004209 return retrc;
4210}
4211
John Allenf6ef6402017-03-17 17:13:42 -05004212static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4213{
4214 struct device *dev = &adapter->vdev->dev;
4215 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05004216 int rc;
4217
John Allenc26eba02017-10-26 16:23:25 -05004218 if (adapter->resetting && !adapter->wait_for_reset) {
Nathan Fontenot28cde752017-05-26 10:31:00 -04004219 rc = ibmvnic_reset_crq(adapter);
4220 if (!rc)
4221 rc = vio_enable_interrupts(adapter->vdev);
4222 } else {
4223 rc = init_crq_queue(adapter);
4224 }
4225
John Allenf6ef6402017-03-17 17:13:42 -05004226 if (rc) {
4227 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4228 return rc;
4229 }
4230
John Allen017892c12017-05-26 10:30:19 -04004231 adapter->from_passive_init = false;
4232
John Allenf6ef6402017-03-17 17:13:42 -05004233 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004234 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05004235 ibmvnic_send_crq_init(adapter);
4236 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4237 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04004238 return -1;
4239 }
4240
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004241 if (adapter->init_done_rc) {
4242 release_crq_queue(adapter);
4243 return adapter->init_done_rc;
4244 }
4245
John Allen017892c12017-05-26 10:30:19 -04004246 if (adapter->from_passive_init) {
4247 adapter->state = VNIC_OPEN;
4248 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05004249 return -1;
4250 }
4251
John Allenc26eba02017-10-26 16:23:25 -05004252 if (adapter->resetting && !adapter->wait_for_reset)
Nathan Fontenot57a49432017-05-26 10:31:12 -04004253 rc = reset_sub_crq_queues(adapter);
4254 else
4255 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004256 if (rc) {
4257 dev_err(dev, "Initialization of sub crqs failed\n");
4258 release_crq_queue(adapter);
Thomas Falcon5df969c2017-06-28 19:55:54 -05004259 return rc;
4260 }
4261
4262 rc = init_sub_crq_irqs(adapter);
4263 if (rc) {
4264 dev_err(dev, "Failed to initialize sub crq irqs\n");
4265 release_crq_queue(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04004266 }
4267
4268 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05004269}
4270
Thomas Falcon40c9db82017-06-12 12:35:04 -05004271static struct device_attribute dev_attr_failover;
4272
Thomas Falcon032c5e82015-12-21 11:26:06 -06004273static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4274{
4275 struct ibmvnic_adapter *adapter;
4276 struct net_device *netdev;
4277 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004278 int rc;
4279
4280 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4281 dev->unit_address);
4282
4283 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4284 VETH_MAC_ADDR, NULL);
4285 if (!mac_addr_p) {
4286 dev_err(&dev->dev,
4287 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4288 __FILE__, __LINE__);
4289 return 0;
4290 }
4291
4292 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
Thomas Falcond45cc3a2017-12-18 12:52:11 -06004293 IBMVNIC_MAX_QUEUES);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004294 if (!netdev)
4295 return -ENOMEM;
4296
4297 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04004298 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004299 dev_set_drvdata(&dev->dev, netdev);
4300 adapter->vdev = dev;
4301 adapter->netdev = netdev;
4302
4303 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4304 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4305 netdev->irq = dev->irq;
4306 netdev->netdev_ops = &ibmvnic_netdev_ops;
4307 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4308 SET_NETDEV_DEV(netdev, &dev->dev);
4309
4310 spin_lock_init(&adapter->stats_lock);
4311
Thomas Falcon032c5e82015-12-21 11:26:06 -06004312 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004313 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004314
Nathan Fontenoted651a12017-05-03 14:04:38 -04004315 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4316 INIT_LIST_HEAD(&adapter->rwi_list);
4317 mutex_init(&adapter->reset_lock);
4318 mutex_init(&adapter->rwi_lock);
4319 adapter->resetting = false;
4320
John Allenc26eba02017-10-26 16:23:25 -05004321 adapter->mac_change_pending = false;
4322
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004323 do {
4324 rc = ibmvnic_init(adapter);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004325 if (rc && rc != EAGAIN)
4326 goto ibmvnic_init_fail;
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04004327 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004328
Thomas Falconf39f0d12017-02-14 10:22:59 -06004329 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenc26eba02017-10-26 16:23:25 -05004330 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4331 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004332
Thomas Falcon40c9db82017-06-12 12:35:04 -05004333 rc = device_create_file(&dev->dev, &dev_attr_failover);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004334 if (rc)
4335 goto ibmvnic_init_fail;
Thomas Falcon40c9db82017-06-12 12:35:04 -05004336
Mick Tarsele876a8a2017-09-28 13:53:18 -07004337 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004338 rc = register_netdev(netdev);
4339 if (rc) {
4340 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004341 goto ibmvnic_register_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004342 }
4343 dev_info(&dev->dev, "ibmvnic registered\n");
4344
Nathan Fontenot90c80142017-05-03 14:04:32 -04004345 adapter->state = VNIC_PROBED;
John Allenc26eba02017-10-26 16:23:25 -05004346
4347 adapter->wait_for_reset = false;
4348
Thomas Falcon032c5e82015-12-21 11:26:06 -06004349 return 0;
Nathan Fontenot7c1885a2017-08-08 14:28:45 -05004350
4351ibmvnic_register_fail:
4352 device_remove_file(&dev->dev, &dev_attr_failover);
4353
4354ibmvnic_init_fail:
4355 release_sub_crqs(adapter);
4356 release_crq_queue(adapter);
4357 free_netdev(netdev);
4358
4359 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004360}
4361
4362static int ibmvnic_remove(struct vio_dev *dev)
4363{
4364 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04004365 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004366
Nathan Fontenot90c80142017-05-03 14:04:32 -04004367 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06004368 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04004369 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04004370
4371 release_resources(adapter);
4372 release_sub_crqs(adapter);
4373 release_crq_queue(adapter);
4374
Nathan Fontenot90c80142017-05-03 14:04:32 -04004375 adapter->state = VNIC_REMOVED;
4376
Nathan Fontenoted651a12017-05-03 14:04:38 -04004377 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05004378 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004379 free_netdev(netdev);
4380 dev_set_drvdata(&dev->dev, NULL);
4381
4382 return 0;
4383}
4384
Thomas Falcon40c9db82017-06-12 12:35:04 -05004385static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4386 const char *buf, size_t count)
4387{
4388 struct net_device *netdev = dev_get_drvdata(dev);
4389 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4390 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4391 __be64 session_token;
4392 long rc;
4393
4394 if (!sysfs_streq(buf, "1"))
4395 return -EINVAL;
4396
4397 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4398 H_GET_SESSION_TOKEN, 0, 0, 0);
4399 if (rc) {
4400 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4401 rc);
4402 return -EINVAL;
4403 }
4404
4405 session_token = (__be64)retbuf[0];
4406 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4407 be64_to_cpu(session_token));
4408 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4409 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4410 if (rc) {
4411 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4412 rc);
4413 return -EINVAL;
4414 }
4415
4416 return count;
4417}
4418
4419static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4420
Thomas Falcon032c5e82015-12-21 11:26:06 -06004421static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4422{
4423 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4424 struct ibmvnic_adapter *adapter;
4425 struct iommu_table *tbl;
4426 unsigned long ret = 0;
4427 int i;
4428
4429 tbl = get_iommu_table_base(&vdev->dev);
4430
4431 /* netdev inits at probe time along with the structures we need below*/
4432 if (!netdev)
4433 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4434
4435 adapter = netdev_priv(netdev);
4436
4437 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06004438 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4439
4440 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4441 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4442
4443 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4444 i++)
4445 ret += adapter->rx_pool[i].size *
4446 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4447
4448 return ret;
4449}
4450
4451static int ibmvnic_resume(struct device *dev)
4452{
4453 struct net_device *netdev = dev_get_drvdata(dev);
4454 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004455
John Allencb89ba22017-06-19 11:27:53 -05004456 if (adapter->state != VNIC_OPEN)
4457 return 0;
4458
John Allena2488782017-07-24 13:26:06 -05004459 tasklet_schedule(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06004460
4461 return 0;
4462}
4463
Arvind Yadav8c37bc62017-08-17 18:52:54 +05304464static const struct vio_device_id ibmvnic_device_table[] = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06004465 {"network", "IBM,vnic"},
4466 {"", "" }
4467};
4468MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4469
4470static const struct dev_pm_ops ibmvnic_pm_ops = {
4471 .resume = ibmvnic_resume
4472};
4473
4474static struct vio_driver ibmvnic_driver = {
4475 .id_table = ibmvnic_device_table,
4476 .probe = ibmvnic_probe,
4477 .remove = ibmvnic_remove,
4478 .get_desired_dma = ibmvnic_get_desired_dma,
4479 .name = ibmvnic_driver_name,
4480 .pm = &ibmvnic_pm_ops,
4481};
4482
4483/* module functions */
4484static int __init ibmvnic_module_init(void)
4485{
4486 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4487 IBMVNIC_DRIVER_VERSION);
4488
4489 return vio_register_driver(&ibmvnic_driver);
4490}
4491
4492static void __exit ibmvnic_module_exit(void)
4493{
4494 vio_unregister_driver(&ibmvnic_driver);
4495}
4496
4497module_init(ibmvnic_module_init);
4498module_exit(ibmvnic_module_exit);