blob: f2d2f1f1ce1c9a423bc1e7bdc4be4bb01bed5e78 [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>
Thomas Falcon032c5e82015-12-21 11:26:06 -060077
78#include "ibmvnic.h"
79
80static const char ibmvnic_driver_name[] = "ibmvnic";
81static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
82
83MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
84MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
87
88static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
89static int ibmvnic_remove(struct vio_dev *);
90static void release_sub_crqs(struct ibmvnic_adapter *);
91static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
92static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
93static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
94static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
95static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
96 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050097static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060098static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
99static int enable_scrq_irq(struct ibmvnic_adapter *,
100 struct ibmvnic_sub_crq_queue *);
101static int disable_scrq_irq(struct ibmvnic_adapter *,
102 struct ibmvnic_sub_crq_queue *);
103static int pending_scrq(struct ibmvnic_adapter *,
104 struct ibmvnic_sub_crq_queue *);
105static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
106 struct ibmvnic_sub_crq_queue *);
107static int ibmvnic_poll(struct napi_struct *napi, int data);
108static void send_map_query(struct ibmvnic_adapter *adapter);
109static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
110static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500111static void send_login(struct ibmvnic_adapter *adapter);
112static void send_cap_queries(struct ibmvnic_adapter *adapter);
113static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500114static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400115static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600116
117struct ibmvnic_stat {
118 char name[ETH_GSTRING_LEN];
119 int offset;
120};
121
122#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
123 offsetof(struct ibmvnic_statistics, stat))
124#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
125
126static const struct ibmvnic_stat ibmvnic_stats[] = {
127 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
128 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
129 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
130 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
131 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
132 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
133 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
134 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
135 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
136 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
137 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
138 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
139 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
140 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
141 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
142 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
143 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
144 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
145 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
146 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
147 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
148 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
149};
150
151static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
152 unsigned long length, unsigned long *number,
153 unsigned long *irq)
154{
155 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
156 long rc;
157
158 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
159 *number = retbuf[0];
160 *irq = retbuf[1];
161
162 return rc;
163}
164
Thomas Falcon032c5e82015-12-21 11:26:06 -0600165static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
166 struct ibmvnic_long_term_buff *ltb, int size)
167{
168 struct device *dev = &adapter->vdev->dev;
169
170 ltb->size = size;
171 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
172 GFP_KERNEL);
173
174 if (!ltb->buff) {
175 dev_err(dev, "Couldn't alloc long term buffer\n");
176 return -ENOMEM;
177 }
178 ltb->map_id = adapter->map_id;
179 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500180
181 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600182 send_request_map(adapter, ltb->addr,
183 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600184 wait_for_completion(&adapter->fw_done);
185 return 0;
186}
187
188static void free_long_term_buff(struct ibmvnic_adapter *adapter,
189 struct ibmvnic_long_term_buff *ltb)
190{
191 struct device *dev = &adapter->vdev->dev;
192
Nathan Fontenotc657e322017-03-30 02:49:06 -0400193 if (!ltb->buff)
194 return;
195
Thomas Falcon032c5e82015-12-21 11:26:06 -0600196 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcondfad09a2016-08-18 11:37:51 -0500197 if (!adapter->failover)
198 send_request_unmap(adapter, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600199}
200
Thomas Falcon032c5e82015-12-21 11:26:06 -0600201static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
202 struct ibmvnic_rx_pool *pool)
203{
204 int count = pool->size - atomic_read(&pool->available);
205 struct device *dev = &adapter->vdev->dev;
206 int buffers_added = 0;
207 unsigned long lpar_rc;
208 union sub_crq sub_crq;
209 struct sk_buff *skb;
210 unsigned int offset;
211 dma_addr_t dma_addr;
212 unsigned char *dst;
213 u64 *handle_array;
214 int shift = 0;
215 int index;
216 int i;
217
218 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
219 be32_to_cpu(adapter->login_rsp_buf->
220 off_rxadd_subcrqs));
221
222 for (i = 0; i < count; ++i) {
223 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
224 if (!skb) {
225 dev_err(dev, "Couldn't replenish rx buff\n");
226 adapter->replenish_no_mem++;
227 break;
228 }
229
230 index = pool->free_map[pool->next_free];
231
232 if (pool->rx_buff[index].skb)
233 dev_err(dev, "Inconsistent free_map!\n");
234
235 /* Copy the skb to the long term mapped DMA buffer */
236 offset = index * pool->buff_size;
237 dst = pool->long_term_buff.buff + offset;
238 memset(dst, 0, pool->buff_size);
239 dma_addr = pool->long_term_buff.addr + offset;
240 pool->rx_buff[index].data = dst;
241
242 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
243 pool->rx_buff[index].dma = dma_addr;
244 pool->rx_buff[index].skb = skb;
245 pool->rx_buff[index].pool_index = pool->index;
246 pool->rx_buff[index].size = pool->buff_size;
247
248 memset(&sub_crq, 0, sizeof(sub_crq));
249 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
250 sub_crq.rx_add.correlator =
251 cpu_to_be64((u64)&pool->rx_buff[index]);
252 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
253 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
254
255 /* The length field of the sCRQ is defined to be 24 bits so the
256 * buffer size needs to be left shifted by a byte before it is
257 * converted to big endian to prevent the last byte from being
258 * truncated.
259 */
260#ifdef __LITTLE_ENDIAN__
261 shift = 8;
262#endif
263 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
264
265 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
266 &sub_crq);
267 if (lpar_rc != H_SUCCESS)
268 goto failure;
269
270 buffers_added++;
271 adapter->replenish_add_buff_success++;
272 pool->next_free = (pool->next_free + 1) % pool->size;
273 }
274 atomic_add(buffers_added, &pool->available);
275 return;
276
277failure:
278 dev_info(dev, "replenish pools failure\n");
279 pool->free_map[pool->next_free] = index;
280 pool->rx_buff[index].skb = NULL;
281 if (!dma_mapping_error(dev, dma_addr))
282 dma_unmap_single(dev, dma_addr, pool->buff_size,
283 DMA_FROM_DEVICE);
284
285 dev_kfree_skb_any(skb);
286 adapter->replenish_add_buff_failure++;
287 atomic_add(buffers_added, &pool->available);
288}
289
290static void replenish_pools(struct ibmvnic_adapter *adapter)
291{
292 int i;
293
294 if (adapter->migrated)
295 return;
296
297 adapter->replenish_task_cycles++;
298 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
299 i++) {
300 if (adapter->rx_pool[i].active)
301 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
302 }
303}
304
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400305static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600306{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400307 struct ibmvnic_rx_pool *rx_pool;
308 int rx_scrqs;
309 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600310
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400311 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600312 return;
313
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400314 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
315 for (i = 0; i < rx_scrqs; i++) {
316 rx_pool = &adapter->rx_pool[i];
317
318 kfree(rx_pool->free_map);
319 free_long_term_buff(adapter, &rx_pool->long_term_buff);
320
321 if (!rx_pool->rx_buff)
322 continue;
323
324 for (j = 0; j < rx_pool->size; j++) {
325 if (rx_pool->rx_buff[j].skb) {
326 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
327 rx_pool->rx_buff[i].skb = NULL;
328 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600329 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400330
331 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600332 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400333
334 kfree(adapter->rx_pool);
335 adapter->rx_pool = NULL;
336}
337
338static int init_rx_pools(struct net_device *netdev)
339{
340 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
341 struct device *dev = &adapter->vdev->dev;
342 struct ibmvnic_rx_pool *rx_pool;
343 int rxadd_subcrqs;
344 u64 *size_array;
345 int i, j;
346
347 rxadd_subcrqs =
348 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
349 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
350 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
351
352 adapter->rx_pool = kcalloc(rxadd_subcrqs,
353 sizeof(struct ibmvnic_rx_pool),
354 GFP_KERNEL);
355 if (!adapter->rx_pool) {
356 dev_err(dev, "Failed to allocate rx pools\n");
357 return -1;
358 }
359
360 for (i = 0; i < rxadd_subcrqs; i++) {
361 rx_pool = &adapter->rx_pool[i];
362
363 netdev_dbg(adapter->netdev,
364 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
365 i, adapter->req_rx_add_entries_per_subcrq,
366 be64_to_cpu(size_array[i]));
367
368 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
369 rx_pool->index = i;
370 rx_pool->buff_size = be64_to_cpu(size_array[i]);
371 rx_pool->active = 1;
372
373 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
374 GFP_KERNEL);
375 if (!rx_pool->free_map) {
376 release_rx_pools(adapter);
377 return -1;
378 }
379
380 rx_pool->rx_buff = kcalloc(rx_pool->size,
381 sizeof(struct ibmvnic_rx_buff),
382 GFP_KERNEL);
383 if (!rx_pool->rx_buff) {
384 dev_err(dev, "Couldn't alloc rx buffers\n");
385 release_rx_pools(adapter);
386 return -1;
387 }
388
389 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
390 rx_pool->size * rx_pool->buff_size)) {
391 release_rx_pools(adapter);
392 return -1;
393 }
394
395 for (j = 0; j < rx_pool->size; ++j)
396 rx_pool->free_map[j] = j;
397
398 atomic_set(&rx_pool->available, 0);
399 rx_pool->next_alloc = 0;
400 rx_pool->next_free = 0;
401 }
402
403 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600404}
405
Nathan Fontenotc657e322017-03-30 02:49:06 -0400406static void release_tx_pools(struct ibmvnic_adapter *adapter)
407{
408 struct ibmvnic_tx_pool *tx_pool;
409 int i, tx_scrqs;
410
411 if (!adapter->tx_pool)
412 return;
413
414 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
415 for (i = 0; i < tx_scrqs; i++) {
416 tx_pool = &adapter->tx_pool[i];
417 kfree(tx_pool->tx_buff);
418 free_long_term_buff(adapter, &tx_pool->long_term_buff);
419 kfree(tx_pool->free_map);
420 }
421
422 kfree(adapter->tx_pool);
423 adapter->tx_pool = NULL;
424}
425
426static int init_tx_pools(struct net_device *netdev)
427{
428 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
429 struct device *dev = &adapter->vdev->dev;
430 struct ibmvnic_tx_pool *tx_pool;
431 int tx_subcrqs;
432 int i, j;
433
434 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
435 adapter->tx_pool = kcalloc(tx_subcrqs,
436 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
437 if (!adapter->tx_pool)
438 return -1;
439
440 for (i = 0; i < tx_subcrqs; i++) {
441 tx_pool = &adapter->tx_pool[i];
442 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
443 sizeof(struct ibmvnic_tx_buff),
444 GFP_KERNEL);
445 if (!tx_pool->tx_buff) {
446 dev_err(dev, "tx pool buffer allocation failed\n");
447 release_tx_pools(adapter);
448 return -1;
449 }
450
451 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
452 adapter->req_tx_entries_per_subcrq *
453 adapter->req_mtu)) {
454 release_tx_pools(adapter);
455 return -1;
456 }
457
458 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
459 sizeof(int), GFP_KERNEL);
460 if (!tx_pool->free_map) {
461 release_tx_pools(adapter);
462 return -1;
463 }
464
465 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
466 tx_pool->free_map[j] = j;
467
468 tx_pool->consumer_index = 0;
469 tx_pool->producer_index = 0;
470 }
471
472 return 0;
473}
474
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400475static void release_bounce_buffer(struct ibmvnic_adapter *adapter)
476{
477 struct device *dev = &adapter->vdev->dev;
478
479 if (!adapter->bounce_buffer)
480 return;
481
482 if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
483 dma_unmap_single(dev, adapter->bounce_buffer_dma,
484 adapter->bounce_buffer_size,
485 DMA_BIDIRECTIONAL);
486 adapter->bounce_buffer_dma = DMA_ERROR_CODE;
487 }
488
489 kfree(adapter->bounce_buffer);
490 adapter->bounce_buffer = NULL;
491}
492
493static int init_bounce_buffer(struct net_device *netdev)
494{
495 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
496 struct device *dev = &adapter->vdev->dev;
497 char *buf;
498 int buf_sz;
499 dma_addr_t map_addr;
500
501 buf_sz = (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
502 buf = kmalloc(adapter->bounce_buffer_size, GFP_KERNEL);
503 if (!buf)
504 return -1;
505
506 map_addr = dma_map_single(dev, buf, buf_sz, DMA_TO_DEVICE);
507 if (dma_mapping_error(dev, map_addr)) {
508 dev_err(dev, "Couldn't map bounce buffer\n");
509 kfree(buf);
510 return -1;
511 }
512
513 adapter->bounce_buffer = buf;
514 adapter->bounce_buffer_size = buf_sz;
515 adapter->bounce_buffer_dma = map_addr;
516 return 0;
517}
518
John Allena57a5d22017-03-17 17:13:41 -0500519static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600520{
521 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500522 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600523 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600524
John Allenbd0b6722017-03-17 17:13:40 -0500525 do {
526 if (adapter->renegotiate) {
527 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400528 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500529
530 reinit_completion(&adapter->init_done);
531 send_cap_queries(adapter);
532 if (!wait_for_completion_timeout(&adapter->init_done,
533 timeout)) {
534 dev_err(dev, "Capabilities query timeout\n");
535 return -1;
536 }
537 }
538
539 reinit_completion(&adapter->init_done);
540 send_login(adapter);
541 if (!wait_for_completion_timeout(&adapter->init_done,
542 timeout)) {
543 dev_err(dev, "Login timeout\n");
544 return -1;
545 }
546 } while (adapter->renegotiate);
547
John Allena57a5d22017-03-17 17:13:41 -0500548 return 0;
549}
550
551static int ibmvnic_open(struct net_device *netdev)
552{
553 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
554 struct device *dev = &adapter->vdev->dev;
John Allena57a5d22017-03-17 17:13:41 -0500555 union ibmvnic_crq crq;
556 int rxadd_subcrqs;
John Allena57a5d22017-03-17 17:13:41 -0500557 int tx_subcrqs;
558 int rc = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400559 int i;
John Allena57a5d22017-03-17 17:13:41 -0500560
John Allenea5509f2017-03-17 17:13:43 -0500561 if (adapter->is_closed) {
562 rc = ibmvnic_init(adapter);
563 if (rc)
564 return rc;
565 }
566
John Allena57a5d22017-03-17 17:13:41 -0500567 rc = ibmvnic_login(netdev);
568 if (rc)
569 return rc;
570
John Allenbd0b6722017-03-17 17:13:40 -0500571 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
572 if (rc) {
573 dev_err(dev, "failed to set the number of tx queues\n");
574 return -1;
575 }
576
577 rc = init_sub_crq_irqs(adapter);
578 if (rc) {
579 dev_err(dev, "failed to initialize sub crq irqs\n");
580 return -1;
581 }
582
Thomas Falcon032c5e82015-12-21 11:26:06 -0600583 rxadd_subcrqs =
584 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
585 tx_subcrqs =
586 be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400587
Thomas Falcon032c5e82015-12-21 11:26:06 -0600588 adapter->map_id = 1;
589 adapter->napi = kcalloc(adapter->req_rx_queues,
590 sizeof(struct napi_struct), GFP_KERNEL);
591 if (!adapter->napi)
592 goto alloc_napi_failed;
593 for (i = 0; i < adapter->req_rx_queues; i++) {
594 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
595 NAPI_POLL_WEIGHT);
596 napi_enable(&adapter->napi[i]);
597 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600598
Thomas Falcon032c5e82015-12-21 11:26:06 -0600599 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400600
601 rc = init_rx_pools(netdev);
602 if (rc)
603 goto rx_pool_failed;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600604
Nathan Fontenotc657e322017-03-30 02:49:06 -0400605 rc = init_tx_pools(netdev);
606 if (rc)
607 goto tx_pool_failed;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600608
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400609 rc = init_bounce_buffer(netdev);
610 if (rc)
611 goto bounce_init_failed;
612
Thomas Falcon032c5e82015-12-21 11:26:06 -0600613 replenish_pools(adapter);
614
615 /* We're ready to receive frames, enable the sub-crq interrupts and
616 * set the logical link state to up
617 */
618 for (i = 0; i < adapter->req_rx_queues; i++)
619 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
620
621 for (i = 0; i < adapter->req_tx_queues; i++)
622 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
623
624 memset(&crq, 0, sizeof(crq));
625 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
626 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
627 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
628 ibmvnic_send_crq(adapter, &crq);
629
Thomas Falconb8efb892016-07-06 15:35:15 -0500630 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500631 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500632
Thomas Falcon032c5e82015-12-21 11:26:06 -0600633 return 0;
634
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400635bounce_init_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600636 i = tx_subcrqs - 1;
637 kfree(adapter->tx_pool[i].free_map);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400638tx_pool_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600639 i = rxadd_subcrqs;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400640rx_pool_failed:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600641 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500642 napi_disable(&adapter->napi[i]);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600643alloc_napi_failed:
John Allenbd0b6722017-03-17 17:13:40 -0500644 release_sub_crqs(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600645 return -ENOMEM;
646}
647
John Allenea5509f2017-03-17 17:13:43 -0500648static void ibmvnic_release_resources(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600649{
Thomas Falcon032c5e82015-12-21 11:26:06 -0600650 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600651
Nathan Fontenotf0b8c962017-03-30 02:49:00 -0400652 release_bounce_buffer(adapter);
Nathan Fontenotc657e322017-03-30 02:49:06 -0400653 release_tx_pools(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400654 release_rx_pools(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500655
656 release_sub_crqs(adapter);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400657 release_crq_queue(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500658
John Allenea5509f2017-03-17 17:13:43 -0500659 if (adapter->stats_token)
660 dma_unmap_single(dev, adapter->stats_token,
661 sizeof(struct ibmvnic_statistics),
662 DMA_FROM_DEVICE);
John Allenea5509f2017-03-17 17:13:43 -0500663}
664
665static int ibmvnic_close(struct net_device *netdev)
666{
667 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
668 union ibmvnic_crq crq;
669 int i;
670
671 adapter->closing = true;
672
673 for (i = 0; i < adapter->req_rx_queues; i++)
674 napi_disable(&adapter->napi[i]);
675
676 if (!adapter->failover)
677 netif_tx_stop_all_queues(netdev);
678
Thomas Falcon032c5e82015-12-21 11:26:06 -0600679 memset(&crq, 0, sizeof(crq));
680 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
681 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
682 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
683 ibmvnic_send_crq(adapter, &crq);
684
John Allenea5509f2017-03-17 17:13:43 -0500685 ibmvnic_release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600686
John Allenea5509f2017-03-17 17:13:43 -0500687 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600688 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600689 return 0;
690}
691
Thomas Falconad7775d2016-04-01 17:20:34 -0500692/**
693 * build_hdr_data - creates L2/L3/L4 header data buffer
694 * @hdr_field - bitfield determining needed headers
695 * @skb - socket buffer
696 * @hdr_len - array of header lengths
697 * @tot_len - total length of data
698 *
699 * Reads hdr_field to determine which headers are needed by firmware.
700 * Builds a buffer containing these headers. Saves individual header
701 * lengths and total buffer length to be used to build descriptors.
702 */
703static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
704 int *hdr_len, u8 *hdr_data)
705{
706 int len = 0;
707 u8 *hdr;
708
709 hdr_len[0] = sizeof(struct ethhdr);
710
711 if (skb->protocol == htons(ETH_P_IP)) {
712 hdr_len[1] = ip_hdr(skb)->ihl * 4;
713 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
714 hdr_len[2] = tcp_hdrlen(skb);
715 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
716 hdr_len[2] = sizeof(struct udphdr);
717 } else if (skb->protocol == htons(ETH_P_IPV6)) {
718 hdr_len[1] = sizeof(struct ipv6hdr);
719 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
720 hdr_len[2] = tcp_hdrlen(skb);
721 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
722 hdr_len[2] = sizeof(struct udphdr);
723 }
724
725 memset(hdr_data, 0, 120);
726 if ((hdr_field >> 6) & 1) {
727 hdr = skb_mac_header(skb);
728 memcpy(hdr_data, hdr, hdr_len[0]);
729 len += hdr_len[0];
730 }
731
732 if ((hdr_field >> 5) & 1) {
733 hdr = skb_network_header(skb);
734 memcpy(hdr_data + len, hdr, hdr_len[1]);
735 len += hdr_len[1];
736 }
737
738 if ((hdr_field >> 4) & 1) {
739 hdr = skb_transport_header(skb);
740 memcpy(hdr_data + len, hdr, hdr_len[2]);
741 len += hdr_len[2];
742 }
743 return len;
744}
745
746/**
747 * create_hdr_descs - create header and header extension descriptors
748 * @hdr_field - bitfield determining needed headers
749 * @data - buffer containing header data
750 * @len - length of data buffer
751 * @hdr_len - array of individual header lengths
752 * @scrq_arr - descriptor array
753 *
754 * Creates header and, if needed, header extension descriptors and
755 * places them in a descriptor array, scrq_arr
756 */
757
758static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
759 union sub_crq *scrq_arr)
760{
761 union sub_crq hdr_desc;
762 int tmp_len = len;
763 u8 *data, *cur;
764 int tmp;
765
766 while (tmp_len > 0) {
767 cur = hdr_data + len - tmp_len;
768
769 memset(&hdr_desc, 0, sizeof(hdr_desc));
770 if (cur != hdr_data) {
771 data = hdr_desc.hdr_ext.data;
772 tmp = tmp_len > 29 ? 29 : tmp_len;
773 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
774 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
775 hdr_desc.hdr_ext.len = tmp;
776 } else {
777 data = hdr_desc.hdr.data;
778 tmp = tmp_len > 24 ? 24 : tmp_len;
779 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
780 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
781 hdr_desc.hdr.len = tmp;
782 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
783 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
784 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
785 hdr_desc.hdr.flag = hdr_field << 1;
786 }
787 memcpy(data, cur, tmp);
788 tmp_len -= tmp;
789 *scrq_arr = hdr_desc;
790 scrq_arr++;
791 }
792}
793
794/**
795 * build_hdr_descs_arr - build a header descriptor array
796 * @skb - socket buffer
797 * @num_entries - number of descriptors to be sent
798 * @subcrq - first TX descriptor
799 * @hdr_field - bit field determining which headers will be sent
800 *
801 * This function will build a TX descriptor array with applicable
802 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
803 */
804
805static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
806 int *num_entries, u8 hdr_field)
807{
808 int hdr_len[3] = {0, 0, 0};
809 int tot_len, len;
810 u8 *hdr_data = txbuff->hdr_data;
811
812 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
813 txbuff->hdr_data);
814 len = tot_len;
815 len -= 24;
816 if (len > 0)
817 num_entries += len % 29 ? len / 29 + 1 : len / 29;
818 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
819 txbuff->indir_arr + 1);
820}
821
Thomas Falcon032c5e82015-12-21 11:26:06 -0600822static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
823{
824 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
825 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500826 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600827 struct device *dev = &adapter->vdev->dev;
828 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600829 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600830 struct ibmvnic_tx_pool *tx_pool;
831 unsigned int tx_send_failed = 0;
832 unsigned int tx_map_failed = 0;
833 unsigned int tx_dropped = 0;
834 unsigned int tx_packets = 0;
835 unsigned int tx_bytes = 0;
836 dma_addr_t data_dma_addr;
837 struct netdev_queue *txq;
838 bool used_bounce = false;
839 unsigned long lpar_rc;
840 union sub_crq tx_crq;
841 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500842 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600843 unsigned char *dst;
844 u64 *handle_array;
845 int index = 0;
846 int ret = 0;
847
848 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600849 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600850 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
851 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
852 be32_to_cpu(adapter->login_rsp_buf->
853 off_txsubm_subcrqs));
854 if (adapter->migrated) {
855 tx_send_failed++;
856 tx_dropped++;
857 ret = NETDEV_TX_BUSY;
858 goto out;
859 }
860
861 index = tx_pool->free_map[tx_pool->consumer_index];
862 offset = index * adapter->req_mtu;
863 dst = tx_pool->long_term_buff.buff + offset;
864 memset(dst, 0, adapter->req_mtu);
865 skb_copy_from_linear_data(skb, dst, skb->len);
866 data_dma_addr = tx_pool->long_term_buff.addr + offset;
867
868 tx_pool->consumer_index =
869 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600870 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600871
872 tx_buff = &tx_pool->tx_buff[index];
873 tx_buff->skb = skb;
874 tx_buff->data_dma[0] = data_dma_addr;
875 tx_buff->data_len[0] = skb->len;
876 tx_buff->index = index;
877 tx_buff->pool_index = queue_num;
878 tx_buff->last_frag = true;
879 tx_buff->used_bounce = used_bounce;
880
881 memset(&tx_crq, 0, sizeof(tx_crq));
882 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
883 tx_crq.v1.type = IBMVNIC_TX_DESC;
884 tx_crq.v1.n_crq_elem = 1;
885 tx_crq.v1.n_sge = 1;
886 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
887 tx_crq.v1.correlator = cpu_to_be32(index);
888 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
889 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
890 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
891
892 if (adapter->vlan_header_insertion) {
893 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
894 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
895 }
896
897 if (skb->protocol == htons(ETH_P_IP)) {
898 if (ip_hdr(skb)->version == 4)
899 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
900 else if (ip_hdr(skb)->version == 6)
901 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
902
903 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
904 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
905 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
906 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
907 }
908
Thomas Falconad7775d2016-04-01 17:20:34 -0500909 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600910 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500911 hdrs += 2;
912 }
913 /* determine if l2/3/4 headers are sent to firmware */
914 if ((*hdrs >> 7) & 1 &&
915 (skb->protocol == htons(ETH_P_IP) ||
916 skb->protocol == htons(ETH_P_IPV6))) {
917 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
918 tx_crq.v1.n_crq_elem = num_entries;
919 tx_buff->indir_arr[0] = tx_crq;
920 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
921 sizeof(tx_buff->indir_arr),
922 DMA_TO_DEVICE);
923 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
924 if (!firmware_has_feature(FW_FEATURE_CMO))
925 dev_err(dev, "tx: unable to map descriptor array\n");
926 tx_map_failed++;
927 tx_dropped++;
928 ret = NETDEV_TX_BUSY;
929 goto out;
930 }
John Allen498cd8e2016-04-06 11:49:55 -0500931 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500932 (u64)tx_buff->indir_dma,
933 (u64)num_entries);
934 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500935 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
936 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500937 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600938 if (lpar_rc != H_SUCCESS) {
939 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
940
941 if (tx_pool->consumer_index == 0)
942 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600943 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600944 else
945 tx_pool->consumer_index--;
946
947 tx_send_failed++;
948 tx_dropped++;
949 ret = NETDEV_TX_BUSY;
950 goto out;
951 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600952
953 atomic_inc(&tx_scrq->used);
954
955 if (atomic_read(&tx_scrq->used) >= adapter->req_tx_entries_per_subcrq) {
956 netdev_info(netdev, "Stopping queue %d\n", queue_num);
957 netif_stop_subqueue(netdev, queue_num);
958 }
959
Thomas Falcon032c5e82015-12-21 11:26:06 -0600960 tx_packets++;
961 tx_bytes += skb->len;
962 txq->trans_start = jiffies;
963 ret = NETDEV_TX_OK;
964
965out:
966 netdev->stats.tx_dropped += tx_dropped;
967 netdev->stats.tx_bytes += tx_bytes;
968 netdev->stats.tx_packets += tx_packets;
969 adapter->tx_send_failed += tx_send_failed;
970 adapter->tx_map_failed += tx_map_failed;
971
972 return ret;
973}
974
975static void ibmvnic_set_multi(struct net_device *netdev)
976{
977 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
978 struct netdev_hw_addr *ha;
979 union ibmvnic_crq crq;
980
981 memset(&crq, 0, sizeof(crq));
982 crq.request_capability.first = IBMVNIC_CRQ_CMD;
983 crq.request_capability.cmd = REQUEST_CAPABILITY;
984
985 if (netdev->flags & IFF_PROMISC) {
986 if (!adapter->promisc_supported)
987 return;
988 } else {
989 if (netdev->flags & IFF_ALLMULTI) {
990 /* Accept all multicast */
991 memset(&crq, 0, sizeof(crq));
992 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
993 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
994 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
995 ibmvnic_send_crq(adapter, &crq);
996 } else if (netdev_mc_empty(netdev)) {
997 /* Reject all multicast */
998 memset(&crq, 0, sizeof(crq));
999 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1000 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1001 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1002 ibmvnic_send_crq(adapter, &crq);
1003 } else {
1004 /* Accept one or more multicast(s) */
1005 netdev_for_each_mc_addr(ha, netdev) {
1006 memset(&crq, 0, sizeof(crq));
1007 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1008 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1009 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1010 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1011 ha->addr);
1012 ibmvnic_send_crq(adapter, &crq);
1013 }
1014 }
1015 }
1016}
1017
1018static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1019{
1020 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1021 struct sockaddr *addr = p;
1022 union ibmvnic_crq crq;
1023
1024 if (!is_valid_ether_addr(addr->sa_data))
1025 return -EADDRNOTAVAIL;
1026
1027 memset(&crq, 0, sizeof(crq));
1028 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1029 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1030 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1031 ibmvnic_send_crq(adapter, &crq);
1032 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1033 return 0;
1034}
1035
Thomas Falcon032c5e82015-12-21 11:26:06 -06001036static void ibmvnic_tx_timeout(struct net_device *dev)
1037{
1038 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1039 int rc;
1040
1041 /* Adapter timed out, resetting it */
1042 release_sub_crqs(adapter);
1043 rc = ibmvnic_reset_crq(adapter);
1044 if (rc)
1045 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1046 else
1047 ibmvnic_send_crq_init(adapter);
1048}
1049
1050static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1051 struct ibmvnic_rx_buff *rx_buff)
1052{
1053 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1054
1055 rx_buff->skb = NULL;
1056
1057 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1058 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1059
1060 atomic_dec(&pool->available);
1061}
1062
1063static int ibmvnic_poll(struct napi_struct *napi, int budget)
1064{
1065 struct net_device *netdev = napi->dev;
1066 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1067 int scrq_num = (int)(napi - adapter->napi);
1068 int frames_processed = 0;
1069restart_poll:
1070 while (frames_processed < budget) {
1071 struct sk_buff *skb;
1072 struct ibmvnic_rx_buff *rx_buff;
1073 union sub_crq *next;
1074 u32 length;
1075 u16 offset;
1076 u8 flags = 0;
1077
1078 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1079 break;
1080 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1081 rx_buff =
1082 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1083 rx_comp.correlator);
1084 /* do error checking */
1085 if (next->rx_comp.rc) {
1086 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1087 /* free the entry */
1088 next->rx_comp.first = 0;
1089 remove_buff_from_pool(adapter, rx_buff);
1090 break;
1091 }
1092
1093 length = be32_to_cpu(next->rx_comp.len);
1094 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1095 flags = next->rx_comp.flags;
1096 skb = rx_buff->skb;
1097 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1098 length);
1099 skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
1100 /* free the entry */
1101 next->rx_comp.first = 0;
1102 remove_buff_from_pool(adapter, rx_buff);
1103
1104 skb_put(skb, length);
1105 skb->protocol = eth_type_trans(skb, netdev);
1106
1107 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1108 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1109 skb->ip_summed = CHECKSUM_UNNECESSARY;
1110 }
1111
1112 length = skb->len;
1113 napi_gro_receive(napi, skb); /* send it up */
1114 netdev->stats.rx_packets++;
1115 netdev->stats.rx_bytes += length;
1116 frames_processed++;
1117 }
John Allen498cd8e2016-04-06 11:49:55 -05001118 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001119
1120 if (frames_processed < budget) {
1121 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001122 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001123 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1124 napi_reschedule(napi)) {
1125 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1126 goto restart_poll;
1127 }
1128 }
1129 return frames_processed;
1130}
1131
1132#ifdef CONFIG_NET_POLL_CONTROLLER
1133static void ibmvnic_netpoll_controller(struct net_device *dev)
1134{
1135 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1136 int i;
1137
1138 replenish_pools(netdev_priv(dev));
1139 for (i = 0; i < adapter->req_rx_queues; i++)
1140 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1141 adapter->rx_scrq[i]);
1142}
1143#endif
1144
1145static const struct net_device_ops ibmvnic_netdev_ops = {
1146 .ndo_open = ibmvnic_open,
1147 .ndo_stop = ibmvnic_close,
1148 .ndo_start_xmit = ibmvnic_xmit,
1149 .ndo_set_rx_mode = ibmvnic_set_multi,
1150 .ndo_set_mac_address = ibmvnic_set_mac,
1151 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001152 .ndo_tx_timeout = ibmvnic_tx_timeout,
1153#ifdef CONFIG_NET_POLL_CONTROLLER
1154 .ndo_poll_controller = ibmvnic_netpoll_controller,
1155#endif
1156};
1157
1158/* ethtool functions */
1159
Philippe Reynes8a433792017-01-07 22:37:29 +01001160static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1161 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001162{
Philippe Reynes8a433792017-01-07 22:37:29 +01001163 u32 supported, advertising;
1164
1165 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001166 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001167 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001168 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001169 cmd->base.speed = SPEED_1000;
1170 cmd->base.duplex = DUPLEX_FULL;
1171 cmd->base.port = PORT_FIBRE;
1172 cmd->base.phy_address = 0;
1173 cmd->base.autoneg = AUTONEG_ENABLE;
1174
1175 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1176 supported);
1177 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1178 advertising);
1179
Thomas Falcon032c5e82015-12-21 11:26:06 -06001180 return 0;
1181}
1182
1183static void ibmvnic_get_drvinfo(struct net_device *dev,
1184 struct ethtool_drvinfo *info)
1185{
1186 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1187 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1188}
1189
1190static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1191{
1192 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1193
1194 return adapter->msg_enable;
1195}
1196
1197static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1198{
1199 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1200
1201 adapter->msg_enable = data;
1202}
1203
1204static u32 ibmvnic_get_link(struct net_device *netdev)
1205{
1206 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1207
1208 /* Don't need to send a query because we request a logical link up at
1209 * init and then we wait for link state indications
1210 */
1211 return adapter->logical_link_state;
1212}
1213
1214static void ibmvnic_get_ringparam(struct net_device *netdev,
1215 struct ethtool_ringparam *ring)
1216{
1217 ring->rx_max_pending = 0;
1218 ring->tx_max_pending = 0;
1219 ring->rx_mini_max_pending = 0;
1220 ring->rx_jumbo_max_pending = 0;
1221 ring->rx_pending = 0;
1222 ring->tx_pending = 0;
1223 ring->rx_mini_pending = 0;
1224 ring->rx_jumbo_pending = 0;
1225}
1226
1227static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1228{
1229 int i;
1230
1231 if (stringset != ETH_SS_STATS)
1232 return;
1233
1234 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1235 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1236}
1237
1238static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1239{
1240 switch (sset) {
1241 case ETH_SS_STATS:
1242 return ARRAY_SIZE(ibmvnic_stats);
1243 default:
1244 return -EOPNOTSUPP;
1245 }
1246}
1247
1248static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1249 struct ethtool_stats *stats, u64 *data)
1250{
1251 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1252 union ibmvnic_crq crq;
1253 int i;
1254
1255 memset(&crq, 0, sizeof(crq));
1256 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1257 crq.request_statistics.cmd = REQUEST_STATISTICS;
1258 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1259 crq.request_statistics.len =
1260 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001261
1262 /* Wait for data to be written */
1263 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001264 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001265 wait_for_completion(&adapter->stats_done);
1266
1267 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1268 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1269}
1270
1271static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001272 .get_drvinfo = ibmvnic_get_drvinfo,
1273 .get_msglevel = ibmvnic_get_msglevel,
1274 .set_msglevel = ibmvnic_set_msglevel,
1275 .get_link = ibmvnic_get_link,
1276 .get_ringparam = ibmvnic_get_ringparam,
1277 .get_strings = ibmvnic_get_strings,
1278 .get_sset_count = ibmvnic_get_sset_count,
1279 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001280 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001281};
1282
1283/* Routines for managing CRQs/sCRQs */
1284
1285static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1286 struct ibmvnic_sub_crq_queue *scrq)
1287{
1288 struct device *dev = &adapter->vdev->dev;
1289 long rc;
1290
1291 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1292
1293 /* Close the sub-crqs */
1294 do {
1295 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1296 adapter->vdev->unit_address,
1297 scrq->crq_num);
1298 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1299
1300 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1301 DMA_BIDIRECTIONAL);
1302 free_pages((unsigned long)scrq->msgs, 2);
1303 kfree(scrq);
1304}
1305
1306static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1307 *adapter)
1308{
1309 struct device *dev = &adapter->vdev->dev;
1310 struct ibmvnic_sub_crq_queue *scrq;
1311 int rc;
1312
1313 scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1314 if (!scrq)
1315 return NULL;
1316
Thomas Falcon12608c22016-10-17 15:28:09 -05001317 scrq->msgs = (union sub_crq *)__get_free_pages(GFP_ATOMIC, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001318 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1319 if (!scrq->msgs) {
1320 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1321 goto zero_page_failed;
1322 }
1323
1324 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1325 DMA_BIDIRECTIONAL);
1326 if (dma_mapping_error(dev, scrq->msg_token)) {
1327 dev_warn(dev, "Couldn't map crq queue messages page\n");
1328 goto map_failed;
1329 }
1330
1331 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1332 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1333
1334 if (rc == H_RESOURCE)
1335 rc = ibmvnic_reset_crq(adapter);
1336
1337 if (rc == H_CLOSED) {
1338 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1339 } else if (rc) {
1340 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1341 goto reg_failed;
1342 }
1343
Thomas Falcon032c5e82015-12-21 11:26:06 -06001344 scrq->adapter = adapter;
1345 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1346 scrq->cur = 0;
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001347 atomic_set(&scrq->used, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001348 scrq->rx_skb_top = NULL;
1349 spin_lock_init(&scrq->lock);
1350
1351 netdev_dbg(adapter->netdev,
1352 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1353 scrq->crq_num, scrq->hw_irq, scrq->irq);
1354
1355 return scrq;
1356
Thomas Falcon032c5e82015-12-21 11:26:06 -06001357reg_failed:
1358 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1359 DMA_BIDIRECTIONAL);
1360map_failed:
1361 free_pages((unsigned long)scrq->msgs, 2);
1362zero_page_failed:
1363 kfree(scrq);
1364
1365 return NULL;
1366}
1367
1368static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1369{
1370 int i;
1371
1372 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001373 for (i = 0; i < adapter->req_tx_queues; i++) {
1374 if (!adapter->tx_scrq[i])
1375 continue;
1376
1377 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001378 free_irq(adapter->tx_scrq[i]->irq,
1379 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001380 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001381 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001382 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001383
1384 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1385 }
1386
Nathan Fontenot9501df32017-03-15 23:38:07 -04001387 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001388 adapter->tx_scrq = NULL;
1389 }
1390
1391 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001392 for (i = 0; i < adapter->req_rx_queues; i++) {
1393 if (!adapter->rx_scrq[i])
1394 continue;
1395
1396 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001397 free_irq(adapter->rx_scrq[i]->irq,
1398 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001399 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001400 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001401 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001402
1403 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1404 }
1405
Nathan Fontenot9501df32017-03-15 23:38:07 -04001406 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001407 adapter->rx_scrq = NULL;
1408 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001409}
1410
1411static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1412 struct ibmvnic_sub_crq_queue *scrq)
1413{
1414 struct device *dev = &adapter->vdev->dev;
1415 unsigned long rc;
1416
1417 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1418 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1419 if (rc)
1420 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1421 scrq->hw_irq, rc);
1422 return rc;
1423}
1424
1425static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1426 struct ibmvnic_sub_crq_queue *scrq)
1427{
1428 struct device *dev = &adapter->vdev->dev;
1429 unsigned long rc;
1430
1431 if (scrq->hw_irq > 0x100000000ULL) {
1432 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1433 return 1;
1434 }
1435
1436 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1437 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1438 if (rc)
1439 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1440 scrq->hw_irq, rc);
1441 return rc;
1442}
1443
1444static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1445 struct ibmvnic_sub_crq_queue *scrq)
1446{
1447 struct device *dev = &adapter->vdev->dev;
1448 struct ibmvnic_tx_buff *txbuff;
1449 union sub_crq *next;
1450 int index;
1451 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001452 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001453
1454restart_loop:
1455 while (pending_scrq(adapter, scrq)) {
1456 unsigned int pool = scrq->pool_index;
1457
1458 next = ibmvnic_next_scrq(adapter, scrq);
1459 for (i = 0; i < next->tx_comp.num_comps; i++) {
1460 if (next->tx_comp.rcs[i]) {
1461 dev_err(dev, "tx error %x\n",
1462 next->tx_comp.rcs[i]);
1463 continue;
1464 }
1465 index = be32_to_cpu(next->tx_comp.correlators[i]);
1466 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1467
1468 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1469 if (!txbuff->data_dma[j])
1470 continue;
1471
1472 txbuff->data_dma[j] = 0;
1473 txbuff->used_bounce = false;
1474 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001475 /* if sub_crq was sent indirectly */
1476 first = txbuff->indir_arr[0].generic.first;
1477 if (first == IBMVNIC_CRQ_CMD) {
1478 dma_unmap_single(dev, txbuff->indir_dma,
1479 sizeof(txbuff->indir_arr),
1480 DMA_TO_DEVICE);
1481 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001482
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001483 if (txbuff->last_frag) {
1484 atomic_dec(&scrq->used);
1485
1486 if (atomic_read(&scrq->used) <=
1487 (adapter->req_tx_entries_per_subcrq / 2) &&
1488 netif_subqueue_stopped(adapter->netdev,
1489 txbuff->skb)) {
1490 netif_wake_subqueue(adapter->netdev,
1491 scrq->pool_index);
1492 netdev_dbg(adapter->netdev,
1493 "Started queue %d\n",
1494 scrq->pool_index);
1495 }
1496
Thomas Falcon032c5e82015-12-21 11:26:06 -06001497 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001498 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001499
1500 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1501 producer_index] = index;
1502 adapter->tx_pool[pool].producer_index =
1503 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001504 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001505 }
1506 /* remove tx_comp scrq*/
1507 next->tx_comp.first = 0;
1508 }
1509
1510 enable_scrq_irq(adapter, scrq);
1511
1512 if (pending_scrq(adapter, scrq)) {
1513 disable_scrq_irq(adapter, scrq);
1514 goto restart_loop;
1515 }
1516
1517 return 0;
1518}
1519
1520static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1521{
1522 struct ibmvnic_sub_crq_queue *scrq = instance;
1523 struct ibmvnic_adapter *adapter = scrq->adapter;
1524
1525 disable_scrq_irq(adapter, scrq);
1526 ibmvnic_complete_tx(adapter, scrq);
1527
1528 return IRQ_HANDLED;
1529}
1530
1531static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1532{
1533 struct ibmvnic_sub_crq_queue *scrq = instance;
1534 struct ibmvnic_adapter *adapter = scrq->adapter;
1535
1536 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1537 disable_scrq_irq(adapter, scrq);
1538 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1539 }
1540
1541 return IRQ_HANDLED;
1542}
1543
Thomas Falconea22d512016-07-06 15:35:17 -05001544static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1545{
1546 struct device *dev = &adapter->vdev->dev;
1547 struct ibmvnic_sub_crq_queue *scrq;
1548 int i = 0, j = 0;
1549 int rc = 0;
1550
1551 for (i = 0; i < adapter->req_tx_queues; i++) {
1552 scrq = adapter->tx_scrq[i];
1553 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1554
Michael Ellerman99c17902016-09-10 19:59:05 +10001555 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001556 rc = -EINVAL;
1557 dev_err(dev, "Error mapping irq\n");
1558 goto req_tx_irq_failed;
1559 }
1560
1561 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1562 0, "ibmvnic_tx", scrq);
1563
1564 if (rc) {
1565 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1566 scrq->irq, rc);
1567 irq_dispose_mapping(scrq->irq);
1568 goto req_rx_irq_failed;
1569 }
1570 }
1571
1572 for (i = 0; i < adapter->req_rx_queues; i++) {
1573 scrq = adapter->rx_scrq[i];
1574 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001575 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001576 rc = -EINVAL;
1577 dev_err(dev, "Error mapping irq\n");
1578 goto req_rx_irq_failed;
1579 }
1580 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1581 0, "ibmvnic_rx", scrq);
1582 if (rc) {
1583 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1584 scrq->irq, rc);
1585 irq_dispose_mapping(scrq->irq);
1586 goto req_rx_irq_failed;
1587 }
1588 }
1589 return rc;
1590
1591req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001592 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001593 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1594 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001595 }
Thomas Falconea22d512016-07-06 15:35:17 -05001596 i = adapter->req_tx_queues;
1597req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001598 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001599 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1600 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001601 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001602 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001603 return rc;
1604}
1605
Thomas Falcon032c5e82015-12-21 11:26:06 -06001606static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1607{
1608 struct device *dev = &adapter->vdev->dev;
1609 struct ibmvnic_sub_crq_queue **allqueues;
1610 int registered_queues = 0;
1611 union ibmvnic_crq crq;
1612 int total_queues;
1613 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001614 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001615
1616 if (!retry) {
1617 /* Sub-CRQ entries are 32 byte long */
1618 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1619
1620 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1621 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1622 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1623 goto allqueues_failed;
1624 }
1625
1626 /* Get the minimum between the queried max and the entries
1627 * that fit in our PAGE_SIZE
1628 */
1629 adapter->req_tx_entries_per_subcrq =
1630 adapter->max_tx_entries_per_subcrq > entries_page ?
1631 entries_page : adapter->max_tx_entries_per_subcrq;
1632 adapter->req_rx_add_entries_per_subcrq =
1633 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1634 entries_page : adapter->max_rx_add_entries_per_subcrq;
1635
John Allen6dbcd8f2016-11-07 14:27:28 -06001636 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1637 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001638 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001639
Thomas Falconf39f0d12017-02-14 10:22:59 -06001640 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001641 }
1642
1643 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1644
1645 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1646 if (!allqueues)
1647 goto allqueues_failed;
1648
1649 for (i = 0; i < total_queues; i++) {
1650 allqueues[i] = init_sub_crq_queue(adapter);
1651 if (!allqueues[i]) {
1652 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1653 break;
1654 }
1655 registered_queues++;
1656 }
1657
1658 /* Make sure we were able to register the minimum number of queues */
1659 if (registered_queues <
1660 adapter->min_tx_queues + adapter->min_rx_queues) {
1661 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1662 goto tx_failed;
1663 }
1664
1665 /* Distribute the failed allocated queues*/
1666 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1667 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1668 switch (i % 3) {
1669 case 0:
1670 if (adapter->req_rx_queues > adapter->min_rx_queues)
1671 adapter->req_rx_queues--;
1672 else
1673 more++;
1674 break;
1675 case 1:
1676 if (adapter->req_tx_queues > adapter->min_tx_queues)
1677 adapter->req_tx_queues--;
1678 else
1679 more++;
1680 break;
1681 }
1682 }
1683
1684 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1685 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1686 if (!adapter->tx_scrq)
1687 goto tx_failed;
1688
1689 for (i = 0; i < adapter->req_tx_queues; i++) {
1690 adapter->tx_scrq[i] = allqueues[i];
1691 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001692 }
1693
1694 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1695 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1696 if (!adapter->rx_scrq)
1697 goto rx_failed;
1698
1699 for (i = 0; i < adapter->req_rx_queues; i++) {
1700 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1701 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001702 }
1703
1704 memset(&crq, 0, sizeof(crq));
1705 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1706 crq.request_capability.cmd = REQUEST_CAPABILITY;
1707
1708 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001709 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001710 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 ibmvnic_send_crq(adapter, &crq);
1712
1713 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001714 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001715 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001716 ibmvnic_send_crq(adapter, &crq);
1717
1718 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001719 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001720 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001721 ibmvnic_send_crq(adapter, &crq);
1722
1723 crq.request_capability.capability =
1724 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1725 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001726 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001727 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001728 ibmvnic_send_crq(adapter, &crq);
1729
1730 crq.request_capability.capability =
1731 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1732 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001733 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001734 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001735 ibmvnic_send_crq(adapter, &crq);
1736
1737 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001738 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001739 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 ibmvnic_send_crq(adapter, &crq);
1741
1742 if (adapter->netdev->flags & IFF_PROMISC) {
1743 if (adapter->promisc_supported) {
1744 crq.request_capability.capability =
1745 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001746 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001747 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001748 ibmvnic_send_crq(adapter, &crq);
1749 }
1750 } else {
1751 crq.request_capability.capability =
1752 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001753 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001754 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001755 ibmvnic_send_crq(adapter, &crq);
1756 }
1757
1758 kfree(allqueues);
1759
1760 return;
1761
Thomas Falcon032c5e82015-12-21 11:26:06 -06001762rx_failed:
1763 kfree(adapter->tx_scrq);
1764 adapter->tx_scrq = NULL;
1765tx_failed:
1766 for (i = 0; i < registered_queues; i++)
1767 release_sub_crq_queue(adapter, allqueues[i]);
1768 kfree(allqueues);
1769allqueues_failed:
1770 ibmvnic_remove(adapter->vdev);
1771}
1772
1773static int pending_scrq(struct ibmvnic_adapter *adapter,
1774 struct ibmvnic_sub_crq_queue *scrq)
1775{
1776 union sub_crq *entry = &scrq->msgs[scrq->cur];
1777
1778 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1779 return 1;
1780 else
1781 return 0;
1782}
1783
1784static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1785 struct ibmvnic_sub_crq_queue *scrq)
1786{
1787 union sub_crq *entry;
1788 unsigned long flags;
1789
1790 spin_lock_irqsave(&scrq->lock, flags);
1791 entry = &scrq->msgs[scrq->cur];
1792 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1793 if (++scrq->cur == scrq->size)
1794 scrq->cur = 0;
1795 } else {
1796 entry = NULL;
1797 }
1798 spin_unlock_irqrestore(&scrq->lock, flags);
1799
1800 return entry;
1801}
1802
1803static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1804{
1805 struct ibmvnic_crq_queue *queue = &adapter->crq;
1806 union ibmvnic_crq *crq;
1807
1808 crq = &queue->msgs[queue->cur];
1809 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1810 if (++queue->cur == queue->size)
1811 queue->cur = 0;
1812 } else {
1813 crq = NULL;
1814 }
1815
1816 return crq;
1817}
1818
1819static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1820 union sub_crq *sub_crq)
1821{
1822 unsigned int ua = adapter->vdev->unit_address;
1823 struct device *dev = &adapter->vdev->dev;
1824 u64 *u64_crq = (u64 *)sub_crq;
1825 int rc;
1826
1827 netdev_dbg(adapter->netdev,
1828 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1829 (unsigned long int)cpu_to_be64(remote_handle),
1830 (unsigned long int)cpu_to_be64(u64_crq[0]),
1831 (unsigned long int)cpu_to_be64(u64_crq[1]),
1832 (unsigned long int)cpu_to_be64(u64_crq[2]),
1833 (unsigned long int)cpu_to_be64(u64_crq[3]));
1834
1835 /* Make sure the hypervisor sees the complete request */
1836 mb();
1837
1838 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1839 cpu_to_be64(remote_handle),
1840 cpu_to_be64(u64_crq[0]),
1841 cpu_to_be64(u64_crq[1]),
1842 cpu_to_be64(u64_crq[2]),
1843 cpu_to_be64(u64_crq[3]));
1844
1845 if (rc) {
1846 if (rc == H_CLOSED)
1847 dev_warn(dev, "CRQ Queue closed\n");
1848 dev_err(dev, "Send error (rc=%d)\n", rc);
1849 }
1850
1851 return rc;
1852}
1853
Thomas Falconad7775d2016-04-01 17:20:34 -05001854static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1855 u64 remote_handle, u64 ioba, u64 num_entries)
1856{
1857 unsigned int ua = adapter->vdev->unit_address;
1858 struct device *dev = &adapter->vdev->dev;
1859 int rc;
1860
1861 /* Make sure the hypervisor sees the complete request */
1862 mb();
1863 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1864 cpu_to_be64(remote_handle),
1865 ioba, num_entries);
1866
1867 if (rc) {
1868 if (rc == H_CLOSED)
1869 dev_warn(dev, "CRQ Queue closed\n");
1870 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1871 }
1872
1873 return rc;
1874}
1875
Thomas Falcon032c5e82015-12-21 11:26:06 -06001876static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1877 union ibmvnic_crq *crq)
1878{
1879 unsigned int ua = adapter->vdev->unit_address;
1880 struct device *dev = &adapter->vdev->dev;
1881 u64 *u64_crq = (u64 *)crq;
1882 int rc;
1883
1884 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1885 (unsigned long int)cpu_to_be64(u64_crq[0]),
1886 (unsigned long int)cpu_to_be64(u64_crq[1]));
1887
1888 /* Make sure the hypervisor sees the complete request */
1889 mb();
1890
1891 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1892 cpu_to_be64(u64_crq[0]),
1893 cpu_to_be64(u64_crq[1]));
1894
1895 if (rc) {
1896 if (rc == H_CLOSED)
1897 dev_warn(dev, "CRQ Queue closed\n");
1898 dev_warn(dev, "Send error (rc=%d)\n", rc);
1899 }
1900
1901 return rc;
1902}
1903
1904static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1905{
1906 union ibmvnic_crq crq;
1907
1908 memset(&crq, 0, sizeof(crq));
1909 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1910 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1911 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1912
1913 return ibmvnic_send_crq(adapter, &crq);
1914}
1915
1916static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1917{
1918 union ibmvnic_crq crq;
1919
1920 memset(&crq, 0, sizeof(crq));
1921 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1922 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1923 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1924
1925 return ibmvnic_send_crq(adapter, &crq);
1926}
1927
1928static int send_version_xchg(struct ibmvnic_adapter *adapter)
1929{
1930 union ibmvnic_crq crq;
1931
1932 memset(&crq, 0, sizeof(crq));
1933 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1934 crq.version_exchange.cmd = VERSION_EXCHANGE;
1935 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1936
1937 return ibmvnic_send_crq(adapter, &crq);
1938}
1939
1940static void send_login(struct ibmvnic_adapter *adapter)
1941{
1942 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1943 struct ibmvnic_login_buffer *login_buffer;
1944 struct ibmvnic_inflight_cmd *inflight_cmd;
1945 struct device *dev = &adapter->vdev->dev;
1946 dma_addr_t rsp_buffer_token;
1947 dma_addr_t buffer_token;
1948 size_t rsp_buffer_size;
1949 union ibmvnic_crq crq;
1950 unsigned long flags;
1951 size_t buffer_size;
1952 __be64 *tx_list_p;
1953 __be64 *rx_list_p;
1954 int i;
1955
1956 buffer_size =
1957 sizeof(struct ibmvnic_login_buffer) +
1958 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1959
1960 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1961 if (!login_buffer)
1962 goto buf_alloc_failed;
1963
1964 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1965 DMA_TO_DEVICE);
1966 if (dma_mapping_error(dev, buffer_token)) {
1967 dev_err(dev, "Couldn't map login buffer\n");
1968 goto buf_map_failed;
1969 }
1970
John Allen498cd8e2016-04-06 11:49:55 -05001971 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1972 sizeof(u64) * adapter->req_tx_queues +
1973 sizeof(u64) * adapter->req_rx_queues +
1974 sizeof(u64) * adapter->req_rx_queues +
1975 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001976
1977 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1978 if (!login_rsp_buffer)
1979 goto buf_rsp_alloc_failed;
1980
1981 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1982 rsp_buffer_size, DMA_FROM_DEVICE);
1983 if (dma_mapping_error(dev, rsp_buffer_token)) {
1984 dev_err(dev, "Couldn't map login rsp buffer\n");
1985 goto buf_rsp_map_failed;
1986 }
1987 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
1988 if (!inflight_cmd) {
1989 dev_err(dev, "Couldn't allocate inflight_cmd\n");
1990 goto inflight_alloc_failed;
1991 }
1992 adapter->login_buf = login_buffer;
1993 adapter->login_buf_token = buffer_token;
1994 adapter->login_buf_sz = buffer_size;
1995 adapter->login_rsp_buf = login_rsp_buffer;
1996 adapter->login_rsp_buf_token = rsp_buffer_token;
1997 adapter->login_rsp_buf_sz = rsp_buffer_size;
1998
1999 login_buffer->len = cpu_to_be32(buffer_size);
2000 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2001 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2002 login_buffer->off_txcomp_subcrqs =
2003 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2004 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2005 login_buffer->off_rxcomp_subcrqs =
2006 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2007 sizeof(u64) * adapter->req_tx_queues);
2008 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2009 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2010
2011 tx_list_p = (__be64 *)((char *)login_buffer +
2012 sizeof(struct ibmvnic_login_buffer));
2013 rx_list_p = (__be64 *)((char *)login_buffer +
2014 sizeof(struct ibmvnic_login_buffer) +
2015 sizeof(u64) * adapter->req_tx_queues);
2016
2017 for (i = 0; i < adapter->req_tx_queues; i++) {
2018 if (adapter->tx_scrq[i]) {
2019 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2020 crq_num);
2021 }
2022 }
2023
2024 for (i = 0; i < adapter->req_rx_queues; i++) {
2025 if (adapter->rx_scrq[i]) {
2026 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2027 crq_num);
2028 }
2029 }
2030
2031 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2032 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2033 netdev_dbg(adapter->netdev, "%016lx\n",
2034 ((unsigned long int *)(adapter->login_buf))[i]);
2035 }
2036
2037 memset(&crq, 0, sizeof(crq));
2038 crq.login.first = IBMVNIC_CRQ_CMD;
2039 crq.login.cmd = LOGIN;
2040 crq.login.ioba = cpu_to_be32(buffer_token);
2041 crq.login.len = cpu_to_be32(buffer_size);
2042
2043 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2044
2045 spin_lock_irqsave(&adapter->inflight_lock, flags);
2046 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2047 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2048
2049 ibmvnic_send_crq(adapter, &crq);
2050
2051 return;
2052
2053inflight_alloc_failed:
2054 dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
2055 DMA_FROM_DEVICE);
2056buf_rsp_map_failed:
2057 kfree(login_rsp_buffer);
2058buf_rsp_alloc_failed:
2059 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2060buf_map_failed:
2061 kfree(login_buffer);
2062buf_alloc_failed:
2063 return;
2064}
2065
2066static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2067 u32 len, u8 map_id)
2068{
2069 union ibmvnic_crq crq;
2070
2071 memset(&crq, 0, sizeof(crq));
2072 crq.request_map.first = IBMVNIC_CRQ_CMD;
2073 crq.request_map.cmd = REQUEST_MAP;
2074 crq.request_map.map_id = map_id;
2075 crq.request_map.ioba = cpu_to_be32(addr);
2076 crq.request_map.len = cpu_to_be32(len);
2077 ibmvnic_send_crq(adapter, &crq);
2078}
2079
2080static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2081{
2082 union ibmvnic_crq crq;
2083
2084 memset(&crq, 0, sizeof(crq));
2085 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2086 crq.request_unmap.cmd = REQUEST_UNMAP;
2087 crq.request_unmap.map_id = map_id;
2088 ibmvnic_send_crq(adapter, &crq);
2089}
2090
2091static void send_map_query(struct ibmvnic_adapter *adapter)
2092{
2093 union ibmvnic_crq crq;
2094
2095 memset(&crq, 0, sizeof(crq));
2096 crq.query_map.first = IBMVNIC_CRQ_CMD;
2097 crq.query_map.cmd = QUERY_MAP;
2098 ibmvnic_send_crq(adapter, &crq);
2099}
2100
2101/* Send a series of CRQs requesting various capabilities of the VNIC server */
2102static void send_cap_queries(struct ibmvnic_adapter *adapter)
2103{
2104 union ibmvnic_crq crq;
2105
Thomas Falcon901e0402017-02-15 12:17:59 -06002106 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002107 memset(&crq, 0, sizeof(crq));
2108 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2109 crq.query_capability.cmd = QUERY_CAPABILITY;
2110
2111 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002112 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002113 ibmvnic_send_crq(adapter, &crq);
2114
2115 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002116 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002117 ibmvnic_send_crq(adapter, &crq);
2118
2119 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002120 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002121 ibmvnic_send_crq(adapter, &crq);
2122
2123 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002124 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002125 ibmvnic_send_crq(adapter, &crq);
2126
2127 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002128 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002129 ibmvnic_send_crq(adapter, &crq);
2130
2131 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002132 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002133 ibmvnic_send_crq(adapter, &crq);
2134
2135 crq.query_capability.capability =
2136 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002137 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002138 ibmvnic_send_crq(adapter, &crq);
2139
2140 crq.query_capability.capability =
2141 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002142 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002143 ibmvnic_send_crq(adapter, &crq);
2144
2145 crq.query_capability.capability =
2146 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002147 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002148 ibmvnic_send_crq(adapter, &crq);
2149
2150 crq.query_capability.capability =
2151 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002152 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002153 ibmvnic_send_crq(adapter, &crq);
2154
2155 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002156 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002157 ibmvnic_send_crq(adapter, &crq);
2158
2159 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002160 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002161 ibmvnic_send_crq(adapter, &crq);
2162
2163 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002164 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002165 ibmvnic_send_crq(adapter, &crq);
2166
2167 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002168 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002169 ibmvnic_send_crq(adapter, &crq);
2170
2171 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002172 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002173 ibmvnic_send_crq(adapter, &crq);
2174
2175 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002176 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002177 ibmvnic_send_crq(adapter, &crq);
2178
2179 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002180 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002181 ibmvnic_send_crq(adapter, &crq);
2182
2183 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002184 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002185 ibmvnic_send_crq(adapter, &crq);
2186
2187 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002188 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002189 ibmvnic_send_crq(adapter, &crq);
2190
2191 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002192 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002193 ibmvnic_send_crq(adapter, &crq);
2194
2195 crq.query_capability.capability =
2196 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002197 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002198 ibmvnic_send_crq(adapter, &crq);
2199
2200 crq.query_capability.capability =
2201 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002202 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002203 ibmvnic_send_crq(adapter, &crq);
2204
2205 crq.query_capability.capability =
2206 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002207 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002208 ibmvnic_send_crq(adapter, &crq);
2209
2210 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002211 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002212 ibmvnic_send_crq(adapter, &crq);
2213}
2214
2215static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2216{
2217 struct device *dev = &adapter->vdev->dev;
2218 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2219 union ibmvnic_crq crq;
2220 int i;
2221
2222 dma_unmap_single(dev, adapter->ip_offload_tok,
2223 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2224
2225 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2226 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2227 netdev_dbg(adapter->netdev, "%016lx\n",
2228 ((unsigned long int *)(buf))[i]);
2229
2230 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2231 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2232 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2233 buf->tcp_ipv4_chksum);
2234 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2235 buf->tcp_ipv6_chksum);
2236 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2237 buf->udp_ipv4_chksum);
2238 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2239 buf->udp_ipv6_chksum);
2240 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2241 buf->large_tx_ipv4);
2242 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2243 buf->large_tx_ipv6);
2244 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2245 buf->large_rx_ipv4);
2246 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2247 buf->large_rx_ipv6);
2248 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2249 buf->max_ipv4_header_size);
2250 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2251 buf->max_ipv6_header_size);
2252 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2253 buf->max_tcp_header_size);
2254 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2255 buf->max_udp_header_size);
2256 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2257 buf->max_large_tx_size);
2258 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2259 buf->max_large_rx_size);
2260 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2261 buf->ipv6_extension_header);
2262 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2263 buf->tcp_pseudosum_req);
2264 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2265 buf->num_ipv6_ext_headers);
2266 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2267 buf->off_ipv6_ext_headers);
2268
2269 adapter->ip_offload_ctrl_tok =
2270 dma_map_single(dev, &adapter->ip_offload_ctrl,
2271 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2272
2273 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2274 dev_err(dev, "Couldn't map ip offload control buffer\n");
2275 return;
2276 }
2277
2278 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2279 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2280 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2281 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2282 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2283
2284 /* large_tx/rx disabled for now, additional features needed */
2285 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2286 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2287 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2288 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2289
2290 adapter->netdev->features = NETIF_F_GSO;
2291
2292 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2293 adapter->netdev->features |= NETIF_F_IP_CSUM;
2294
2295 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2296 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2297
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002298 if ((adapter->netdev->features &
2299 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2300 adapter->netdev->features |= NETIF_F_RXCSUM;
2301
Thomas Falcon032c5e82015-12-21 11:26:06 -06002302 memset(&crq, 0, sizeof(crq));
2303 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2304 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2305 crq.control_ip_offload.len =
2306 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2307 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2308 ibmvnic_send_crq(adapter, &crq);
2309}
2310
2311static void handle_error_info_rsp(union ibmvnic_crq *crq,
2312 struct ibmvnic_adapter *adapter)
2313{
2314 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002315 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002316 unsigned long flags;
2317 bool found = false;
2318 int i;
2319
2320 if (!crq->request_error_rsp.rc.code) {
2321 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2322 crq->request_error_rsp.rc.code);
2323 return;
2324 }
2325
2326 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002327 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002328 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2329 found = true;
2330 list_del(&error_buff->list);
2331 break;
2332 }
2333 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2334
2335 if (!found) {
2336 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002337 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002338 return;
2339 }
2340
2341 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002342 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002343
2344 for (i = 0; i < error_buff->len; i++) {
2345 pr_cont("%02x", (int)error_buff->buff[i]);
2346 if (i % 8 == 7)
2347 pr_cont(" ");
2348 }
2349 pr_cont("\n");
2350
2351 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2352 DMA_FROM_DEVICE);
2353 kfree(error_buff->buff);
2354 kfree(error_buff);
2355}
2356
Thomas Falcon032c5e82015-12-21 11:26:06 -06002357static void handle_error_indication(union ibmvnic_crq *crq,
2358 struct ibmvnic_adapter *adapter)
2359{
2360 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2361 struct ibmvnic_inflight_cmd *inflight_cmd;
2362 struct device *dev = &adapter->vdev->dev;
2363 struct ibmvnic_error_buff *error_buff;
2364 union ibmvnic_crq new_crq;
2365 unsigned long flags;
2366
2367 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2368 crq->error_indication.
2369 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
Thomas Falcon75224c92017-02-15 10:33:33 -06002370 be32_to_cpu(crq->error_indication.error_id),
2371 be16_to_cpu(crq->error_indication.error_cause));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002372
2373 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2374 if (!error_buff)
2375 return;
2376
2377 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2378 if (!error_buff->buff) {
2379 kfree(error_buff);
2380 return;
2381 }
2382
2383 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2384 DMA_FROM_DEVICE);
2385 if (dma_mapping_error(dev, error_buff->dma)) {
2386 if (!firmware_has_feature(FW_FEATURE_CMO))
2387 dev_err(dev, "Couldn't map error buffer\n");
2388 kfree(error_buff->buff);
2389 kfree(error_buff);
2390 return;
2391 }
2392
2393 inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2394 if (!inflight_cmd) {
2395 dma_unmap_single(dev, error_buff->dma, detail_len,
2396 DMA_FROM_DEVICE);
2397 kfree(error_buff->buff);
2398 kfree(error_buff);
2399 return;
2400 }
2401
2402 error_buff->len = detail_len;
2403 error_buff->error_id = crq->error_indication.error_id;
2404
2405 spin_lock_irqsave(&adapter->error_list_lock, flags);
2406 list_add_tail(&error_buff->list, &adapter->errors);
2407 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2408
2409 memset(&new_crq, 0, sizeof(new_crq));
2410 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2411 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2412 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2413 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2414 new_crq.request_error_info.error_id = crq->error_indication.error_id;
2415
2416 memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2417
2418 spin_lock_irqsave(&adapter->inflight_lock, flags);
2419 list_add_tail(&inflight_cmd->list, &adapter->inflight);
2420 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2421
2422 ibmvnic_send_crq(adapter, &new_crq);
2423}
2424
2425static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2426 struct ibmvnic_adapter *adapter)
2427{
2428 struct net_device *netdev = adapter->netdev;
2429 struct device *dev = &adapter->vdev->dev;
2430 long rc;
2431
2432 rc = crq->change_mac_addr_rsp.rc.code;
2433 if (rc) {
2434 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2435 return;
2436 }
2437 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2438 ETH_ALEN);
2439}
2440
2441static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2442 struct ibmvnic_adapter *adapter)
2443{
2444 struct device *dev = &adapter->vdev->dev;
2445 u64 *req_value;
2446 char *name;
2447
Thomas Falcon901e0402017-02-15 12:17:59 -06002448 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002449 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2450 case REQ_TX_QUEUES:
2451 req_value = &adapter->req_tx_queues;
2452 name = "tx";
2453 break;
2454 case REQ_RX_QUEUES:
2455 req_value = &adapter->req_rx_queues;
2456 name = "rx";
2457 break;
2458 case REQ_RX_ADD_QUEUES:
2459 req_value = &adapter->req_rx_add_queues;
2460 name = "rx_add";
2461 break;
2462 case REQ_TX_ENTRIES_PER_SUBCRQ:
2463 req_value = &adapter->req_tx_entries_per_subcrq;
2464 name = "tx_entries_per_subcrq";
2465 break;
2466 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2467 req_value = &adapter->req_rx_add_entries_per_subcrq;
2468 name = "rx_add_entries_per_subcrq";
2469 break;
2470 case REQ_MTU:
2471 req_value = &adapter->req_mtu;
2472 name = "mtu";
2473 break;
2474 case PROMISC_REQUESTED:
2475 req_value = &adapter->promisc;
2476 name = "promisc";
2477 break;
2478 default:
2479 dev_err(dev, "Got invalid cap request rsp %d\n",
2480 crq->request_capability.capability);
2481 return;
2482 }
2483
2484 switch (crq->request_capability_rsp.rc.code) {
2485 case SUCCESS:
2486 break;
2487 case PARTIALSUCCESS:
2488 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2489 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002490 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002491 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002492 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002493 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002494 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002495 return;
2496 default:
2497 dev_err(dev, "Error %d in request cap rsp\n",
2498 crq->request_capability_rsp.rc.code);
2499 return;
2500 }
2501
2502 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002503 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002504 union ibmvnic_crq newcrq;
2505 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2506 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2507 &adapter->ip_offload_buf;
2508
Thomas Falcon249168a2017-02-15 12:18:00 -06002509 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002510 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2511 buf_sz,
2512 DMA_FROM_DEVICE);
2513
2514 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2515 if (!firmware_has_feature(FW_FEATURE_CMO))
2516 dev_err(dev, "Couldn't map offload buffer\n");
2517 return;
2518 }
2519
2520 memset(&newcrq, 0, sizeof(newcrq));
2521 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2522 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2523 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2524 newcrq.query_ip_offload.ioba =
2525 cpu_to_be32(adapter->ip_offload_tok);
2526
2527 ibmvnic_send_crq(adapter, &newcrq);
2528 }
2529}
2530
2531static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2532 struct ibmvnic_adapter *adapter)
2533{
2534 struct device *dev = &adapter->vdev->dev;
2535 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2536 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002537 int i;
2538
2539 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2540 DMA_BIDIRECTIONAL);
2541 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2542 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2543
John Allen498cd8e2016-04-06 11:49:55 -05002544 /* If the number of queues requested can't be allocated by the
2545 * server, the login response will return with code 1. We will need
2546 * to resend the login buffer with fewer queues requested.
2547 */
2548 if (login_rsp_crq->generic.rc.code) {
2549 adapter->renegotiate = true;
2550 complete(&adapter->init_done);
2551 return 0;
2552 }
2553
Thomas Falcon032c5e82015-12-21 11:26:06 -06002554 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2555 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2556 netdev_dbg(adapter->netdev, "%016lx\n",
2557 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2558 }
2559
2560 /* Sanity checks */
2561 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2562 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2563 adapter->req_rx_add_queues !=
2564 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2565 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2566 ibmvnic_remove(adapter->vdev);
2567 return -EIO;
2568 }
2569 complete(&adapter->init_done);
2570
Thomas Falcon032c5e82015-12-21 11:26:06 -06002571 return 0;
2572}
2573
2574static void handle_request_map_rsp(union ibmvnic_crq *crq,
2575 struct ibmvnic_adapter *adapter)
2576{
2577 struct device *dev = &adapter->vdev->dev;
2578 u8 map_id = crq->request_map_rsp.map_id;
2579 int tx_subcrqs;
2580 int rx_subcrqs;
2581 long rc;
2582 int i;
2583
2584 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2585 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2586
2587 rc = crq->request_map_rsp.rc.code;
2588 if (rc) {
2589 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2590 adapter->map_id--;
2591 /* need to find and zero tx/rx_pool map_id */
2592 for (i = 0; i < tx_subcrqs; i++) {
2593 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2594 adapter->tx_pool[i].long_term_buff.map_id = 0;
2595 }
2596 for (i = 0; i < rx_subcrqs; i++) {
2597 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2598 adapter->rx_pool[i].long_term_buff.map_id = 0;
2599 }
2600 }
2601 complete(&adapter->fw_done);
2602}
2603
2604static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2605 struct ibmvnic_adapter *adapter)
2606{
2607 struct device *dev = &adapter->vdev->dev;
2608 long rc;
2609
2610 rc = crq->request_unmap_rsp.rc.code;
2611 if (rc)
2612 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2613}
2614
2615static void handle_query_map_rsp(union ibmvnic_crq *crq,
2616 struct ibmvnic_adapter *adapter)
2617{
2618 struct net_device *netdev = adapter->netdev;
2619 struct device *dev = &adapter->vdev->dev;
2620 long rc;
2621
2622 rc = crq->query_map_rsp.rc.code;
2623 if (rc) {
2624 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2625 return;
2626 }
2627 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2628 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2629 crq->query_map_rsp.free_pages);
2630}
2631
2632static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2633 struct ibmvnic_adapter *adapter)
2634{
2635 struct net_device *netdev = adapter->netdev;
2636 struct device *dev = &adapter->vdev->dev;
2637 long rc;
2638
Thomas Falcon901e0402017-02-15 12:17:59 -06002639 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002640 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002641 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 rc = crq->query_capability.rc.code;
2643 if (rc) {
2644 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2645 goto out;
2646 }
2647
2648 switch (be16_to_cpu(crq->query_capability.capability)) {
2649 case MIN_TX_QUEUES:
2650 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002651 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002652 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2653 adapter->min_tx_queues);
2654 break;
2655 case MIN_RX_QUEUES:
2656 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002657 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002658 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2659 adapter->min_rx_queues);
2660 break;
2661 case MIN_RX_ADD_QUEUES:
2662 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002663 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002664 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2665 adapter->min_rx_add_queues);
2666 break;
2667 case MAX_TX_QUEUES:
2668 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002669 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002670 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2671 adapter->max_tx_queues);
2672 break;
2673 case MAX_RX_QUEUES:
2674 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002675 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002676 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2677 adapter->max_rx_queues);
2678 break;
2679 case MAX_RX_ADD_QUEUES:
2680 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002681 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002682 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2683 adapter->max_rx_add_queues);
2684 break;
2685 case MIN_TX_ENTRIES_PER_SUBCRQ:
2686 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002687 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002688 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2689 adapter->min_tx_entries_per_subcrq);
2690 break;
2691 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2692 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002693 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002694 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2695 adapter->min_rx_add_entries_per_subcrq);
2696 break;
2697 case MAX_TX_ENTRIES_PER_SUBCRQ:
2698 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002699 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002700 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2701 adapter->max_tx_entries_per_subcrq);
2702 break;
2703 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2704 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002705 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002706 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2707 adapter->max_rx_add_entries_per_subcrq);
2708 break;
2709 case TCP_IP_OFFLOAD:
2710 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002711 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002712 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2713 adapter->tcp_ip_offload);
2714 break;
2715 case PROMISC_SUPPORTED:
2716 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002717 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002718 netdev_dbg(netdev, "promisc_supported = %lld\n",
2719 adapter->promisc_supported);
2720 break;
2721 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002722 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002723 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2725 break;
2726 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002727 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002728 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002729 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2730 break;
2731 case MAX_MULTICAST_FILTERS:
2732 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002733 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002734 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2735 adapter->max_multicast_filters);
2736 break;
2737 case VLAN_HEADER_INSERTION:
2738 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002739 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002740 if (adapter->vlan_header_insertion)
2741 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2742 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2743 adapter->vlan_header_insertion);
2744 break;
2745 case MAX_TX_SG_ENTRIES:
2746 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002747 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002748 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2749 adapter->max_tx_sg_entries);
2750 break;
2751 case RX_SG_SUPPORTED:
2752 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002753 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002754 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2755 adapter->rx_sg_supported);
2756 break;
2757 case OPT_TX_COMP_SUB_QUEUES:
2758 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002759 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002760 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2761 adapter->opt_tx_comp_sub_queues);
2762 break;
2763 case OPT_RX_COMP_QUEUES:
2764 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002765 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002766 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2767 adapter->opt_rx_comp_queues);
2768 break;
2769 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2770 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002771 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002772 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2773 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2774 break;
2775 case OPT_TX_ENTRIES_PER_SUBCRQ:
2776 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002777 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002778 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2779 adapter->opt_tx_entries_per_subcrq);
2780 break;
2781 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2782 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002783 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002784 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2785 adapter->opt_rxba_entries_per_subcrq);
2786 break;
2787 case TX_RX_DESC_REQ:
2788 adapter->tx_rx_desc_req = crq->query_capability.number;
2789 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2790 adapter->tx_rx_desc_req);
2791 break;
2792
2793 default:
2794 netdev_err(netdev, "Got invalid cap rsp %d\n",
2795 crq->query_capability.capability);
2796 }
2797
2798out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002799 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2800 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002801 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002802 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002803 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002804}
2805
Thomas Falcon032c5e82015-12-21 11:26:06 -06002806static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
2807{
Wei Yongjun96183182016-06-27 20:48:53 +08002808 struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002810 struct ibmvnic_error_buff *error_buff, *tmp2;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002811 unsigned long flags;
2812 unsigned long flags2;
2813
2814 spin_lock_irqsave(&adapter->inflight_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002815 list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002816 switch (inflight_cmd->crq.generic.cmd) {
2817 case LOGIN:
2818 dma_unmap_single(dev, adapter->login_buf_token,
2819 adapter->login_buf_sz,
2820 DMA_BIDIRECTIONAL);
2821 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2822 adapter->login_rsp_buf_sz,
2823 DMA_BIDIRECTIONAL);
2824 kfree(adapter->login_rsp_buf);
2825 kfree(adapter->login_buf);
2826 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002827 case REQUEST_ERROR_INFO:
2828 spin_lock_irqsave(&adapter->error_list_lock, flags2);
Wei Yongjun96183182016-06-27 20:48:53 +08002829 list_for_each_entry_safe(error_buff, tmp2,
2830 &adapter->errors, list) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002831 dma_unmap_single(dev, error_buff->dma,
2832 error_buff->len,
2833 DMA_FROM_DEVICE);
2834 kfree(error_buff->buff);
2835 list_del(&error_buff->list);
2836 kfree(error_buff);
2837 }
2838 spin_unlock_irqrestore(&adapter->error_list_lock,
2839 flags2);
2840 break;
2841 }
2842 list_del(&inflight_cmd->list);
2843 kfree(inflight_cmd);
2844 }
2845 spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2846}
2847
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002848static void ibmvnic_xport_event(struct work_struct *work)
2849{
2850 struct ibmvnic_adapter *adapter = container_of(work,
2851 struct ibmvnic_adapter,
2852 ibmvnic_xport);
2853 struct device *dev = &adapter->vdev->dev;
2854 long rc;
2855
2856 ibmvnic_free_inflight(adapter);
2857 release_sub_crqs(adapter);
2858 if (adapter->migrated) {
2859 rc = ibmvnic_reenable_crq_queue(adapter);
2860 if (rc)
2861 dev_err(dev, "Error after enable rc=%ld\n", rc);
2862 adapter->migrated = false;
2863 rc = ibmvnic_send_crq_init(adapter);
2864 if (rc)
2865 dev_err(dev, "Error sending init rc=%ld\n", rc);
2866 }
2867}
2868
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2870 struct ibmvnic_adapter *adapter)
2871{
2872 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2873 struct net_device *netdev = adapter->netdev;
2874 struct device *dev = &adapter->vdev->dev;
2875 long rc;
2876
2877 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
2878 ((unsigned long int *)crq)[0],
2879 ((unsigned long int *)crq)[1]);
2880 switch (gen_crq->first) {
2881 case IBMVNIC_CRQ_INIT_RSP:
2882 switch (gen_crq->cmd) {
2883 case IBMVNIC_CRQ_INIT:
2884 dev_info(dev, "Partner initialized\n");
2885 /* Send back a response */
2886 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002887 if (!rc)
2888 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002889 else
2890 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2891 break;
2892 case IBMVNIC_CRQ_INIT_COMPLETE:
2893 dev_info(dev, "Partner initialization complete\n");
2894 send_version_xchg(adapter);
2895 break;
2896 default:
2897 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2898 }
2899 return;
2900 case IBMVNIC_CRQ_XPORT_EVENT:
2901 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2902 dev_info(dev, "Re-enabling adapter\n");
2903 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002904 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002905 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2906 dev_info(dev, "Backing device failover detected\n");
2907 netif_carrier_off(netdev);
2908 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002909 } else {
2910 /* The adapter lost the connection */
2911 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2912 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002913 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002914 }
2915 return;
2916 case IBMVNIC_CRQ_CMD_RSP:
2917 break;
2918 default:
2919 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2920 gen_crq->first);
2921 return;
2922 }
2923
2924 switch (gen_crq->cmd) {
2925 case VERSION_EXCHANGE_RSP:
2926 rc = crq->version_exchange_rsp.rc.code;
2927 if (rc) {
2928 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2929 break;
2930 }
2931 dev_info(dev, "Partner protocol version is %d\n",
2932 crq->version_exchange_rsp.version);
2933 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2934 ibmvnic_version)
2935 ibmvnic_version =
2936 be16_to_cpu(crq->version_exchange_rsp.version);
2937 send_cap_queries(adapter);
2938 break;
2939 case QUERY_CAPABILITY_RSP:
2940 handle_query_cap_rsp(crq, adapter);
2941 break;
2942 case QUERY_MAP_RSP:
2943 handle_query_map_rsp(crq, adapter);
2944 break;
2945 case REQUEST_MAP_RSP:
2946 handle_request_map_rsp(crq, adapter);
2947 break;
2948 case REQUEST_UNMAP_RSP:
2949 handle_request_unmap_rsp(crq, adapter);
2950 break;
2951 case REQUEST_CAPABILITY_RSP:
2952 handle_request_cap_rsp(crq, adapter);
2953 break;
2954 case LOGIN_RSP:
2955 netdev_dbg(netdev, "Got Login Response\n");
2956 handle_login_rsp(crq, adapter);
2957 break;
2958 case LOGICAL_LINK_STATE_RSP:
2959 netdev_dbg(netdev, "Got Logical Link State Response\n");
2960 adapter->logical_link_state =
2961 crq->logical_link_state_rsp.link_state;
2962 break;
2963 case LINK_STATE_INDICATION:
2964 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2965 adapter->phys_link_state =
2966 crq->link_state_indication.phys_link_state;
2967 adapter->logical_link_state =
2968 crq->link_state_indication.logical_link_state;
2969 break;
2970 case CHANGE_MAC_ADDR_RSP:
2971 netdev_dbg(netdev, "Got MAC address change Response\n");
2972 handle_change_mac_rsp(crq, adapter);
2973 break;
2974 case ERROR_INDICATION:
2975 netdev_dbg(netdev, "Got Error Indication\n");
2976 handle_error_indication(crq, adapter);
2977 break;
2978 case REQUEST_ERROR_RSP:
2979 netdev_dbg(netdev, "Got Error Detail Response\n");
2980 handle_error_info_rsp(crq, adapter);
2981 break;
2982 case REQUEST_STATISTICS_RSP:
2983 netdev_dbg(netdev, "Got Statistics Response\n");
2984 complete(&adapter->stats_done);
2985 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002986 case QUERY_IP_OFFLOAD_RSP:
2987 netdev_dbg(netdev, "Got Query IP offload Response\n");
2988 handle_query_ip_offload_rsp(adapter);
2989 break;
2990 case MULTICAST_CTRL_RSP:
2991 netdev_dbg(netdev, "Got multicast control Response\n");
2992 break;
2993 case CONTROL_IP_OFFLOAD_RSP:
2994 netdev_dbg(netdev, "Got Control IP offload Response\n");
2995 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
2996 sizeof(adapter->ip_offload_ctrl),
2997 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05002998 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002999 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003000 case COLLECT_FW_TRACE_RSP:
3001 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3002 complete(&adapter->fw_done);
3003 break;
3004 default:
3005 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3006 gen_crq->cmd);
3007 }
3008}
3009
3010static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3011{
3012 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003013 unsigned long flags;
3014
3015 spin_lock_irqsave(&adapter->crq.lock, flags);
3016 vio_disable_interrupts(adapter->vdev);
3017 tasklet_schedule(&adapter->tasklet);
3018 spin_unlock_irqrestore(&adapter->crq.lock, flags);
3019 return IRQ_HANDLED;
3020}
3021
3022static void ibmvnic_tasklet(void *data)
3023{
3024 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003025 struct ibmvnic_crq_queue *queue = &adapter->crq;
3026 struct vio_dev *vdev = adapter->vdev;
3027 union ibmvnic_crq *crq;
3028 unsigned long flags;
3029 bool done = false;
3030
3031 spin_lock_irqsave(&queue->lock, flags);
3032 vio_disable_interrupts(vdev);
3033 while (!done) {
3034 /* Pull all the valid messages off the CRQ */
3035 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3036 ibmvnic_handle_crq(crq, adapter);
3037 crq->generic.first = 0;
3038 }
3039 vio_enable_interrupts(vdev);
3040 crq = ibmvnic_next_crq(adapter);
3041 if (crq) {
3042 vio_disable_interrupts(vdev);
3043 ibmvnic_handle_crq(crq, adapter);
3044 crq->generic.first = 0;
3045 } else {
Thomas Falcon249168a2017-02-15 12:18:00 -06003046 /* remain in tasklet until all
3047 * capabilities responses are received
3048 */
3049 if (!adapter->wait_capability)
3050 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003051 }
3052 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003053 /* if capabilities CRQ's were sent in this tasklet, the following
3054 * tasklet must wait until all responses are received
3055 */
3056 if (atomic_read(&adapter->running_cap_crqs) != 0)
3057 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003058 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003059}
3060
3061static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3062{
3063 struct vio_dev *vdev = adapter->vdev;
3064 int rc;
3065
3066 do {
3067 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3068 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3069
3070 if (rc)
3071 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3072
3073 return rc;
3074}
3075
3076static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3077{
3078 struct ibmvnic_crq_queue *crq = &adapter->crq;
3079 struct device *dev = &adapter->vdev->dev;
3080 struct vio_dev *vdev = adapter->vdev;
3081 int rc;
3082
3083 /* Close the CRQ */
3084 do {
3085 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3086 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3087
3088 /* Clean out the queue */
3089 memset(crq->msgs, 0, PAGE_SIZE);
3090 crq->cur = 0;
3091
3092 /* And re-open it again */
3093 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3094 crq->msg_token, PAGE_SIZE);
3095
3096 if (rc == H_CLOSED)
3097 /* Adapter is good, but other end is not ready */
3098 dev_warn(dev, "Partner adapter not ready\n");
3099 else if (rc != 0)
3100 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3101
3102 return rc;
3103}
3104
Nathan Fontenotf9928872017-03-30 02:48:54 -04003105static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003106{
3107 struct ibmvnic_crq_queue *crq = &adapter->crq;
3108 struct vio_dev *vdev = adapter->vdev;
3109 long rc;
3110
Nathan Fontenotf9928872017-03-30 02:48:54 -04003111 if (!crq->msgs)
3112 return;
3113
Thomas Falcon032c5e82015-12-21 11:26:06 -06003114 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3115 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003116 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003117 do {
3118 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3119 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3120
3121 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3122 DMA_BIDIRECTIONAL);
3123 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003124 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003125}
3126
Nathan Fontenotf9928872017-03-30 02:48:54 -04003127static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003128{
3129 struct ibmvnic_crq_queue *crq = &adapter->crq;
3130 struct device *dev = &adapter->vdev->dev;
3131 struct vio_dev *vdev = adapter->vdev;
3132 int rc, retrc = -ENOMEM;
3133
Nathan Fontenotf9928872017-03-30 02:48:54 -04003134 if (crq->msgs)
3135 return 0;
3136
Thomas Falcon032c5e82015-12-21 11:26:06 -06003137 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3138 /* Should we allocate more than one page? */
3139
3140 if (!crq->msgs)
3141 return -ENOMEM;
3142
3143 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3144 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3145 DMA_BIDIRECTIONAL);
3146 if (dma_mapping_error(dev, crq->msg_token))
3147 goto map_failed;
3148
3149 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3150 crq->msg_token, PAGE_SIZE);
3151
3152 if (rc == H_RESOURCE)
3153 /* maybe kexecing and resource is busy. try a reset */
3154 rc = ibmvnic_reset_crq(adapter);
3155 retrc = rc;
3156
3157 if (rc == H_CLOSED) {
3158 dev_warn(dev, "Partner adapter not ready\n");
3159 } else if (rc) {
3160 dev_warn(dev, "Error %d opening adapter\n", rc);
3161 goto reg_crq_failed;
3162 }
3163
3164 retrc = 0;
3165
Thomas Falcon6c267b32017-02-15 12:17:58 -06003166 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3167 (unsigned long)adapter);
3168
Thomas Falcon032c5e82015-12-21 11:26:06 -06003169 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3170 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3171 adapter);
3172 if (rc) {
3173 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3174 vdev->irq, rc);
3175 goto req_irq_failed;
3176 }
3177
3178 rc = vio_enable_interrupts(vdev);
3179 if (rc) {
3180 dev_err(dev, "Error %d enabling interrupts\n", rc);
3181 goto req_irq_failed;
3182 }
3183
3184 crq->cur = 0;
3185 spin_lock_init(&crq->lock);
3186
3187 return retrc;
3188
3189req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003190 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003191 do {
3192 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3193 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3194reg_crq_failed:
3195 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3196map_failed:
3197 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003198 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003199 return retrc;
3200}
3201
Thomas Falcon65dc6892016-07-06 15:35:18 -05003202static void handle_crq_init_rsp(struct work_struct *work)
3203{
3204 struct ibmvnic_adapter *adapter = container_of(work,
3205 struct ibmvnic_adapter,
3206 vnic_crq_init);
3207 struct device *dev = &adapter->vdev->dev;
3208 struct net_device *netdev = adapter->netdev;
3209 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003210 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003211 int rc;
3212
Thomas Falcondfad09a2016-08-18 11:37:51 -05003213 if (adapter->failover) {
3214 release_sub_crqs(adapter);
3215 if (netif_running(netdev)) {
3216 netif_tx_disable(netdev);
3217 ibmvnic_close(netdev);
3218 restart = true;
3219 }
3220 }
3221
Thomas Falcon65dc6892016-07-06 15:35:18 -05003222 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003223 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003224 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3225 dev_err(dev, "Passive init timeout\n");
3226 goto task_failed;
3227 }
3228
Thomas Falconf39f0d12017-02-14 10:22:59 -06003229 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003230
Thomas Falcondfad09a2016-08-18 11:37:51 -05003231 if (adapter->failover) {
3232 adapter->failover = false;
3233 if (restart) {
3234 rc = ibmvnic_open(netdev);
3235 if (rc)
3236 goto restart_failed;
3237 }
3238 netif_carrier_on(netdev);
3239 return;
3240 }
3241
Thomas Falcon65dc6892016-07-06 15:35:18 -05003242 rc = register_netdev(netdev);
3243 if (rc) {
3244 dev_err(dev,
3245 "failed to register netdev rc=%d\n", rc);
3246 goto register_failed;
3247 }
3248 dev_info(dev, "ibmvnic registered\n");
3249
3250 return;
3251
Thomas Falcondfad09a2016-08-18 11:37:51 -05003252restart_failed:
3253 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003254register_failed:
3255 release_sub_crqs(adapter);
3256task_failed:
3257 dev_err(dev, "Passive initialization was not successful\n");
3258}
3259
John Allenf6ef6402017-03-17 17:13:42 -05003260static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3261{
3262 struct device *dev = &adapter->vdev->dev;
3263 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003264 int rc;
3265
Nathan Fontenotf9928872017-03-30 02:48:54 -04003266 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003267 if (rc) {
3268 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3269 return rc;
3270 }
3271
3272 adapter->stats_token = dma_map_single(dev, &adapter->stats,
3273 sizeof(struct ibmvnic_statistics),
3274 DMA_FROM_DEVICE);
3275 if (dma_mapping_error(dev, adapter->stats_token)) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003276 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003277 dev_err(dev, "Couldn't map stats buffer\n");
3278 return -ENOMEM;
3279 }
3280
John Allenf6ef6402017-03-17 17:13:42 -05003281 init_completion(&adapter->init_done);
3282 ibmvnic_send_crq_init(adapter);
3283 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3284 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003285 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003286 return -1;
3287 }
3288
3289 return 0;
3290}
3291
Thomas Falcon032c5e82015-12-21 11:26:06 -06003292static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3293{
3294 struct ibmvnic_adapter *adapter;
3295 struct net_device *netdev;
3296 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003297 int rc;
3298
3299 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3300 dev->unit_address);
3301
3302 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3303 VETH_MAC_ADDR, NULL);
3304 if (!mac_addr_p) {
3305 dev_err(&dev->dev,
3306 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3307 __FILE__, __LINE__);
3308 return 0;
3309 }
3310
3311 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3312 IBMVNIC_MAX_TX_QUEUES);
3313 if (!netdev)
3314 return -ENOMEM;
3315
3316 adapter = netdev_priv(netdev);
3317 dev_set_drvdata(&dev->dev, netdev);
3318 adapter->vdev = dev;
3319 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003320 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003321
3322 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3323 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3324 netdev->irq = dev->irq;
3325 netdev->netdev_ops = &ibmvnic_netdev_ops;
3326 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3327 SET_NETDEV_DEV(netdev, &dev->dev);
3328
Thomas Falcon65dc6892016-07-06 15:35:18 -05003329 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003330 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003331
Thomas Falcon032c5e82015-12-21 11:26:06 -06003332 spin_lock_init(&adapter->stats_lock);
3333
Thomas Falcon032c5e82015-12-21 11:26:06 -06003334 INIT_LIST_HEAD(&adapter->errors);
3335 INIT_LIST_HEAD(&adapter->inflight);
3336 spin_lock_init(&adapter->error_list_lock);
3337 spin_lock_init(&adapter->inflight_lock);
3338
John Allenf6ef6402017-03-17 17:13:42 -05003339 rc = ibmvnic_init(adapter);
3340 if (rc) {
3341 free_netdev(netdev);
3342 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003343 }
3344
Thomas Falconf39f0d12017-02-14 10:22:59 -06003345 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003346 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003347
3348 rc = register_netdev(netdev);
3349 if (rc) {
3350 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003351 free_netdev(netdev);
3352 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 }
3354 dev_info(&dev->dev, "ibmvnic registered\n");
3355
3356 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003357}
3358
3359static int ibmvnic_remove(struct vio_dev *dev)
3360{
3361 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003362
3363 unregister_netdev(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003364 free_netdev(netdev);
3365 dev_set_drvdata(&dev->dev, NULL);
3366
3367 return 0;
3368}
3369
3370static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3371{
3372 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3373 struct ibmvnic_adapter *adapter;
3374 struct iommu_table *tbl;
3375 unsigned long ret = 0;
3376 int i;
3377
3378 tbl = get_iommu_table_base(&vdev->dev);
3379
3380 /* netdev inits at probe time along with the structures we need below*/
3381 if (!netdev)
3382 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3383
3384 adapter = netdev_priv(netdev);
3385
3386 ret += PAGE_SIZE; /* the crq message queue */
3387 ret += adapter->bounce_buffer_size;
3388 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3389
3390 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3391 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3392
3393 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3394 i++)
3395 ret += adapter->rx_pool[i].size *
3396 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3397
3398 return ret;
3399}
3400
3401static int ibmvnic_resume(struct device *dev)
3402{
3403 struct net_device *netdev = dev_get_drvdata(dev);
3404 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3405 int i;
3406
3407 /* kick the interrupt handlers just in case we lost an interrupt */
3408 for (i = 0; i < adapter->req_rx_queues; i++)
3409 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3410 adapter->rx_scrq[i]);
3411
3412 return 0;
3413}
3414
3415static struct vio_device_id ibmvnic_device_table[] = {
3416 {"network", "IBM,vnic"},
3417 {"", "" }
3418};
3419MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3420
3421static const struct dev_pm_ops ibmvnic_pm_ops = {
3422 .resume = ibmvnic_resume
3423};
3424
3425static struct vio_driver ibmvnic_driver = {
3426 .id_table = ibmvnic_device_table,
3427 .probe = ibmvnic_probe,
3428 .remove = ibmvnic_remove,
3429 .get_desired_dma = ibmvnic_get_desired_dma,
3430 .name = ibmvnic_driver_name,
3431 .pm = &ibmvnic_pm_ops,
3432};
3433
3434/* module functions */
3435static int __init ibmvnic_module_init(void)
3436{
3437 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3438 IBMVNIC_DRIVER_VERSION);
3439
3440 return vio_register_driver(&ibmvnic_driver);
3441}
3442
3443static void __exit ibmvnic_module_exit(void)
3444{
3445 vio_unregister_driver(&ibmvnic_driver);
3446}
3447
3448module_init(ibmvnic_module_init);
3449module_exit(ibmvnic_module_exit);