blob: 722daf55d75710c315c6361361d62a6268176b81 [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>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040077#include <linux/if_vlan.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060078
79#include "ibmvnic.h"
80
81static const char ibmvnic_driver_name[] = "ibmvnic";
82static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
83
Thomas Falcon78b07ac2017-06-01 15:32:34 -050084MODULE_AUTHOR("Santiago Leon");
Thomas Falcon032c5e82015-12-21 11:26:06 -060085MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
92static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400166static void reset_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb)
168{
169 memset(ltb->buff, 0, ltb->size);
170
171 init_completion(&adapter->fw_done);
172 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
173 wait_for_completion(&adapter->fw_done);
174}
175
Thomas Falcon032c5e82015-12-21 11:26:06 -0600176static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
177 struct ibmvnic_long_term_buff *ltb, int size)
178{
179 struct device *dev = &adapter->vdev->dev;
180
181 ltb->size = size;
182 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
183 GFP_KERNEL);
184
185 if (!ltb->buff) {
186 dev_err(dev, "Couldn't alloc long term buffer\n");
187 return -ENOMEM;
188 }
189 ltb->map_id = adapter->map_id;
190 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500191
192 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600193 send_request_map(adapter, ltb->addr,
194 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600195 wait_for_completion(&adapter->fw_done);
196 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 Falconf185a492017-05-26 10:30:48 -0400213static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
214{
215 int i;
216
217 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
218 i++)
219 adapter->rx_pool[i].active = 0;
220}
221
Thomas Falcon032c5e82015-12-21 11:26:06 -0600222static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
223 struct ibmvnic_rx_pool *pool)
224{
225 int count = pool->size - atomic_read(&pool->available);
226 struct device *dev = &adapter->vdev->dev;
227 int buffers_added = 0;
228 unsigned long lpar_rc;
229 union sub_crq sub_crq;
230 struct sk_buff *skb;
231 unsigned int offset;
232 dma_addr_t dma_addr;
233 unsigned char *dst;
234 u64 *handle_array;
235 int shift = 0;
236 int index;
237 int i;
238
Thomas Falconf185a492017-05-26 10:30:48 -0400239 if (!pool->active)
240 return;
241
Thomas Falcon032c5e82015-12-21 11:26:06 -0600242 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
243 be32_to_cpu(adapter->login_rsp_buf->
244 off_rxadd_subcrqs));
245
246 for (i = 0; i < count; ++i) {
247 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
248 if (!skb) {
249 dev_err(dev, "Couldn't replenish rx buff\n");
250 adapter->replenish_no_mem++;
251 break;
252 }
253
254 index = pool->free_map[pool->next_free];
255
256 if (pool->rx_buff[index].skb)
257 dev_err(dev, "Inconsistent free_map!\n");
258
259 /* Copy the skb to the long term mapped DMA buffer */
260 offset = index * pool->buff_size;
261 dst = pool->long_term_buff.buff + offset;
262 memset(dst, 0, pool->buff_size);
263 dma_addr = pool->long_term_buff.addr + offset;
264 pool->rx_buff[index].data = dst;
265
266 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
267 pool->rx_buff[index].dma = dma_addr;
268 pool->rx_buff[index].skb = skb;
269 pool->rx_buff[index].pool_index = pool->index;
270 pool->rx_buff[index].size = pool->buff_size;
271
272 memset(&sub_crq, 0, sizeof(sub_crq));
273 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
274 sub_crq.rx_add.correlator =
275 cpu_to_be64((u64)&pool->rx_buff[index]);
276 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
277 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
278
279 /* The length field of the sCRQ is defined to be 24 bits so the
280 * buffer size needs to be left shifted by a byte before it is
281 * converted to big endian to prevent the last byte from being
282 * truncated.
283 */
284#ifdef __LITTLE_ENDIAN__
285 shift = 8;
286#endif
287 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
288
289 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
290 &sub_crq);
291 if (lpar_rc != H_SUCCESS)
292 goto failure;
293
294 buffers_added++;
295 adapter->replenish_add_buff_success++;
296 pool->next_free = (pool->next_free + 1) % pool->size;
297 }
298 atomic_add(buffers_added, &pool->available);
299 return;
300
301failure:
302 dev_info(dev, "replenish pools failure\n");
303 pool->free_map[pool->next_free] = index;
304 pool->rx_buff[index].skb = NULL;
305 if (!dma_mapping_error(dev, dma_addr))
306 dma_unmap_single(dev, dma_addr, pool->buff_size,
307 DMA_FROM_DEVICE);
308
309 dev_kfree_skb_any(skb);
310 adapter->replenish_add_buff_failure++;
311 atomic_add(buffers_added, &pool->available);
Thomas Falconf185a492017-05-26 10:30:48 -0400312
313 if (lpar_rc == H_CLOSED) {
314 /* Disable buffer pool replenishment and report carrier off if
315 * queue is closed. Firmware guarantees that a signal will
316 * be sent to the driver, triggering a reset.
317 */
318 deactivate_rx_pools(adapter);
319 netif_carrier_off(adapter->netdev);
320 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600321}
322
323static void replenish_pools(struct ibmvnic_adapter *adapter)
324{
325 int i;
326
Thomas Falcon032c5e82015-12-21 11:26:06 -0600327 adapter->replenish_task_cycles++;
328 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
329 i++) {
330 if (adapter->rx_pool[i].active)
331 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
332 }
333}
334
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400335static void release_stats_token(struct ibmvnic_adapter *adapter)
336{
337 struct device *dev = &adapter->vdev->dev;
338
339 if (!adapter->stats_token)
340 return;
341
342 dma_unmap_single(dev, adapter->stats_token,
343 sizeof(struct ibmvnic_statistics),
344 DMA_FROM_DEVICE);
345 adapter->stats_token = 0;
346}
347
348static int init_stats_token(struct ibmvnic_adapter *adapter)
349{
350 struct device *dev = &adapter->vdev->dev;
351 dma_addr_t stok;
352
353 stok = dma_map_single(dev, &adapter->stats,
354 sizeof(struct ibmvnic_statistics),
355 DMA_FROM_DEVICE);
356 if (dma_mapping_error(dev, stok)) {
357 dev_err(dev, "Couldn't map stats buffer\n");
358 return -1;
359 }
360
361 adapter->stats_token = stok;
362 return 0;
363}
364
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400365static int reset_rx_pools(struct ibmvnic_adapter *adapter)
366{
367 struct ibmvnic_rx_pool *rx_pool;
368 int rx_scrqs;
369 int i, j;
370
371 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
372 for (i = 0; i < rx_scrqs; i++) {
373 rx_pool = &adapter->rx_pool[i];
374
375 reset_long_term_buff(adapter, &rx_pool->long_term_buff);
376
377 for (j = 0; j < rx_pool->size; j++)
378 rx_pool->free_map[j] = j;
379
380 memset(rx_pool->rx_buff, 0,
381 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
382
383 atomic_set(&rx_pool->available, 0);
384 rx_pool->next_alloc = 0;
385 rx_pool->next_free = 0;
Thomas Falconc3e53b92017-06-14 23:50:05 -0500386 rx_pool->active = 1;
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400387 }
388
389 return 0;
390}
391
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400392static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600393{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400394 struct ibmvnic_rx_pool *rx_pool;
395 int rx_scrqs;
396 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600397
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400398 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600399 return;
400
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400401 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
402 for (i = 0; i < rx_scrqs; i++) {
403 rx_pool = &adapter->rx_pool[i];
404
405 kfree(rx_pool->free_map);
406 free_long_term_buff(adapter, &rx_pool->long_term_buff);
407
408 if (!rx_pool->rx_buff)
Nathan Fontenote0ebe9422017-05-03 14:04:50 -0400409 continue;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400410
411 for (j = 0; j < rx_pool->size; j++) {
412 if (rx_pool->rx_buff[j].skb) {
413 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
414 rx_pool->rx_buff[i].skb = NULL;
415 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600416 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400417
418 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600419 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400420
421 kfree(adapter->rx_pool);
422 adapter->rx_pool = NULL;
423}
424
425static int init_rx_pools(struct net_device *netdev)
426{
427 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
428 struct device *dev = &adapter->vdev->dev;
429 struct ibmvnic_rx_pool *rx_pool;
430 int rxadd_subcrqs;
431 u64 *size_array;
432 int i, j;
433
434 rxadd_subcrqs =
435 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
436 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
437 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
438
439 adapter->rx_pool = kcalloc(rxadd_subcrqs,
440 sizeof(struct ibmvnic_rx_pool),
441 GFP_KERNEL);
442 if (!adapter->rx_pool) {
443 dev_err(dev, "Failed to allocate rx pools\n");
444 return -1;
445 }
446
447 for (i = 0; i < rxadd_subcrqs; i++) {
448 rx_pool = &adapter->rx_pool[i];
449
450 netdev_dbg(adapter->netdev,
451 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
452 i, adapter->req_rx_add_entries_per_subcrq,
453 be64_to_cpu(size_array[i]));
454
455 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
456 rx_pool->index = i;
457 rx_pool->buff_size = be64_to_cpu(size_array[i]);
458 rx_pool->active = 1;
459
460 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
461 GFP_KERNEL);
462 if (!rx_pool->free_map) {
463 release_rx_pools(adapter);
464 return -1;
465 }
466
467 rx_pool->rx_buff = kcalloc(rx_pool->size,
468 sizeof(struct ibmvnic_rx_buff),
469 GFP_KERNEL);
470 if (!rx_pool->rx_buff) {
471 dev_err(dev, "Couldn't alloc rx buffers\n");
472 release_rx_pools(adapter);
473 return -1;
474 }
475
476 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
477 rx_pool->size * rx_pool->buff_size)) {
478 release_rx_pools(adapter);
479 return -1;
480 }
481
482 for (j = 0; j < rx_pool->size; ++j)
483 rx_pool->free_map[j] = j;
484
485 atomic_set(&rx_pool->available, 0);
486 rx_pool->next_alloc = 0;
487 rx_pool->next_free = 0;
488 }
489
490 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600491}
492
Nathan Fontenot8c0543a2017-05-26 10:31:06 -0400493static int reset_tx_pools(struct ibmvnic_adapter *adapter)
494{
495 struct ibmvnic_tx_pool *tx_pool;
496 int tx_scrqs;
497 int i, j;
498
499 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
500 for (i = 0; i < tx_scrqs; i++) {
501 tx_pool = &adapter->tx_pool[i];
502
503 reset_long_term_buff(adapter, &tx_pool->long_term_buff);
504
505 memset(tx_pool->tx_buff, 0,
506 adapter->req_tx_entries_per_subcrq *
507 sizeof(struct ibmvnic_tx_buff));
508
509 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
510 tx_pool->free_map[j] = j;
511
512 tx_pool->consumer_index = 0;
513 tx_pool->producer_index = 0;
514 }
515
516 return 0;
517}
518
Nathan Fontenotc657e322017-03-30 02:49:06 -0400519static void release_tx_pools(struct ibmvnic_adapter *adapter)
520{
521 struct ibmvnic_tx_pool *tx_pool;
522 int i, tx_scrqs;
523
524 if (!adapter->tx_pool)
525 return;
526
527 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
528 for (i = 0; i < tx_scrqs; i++) {
529 tx_pool = &adapter->tx_pool[i];
530 kfree(tx_pool->tx_buff);
531 free_long_term_buff(adapter, &tx_pool->long_term_buff);
532 kfree(tx_pool->free_map);
533 }
534
535 kfree(adapter->tx_pool);
536 adapter->tx_pool = NULL;
537}
538
539static int init_tx_pools(struct net_device *netdev)
540{
541 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
542 struct device *dev = &adapter->vdev->dev;
543 struct ibmvnic_tx_pool *tx_pool;
544 int tx_subcrqs;
545 int i, j;
546
547 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
548 adapter->tx_pool = kcalloc(tx_subcrqs,
549 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
550 if (!adapter->tx_pool)
551 return -1;
552
553 for (i = 0; i < tx_subcrqs; i++) {
554 tx_pool = &adapter->tx_pool[i];
555 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
556 sizeof(struct ibmvnic_tx_buff),
557 GFP_KERNEL);
558 if (!tx_pool->tx_buff) {
559 dev_err(dev, "tx pool buffer allocation failed\n");
560 release_tx_pools(adapter);
561 return -1;
562 }
563
564 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
565 adapter->req_tx_entries_per_subcrq *
566 adapter->req_mtu)) {
567 release_tx_pools(adapter);
568 return -1;
569 }
570
571 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
572 sizeof(int), GFP_KERNEL);
573 if (!tx_pool->free_map) {
574 release_tx_pools(adapter);
575 return -1;
576 }
577
578 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
579 tx_pool->free_map[j] = j;
580
581 tx_pool->consumer_index = 0;
582 tx_pool->producer_index = 0;
583 }
584
585 return 0;
586}
587
Nathan Fontenot661a2622017-04-19 13:44:58 -0400588static void release_error_buffers(struct ibmvnic_adapter *adapter)
589{
590 struct device *dev = &adapter->vdev->dev;
591 struct ibmvnic_error_buff *error_buff, *tmp;
592 unsigned long flags;
593
594 spin_lock_irqsave(&adapter->error_list_lock, flags);
595 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
596 list_del(&error_buff->list);
597 dma_unmap_single(dev, error_buff->dma, error_buff->len,
598 DMA_FROM_DEVICE);
599 kfree(error_buff->buff);
600 kfree(error_buff);
601 }
602 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
603}
604
John Allend944c3d62017-05-26 10:30:13 -0400605static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
606{
607 int i;
608
609 if (adapter->napi_enabled)
610 return;
611
612 for (i = 0; i < adapter->req_rx_queues; i++)
613 napi_enable(&adapter->napi[i]);
614
615 adapter->napi_enabled = true;
616}
617
618static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
619{
620 int i;
621
622 if (!adapter->napi_enabled)
623 return;
624
625 for (i = 0; i < adapter->req_rx_queues; i++)
626 napi_disable(&adapter->napi[i]);
627
628 adapter->napi_enabled = false;
629}
630
John Allena57a5d22017-03-17 17:13:41 -0500631static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600632{
633 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500634 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600635 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600636
John Allenbd0b6722017-03-17 17:13:40 -0500637 do {
638 if (adapter->renegotiate) {
639 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400640 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500641
642 reinit_completion(&adapter->init_done);
643 send_cap_queries(adapter);
644 if (!wait_for_completion_timeout(&adapter->init_done,
645 timeout)) {
646 dev_err(dev, "Capabilities query timeout\n");
647 return -1;
648 }
649 }
650
651 reinit_completion(&adapter->init_done);
652 send_login(adapter);
653 if (!wait_for_completion_timeout(&adapter->init_done,
654 timeout)) {
655 dev_err(dev, "Login timeout\n");
656 return -1;
657 }
658 } while (adapter->renegotiate);
659
John Allena57a5d22017-03-17 17:13:41 -0500660 return 0;
661}
662
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400663static void release_resources(struct ibmvnic_adapter *adapter)
664{
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400665 int i;
666
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400667 release_tx_pools(adapter);
668 release_rx_pools(adapter);
669
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400670 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400671 release_error_buffers(adapter);
Nathan Fontenotc7bac002017-05-03 14:04:44 -0400672
673 if (adapter->napi) {
674 for (i = 0; i < adapter->req_rx_queues; i++) {
675 if (&adapter->napi[i])
676 netif_napi_del(&adapter->napi[i]);
677 }
678 }
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400679}
680
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400681static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
682{
683 struct net_device *netdev = adapter->netdev;
684 unsigned long timeout = msecs_to_jiffies(30000);
685 union ibmvnic_crq crq;
686 bool resend;
687 int rc;
688
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400689 netdev_err(netdev, "setting link state %d\n", link_state);
690 memset(&crq, 0, sizeof(crq));
691 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
692 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
693 crq.logical_link_state.link_state = link_state;
694
695 do {
696 resend = false;
697
698 reinit_completion(&adapter->init_done);
699 rc = ibmvnic_send_crq(adapter, &crq);
700 if (rc) {
701 netdev_err(netdev, "Failed to set link state\n");
702 return rc;
703 }
704
705 if (!wait_for_completion_timeout(&adapter->init_done,
706 timeout)) {
707 netdev_err(netdev, "timeout setting link state\n");
708 return -1;
709 }
710
711 if (adapter->init_done_rc == 1) {
712 /* Partuial success, delay and re-send */
713 mdelay(1000);
714 resend = true;
715 }
716 } while (resend);
717
718 return 0;
719}
720
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400721static int set_real_num_queues(struct net_device *netdev)
722{
723 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
724 int rc;
725
726 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
727 if (rc) {
728 netdev_err(netdev, "failed to set the number of tx queues\n");
729 return rc;
730 }
731
732 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
733 if (rc)
734 netdev_err(netdev, "failed to set the number of rx queues\n");
735
736 return rc;
737}
738
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400739static int init_resources(struct ibmvnic_adapter *adapter)
John Allena57a5d22017-03-17 17:13:41 -0500740{
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400741 struct net_device *netdev = adapter->netdev;
742 int i, rc;
John Allena57a5d22017-03-17 17:13:41 -0500743
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400744 rc = set_real_num_queues(netdev);
745 if (rc)
746 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500747
748 rc = init_sub_crq_irqs(adapter);
749 if (rc) {
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400750 netdev_err(netdev, "failed to initialize sub crq irqs\n");
John Allenbd0b6722017-03-17 17:13:40 -0500751 return -1;
752 }
753
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400754 rc = init_stats_token(adapter);
755 if (rc)
756 return rc;
757
Thomas Falcon032c5e82015-12-21 11:26:06 -0600758 adapter->map_id = 1;
759 adapter->napi = kcalloc(adapter->req_rx_queues,
760 sizeof(struct napi_struct), GFP_KERNEL);
761 if (!adapter->napi)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400762 return -ENOMEM;
763
Thomas Falcon032c5e82015-12-21 11:26:06 -0600764 for (i = 0; i < adapter->req_rx_queues; i++) {
765 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
766 NAPI_POLL_WEIGHT);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600767 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600768
Thomas Falcon032c5e82015-12-21 11:26:06 -0600769 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400770
771 rc = init_rx_pools(netdev);
772 if (rc)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400773 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600774
Nathan Fontenotc657e322017-03-30 02:49:06 -0400775 rc = init_tx_pools(netdev);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400776 return rc;
777}
778
Nathan Fontenoted651a12017-05-03 14:04:38 -0400779static int __ibmvnic_open(struct net_device *netdev)
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400780{
781 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400782 enum vnic_state prev_state = adapter->state;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400783 int i, rc;
784
Nathan Fontenot90c80142017-05-03 14:04:32 -0400785 adapter->state = VNIC_OPENING;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600786 replenish_pools(adapter);
John Allend944c3d62017-05-26 10:30:13 -0400787 ibmvnic_napi_enable(adapter);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400788
Thomas Falcon032c5e82015-12-21 11:26:06 -0600789 /* We're ready to receive frames, enable the sub-crq interrupts and
790 * set the logical link state to up
791 */
Nathan Fontenoted651a12017-05-03 14:04:38 -0400792 for (i = 0; i < adapter->req_rx_queues; i++) {
793 if (prev_state == VNIC_CLOSED)
794 enable_irq(adapter->rx_scrq[i]->irq);
795 else
796 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
797 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600798
Nathan Fontenoted651a12017-05-03 14:04:38 -0400799 for (i = 0; i < adapter->req_tx_queues; i++) {
800 if (prev_state == VNIC_CLOSED)
801 enable_irq(adapter->tx_scrq[i]->irq);
802 else
803 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
804 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600805
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400806 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400807 if (rc) {
808 for (i = 0; i < adapter->req_rx_queues; i++)
809 napi_disable(&adapter->napi[i]);
810 release_resources(adapter);
Nathan Fontenoted651a12017-05-03 14:04:38 -0400811 return rc;
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400812 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600813
Nathan Fontenoted651a12017-05-03 14:04:38 -0400814 netif_tx_start_all_queues(netdev);
815
816 if (prev_state == VNIC_CLOSED) {
817 for (i = 0; i < adapter->req_rx_queues; i++)
818 napi_schedule(&adapter->napi[i]);
819 }
820
821 adapter->state = VNIC_OPEN;
822 return rc;
823}
824
825static int ibmvnic_open(struct net_device *netdev)
826{
827 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
828 int rc;
829
830 mutex_lock(&adapter->reset_lock);
831
832 if (adapter->state != VNIC_CLOSED) {
833 rc = ibmvnic_login(netdev);
834 if (rc) {
835 mutex_unlock(&adapter->reset_lock);
836 return rc;
837 }
838
839 rc = init_resources(adapter);
840 if (rc) {
841 netdev_err(netdev, "failed to initialize resources\n");
842 release_resources(adapter);
843 mutex_unlock(&adapter->reset_lock);
844 return rc;
845 }
846 }
847
848 rc = __ibmvnic_open(netdev);
849 mutex_unlock(&adapter->reset_lock);
850
Nathan Fontenotbfc32f22017-05-03 14:04:26 -0400851 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600852}
853
Nathan Fontenotb41b83e2017-05-03 14:04:56 -0400854static void clean_tx_pools(struct ibmvnic_adapter *adapter)
855{
856 struct ibmvnic_tx_pool *tx_pool;
857 u64 tx_entries;
858 int tx_scrqs;
859 int i, j;
860
861 if (!adapter->tx_pool)
862 return;
863
864 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
865 tx_entries = adapter->req_tx_entries_per_subcrq;
866
867 /* Free any remaining skbs in the tx buffer pools */
868 for (i = 0; i < tx_scrqs; i++) {
869 tx_pool = &adapter->tx_pool[i];
870 if (!tx_pool)
871 continue;
872
873 for (j = 0; j < tx_entries; j++) {
874 if (tx_pool->tx_buff[j].skb) {
875 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
876 tx_pool->tx_buff[j].skb = NULL;
877 }
878 }
879 }
880}
881
Nathan Fontenoted651a12017-05-03 14:04:38 -0400882static int __ibmvnic_close(struct net_device *netdev)
John Allenea5509f2017-03-17 17:13:43 -0500883{
884 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400885 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500886 int i;
887
Nathan Fontenot90c80142017-05-03 14:04:32 -0400888 adapter->state = VNIC_CLOSING;
Thomas Falcon4c2687a2017-06-14 23:50:06 -0500889
890 /* ensure that transmissions are stopped if called by do_reset */
891 if (adapter->resetting)
892 netif_tx_disable(netdev);
893 else
894 netif_tx_stop_all_queues(netdev);
895
John Allend944c3d62017-05-26 10:30:13 -0400896 ibmvnic_napi_disable(adapter);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400897
898 if (adapter->tx_scrq) {
899 for (i = 0; i < adapter->req_tx_queues; i++)
900 if (adapter->tx_scrq[i]->irq)
901 disable_irq(adapter->tx_scrq[i]->irq);
902 }
903
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400904 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Nathan Fontenot46293b92017-05-03 14:05:02 -0400905 if (rc)
906 return rc;
907
908 if (adapter->rx_scrq) {
909 for (i = 0; i < adapter->req_rx_queues; i++) {
910 int retries = 10;
911
912 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
913 retries--;
914 mdelay(100);
915
916 if (retries == 0)
917 break;
918 }
919
920 if (adapter->rx_scrq[i]->irq)
921 disable_irq(adapter->rx_scrq[i]->irq);
922 }
923 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600924
Thomas Falcon10f76212017-05-26 10:30:31 -0400925 clean_tx_pools(adapter);
Nathan Fontenot90c80142017-05-03 14:04:32 -0400926 adapter->state = VNIC_CLOSED;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400927 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600928}
929
Nathan Fontenoted651a12017-05-03 14:04:38 -0400930static int ibmvnic_close(struct net_device *netdev)
931{
932 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
933 int rc;
934
935 mutex_lock(&adapter->reset_lock);
936 rc = __ibmvnic_close(netdev);
937 mutex_unlock(&adapter->reset_lock);
938
939 return rc;
940}
941
Thomas Falconad7775d2016-04-01 17:20:34 -0500942/**
943 * build_hdr_data - creates L2/L3/L4 header data buffer
944 * @hdr_field - bitfield determining needed headers
945 * @skb - socket buffer
946 * @hdr_len - array of header lengths
947 * @tot_len - total length of data
948 *
949 * Reads hdr_field to determine which headers are needed by firmware.
950 * Builds a buffer containing these headers. Saves individual header
951 * lengths and total buffer length to be used to build descriptors.
952 */
953static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
954 int *hdr_len, u8 *hdr_data)
955{
956 int len = 0;
957 u8 *hdr;
958
959 hdr_len[0] = sizeof(struct ethhdr);
960
961 if (skb->protocol == htons(ETH_P_IP)) {
962 hdr_len[1] = ip_hdr(skb)->ihl * 4;
963 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
964 hdr_len[2] = tcp_hdrlen(skb);
965 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
966 hdr_len[2] = sizeof(struct udphdr);
967 } else if (skb->protocol == htons(ETH_P_IPV6)) {
968 hdr_len[1] = sizeof(struct ipv6hdr);
969 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
970 hdr_len[2] = tcp_hdrlen(skb);
971 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
972 hdr_len[2] = sizeof(struct udphdr);
973 }
974
975 memset(hdr_data, 0, 120);
976 if ((hdr_field >> 6) & 1) {
977 hdr = skb_mac_header(skb);
978 memcpy(hdr_data, hdr, hdr_len[0]);
979 len += hdr_len[0];
980 }
981
982 if ((hdr_field >> 5) & 1) {
983 hdr = skb_network_header(skb);
984 memcpy(hdr_data + len, hdr, hdr_len[1]);
985 len += hdr_len[1];
986 }
987
988 if ((hdr_field >> 4) & 1) {
989 hdr = skb_transport_header(skb);
990 memcpy(hdr_data + len, hdr, hdr_len[2]);
991 len += hdr_len[2];
992 }
993 return len;
994}
995
996/**
997 * create_hdr_descs - create header and header extension descriptors
998 * @hdr_field - bitfield determining needed headers
999 * @data - buffer containing header data
1000 * @len - length of data buffer
1001 * @hdr_len - array of individual header lengths
1002 * @scrq_arr - descriptor array
1003 *
1004 * Creates header and, if needed, header extension descriptors and
1005 * places them in a descriptor array, scrq_arr
1006 */
1007
1008static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1009 union sub_crq *scrq_arr)
1010{
1011 union sub_crq hdr_desc;
1012 int tmp_len = len;
1013 u8 *data, *cur;
1014 int tmp;
1015
1016 while (tmp_len > 0) {
1017 cur = hdr_data + len - tmp_len;
1018
1019 memset(&hdr_desc, 0, sizeof(hdr_desc));
1020 if (cur != hdr_data) {
1021 data = hdr_desc.hdr_ext.data;
1022 tmp = tmp_len > 29 ? 29 : tmp_len;
1023 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1024 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1025 hdr_desc.hdr_ext.len = tmp;
1026 } else {
1027 data = hdr_desc.hdr.data;
1028 tmp = tmp_len > 24 ? 24 : tmp_len;
1029 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1030 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1031 hdr_desc.hdr.len = tmp;
1032 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1033 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1034 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1035 hdr_desc.hdr.flag = hdr_field << 1;
1036 }
1037 memcpy(data, cur, tmp);
1038 tmp_len -= tmp;
1039 *scrq_arr = hdr_desc;
1040 scrq_arr++;
1041 }
1042}
1043
1044/**
1045 * build_hdr_descs_arr - build a header descriptor array
1046 * @skb - socket buffer
1047 * @num_entries - number of descriptors to be sent
1048 * @subcrq - first TX descriptor
1049 * @hdr_field - bit field determining which headers will be sent
1050 *
1051 * This function will build a TX descriptor array with applicable
1052 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1053 */
1054
1055static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1056 int *num_entries, u8 hdr_field)
1057{
1058 int hdr_len[3] = {0, 0, 0};
1059 int tot_len, len;
1060 u8 *hdr_data = txbuff->hdr_data;
1061
1062 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1063 txbuff->hdr_data);
1064 len = tot_len;
1065 len -= 24;
1066 if (len > 0)
1067 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1068 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1069 txbuff->indir_arr + 1);
1070}
1071
Thomas Falcon032c5e82015-12-21 11:26:06 -06001072static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1073{
1074 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1075 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -05001076 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001077 struct device *dev = &adapter->vdev->dev;
1078 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001079 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001080 struct ibmvnic_tx_pool *tx_pool;
1081 unsigned int tx_send_failed = 0;
1082 unsigned int tx_map_failed = 0;
1083 unsigned int tx_dropped = 0;
1084 unsigned int tx_packets = 0;
1085 unsigned int tx_bytes = 0;
1086 dma_addr_t data_dma_addr;
1087 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001088 unsigned long lpar_rc;
1089 union sub_crq tx_crq;
1090 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -05001091 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001092 unsigned char *dst;
1093 u64 *handle_array;
1094 int index = 0;
1095 int ret = 0;
1096
Nathan Fontenoted651a12017-05-03 14:04:38 -04001097 if (adapter->resetting) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001098 if (!netif_subqueue_stopped(netdev, skb))
1099 netif_stop_subqueue(netdev, queue_num);
1100 dev_kfree_skb_any(skb);
1101
Thomas Falcon032c5e82015-12-21 11:26:06 -06001102 tx_send_failed++;
1103 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001104 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001105 goto out;
1106 }
1107
Nathan Fontenot161b8a82017-05-03 14:05:08 -04001108 tx_pool = &adapter->tx_pool[queue_num];
1109 tx_scrq = adapter->tx_scrq[queue_num];
1110 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1111 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1112 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1113
Thomas Falcon032c5e82015-12-21 11:26:06 -06001114 index = tx_pool->free_map[tx_pool->consumer_index];
1115 offset = index * adapter->req_mtu;
1116 dst = tx_pool->long_term_buff.buff + offset;
1117 memset(dst, 0, adapter->req_mtu);
1118 skb_copy_from_linear_data(skb, dst, skb->len);
1119 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1120
1121 tx_pool->consumer_index =
1122 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001123 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001124
1125 tx_buff = &tx_pool->tx_buff[index];
1126 tx_buff->skb = skb;
1127 tx_buff->data_dma[0] = data_dma_addr;
1128 tx_buff->data_len[0] = skb->len;
1129 tx_buff->index = index;
1130 tx_buff->pool_index = queue_num;
1131 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001132
1133 memset(&tx_crq, 0, sizeof(tx_crq));
1134 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1135 tx_crq.v1.type = IBMVNIC_TX_DESC;
1136 tx_crq.v1.n_crq_elem = 1;
1137 tx_crq.v1.n_sge = 1;
1138 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1139 tx_crq.v1.correlator = cpu_to_be32(index);
1140 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1141 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1142 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1143
1144 if (adapter->vlan_header_insertion) {
1145 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1146 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1147 }
1148
1149 if (skb->protocol == htons(ETH_P_IP)) {
1150 if (ip_hdr(skb)->version == 4)
1151 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1152 else if (ip_hdr(skb)->version == 6)
1153 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1154
1155 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1156 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1157 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1158 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1159 }
1160
Thomas Falconad7775d2016-04-01 17:20:34 -05001161 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001162 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -05001163 hdrs += 2;
1164 }
1165 /* determine if l2/3/4 headers are sent to firmware */
1166 if ((*hdrs >> 7) & 1 &&
1167 (skb->protocol == htons(ETH_P_IP) ||
1168 skb->protocol == htons(ETH_P_IPV6))) {
1169 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1170 tx_crq.v1.n_crq_elem = num_entries;
1171 tx_buff->indir_arr[0] = tx_crq;
1172 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1173 sizeof(tx_buff->indir_arr),
1174 DMA_TO_DEVICE);
1175 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001176 dev_kfree_skb_any(skb);
1177 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -05001178 if (!firmware_has_feature(FW_FEATURE_CMO))
1179 dev_err(dev, "tx: unable to map descriptor array\n");
1180 tx_map_failed++;
1181 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001182 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -05001183 goto out;
1184 }
John Allen498cd8e2016-04-06 11:49:55 -05001185 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -05001186 (u64)tx_buff->indir_dma,
1187 (u64)num_entries);
1188 } else {
John Allen498cd8e2016-04-06 11:49:55 -05001189 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1190 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -05001191 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001192 if (lpar_rc != H_SUCCESS) {
1193 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1194
1195 if (tx_pool->consumer_index == 0)
1196 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001197 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001198 else
1199 tx_pool->consumer_index--;
1200
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001201 dev_kfree_skb_any(skb);
1202 tx_buff->skb = NULL;
1203
Thomas Falconb8c80b82017-05-26 10:30:42 -04001204 if (lpar_rc == H_CLOSED) {
1205 /* Disable TX and report carrier off if queue is closed.
1206 * Firmware guarantees that a signal will be sent to the
1207 * driver, triggering a reset or some other action.
1208 */
1209 netif_tx_stop_all_queues(netdev);
1210 netif_carrier_off(netdev);
1211 }
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001212
Thomas Falcon032c5e82015-12-21 11:26:06 -06001213 tx_send_failed++;
1214 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001215 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001216 goto out;
1217 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001218
Brian King58c8c0c2017-04-19 13:44:47 -04001219 if (atomic_inc_return(&tx_scrq->used)
1220 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001221 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1222 netif_stop_subqueue(netdev, queue_num);
1223 }
1224
Thomas Falcon032c5e82015-12-21 11:26:06 -06001225 tx_packets++;
1226 tx_bytes += skb->len;
1227 txq->trans_start = jiffies;
1228 ret = NETDEV_TX_OK;
1229
1230out:
1231 netdev->stats.tx_dropped += tx_dropped;
1232 netdev->stats.tx_bytes += tx_bytes;
1233 netdev->stats.tx_packets += tx_packets;
1234 adapter->tx_send_failed += tx_send_failed;
1235 adapter->tx_map_failed += tx_map_failed;
1236
1237 return ret;
1238}
1239
1240static void ibmvnic_set_multi(struct net_device *netdev)
1241{
1242 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1243 struct netdev_hw_addr *ha;
1244 union ibmvnic_crq crq;
1245
1246 memset(&crq, 0, sizeof(crq));
1247 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1248 crq.request_capability.cmd = REQUEST_CAPABILITY;
1249
1250 if (netdev->flags & IFF_PROMISC) {
1251 if (!adapter->promisc_supported)
1252 return;
1253 } else {
1254 if (netdev->flags & IFF_ALLMULTI) {
1255 /* Accept all multicast */
1256 memset(&crq, 0, sizeof(crq));
1257 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1258 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1259 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1260 ibmvnic_send_crq(adapter, &crq);
1261 } else if (netdev_mc_empty(netdev)) {
1262 /* Reject all multicast */
1263 memset(&crq, 0, sizeof(crq));
1264 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1265 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1266 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1267 ibmvnic_send_crq(adapter, &crq);
1268 } else {
1269 /* Accept one or more multicast(s) */
1270 netdev_for_each_mc_addr(ha, netdev) {
1271 memset(&crq, 0, sizeof(crq));
1272 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1273 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1274 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1275 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1276 ha->addr);
1277 ibmvnic_send_crq(adapter, &crq);
1278 }
1279 }
1280 }
1281}
1282
1283static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1284{
1285 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1286 struct sockaddr *addr = p;
1287 union ibmvnic_crq crq;
1288
1289 if (!is_valid_ether_addr(addr->sa_data))
1290 return -EADDRNOTAVAIL;
1291
1292 memset(&crq, 0, sizeof(crq));
1293 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1294 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1295 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1296 ibmvnic_send_crq(adapter, &crq);
1297 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1298 return 0;
1299}
1300
Nathan Fontenoted651a12017-05-03 14:04:38 -04001301/**
1302 * do_reset returns zero if we are able to keep processing reset events, or
1303 * non-zero if we hit a fatal error and must halt.
1304 */
1305static int do_reset(struct ibmvnic_adapter *adapter,
1306 struct ibmvnic_rwi *rwi, u32 reset_state)
1307{
1308 struct net_device *netdev = adapter->netdev;
1309 int i, rc;
1310
1311 netif_carrier_off(netdev);
1312 adapter->reset_reason = rwi->reset_reason;
1313
1314 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1315 rc = ibmvnic_reenable_crq_queue(adapter);
1316 if (rc)
1317 return 0;
1318 }
1319
1320 rc = __ibmvnic_close(netdev);
1321 if (rc)
1322 return rc;
1323
John Allen8cb31cf2017-05-26 10:30:37 -04001324 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1325 /* remove the closed state so when we call open it appears
1326 * we are coming from the probed state.
1327 */
Nathan Fontenoted651a12017-05-03 14:04:38 -04001328 adapter->state = VNIC_PROBED;
John Allen8cb31cf2017-05-26 10:30:37 -04001329
John Allen8cb31cf2017-05-26 10:30:37 -04001330 rc = ibmvnic_init(adapter);
1331 if (rc)
1332 return 0;
1333
1334 /* If the adapter was in PROBE state prior to the reset,
1335 * exit here.
1336 */
1337 if (reset_state == VNIC_PROBED)
1338 return 0;
1339
1340 rc = ibmvnic_login(netdev);
1341 if (rc) {
1342 adapter->state = VNIC_PROBED;
1343 return 0;
1344 }
1345
Nathan Fontenot8c0543a2017-05-26 10:31:06 -04001346 rc = reset_tx_pools(adapter);
1347 if (rc)
1348 return rc;
1349
1350 rc = reset_rx_pools(adapter);
John Allen8cb31cf2017-05-26 10:30:37 -04001351 if (rc)
1352 return rc;
1353
1354 if (reset_state == VNIC_CLOSED)
1355 return 0;
Nathan Fontenoted651a12017-05-03 14:04:38 -04001356 }
1357
Nathan Fontenoted651a12017-05-03 14:04:38 -04001358 rc = __ibmvnic_open(netdev);
1359 if (rc) {
1360 if (list_empty(&adapter->rwi_list))
1361 adapter->state = VNIC_CLOSED;
1362 else
1363 adapter->state = reset_state;
1364
1365 return 0;
1366 }
1367
1368 netif_carrier_on(netdev);
1369
1370 /* kick napi */
1371 for (i = 0; i < adapter->req_rx_queues; i++)
1372 napi_schedule(&adapter->napi[i]);
1373
Nathan Fontenot61d3e1d2017-06-12 20:47:45 -04001374 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1375 netdev_notify_peers(netdev);
1376
Nathan Fontenoted651a12017-05-03 14:04:38 -04001377 return 0;
1378}
1379
1380static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1381{
1382 struct ibmvnic_rwi *rwi;
1383
1384 mutex_lock(&adapter->rwi_lock);
1385
1386 if (!list_empty(&adapter->rwi_list)) {
1387 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1388 list);
1389 list_del(&rwi->list);
1390 } else {
1391 rwi = NULL;
1392 }
1393
1394 mutex_unlock(&adapter->rwi_lock);
1395 return rwi;
1396}
1397
1398static void free_all_rwi(struct ibmvnic_adapter *adapter)
1399{
1400 struct ibmvnic_rwi *rwi;
1401
1402 rwi = get_next_rwi(adapter);
1403 while (rwi) {
1404 kfree(rwi);
1405 rwi = get_next_rwi(adapter);
1406 }
1407}
1408
1409static void __ibmvnic_reset(struct work_struct *work)
1410{
1411 struct ibmvnic_rwi *rwi;
1412 struct ibmvnic_adapter *adapter;
1413 struct net_device *netdev;
1414 u32 reset_state;
1415 int rc;
1416
1417 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1418 netdev = adapter->netdev;
1419
1420 mutex_lock(&adapter->reset_lock);
1421 adapter->resetting = true;
1422 reset_state = adapter->state;
1423
1424 rwi = get_next_rwi(adapter);
1425 while (rwi) {
1426 rc = do_reset(adapter, rwi, reset_state);
1427 kfree(rwi);
1428 if (rc)
1429 break;
1430
1431 rwi = get_next_rwi(adapter);
1432 }
1433
1434 if (rc) {
1435 free_all_rwi(adapter);
Wei Yongjun6d0af072017-05-18 15:24:52 +00001436 mutex_unlock(&adapter->reset_lock);
Nathan Fontenoted651a12017-05-03 14:04:38 -04001437 return;
1438 }
1439
1440 adapter->resetting = false;
1441 mutex_unlock(&adapter->reset_lock);
1442}
1443
1444static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1445 enum ibmvnic_reset_reason reason)
1446{
1447 struct ibmvnic_rwi *rwi, *tmp;
1448 struct net_device *netdev = adapter->netdev;
1449 struct list_head *entry;
1450
1451 if (adapter->state == VNIC_REMOVING ||
1452 adapter->state == VNIC_REMOVED) {
1453 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1454 return;
1455 }
1456
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04001457 if (adapter->state == VNIC_PROBING) {
1458 netdev_warn(netdev, "Adapter reset during probe\n");
1459 adapter->init_done_rc = EAGAIN;
1460 return;
1461 }
1462
Nathan Fontenoted651a12017-05-03 14:04:38 -04001463 mutex_lock(&adapter->rwi_lock);
1464
1465 list_for_each(entry, &adapter->rwi_list) {
1466 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1467 if (tmp->reset_reason == reason) {
1468 netdev_err(netdev, "Matching reset found, skipping\n");
1469 mutex_unlock(&adapter->rwi_lock);
1470 return;
1471 }
1472 }
1473
1474 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1475 if (!rwi) {
1476 mutex_unlock(&adapter->rwi_lock);
1477 ibmvnic_close(netdev);
1478 return;
1479 }
1480
1481 rwi->reset_reason = reason;
1482 list_add_tail(&rwi->list, &adapter->rwi_list);
1483 mutex_unlock(&adapter->rwi_lock);
1484 schedule_work(&adapter->ibmvnic_reset);
1485}
1486
Thomas Falcon032c5e82015-12-21 11:26:06 -06001487static void ibmvnic_tx_timeout(struct net_device *dev)
1488{
1489 struct ibmvnic_adapter *adapter = netdev_priv(dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001490
Nathan Fontenoted651a12017-05-03 14:04:38 -04001491 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001492}
1493
1494static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1495 struct ibmvnic_rx_buff *rx_buff)
1496{
1497 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1498
1499 rx_buff->skb = NULL;
1500
1501 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1502 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1503
1504 atomic_dec(&pool->available);
1505}
1506
1507static int ibmvnic_poll(struct napi_struct *napi, int budget)
1508{
1509 struct net_device *netdev = napi->dev;
1510 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1511 int scrq_num = (int)(napi - adapter->napi);
1512 int frames_processed = 0;
Nathan Fontenot152ce472017-05-26 10:30:54 -04001513
Thomas Falcon032c5e82015-12-21 11:26:06 -06001514restart_poll:
1515 while (frames_processed < budget) {
1516 struct sk_buff *skb;
1517 struct ibmvnic_rx_buff *rx_buff;
1518 union sub_crq *next;
1519 u32 length;
1520 u16 offset;
1521 u8 flags = 0;
1522
Thomas Falcon21ecba62017-06-14 23:50:09 -05001523 if (unlikely(adapter->resetting)) {
1524 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1525 napi_complete_done(napi, frames_processed);
1526 return frames_processed;
1527 }
1528
Thomas Falcon032c5e82015-12-21 11:26:06 -06001529 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1530 break;
1531 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1532 rx_buff =
1533 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1534 rx_comp.correlator);
1535 /* do error checking */
1536 if (next->rx_comp.rc) {
1537 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1538 /* free the entry */
1539 next->rx_comp.first = 0;
1540 remove_buff_from_pool(adapter, rx_buff);
Nathan Fontenotca05e312017-05-03 14:05:14 -04001541 continue;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001542 }
1543
1544 length = be32_to_cpu(next->rx_comp.len);
1545 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1546 flags = next->rx_comp.flags;
1547 skb = rx_buff->skb;
1548 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1549 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001550
1551 /* VLAN Header has been stripped by the system firmware and
1552 * needs to be inserted by the driver
1553 */
1554 if (adapter->rx_vlan_header_insertion &&
1555 (flags & IBMVNIC_VLAN_STRIPPED))
1556 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1557 ntohs(next->rx_comp.vlan_tci));
1558
Thomas Falcon032c5e82015-12-21 11:26:06 -06001559 /* free the entry */
1560 next->rx_comp.first = 0;
1561 remove_buff_from_pool(adapter, rx_buff);
1562
1563 skb_put(skb, length);
1564 skb->protocol = eth_type_trans(skb, netdev);
Thomas Falcon94ca3052017-05-03 14:05:20 -04001565 skb_record_rx_queue(skb, scrq_num);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001566
1567 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1568 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1569 skb->ip_summed = CHECKSUM_UNNECESSARY;
1570 }
1571
1572 length = skb->len;
1573 napi_gro_receive(napi, skb); /* send it up */
1574 netdev->stats.rx_packets++;
1575 netdev->stats.rx_bytes += length;
1576 frames_processed++;
1577 }
Nathan Fontenot152ce472017-05-26 10:30:54 -04001578
1579 if (adapter->state != VNIC_CLOSING)
1580 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001581
1582 if (frames_processed < budget) {
1583 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001584 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001585 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1586 napi_reschedule(napi)) {
1587 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1588 goto restart_poll;
1589 }
1590 }
1591 return frames_processed;
1592}
1593
1594#ifdef CONFIG_NET_POLL_CONTROLLER
1595static void ibmvnic_netpoll_controller(struct net_device *dev)
1596{
1597 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1598 int i;
1599
1600 replenish_pools(netdev_priv(dev));
1601 for (i = 0; i < adapter->req_rx_queues; i++)
1602 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1603 adapter->rx_scrq[i]);
1604}
1605#endif
1606
John Allen3a807b72017-06-06 16:55:52 -05001607static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1608{
1609 return -EOPNOTSUPP;
1610}
1611
Thomas Falcon032c5e82015-12-21 11:26:06 -06001612static const struct net_device_ops ibmvnic_netdev_ops = {
1613 .ndo_open = ibmvnic_open,
1614 .ndo_stop = ibmvnic_close,
1615 .ndo_start_xmit = ibmvnic_xmit,
1616 .ndo_set_rx_mode = ibmvnic_set_multi,
1617 .ndo_set_mac_address = ibmvnic_set_mac,
1618 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001619 .ndo_tx_timeout = ibmvnic_tx_timeout,
1620#ifdef CONFIG_NET_POLL_CONTROLLER
1621 .ndo_poll_controller = ibmvnic_netpoll_controller,
1622#endif
John Allen3a807b72017-06-06 16:55:52 -05001623 .ndo_change_mtu = ibmvnic_change_mtu,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001624};
1625
1626/* ethtool functions */
1627
Philippe Reynes8a433792017-01-07 22:37:29 +01001628static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1629 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001630{
Philippe Reynes8a433792017-01-07 22:37:29 +01001631 u32 supported, advertising;
1632
1633 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001634 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001635 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001636 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001637 cmd->base.speed = SPEED_1000;
1638 cmd->base.duplex = DUPLEX_FULL;
1639 cmd->base.port = PORT_FIBRE;
1640 cmd->base.phy_address = 0;
1641 cmd->base.autoneg = AUTONEG_ENABLE;
1642
1643 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1644 supported);
1645 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1646 advertising);
1647
Thomas Falcon032c5e82015-12-21 11:26:06 -06001648 return 0;
1649}
1650
1651static void ibmvnic_get_drvinfo(struct net_device *dev,
1652 struct ethtool_drvinfo *info)
1653{
1654 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1655 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1656}
1657
1658static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1659{
1660 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1661
1662 return adapter->msg_enable;
1663}
1664
1665static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1666{
1667 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1668
1669 adapter->msg_enable = data;
1670}
1671
1672static u32 ibmvnic_get_link(struct net_device *netdev)
1673{
1674 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1675
1676 /* Don't need to send a query because we request a logical link up at
1677 * init and then we wait for link state indications
1678 */
1679 return adapter->logical_link_state;
1680}
1681
1682static void ibmvnic_get_ringparam(struct net_device *netdev,
1683 struct ethtool_ringparam *ring)
1684{
1685 ring->rx_max_pending = 0;
1686 ring->tx_max_pending = 0;
1687 ring->rx_mini_max_pending = 0;
1688 ring->rx_jumbo_max_pending = 0;
1689 ring->rx_pending = 0;
1690 ring->tx_pending = 0;
1691 ring->rx_mini_pending = 0;
1692 ring->rx_jumbo_pending = 0;
1693}
1694
1695static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1696{
1697 int i;
1698
1699 if (stringset != ETH_SS_STATS)
1700 return;
1701
1702 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1703 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1704}
1705
1706static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1707{
1708 switch (sset) {
1709 case ETH_SS_STATS:
1710 return ARRAY_SIZE(ibmvnic_stats);
1711 default:
1712 return -EOPNOTSUPP;
1713 }
1714}
1715
1716static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1717 struct ethtool_stats *stats, u64 *data)
1718{
1719 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1720 union ibmvnic_crq crq;
1721 int i;
1722
1723 memset(&crq, 0, sizeof(crq));
1724 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1725 crq.request_statistics.cmd = REQUEST_STATISTICS;
1726 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1727 crq.request_statistics.len =
1728 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001729
1730 /* Wait for data to be written */
1731 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001732 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001733 wait_for_completion(&adapter->stats_done);
1734
1735 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1736 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1737}
1738
1739static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 .get_drvinfo = ibmvnic_get_drvinfo,
1741 .get_msglevel = ibmvnic_get_msglevel,
1742 .set_msglevel = ibmvnic_set_msglevel,
1743 .get_link = ibmvnic_get_link,
1744 .get_ringparam = ibmvnic_get_ringparam,
1745 .get_strings = ibmvnic_get_strings,
1746 .get_sset_count = ibmvnic_get_sset_count,
1747 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001748 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001749};
1750
1751/* Routines for managing CRQs/sCRQs */
1752
Nathan Fontenot57a49432017-05-26 10:31:12 -04001753static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1754 struct ibmvnic_sub_crq_queue *scrq)
1755{
1756 int rc;
1757
1758 if (scrq->irq) {
1759 free_irq(scrq->irq, scrq);
1760 irq_dispose_mapping(scrq->irq);
1761 scrq->irq = 0;
1762 }
1763
Thomas Falconc8b2ad02017-06-14 23:50:07 -05001764 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
Nathan Fontenot57a49432017-05-26 10:31:12 -04001765 scrq->cur = 0;
1766
1767 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1768 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1769 return rc;
1770}
1771
1772static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1773{
1774 int i, rc;
1775
1776 for (i = 0; i < adapter->req_tx_queues; i++) {
1777 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1778 if (rc)
1779 return rc;
1780 }
1781
1782 for (i = 0; i < adapter->req_rx_queues; i++) {
1783 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1784 if (rc)
1785 return rc;
1786 }
1787
1788 rc = init_sub_crq_irqs(adapter);
1789 return rc;
1790}
1791
Thomas Falcon032c5e82015-12-21 11:26:06 -06001792static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1793 struct ibmvnic_sub_crq_queue *scrq)
1794{
1795 struct device *dev = &adapter->vdev->dev;
1796 long rc;
1797
1798 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1799
1800 /* Close the sub-crqs */
1801 do {
1802 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1803 adapter->vdev->unit_address,
1804 scrq->crq_num);
1805 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1806
Thomas Falconffa73852017-04-19 13:44:29 -04001807 if (rc) {
1808 netdev_err(adapter->netdev,
1809 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1810 scrq->crq_num, rc);
1811 }
1812
Thomas Falcon032c5e82015-12-21 11:26:06 -06001813 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1814 DMA_BIDIRECTIONAL);
1815 free_pages((unsigned long)scrq->msgs, 2);
1816 kfree(scrq);
1817}
1818
1819static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1820 *adapter)
1821{
1822 struct device *dev = &adapter->vdev->dev;
1823 struct ibmvnic_sub_crq_queue *scrq;
1824 int rc;
1825
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001826 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001827 if (!scrq)
1828 return NULL;
1829
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001830 scrq->msgs =
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04001831 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832 if (!scrq->msgs) {
1833 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1834 goto zero_page_failed;
1835 }
1836
1837 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1838 DMA_BIDIRECTIONAL);
1839 if (dma_mapping_error(dev, scrq->msg_token)) {
1840 dev_warn(dev, "Couldn't map crq queue messages page\n");
1841 goto map_failed;
1842 }
1843
1844 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1845 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1846
1847 if (rc == H_RESOURCE)
1848 rc = ibmvnic_reset_crq(adapter);
1849
1850 if (rc == H_CLOSED) {
1851 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1852 } else if (rc) {
1853 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1854 goto reg_failed;
1855 }
1856
Thomas Falcon032c5e82015-12-21 11:26:06 -06001857 scrq->adapter = adapter;
1858 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001859 spin_lock_init(&scrq->lock);
1860
1861 netdev_dbg(adapter->netdev,
1862 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1863 scrq->crq_num, scrq->hw_irq, scrq->irq);
1864
1865 return scrq;
1866
Thomas Falcon032c5e82015-12-21 11:26:06 -06001867reg_failed:
1868 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1869 DMA_BIDIRECTIONAL);
1870map_failed:
1871 free_pages((unsigned long)scrq->msgs, 2);
1872zero_page_failed:
1873 kfree(scrq);
1874
1875 return NULL;
1876}
1877
1878static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1879{
1880 int i;
1881
1882 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001883 for (i = 0; i < adapter->req_tx_queues; i++) {
1884 if (!adapter->tx_scrq[i])
1885 continue;
1886
1887 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001888 free_irq(adapter->tx_scrq[i]->irq,
1889 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001890 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001891 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001892 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001893
1894 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1895 }
1896
Nathan Fontenot9501df32017-03-15 23:38:07 -04001897 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001898 adapter->tx_scrq = NULL;
1899 }
1900
1901 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001902 for (i = 0; i < adapter->req_rx_queues; i++) {
1903 if (!adapter->rx_scrq[i])
1904 continue;
1905
1906 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001907 free_irq(adapter->rx_scrq[i]->irq,
1908 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001909 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001910 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001911 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001912
1913 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1914 }
1915
Nathan Fontenot9501df32017-03-15 23:38:07 -04001916 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001917 adapter->rx_scrq = NULL;
1918 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001919}
1920
1921static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1922 struct ibmvnic_sub_crq_queue *scrq)
1923{
1924 struct device *dev = &adapter->vdev->dev;
1925 unsigned long rc;
1926
1927 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1928 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1929 if (rc)
1930 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1931 scrq->hw_irq, rc);
1932 return rc;
1933}
1934
1935static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1936 struct ibmvnic_sub_crq_queue *scrq)
1937{
1938 struct device *dev = &adapter->vdev->dev;
1939 unsigned long rc;
1940
1941 if (scrq->hw_irq > 0x100000000ULL) {
1942 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1943 return 1;
1944 }
1945
1946 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1947 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1948 if (rc)
1949 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1950 scrq->hw_irq, rc);
1951 return rc;
1952}
1953
1954static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1955 struct ibmvnic_sub_crq_queue *scrq)
1956{
1957 struct device *dev = &adapter->vdev->dev;
1958 struct ibmvnic_tx_buff *txbuff;
1959 union sub_crq *next;
1960 int index;
1961 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001962 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001963
1964restart_loop:
1965 while (pending_scrq(adapter, scrq)) {
1966 unsigned int pool = scrq->pool_index;
1967
1968 next = ibmvnic_next_scrq(adapter, scrq);
1969 for (i = 0; i < next->tx_comp.num_comps; i++) {
1970 if (next->tx_comp.rcs[i]) {
1971 dev_err(dev, "tx error %x\n",
1972 next->tx_comp.rcs[i]);
1973 continue;
1974 }
1975 index = be32_to_cpu(next->tx_comp.correlators[i]);
1976 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1977
1978 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1979 if (!txbuff->data_dma[j])
1980 continue;
1981
1982 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001983 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001984 /* if sub_crq was sent indirectly */
1985 first = txbuff->indir_arr[0].generic.first;
1986 if (first == IBMVNIC_CRQ_CMD) {
1987 dma_unmap_single(dev, txbuff->indir_dma,
1988 sizeof(txbuff->indir_arr),
1989 DMA_TO_DEVICE);
1990 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001991
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001992 if (txbuff->last_frag) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001993 dev_kfree_skb_any(txbuff->skb);
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04001994 txbuff->skb = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001995 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001996
1997 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1998 producer_index] = index;
1999 adapter->tx_pool[pool].producer_index =
2000 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06002001 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002002 }
2003 /* remove tx_comp scrq*/
2004 next->tx_comp.first = 0;
Nathan Fontenot7c3e7de2017-05-03 14:05:25 -04002005
2006 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2007 (adapter->req_tx_entries_per_subcrq / 2) &&
2008 __netif_subqueue_stopped(adapter->netdev,
2009 scrq->pool_index)) {
2010 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2011 netdev_info(adapter->netdev, "Started queue %d\n",
2012 scrq->pool_index);
2013 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002014 }
2015
2016 enable_scrq_irq(adapter, scrq);
2017
2018 if (pending_scrq(adapter, scrq)) {
2019 disable_scrq_irq(adapter, scrq);
2020 goto restart_loop;
2021 }
2022
2023 return 0;
2024}
2025
2026static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2027{
2028 struct ibmvnic_sub_crq_queue *scrq = instance;
2029 struct ibmvnic_adapter *adapter = scrq->adapter;
2030
2031 disable_scrq_irq(adapter, scrq);
2032 ibmvnic_complete_tx(adapter, scrq);
2033
2034 return IRQ_HANDLED;
2035}
2036
2037static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2038{
2039 struct ibmvnic_sub_crq_queue *scrq = instance;
2040 struct ibmvnic_adapter *adapter = scrq->adapter;
2041
2042 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2043 disable_scrq_irq(adapter, scrq);
2044 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2045 }
2046
2047 return IRQ_HANDLED;
2048}
2049
Thomas Falconea22d512016-07-06 15:35:17 -05002050static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2051{
2052 struct device *dev = &adapter->vdev->dev;
2053 struct ibmvnic_sub_crq_queue *scrq;
2054 int i = 0, j = 0;
2055 int rc = 0;
2056
2057 for (i = 0; i < adapter->req_tx_queues; i++) {
2058 scrq = adapter->tx_scrq[i];
2059 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2060
Michael Ellerman99c17902016-09-10 19:59:05 +10002061 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002062 rc = -EINVAL;
2063 dev_err(dev, "Error mapping irq\n");
2064 goto req_tx_irq_failed;
2065 }
2066
2067 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2068 0, "ibmvnic_tx", scrq);
2069
2070 if (rc) {
2071 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2072 scrq->irq, rc);
2073 irq_dispose_mapping(scrq->irq);
2074 goto req_rx_irq_failed;
2075 }
2076 }
2077
2078 for (i = 0; i < adapter->req_rx_queues; i++) {
2079 scrq = adapter->rx_scrq[i];
2080 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10002081 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05002082 rc = -EINVAL;
2083 dev_err(dev, "Error mapping irq\n");
2084 goto req_rx_irq_failed;
2085 }
2086 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2087 0, "ibmvnic_rx", scrq);
2088 if (rc) {
2089 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2090 scrq->irq, rc);
2091 irq_dispose_mapping(scrq->irq);
2092 goto req_rx_irq_failed;
2093 }
2094 }
2095 return rc;
2096
2097req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002098 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002099 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2100 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002101 }
Thomas Falconea22d512016-07-06 15:35:17 -05002102 i = adapter->req_tx_queues;
2103req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002104 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05002105 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2106 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05002107 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04002108 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05002109 return rc;
2110}
2111
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002112static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002113{
2114 struct device *dev = &adapter->vdev->dev;
2115 struct ibmvnic_sub_crq_queue **allqueues;
2116 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002117 int total_queues;
2118 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05002119 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002120
Thomas Falcon032c5e82015-12-21 11:26:06 -06002121 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2122
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002123 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002124 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002125 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002126
2127 for (i = 0; i < total_queues; i++) {
2128 allqueues[i] = init_sub_crq_queue(adapter);
2129 if (!allqueues[i]) {
2130 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2131 break;
2132 }
2133 registered_queues++;
2134 }
2135
2136 /* Make sure we were able to register the minimum number of queues */
2137 if (registered_queues <
2138 adapter->min_tx_queues + adapter->min_rx_queues) {
2139 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2140 goto tx_failed;
2141 }
2142
2143 /* Distribute the failed allocated queues*/
2144 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2145 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2146 switch (i % 3) {
2147 case 0:
2148 if (adapter->req_rx_queues > adapter->min_rx_queues)
2149 adapter->req_rx_queues--;
2150 else
2151 more++;
2152 break;
2153 case 1:
2154 if (adapter->req_tx_queues > adapter->min_tx_queues)
2155 adapter->req_tx_queues--;
2156 else
2157 more++;
2158 break;
2159 }
2160 }
2161
2162 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002163 sizeof(*adapter->tx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002164 if (!adapter->tx_scrq)
2165 goto tx_failed;
2166
2167 for (i = 0; i < adapter->req_tx_queues; i++) {
2168 adapter->tx_scrq[i] = allqueues[i];
2169 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002170 }
2171
2172 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04002173 sizeof(*adapter->rx_scrq), GFP_KERNEL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002174 if (!adapter->rx_scrq)
2175 goto rx_failed;
2176
2177 for (i = 0; i < adapter->req_rx_queues; i++) {
2178 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2179 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002180 }
2181
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002182 kfree(allqueues);
2183 return 0;
2184
2185rx_failed:
2186 kfree(adapter->tx_scrq);
2187 adapter->tx_scrq = NULL;
2188tx_failed:
2189 for (i = 0; i < registered_queues; i++)
2190 release_sub_crq_queue(adapter, allqueues[i]);
2191 kfree(allqueues);
2192 return -1;
2193}
2194
2195static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2196{
2197 struct device *dev = &adapter->vdev->dev;
2198 union ibmvnic_crq crq;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002199
2200 if (!retry) {
2201 /* Sub-CRQ entries are 32 byte long */
2202 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2203
2204 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2205 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2206 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2207 return;
2208 }
2209
2210 /* Get the minimum between the queried max and the entries
2211 * that fit in our PAGE_SIZE
2212 */
2213 adapter->req_tx_entries_per_subcrq =
2214 adapter->max_tx_entries_per_subcrq > entries_page ?
2215 entries_page : adapter->max_tx_entries_per_subcrq;
2216 adapter->req_rx_add_entries_per_subcrq =
2217 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2218 entries_page : adapter->max_rx_add_entries_per_subcrq;
2219
2220 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2221 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2222 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2223
2224 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2225 }
2226
Thomas Falcon032c5e82015-12-21 11:26:06 -06002227 memset(&crq, 0, sizeof(crq));
2228 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2229 crq.request_capability.cmd = REQUEST_CAPABILITY;
2230
2231 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002232 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002233 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002234 ibmvnic_send_crq(adapter, &crq);
2235
2236 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002237 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002238 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002239 ibmvnic_send_crq(adapter, &crq);
2240
2241 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06002242 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06002243 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002244 ibmvnic_send_crq(adapter, &crq);
2245
2246 crq.request_capability.capability =
2247 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2248 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002249 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002250 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002251 ibmvnic_send_crq(adapter, &crq);
2252
2253 crq.request_capability.capability =
2254 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2255 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06002256 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06002257 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002258 ibmvnic_send_crq(adapter, &crq);
2259
2260 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06002261 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06002262 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002263 ibmvnic_send_crq(adapter, &crq);
2264
2265 if (adapter->netdev->flags & IFF_PROMISC) {
2266 if (adapter->promisc_supported) {
2267 crq.request_capability.capability =
2268 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002269 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06002270 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002271 ibmvnic_send_crq(adapter, &crq);
2272 }
2273 } else {
2274 crq.request_capability.capability =
2275 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06002276 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06002277 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002278 ibmvnic_send_crq(adapter, &crq);
2279 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002280}
2281
2282static int pending_scrq(struct ibmvnic_adapter *adapter,
2283 struct ibmvnic_sub_crq_queue *scrq)
2284{
2285 union sub_crq *entry = &scrq->msgs[scrq->cur];
2286
Thomas Falcon1cf9cc72017-06-14 23:50:08 -05002287 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002288 return 1;
2289 else
2290 return 0;
2291}
2292
2293static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2294 struct ibmvnic_sub_crq_queue *scrq)
2295{
2296 union sub_crq *entry;
2297 unsigned long flags;
2298
2299 spin_lock_irqsave(&scrq->lock, flags);
2300 entry = &scrq->msgs[scrq->cur];
2301 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2302 if (++scrq->cur == scrq->size)
2303 scrq->cur = 0;
2304 } else {
2305 entry = NULL;
2306 }
2307 spin_unlock_irqrestore(&scrq->lock, flags);
2308
2309 return entry;
2310}
2311
2312static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2313{
2314 struct ibmvnic_crq_queue *queue = &adapter->crq;
2315 union ibmvnic_crq *crq;
2316
2317 crq = &queue->msgs[queue->cur];
2318 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2319 if (++queue->cur == queue->size)
2320 queue->cur = 0;
2321 } else {
2322 crq = NULL;
2323 }
2324
2325 return crq;
2326}
2327
2328static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2329 union sub_crq *sub_crq)
2330{
2331 unsigned int ua = adapter->vdev->unit_address;
2332 struct device *dev = &adapter->vdev->dev;
2333 u64 *u64_crq = (u64 *)sub_crq;
2334 int rc;
2335
2336 netdev_dbg(adapter->netdev,
2337 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2338 (unsigned long int)cpu_to_be64(remote_handle),
2339 (unsigned long int)cpu_to_be64(u64_crq[0]),
2340 (unsigned long int)cpu_to_be64(u64_crq[1]),
2341 (unsigned long int)cpu_to_be64(u64_crq[2]),
2342 (unsigned long int)cpu_to_be64(u64_crq[3]));
2343
2344 /* Make sure the hypervisor sees the complete request */
2345 mb();
2346
2347 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2348 cpu_to_be64(remote_handle),
2349 cpu_to_be64(u64_crq[0]),
2350 cpu_to_be64(u64_crq[1]),
2351 cpu_to_be64(u64_crq[2]),
2352 cpu_to_be64(u64_crq[3]));
2353
2354 if (rc) {
2355 if (rc == H_CLOSED)
2356 dev_warn(dev, "CRQ Queue closed\n");
2357 dev_err(dev, "Send error (rc=%d)\n", rc);
2358 }
2359
2360 return rc;
2361}
2362
Thomas Falconad7775d2016-04-01 17:20:34 -05002363static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2364 u64 remote_handle, u64 ioba, u64 num_entries)
2365{
2366 unsigned int ua = adapter->vdev->unit_address;
2367 struct device *dev = &adapter->vdev->dev;
2368 int rc;
2369
2370 /* Make sure the hypervisor sees the complete request */
2371 mb();
2372 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2373 cpu_to_be64(remote_handle),
2374 ioba, num_entries);
2375
2376 if (rc) {
2377 if (rc == H_CLOSED)
2378 dev_warn(dev, "CRQ Queue closed\n");
2379 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2380 }
2381
2382 return rc;
2383}
2384
Thomas Falcon032c5e82015-12-21 11:26:06 -06002385static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2386 union ibmvnic_crq *crq)
2387{
2388 unsigned int ua = adapter->vdev->unit_address;
2389 struct device *dev = &adapter->vdev->dev;
2390 u64 *u64_crq = (u64 *)crq;
2391 int rc;
2392
2393 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2394 (unsigned long int)cpu_to_be64(u64_crq[0]),
2395 (unsigned long int)cpu_to_be64(u64_crq[1]));
2396
2397 /* Make sure the hypervisor sees the complete request */
2398 mb();
2399
2400 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2401 cpu_to_be64(u64_crq[0]),
2402 cpu_to_be64(u64_crq[1]));
2403
2404 if (rc) {
2405 if (rc == H_CLOSED)
2406 dev_warn(dev, "CRQ Queue closed\n");
2407 dev_warn(dev, "Send error (rc=%d)\n", rc);
2408 }
2409
2410 return rc;
2411}
2412
2413static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2414{
2415 union ibmvnic_crq crq;
2416
2417 memset(&crq, 0, sizeof(crq));
2418 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2419 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2420 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2421
2422 return ibmvnic_send_crq(adapter, &crq);
2423}
2424
Thomas Falcon032c5e82015-12-21 11:26:06 -06002425static int send_version_xchg(struct ibmvnic_adapter *adapter)
2426{
2427 union ibmvnic_crq crq;
2428
2429 memset(&crq, 0, sizeof(crq));
2430 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2431 crq.version_exchange.cmd = VERSION_EXCHANGE;
2432 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2433
2434 return ibmvnic_send_crq(adapter, &crq);
2435}
2436
2437static void send_login(struct ibmvnic_adapter *adapter)
2438{
2439 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2440 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002441 struct device *dev = &adapter->vdev->dev;
2442 dma_addr_t rsp_buffer_token;
2443 dma_addr_t buffer_token;
2444 size_t rsp_buffer_size;
2445 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002446 size_t buffer_size;
2447 __be64 *tx_list_p;
2448 __be64 *rx_list_p;
2449 int i;
2450
2451 buffer_size =
2452 sizeof(struct ibmvnic_login_buffer) +
2453 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2454
2455 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2456 if (!login_buffer)
2457 goto buf_alloc_failed;
2458
2459 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2460 DMA_TO_DEVICE);
2461 if (dma_mapping_error(dev, buffer_token)) {
2462 dev_err(dev, "Couldn't map login buffer\n");
2463 goto buf_map_failed;
2464 }
2465
John Allen498cd8e2016-04-06 11:49:55 -05002466 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2467 sizeof(u64) * adapter->req_tx_queues +
2468 sizeof(u64) * adapter->req_rx_queues +
2469 sizeof(u64) * adapter->req_rx_queues +
2470 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002471
2472 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2473 if (!login_rsp_buffer)
2474 goto buf_rsp_alloc_failed;
2475
2476 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2477 rsp_buffer_size, DMA_FROM_DEVICE);
2478 if (dma_mapping_error(dev, rsp_buffer_token)) {
2479 dev_err(dev, "Couldn't map login rsp buffer\n");
2480 goto buf_rsp_map_failed;
2481 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002482
Thomas Falcon032c5e82015-12-21 11:26:06 -06002483 adapter->login_buf = login_buffer;
2484 adapter->login_buf_token = buffer_token;
2485 adapter->login_buf_sz = buffer_size;
2486 adapter->login_rsp_buf = login_rsp_buffer;
2487 adapter->login_rsp_buf_token = rsp_buffer_token;
2488 adapter->login_rsp_buf_sz = rsp_buffer_size;
2489
2490 login_buffer->len = cpu_to_be32(buffer_size);
2491 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2492 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2493 login_buffer->off_txcomp_subcrqs =
2494 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2495 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2496 login_buffer->off_rxcomp_subcrqs =
2497 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2498 sizeof(u64) * adapter->req_tx_queues);
2499 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2500 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2501
2502 tx_list_p = (__be64 *)((char *)login_buffer +
2503 sizeof(struct ibmvnic_login_buffer));
2504 rx_list_p = (__be64 *)((char *)login_buffer +
2505 sizeof(struct ibmvnic_login_buffer) +
2506 sizeof(u64) * adapter->req_tx_queues);
2507
2508 for (i = 0; i < adapter->req_tx_queues; i++) {
2509 if (adapter->tx_scrq[i]) {
2510 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2511 crq_num);
2512 }
2513 }
2514
2515 for (i = 0; i < adapter->req_rx_queues; i++) {
2516 if (adapter->rx_scrq[i]) {
2517 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2518 crq_num);
2519 }
2520 }
2521
2522 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2523 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2524 netdev_dbg(adapter->netdev, "%016lx\n",
2525 ((unsigned long int *)(adapter->login_buf))[i]);
2526 }
2527
2528 memset(&crq, 0, sizeof(crq));
2529 crq.login.first = IBMVNIC_CRQ_CMD;
2530 crq.login.cmd = LOGIN;
2531 crq.login.ioba = cpu_to_be32(buffer_token);
2532 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002533 ibmvnic_send_crq(adapter, &crq);
2534
2535 return;
2536
Thomas Falcon032c5e82015-12-21 11:26:06 -06002537buf_rsp_map_failed:
2538 kfree(login_rsp_buffer);
2539buf_rsp_alloc_failed:
2540 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2541buf_map_failed:
2542 kfree(login_buffer);
2543buf_alloc_failed:
2544 return;
2545}
2546
2547static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2548 u32 len, u8 map_id)
2549{
2550 union ibmvnic_crq crq;
2551
2552 memset(&crq, 0, sizeof(crq));
2553 crq.request_map.first = IBMVNIC_CRQ_CMD;
2554 crq.request_map.cmd = REQUEST_MAP;
2555 crq.request_map.map_id = map_id;
2556 crq.request_map.ioba = cpu_to_be32(addr);
2557 crq.request_map.len = cpu_to_be32(len);
2558 ibmvnic_send_crq(adapter, &crq);
2559}
2560
2561static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2562{
2563 union ibmvnic_crq crq;
2564
2565 memset(&crq, 0, sizeof(crq));
2566 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2567 crq.request_unmap.cmd = REQUEST_UNMAP;
2568 crq.request_unmap.map_id = map_id;
2569 ibmvnic_send_crq(adapter, &crq);
2570}
2571
2572static void send_map_query(struct ibmvnic_adapter *adapter)
2573{
2574 union ibmvnic_crq crq;
2575
2576 memset(&crq, 0, sizeof(crq));
2577 crq.query_map.first = IBMVNIC_CRQ_CMD;
2578 crq.query_map.cmd = QUERY_MAP;
2579 ibmvnic_send_crq(adapter, &crq);
2580}
2581
2582/* Send a series of CRQs requesting various capabilities of the VNIC server */
2583static void send_cap_queries(struct ibmvnic_adapter *adapter)
2584{
2585 union ibmvnic_crq crq;
2586
Thomas Falcon901e0402017-02-15 12:17:59 -06002587 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002588 memset(&crq, 0, sizeof(crq));
2589 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2590 crq.query_capability.cmd = QUERY_CAPABILITY;
2591
2592 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002593 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002594 ibmvnic_send_crq(adapter, &crq);
2595
2596 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002597 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002598 ibmvnic_send_crq(adapter, &crq);
2599
2600 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002601 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002602 ibmvnic_send_crq(adapter, &crq);
2603
2604 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002605 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002606 ibmvnic_send_crq(adapter, &crq);
2607
2608 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002609 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002610 ibmvnic_send_crq(adapter, &crq);
2611
2612 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002613 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002614 ibmvnic_send_crq(adapter, &crq);
2615
2616 crq.query_capability.capability =
2617 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002618 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002619 ibmvnic_send_crq(adapter, &crq);
2620
2621 crq.query_capability.capability =
2622 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002623 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002624 ibmvnic_send_crq(adapter, &crq);
2625
2626 crq.query_capability.capability =
2627 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002628 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002629 ibmvnic_send_crq(adapter, &crq);
2630
2631 crq.query_capability.capability =
2632 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002633 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002634 ibmvnic_send_crq(adapter, &crq);
2635
2636 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002637 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002638 ibmvnic_send_crq(adapter, &crq);
2639
2640 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002641 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 ibmvnic_send_crq(adapter, &crq);
2643
2644 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002645 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002646 ibmvnic_send_crq(adapter, &crq);
2647
2648 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002649 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002650 ibmvnic_send_crq(adapter, &crq);
2651
2652 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002653 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654 ibmvnic_send_crq(adapter, &crq);
2655
2656 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002657 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 ibmvnic_send_crq(adapter, &crq);
2659
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002660 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2661 atomic_inc(&adapter->running_cap_crqs);
2662 ibmvnic_send_crq(adapter, &crq);
2663
Thomas Falcon032c5e82015-12-21 11:26:06 -06002664 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002665 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 ibmvnic_send_crq(adapter, &crq);
2667
2668 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002669 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002670 ibmvnic_send_crq(adapter, &crq);
2671
2672 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002673 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002674 ibmvnic_send_crq(adapter, &crq);
2675
2676 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002677 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002678 ibmvnic_send_crq(adapter, &crq);
2679
2680 crq.query_capability.capability =
2681 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002682 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002683 ibmvnic_send_crq(adapter, &crq);
2684
2685 crq.query_capability.capability =
2686 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002687 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002688 ibmvnic_send_crq(adapter, &crq);
2689
2690 crq.query_capability.capability =
2691 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002692 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002693 ibmvnic_send_crq(adapter, &crq);
2694
2695 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002696 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002697 ibmvnic_send_crq(adapter, &crq);
2698}
2699
2700static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2701{
2702 struct device *dev = &adapter->vdev->dev;
2703 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2704 union ibmvnic_crq crq;
2705 int i;
2706
2707 dma_unmap_single(dev, adapter->ip_offload_tok,
2708 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2709
2710 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2711 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2712 netdev_dbg(adapter->netdev, "%016lx\n",
2713 ((unsigned long int *)(buf))[i]);
2714
2715 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2716 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2717 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2718 buf->tcp_ipv4_chksum);
2719 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2720 buf->tcp_ipv6_chksum);
2721 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2722 buf->udp_ipv4_chksum);
2723 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2724 buf->udp_ipv6_chksum);
2725 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2726 buf->large_tx_ipv4);
2727 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2728 buf->large_tx_ipv6);
2729 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2730 buf->large_rx_ipv4);
2731 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2732 buf->large_rx_ipv6);
2733 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2734 buf->max_ipv4_header_size);
2735 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2736 buf->max_ipv6_header_size);
2737 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2738 buf->max_tcp_header_size);
2739 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2740 buf->max_udp_header_size);
2741 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2742 buf->max_large_tx_size);
2743 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2744 buf->max_large_rx_size);
2745 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2746 buf->ipv6_extension_header);
2747 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2748 buf->tcp_pseudosum_req);
2749 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2750 buf->num_ipv6_ext_headers);
2751 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2752 buf->off_ipv6_ext_headers);
2753
2754 adapter->ip_offload_ctrl_tok =
2755 dma_map_single(dev, &adapter->ip_offload_ctrl,
2756 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2757
2758 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2759 dev_err(dev, "Couldn't map ip offload control buffer\n");
2760 return;
2761 }
2762
2763 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2764 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2765 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2766 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2767 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2768
2769 /* large_tx/rx disabled for now, additional features needed */
2770 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2771 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2772 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2773 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2774
2775 adapter->netdev->features = NETIF_F_GSO;
2776
2777 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2778 adapter->netdev->features |= NETIF_F_IP_CSUM;
2779
2780 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2781 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2782
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002783 if ((adapter->netdev->features &
2784 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2785 adapter->netdev->features |= NETIF_F_RXCSUM;
2786
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 memset(&crq, 0, sizeof(crq));
2788 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2789 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2790 crq.control_ip_offload.len =
2791 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2792 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2793 ibmvnic_send_crq(adapter, &crq);
2794}
2795
2796static void handle_error_info_rsp(union ibmvnic_crq *crq,
2797 struct ibmvnic_adapter *adapter)
2798{
2799 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002800 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002801 unsigned long flags;
2802 bool found = false;
2803 int i;
2804
2805 if (!crq->request_error_rsp.rc.code) {
2806 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2807 crq->request_error_rsp.rc.code);
2808 return;
2809 }
2810
2811 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002812 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002813 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2814 found = true;
2815 list_del(&error_buff->list);
2816 break;
2817 }
2818 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2819
2820 if (!found) {
2821 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002822 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002823 return;
2824 }
2825
2826 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002827 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002828
2829 for (i = 0; i < error_buff->len; i++) {
2830 pr_cont("%02x", (int)error_buff->buff[i]);
2831 if (i % 8 == 7)
2832 pr_cont(" ");
2833 }
2834 pr_cont("\n");
2835
2836 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2837 DMA_FROM_DEVICE);
2838 kfree(error_buff->buff);
2839 kfree(error_buff);
2840}
2841
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002842static void request_error_information(struct ibmvnic_adapter *adapter,
2843 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002844{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002846 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002847 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002848 unsigned long timeout = msecs_to_jiffies(30000);
2849 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002850 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002851 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002852
2853 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2854 if (!error_buff)
2855 return;
2856
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002857 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002858 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2859 if (!error_buff->buff) {
2860 kfree(error_buff);
2861 return;
2862 }
2863
2864 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2865 DMA_FROM_DEVICE);
2866 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002867 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002868 kfree(error_buff->buff);
2869 kfree(error_buff);
2870 return;
2871 }
2872
Thomas Falcon032c5e82015-12-21 11:26:06 -06002873 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002874 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002875
2876 spin_lock_irqsave(&adapter->error_list_lock, flags);
2877 list_add_tail(&error_buff->list, &adapter->errors);
2878 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2879
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002880 memset(&crq, 0, sizeof(crq));
2881 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2882 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2883 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2884 crq.request_error_info.len = cpu_to_be32(detail_len);
2885 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2886
2887 rc = ibmvnic_send_crq(adapter, &crq);
2888 if (rc) {
2889 netdev_err(netdev, "failed to request error information\n");
2890 goto err_info_fail;
2891 }
2892
2893 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2894 netdev_err(netdev, "timeout waiting for error information\n");
2895 goto err_info_fail;
2896 }
2897
2898 return;
2899
2900err_info_fail:
2901 spin_lock_irqsave(&adapter->error_list_lock, flags);
2902 list_del(&error_buff->list);
2903 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2904
2905 kfree(error_buff->buff);
2906 kfree(error_buff);
2907}
2908
2909static void handle_error_indication(union ibmvnic_crq *crq,
2910 struct ibmvnic_adapter *adapter)
2911{
2912 struct device *dev = &adapter->vdev->dev;
2913
2914 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2915 crq->error_indication.flags
2916 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2917 be32_to_cpu(crq->error_indication.error_id),
2918 be16_to_cpu(crq->error_indication.error_cause));
2919
2920 if (be32_to_cpu(crq->error_indication.error_id))
2921 request_error_information(adapter, crq);
Nathan Fontenoted651a12017-05-03 14:04:38 -04002922
2923 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2924 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
John Allen8cb31cf2017-05-26 10:30:37 -04002925 else
2926 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002927}
2928
2929static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2930 struct ibmvnic_adapter *adapter)
2931{
2932 struct net_device *netdev = adapter->netdev;
2933 struct device *dev = &adapter->vdev->dev;
2934 long rc;
2935
2936 rc = crq->change_mac_addr_rsp.rc.code;
2937 if (rc) {
2938 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2939 return;
2940 }
2941 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2942 ETH_ALEN);
2943}
2944
2945static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2946 struct ibmvnic_adapter *adapter)
2947{
2948 struct device *dev = &adapter->vdev->dev;
2949 u64 *req_value;
2950 char *name;
2951
Thomas Falcon901e0402017-02-15 12:17:59 -06002952 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002953 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2954 case REQ_TX_QUEUES:
2955 req_value = &adapter->req_tx_queues;
2956 name = "tx";
2957 break;
2958 case REQ_RX_QUEUES:
2959 req_value = &adapter->req_rx_queues;
2960 name = "rx";
2961 break;
2962 case REQ_RX_ADD_QUEUES:
2963 req_value = &adapter->req_rx_add_queues;
2964 name = "rx_add";
2965 break;
2966 case REQ_TX_ENTRIES_PER_SUBCRQ:
2967 req_value = &adapter->req_tx_entries_per_subcrq;
2968 name = "tx_entries_per_subcrq";
2969 break;
2970 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2971 req_value = &adapter->req_rx_add_entries_per_subcrq;
2972 name = "rx_add_entries_per_subcrq";
2973 break;
2974 case REQ_MTU:
2975 req_value = &adapter->req_mtu;
2976 name = "mtu";
2977 break;
2978 case PROMISC_REQUESTED:
2979 req_value = &adapter->promisc;
2980 name = "promisc";
2981 break;
2982 default:
2983 dev_err(dev, "Got invalid cap request rsp %d\n",
2984 crq->request_capability.capability);
2985 return;
2986 }
2987
2988 switch (crq->request_capability_rsp.rc.code) {
2989 case SUCCESS:
2990 break;
2991 case PARTIALSUCCESS:
2992 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2993 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002994 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002995 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002996 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002997 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002998 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002999 return;
3000 default:
3001 dev_err(dev, "Error %d in request cap rsp\n",
3002 crq->request_capability_rsp.rc.code);
3003 return;
3004 }
3005
3006 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06003007 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06003008 union ibmvnic_crq newcrq;
3009 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3010 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3011 &adapter->ip_offload_buf;
3012
Thomas Falcon249168a2017-02-15 12:18:00 -06003013 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003014 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3015 buf_sz,
3016 DMA_FROM_DEVICE);
3017
3018 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3019 if (!firmware_has_feature(FW_FEATURE_CMO))
3020 dev_err(dev, "Couldn't map offload buffer\n");
3021 return;
3022 }
3023
3024 memset(&newcrq, 0, sizeof(newcrq));
3025 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3026 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3027 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3028 newcrq.query_ip_offload.ioba =
3029 cpu_to_be32(adapter->ip_offload_tok);
3030
3031 ibmvnic_send_crq(adapter, &newcrq);
3032 }
3033}
3034
3035static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3036 struct ibmvnic_adapter *adapter)
3037{
3038 struct device *dev = &adapter->vdev->dev;
3039 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3040 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003041 int i;
3042
3043 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3044 DMA_BIDIRECTIONAL);
3045 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3046 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3047
John Allen498cd8e2016-04-06 11:49:55 -05003048 /* If the number of queues requested can't be allocated by the
3049 * server, the login response will return with code 1. We will need
3050 * to resend the login buffer with fewer queues requested.
3051 */
3052 if (login_rsp_crq->generic.rc.code) {
3053 adapter->renegotiate = true;
3054 complete(&adapter->init_done);
3055 return 0;
3056 }
3057
Thomas Falcon032c5e82015-12-21 11:26:06 -06003058 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3059 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3060 netdev_dbg(adapter->netdev, "%016lx\n",
3061 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3062 }
3063
3064 /* Sanity checks */
3065 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3066 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3067 adapter->req_rx_add_queues !=
3068 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3069 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3070 ibmvnic_remove(adapter->vdev);
3071 return -EIO;
3072 }
3073 complete(&adapter->init_done);
3074
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 return 0;
3076}
3077
3078static void handle_request_map_rsp(union ibmvnic_crq *crq,
3079 struct ibmvnic_adapter *adapter)
3080{
3081 struct device *dev = &adapter->vdev->dev;
3082 u8 map_id = crq->request_map_rsp.map_id;
3083 int tx_subcrqs;
3084 int rx_subcrqs;
3085 long rc;
3086 int i;
3087
3088 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
3089 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3090
3091 rc = crq->request_map_rsp.rc.code;
3092 if (rc) {
3093 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
3094 adapter->map_id--;
3095 /* need to find and zero tx/rx_pool map_id */
3096 for (i = 0; i < tx_subcrqs; i++) {
3097 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
3098 adapter->tx_pool[i].long_term_buff.map_id = 0;
3099 }
3100 for (i = 0; i < rx_subcrqs; i++) {
3101 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
3102 adapter->rx_pool[i].long_term_buff.map_id = 0;
3103 }
3104 }
3105 complete(&adapter->fw_done);
3106}
3107
3108static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3109 struct ibmvnic_adapter *adapter)
3110{
3111 struct device *dev = &adapter->vdev->dev;
3112 long rc;
3113
3114 rc = crq->request_unmap_rsp.rc.code;
3115 if (rc)
3116 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3117}
3118
3119static void handle_query_map_rsp(union ibmvnic_crq *crq,
3120 struct ibmvnic_adapter *adapter)
3121{
3122 struct net_device *netdev = adapter->netdev;
3123 struct device *dev = &adapter->vdev->dev;
3124 long rc;
3125
3126 rc = crq->query_map_rsp.rc.code;
3127 if (rc) {
3128 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3129 return;
3130 }
3131 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3132 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3133 crq->query_map_rsp.free_pages);
3134}
3135
3136static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3137 struct ibmvnic_adapter *adapter)
3138{
3139 struct net_device *netdev = adapter->netdev;
3140 struct device *dev = &adapter->vdev->dev;
3141 long rc;
3142
Thomas Falcon901e0402017-02-15 12:17:59 -06003143 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003144 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06003145 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003146 rc = crq->query_capability.rc.code;
3147 if (rc) {
3148 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3149 goto out;
3150 }
3151
3152 switch (be16_to_cpu(crq->query_capability.capability)) {
3153 case MIN_TX_QUEUES:
3154 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003155 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003156 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3157 adapter->min_tx_queues);
3158 break;
3159 case MIN_RX_QUEUES:
3160 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003161 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003162 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3163 adapter->min_rx_queues);
3164 break;
3165 case MIN_RX_ADD_QUEUES:
3166 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003167 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3169 adapter->min_rx_add_queues);
3170 break;
3171 case MAX_TX_QUEUES:
3172 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003173 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003174 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3175 adapter->max_tx_queues);
3176 break;
3177 case MAX_RX_QUEUES:
3178 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003179 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003180 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3181 adapter->max_rx_queues);
3182 break;
3183 case MAX_RX_ADD_QUEUES:
3184 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003185 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003186 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3187 adapter->max_rx_add_queues);
3188 break;
3189 case MIN_TX_ENTRIES_PER_SUBCRQ:
3190 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003191 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003192 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3193 adapter->min_tx_entries_per_subcrq);
3194 break;
3195 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3196 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003197 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003198 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3199 adapter->min_rx_add_entries_per_subcrq);
3200 break;
3201 case MAX_TX_ENTRIES_PER_SUBCRQ:
3202 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003203 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003204 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3205 adapter->max_tx_entries_per_subcrq);
3206 break;
3207 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3208 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003209 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3211 adapter->max_rx_add_entries_per_subcrq);
3212 break;
3213 case TCP_IP_OFFLOAD:
3214 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06003215 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003216 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3217 adapter->tcp_ip_offload);
3218 break;
3219 case PROMISC_SUPPORTED:
3220 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003221 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003222 netdev_dbg(netdev, "promisc_supported = %lld\n",
3223 adapter->promisc_supported);
3224 break;
3225 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003226 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003227 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003228 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3229 break;
3230 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06003231 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06003232 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003233 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3234 break;
3235 case MAX_MULTICAST_FILTERS:
3236 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06003237 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003238 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3239 adapter->max_multicast_filters);
3240 break;
3241 case VLAN_HEADER_INSERTION:
3242 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06003243 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003244 if (adapter->vlan_header_insertion)
3245 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3246 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3247 adapter->vlan_header_insertion);
3248 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04003249 case RX_VLAN_HEADER_INSERTION:
3250 adapter->rx_vlan_header_insertion =
3251 be64_to_cpu(crq->query_capability.number);
3252 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3253 adapter->rx_vlan_header_insertion);
3254 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003255 case MAX_TX_SG_ENTRIES:
3256 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06003257 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003258 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3259 adapter->max_tx_sg_entries);
3260 break;
3261 case RX_SG_SUPPORTED:
3262 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06003263 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003264 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3265 adapter->rx_sg_supported);
3266 break;
3267 case OPT_TX_COMP_SUB_QUEUES:
3268 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003269 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003270 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3271 adapter->opt_tx_comp_sub_queues);
3272 break;
3273 case OPT_RX_COMP_QUEUES:
3274 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06003275 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003276 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3277 adapter->opt_rx_comp_queues);
3278 break;
3279 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3280 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06003281 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003282 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3283 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3284 break;
3285 case OPT_TX_ENTRIES_PER_SUBCRQ:
3286 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003287 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003288 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3289 adapter->opt_tx_entries_per_subcrq);
3290 break;
3291 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3292 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06003293 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003294 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3295 adapter->opt_rxba_entries_per_subcrq);
3296 break;
3297 case TX_RX_DESC_REQ:
3298 adapter->tx_rx_desc_req = crq->query_capability.number;
3299 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3300 adapter->tx_rx_desc_req);
3301 break;
3302
3303 default:
3304 netdev_err(netdev, "Got invalid cap rsp %d\n",
3305 crq->query_capability.capability);
3306 }
3307
3308out:
Thomas Falcon249168a2017-02-15 12:18:00 -06003309 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3310 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04003311 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06003312 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06003313}
3314
Thomas Falcon032c5e82015-12-21 11:26:06 -06003315static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3316 struct ibmvnic_adapter *adapter)
3317{
3318 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3319 struct net_device *netdev = adapter->netdev;
3320 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003321 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322 long rc;
3323
3324 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04003325 (unsigned long int)cpu_to_be64(u64_crq[0]),
3326 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06003327 switch (gen_crq->first) {
3328 case IBMVNIC_CRQ_INIT_RSP:
3329 switch (gen_crq->cmd) {
3330 case IBMVNIC_CRQ_INIT:
3331 dev_info(dev, "Partner initialized\n");
John Allen017892c12017-05-26 10:30:19 -04003332 adapter->from_passive_init = true;
3333 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003334 break;
3335 case IBMVNIC_CRQ_INIT_COMPLETE:
3336 dev_info(dev, "Partner initialization complete\n");
3337 send_version_xchg(adapter);
3338 break;
3339 default:
3340 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3341 }
3342 return;
3343 case IBMVNIC_CRQ_XPORT_EVENT:
Nathan Fontenoted651a12017-05-03 14:04:38 -04003344 netif_carrier_off(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003345 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
Nathan Fontenoted651a12017-05-03 14:04:38 -04003346 dev_info(dev, "Migrated, re-enabling adapter\n");
3347 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003348 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3349 dev_info(dev, "Backing device failover detected\n");
Nathan Fontenoted651a12017-05-03 14:04:38 -04003350 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351 } else {
3352 /* The adapter lost the connection */
3353 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3354 gen_crq->cmd);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003355 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003356 }
3357 return;
3358 case IBMVNIC_CRQ_CMD_RSP:
3359 break;
3360 default:
3361 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3362 gen_crq->first);
3363 return;
3364 }
3365
3366 switch (gen_crq->cmd) {
3367 case VERSION_EXCHANGE_RSP:
3368 rc = crq->version_exchange_rsp.rc.code;
3369 if (rc) {
3370 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3371 break;
3372 }
3373 dev_info(dev, "Partner protocol version is %d\n",
3374 crq->version_exchange_rsp.version);
3375 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3376 ibmvnic_version)
3377 ibmvnic_version =
3378 be16_to_cpu(crq->version_exchange_rsp.version);
3379 send_cap_queries(adapter);
3380 break;
3381 case QUERY_CAPABILITY_RSP:
3382 handle_query_cap_rsp(crq, adapter);
3383 break;
3384 case QUERY_MAP_RSP:
3385 handle_query_map_rsp(crq, adapter);
3386 break;
3387 case REQUEST_MAP_RSP:
3388 handle_request_map_rsp(crq, adapter);
3389 break;
3390 case REQUEST_UNMAP_RSP:
3391 handle_request_unmap_rsp(crq, adapter);
3392 break;
3393 case REQUEST_CAPABILITY_RSP:
3394 handle_request_cap_rsp(crq, adapter);
3395 break;
3396 case LOGIN_RSP:
3397 netdev_dbg(netdev, "Got Login Response\n");
3398 handle_login_rsp(crq, adapter);
3399 break;
3400 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003401 netdev_dbg(netdev,
3402 "Got Logical Link State Response, state: %d rc: %d\n",
3403 crq->logical_link_state_rsp.link_state,
3404 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003405 adapter->logical_link_state =
3406 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003407 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3408 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003409 break;
3410 case LINK_STATE_INDICATION:
3411 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3412 adapter->phys_link_state =
3413 crq->link_state_indication.phys_link_state;
3414 adapter->logical_link_state =
3415 crq->link_state_indication.logical_link_state;
3416 break;
3417 case CHANGE_MAC_ADDR_RSP:
3418 netdev_dbg(netdev, "Got MAC address change Response\n");
3419 handle_change_mac_rsp(crq, adapter);
3420 break;
3421 case ERROR_INDICATION:
3422 netdev_dbg(netdev, "Got Error Indication\n");
3423 handle_error_indication(crq, adapter);
3424 break;
3425 case REQUEST_ERROR_RSP:
3426 netdev_dbg(netdev, "Got Error Detail Response\n");
3427 handle_error_info_rsp(crq, adapter);
3428 break;
3429 case REQUEST_STATISTICS_RSP:
3430 netdev_dbg(netdev, "Got Statistics Response\n");
3431 complete(&adapter->stats_done);
3432 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003433 case QUERY_IP_OFFLOAD_RSP:
3434 netdev_dbg(netdev, "Got Query IP offload Response\n");
3435 handle_query_ip_offload_rsp(adapter);
3436 break;
3437 case MULTICAST_CTRL_RSP:
3438 netdev_dbg(netdev, "Got multicast control Response\n");
3439 break;
3440 case CONTROL_IP_OFFLOAD_RSP:
3441 netdev_dbg(netdev, "Got Control IP offload Response\n");
3442 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3443 sizeof(adapter->ip_offload_ctrl),
3444 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003445 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003446 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003447 case COLLECT_FW_TRACE_RSP:
3448 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3449 complete(&adapter->fw_done);
3450 break;
3451 default:
3452 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3453 gen_crq->cmd);
3454 }
3455}
3456
3457static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3458{
3459 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003460
Thomas Falcon6c267b32017-02-15 12:17:58 -06003461 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003462 return IRQ_HANDLED;
3463}
3464
3465static void ibmvnic_tasklet(void *data)
3466{
3467 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003468 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003469 union ibmvnic_crq *crq;
3470 unsigned long flags;
3471 bool done = false;
3472
3473 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003474 while (!done) {
3475 /* Pull all the valid messages off the CRQ */
3476 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3477 ibmvnic_handle_crq(crq, adapter);
3478 crq->generic.first = 0;
3479 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003480
3481 /* remain in tasklet until all
3482 * capabilities responses are received
3483 */
3484 if (!adapter->wait_capability)
3485 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003486 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003487 /* if capabilities CRQ's were sent in this tasklet, the following
3488 * tasklet must wait until all responses are received
3489 */
3490 if (atomic_read(&adapter->running_cap_crqs) != 0)
3491 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003492 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003493}
3494
3495static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3496{
3497 struct vio_dev *vdev = adapter->vdev;
3498 int rc;
3499
3500 do {
3501 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3502 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3503
3504 if (rc)
3505 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3506
3507 return rc;
3508}
3509
3510static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3511{
3512 struct ibmvnic_crq_queue *crq = &adapter->crq;
3513 struct device *dev = &adapter->vdev->dev;
3514 struct vio_dev *vdev = adapter->vdev;
3515 int rc;
3516
3517 /* Close the CRQ */
3518 do {
3519 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3520 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3521
3522 /* Clean out the queue */
3523 memset(crq->msgs, 0, PAGE_SIZE);
3524 crq->cur = 0;
3525
3526 /* And re-open it again */
3527 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3528 crq->msg_token, PAGE_SIZE);
3529
3530 if (rc == H_CLOSED)
3531 /* Adapter is good, but other end is not ready */
3532 dev_warn(dev, "Partner adapter not ready\n");
3533 else if (rc != 0)
3534 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3535
3536 return rc;
3537}
3538
Nathan Fontenotf9928872017-03-30 02:48:54 -04003539static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003540{
3541 struct ibmvnic_crq_queue *crq = &adapter->crq;
3542 struct vio_dev *vdev = adapter->vdev;
3543 long rc;
3544
Nathan Fontenotf9928872017-03-30 02:48:54 -04003545 if (!crq->msgs)
3546 return;
3547
Thomas Falcon032c5e82015-12-21 11:26:06 -06003548 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3549 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003550 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003551 do {
3552 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3553 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3554
3555 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3556 DMA_BIDIRECTIONAL);
3557 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003558 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003559}
3560
Nathan Fontenotf9928872017-03-30 02:48:54 -04003561static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003562{
3563 struct ibmvnic_crq_queue *crq = &adapter->crq;
3564 struct device *dev = &adapter->vdev->dev;
3565 struct vio_dev *vdev = adapter->vdev;
3566 int rc, retrc = -ENOMEM;
3567
Nathan Fontenotf9928872017-03-30 02:48:54 -04003568 if (crq->msgs)
3569 return 0;
3570
Thomas Falcon032c5e82015-12-21 11:26:06 -06003571 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3572 /* Should we allocate more than one page? */
3573
3574 if (!crq->msgs)
3575 return -ENOMEM;
3576
3577 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3578 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3579 DMA_BIDIRECTIONAL);
3580 if (dma_mapping_error(dev, crq->msg_token))
3581 goto map_failed;
3582
3583 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3584 crq->msg_token, PAGE_SIZE);
3585
3586 if (rc == H_RESOURCE)
3587 /* maybe kexecing and resource is busy. try a reset */
3588 rc = ibmvnic_reset_crq(adapter);
3589 retrc = rc;
3590
3591 if (rc == H_CLOSED) {
3592 dev_warn(dev, "Partner adapter not ready\n");
3593 } else if (rc) {
3594 dev_warn(dev, "Error %d opening adapter\n", rc);
3595 goto reg_crq_failed;
3596 }
3597
3598 retrc = 0;
3599
Thomas Falcon6c267b32017-02-15 12:17:58 -06003600 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3601 (unsigned long)adapter);
3602
Thomas Falcon032c5e82015-12-21 11:26:06 -06003603 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3604 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3605 adapter);
3606 if (rc) {
3607 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3608 vdev->irq, rc);
3609 goto req_irq_failed;
3610 }
3611
3612 rc = vio_enable_interrupts(vdev);
3613 if (rc) {
3614 dev_err(dev, "Error %d enabling interrupts\n", rc);
3615 goto req_irq_failed;
3616 }
3617
3618 crq->cur = 0;
3619 spin_lock_init(&crq->lock);
3620
3621 return retrc;
3622
3623req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003624 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003625 do {
3626 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3627 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3628reg_crq_failed:
3629 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3630map_failed:
3631 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003632 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003633 return retrc;
3634}
3635
John Allenf6ef6402017-03-17 17:13:42 -05003636static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3637{
3638 struct device *dev = &adapter->vdev->dev;
3639 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003640 int rc;
3641
Nathan Fontenot28cde752017-05-26 10:31:00 -04003642 if (adapter->resetting) {
3643 rc = ibmvnic_reset_crq(adapter);
3644 if (!rc)
3645 rc = vio_enable_interrupts(adapter->vdev);
3646 } else {
3647 rc = init_crq_queue(adapter);
3648 }
3649
John Allenf6ef6402017-03-17 17:13:42 -05003650 if (rc) {
3651 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3652 return rc;
3653 }
3654
John Allen017892c12017-05-26 10:30:19 -04003655 adapter->from_passive_init = false;
3656
John Allenf6ef6402017-03-17 17:13:42 -05003657 init_completion(&adapter->init_done);
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003658 adapter->init_done_rc = 0;
John Allenf6ef6402017-03-17 17:13:42 -05003659 ibmvnic_send_crq_init(adapter);
3660 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3661 dev_err(dev, "Initialization sequence timed out\n");
John Allen017892c12017-05-26 10:30:19 -04003662 return -1;
3663 }
3664
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003665 if (adapter->init_done_rc) {
3666 release_crq_queue(adapter);
3667 return adapter->init_done_rc;
3668 }
3669
John Allen017892c12017-05-26 10:30:19 -04003670 if (adapter->from_passive_init) {
3671 adapter->state = VNIC_OPEN;
3672 adapter->from_passive_init = false;
John Allenf6ef6402017-03-17 17:13:42 -05003673 return -1;
3674 }
3675
Nathan Fontenot57a49432017-05-26 10:31:12 -04003676 if (adapter->resetting)
3677 rc = reset_sub_crq_queues(adapter);
3678 else
3679 rc = init_sub_crqs(adapter);
Nathan Fontenot1bb3c732017-04-25 15:01:10 -04003680 if (rc) {
3681 dev_err(dev, "Initialization of sub crqs failed\n");
3682 release_crq_queue(adapter);
3683 }
3684
3685 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003686}
3687
Thomas Falcon40c9db82017-06-12 12:35:04 -05003688static struct device_attribute dev_attr_failover;
3689
Thomas Falcon032c5e82015-12-21 11:26:06 -06003690static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3691{
3692 struct ibmvnic_adapter *adapter;
3693 struct net_device *netdev;
3694 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003695 int rc;
3696
3697 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3698 dev->unit_address);
3699
3700 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3701 VETH_MAC_ADDR, NULL);
3702 if (!mac_addr_p) {
3703 dev_err(&dev->dev,
3704 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3705 __FILE__, __LINE__);
3706 return 0;
3707 }
3708
3709 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3710 IBMVNIC_MAX_TX_QUEUES);
3711 if (!netdev)
3712 return -ENOMEM;
3713
3714 adapter = netdev_priv(netdev);
Nathan Fontenot90c80142017-05-03 14:04:32 -04003715 adapter->state = VNIC_PROBING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003716 dev_set_drvdata(&dev->dev, netdev);
3717 adapter->vdev = dev;
3718 adapter->netdev = netdev;
3719
3720 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3721 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3722 netdev->irq = dev->irq;
3723 netdev->netdev_ops = &ibmvnic_netdev_ops;
3724 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3725 SET_NETDEV_DEV(netdev, &dev->dev);
3726
3727 spin_lock_init(&adapter->stats_lock);
3728
Thomas Falcon032c5e82015-12-21 11:26:06 -06003729 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003730 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003731
Nathan Fontenoted651a12017-05-03 14:04:38 -04003732 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3733 INIT_LIST_HEAD(&adapter->rwi_list);
3734 mutex_init(&adapter->reset_lock);
3735 mutex_init(&adapter->rwi_lock);
3736 adapter->resetting = false;
3737
Nathan Fontenot6a2fb0e2017-06-15 14:48:09 -04003738 do {
3739 rc = ibmvnic_init(adapter);
3740 if (rc != EAGAIN) {
3741 free_netdev(netdev);
3742 return rc;
3743 }
3744 } while (rc == EAGAIN);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003745
Thomas Falconf39f0d12017-02-14 10:22:59 -06003746 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003747
Thomas Falcon40c9db82017-06-12 12:35:04 -05003748 rc = device_create_file(&dev->dev, &dev_attr_failover);
3749 if (rc) {
3750 free_netdev(netdev);
3751 return rc;
3752 }
3753
Thomas Falcon032c5e82015-12-21 11:26:06 -06003754 rc = register_netdev(netdev);
3755 if (rc) {
3756 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003757 device_remove_file(&dev->dev, &dev_attr_failover);
John Allenf6ef6402017-03-17 17:13:42 -05003758 free_netdev(netdev);
3759 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003760 }
3761 dev_info(&dev->dev, "ibmvnic registered\n");
3762
Nathan Fontenot90c80142017-05-03 14:04:32 -04003763 adapter->state = VNIC_PROBED;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003764 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003765}
3766
3767static int ibmvnic_remove(struct vio_dev *dev)
3768{
3769 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003770 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003771
Nathan Fontenot90c80142017-05-03 14:04:32 -04003772 adapter->state = VNIC_REMOVING;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003773 unregister_netdev(netdev);
Nathan Fontenoted651a12017-05-03 14:04:38 -04003774 mutex_lock(&adapter->reset_lock);
Nathan Fontenot37489052017-04-19 13:45:04 -04003775
3776 release_resources(adapter);
3777 release_sub_crqs(adapter);
3778 release_crq_queue(adapter);
3779
Nathan Fontenot90c80142017-05-03 14:04:32 -04003780 adapter->state = VNIC_REMOVED;
3781
Nathan Fontenoted651a12017-05-03 14:04:38 -04003782 mutex_unlock(&adapter->reset_lock);
Thomas Falcon40c9db82017-06-12 12:35:04 -05003783 device_remove_file(&dev->dev, &dev_attr_failover);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003784 free_netdev(netdev);
3785 dev_set_drvdata(&dev->dev, NULL);
3786
3787 return 0;
3788}
3789
Thomas Falcon40c9db82017-06-12 12:35:04 -05003790static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3791 const char *buf, size_t count)
3792{
3793 struct net_device *netdev = dev_get_drvdata(dev);
3794 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3795 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3796 __be64 session_token;
3797 long rc;
3798
3799 if (!sysfs_streq(buf, "1"))
3800 return -EINVAL;
3801
3802 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3803 H_GET_SESSION_TOKEN, 0, 0, 0);
3804 if (rc) {
3805 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3806 rc);
3807 return -EINVAL;
3808 }
3809
3810 session_token = (__be64)retbuf[0];
3811 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3812 be64_to_cpu(session_token));
3813 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3814 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3815 if (rc) {
3816 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3817 rc);
3818 return -EINVAL;
3819 }
3820
3821 return count;
3822}
3823
3824static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3825
Thomas Falcon032c5e82015-12-21 11:26:06 -06003826static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3827{
3828 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3829 struct ibmvnic_adapter *adapter;
3830 struct iommu_table *tbl;
3831 unsigned long ret = 0;
3832 int i;
3833
3834 tbl = get_iommu_table_base(&vdev->dev);
3835
3836 /* netdev inits at probe time along with the structures we need below*/
3837 if (!netdev)
3838 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3839
3840 adapter = netdev_priv(netdev);
3841
3842 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003843 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3844
3845 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3846 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3847
3848 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3849 i++)
3850 ret += adapter->rx_pool[i].size *
3851 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3852
3853 return ret;
3854}
3855
3856static int ibmvnic_resume(struct device *dev)
3857{
3858 struct net_device *netdev = dev_get_drvdata(dev);
3859 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3860 int i;
3861
3862 /* kick the interrupt handlers just in case we lost an interrupt */
3863 for (i = 0; i < adapter->req_rx_queues; i++)
3864 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3865 adapter->rx_scrq[i]);
3866
3867 return 0;
3868}
3869
3870static struct vio_device_id ibmvnic_device_table[] = {
3871 {"network", "IBM,vnic"},
3872 {"", "" }
3873};
3874MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3875
3876static const struct dev_pm_ops ibmvnic_pm_ops = {
3877 .resume = ibmvnic_resume
3878};
3879
3880static struct vio_driver ibmvnic_driver = {
3881 .id_table = ibmvnic_device_table,
3882 .probe = ibmvnic_probe,
3883 .remove = ibmvnic_remove,
3884 .get_desired_dma = ibmvnic_get_desired_dma,
3885 .name = ibmvnic_driver_name,
3886 .pm = &ibmvnic_pm_ops,
3887};
3888
3889/* module functions */
3890static int __init ibmvnic_module_init(void)
3891{
3892 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3893 IBMVNIC_DRIVER_VERSION);
3894
3895 return vio_register_driver(&ibmvnic_driver);
3896}
3897
3898static void __exit ibmvnic_module_exit(void)
3899{
3900 vio_unregister_driver(&ibmvnic_driver);
3901}
3902
3903module_init(ibmvnic_module_init);
3904module_exit(ibmvnic_module_exit);