blob: 199cccbb577a1e32a19a60f49a7dce85a7540608 [file] [log] [blame]
Thomas Falcon032c5e82015-12-21 11:26:06 -06001/**************************************************************************/
2/* */
3/* IBM System i and System p Virtual NIC Device Driver */
4/* Copyright (C) 2014 IBM Corp. */
5/* Santiago Leon (santi_leon@yahoo.com) */
6/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7/* John Allen (jallen@linux.vnet.ibm.com) */
8/* */
9/* This program is free software; you can redistribute it and/or modify */
10/* it under the terms of the GNU General Public License as published by */
11/* the Free Software Foundation; either version 2 of the License, or */
12/* (at your option) any later version. */
13/* */
14/* This program is distributed in the hope that it will be useful, */
15/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17/* GNU General Public License for more details. */
18/* */
19/* You should have received a copy of the GNU General Public License */
20/* along with this program. */
21/* */
22/* This module contains the implementation of a virtual ethernet device */
23/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24/* option of the RS/6000 Platform Architecture to interface with virtual */
25/* ethernet NICs that are presented to the partition by the hypervisor. */
26/* */
27/* Messages are passed between the VNIC driver and the VNIC server using */
28/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29/* issue and receive commands that initiate communication with the server */
30/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31/* are used by the driver to notify the server that a packet is */
32/* ready for transmission or that a buffer has been added to receive a */
33/* packet. Subsequently, sCRQs are used by the server to notify the */
34/* driver that a packet transmission has been completed or that a packet */
35/* has been received and placed in a waiting buffer. */
36/* */
37/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38/* which skbs are DMA mapped and immediately unmapped when the transmit */
39/* or receive has been completed, the VNIC driver is required to use */
40/* "long term mapping". This entails that large, continuous DMA mapped */
41/* buffers are allocated on driver initialization and these buffers are */
42/* then continuously reused to pass skbs to and from the VNIC server. */
43/* */
44/**************************************************************************/
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/errno.h>
50#include <linux/completion.h>
51#include <linux/ioport.h>
52#include <linux/dma-mapping.h>
53#include <linux/kernel.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/skbuff.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/mm.h>
60#include <linux/ethtool.h>
61#include <linux/proc_fs.h>
62#include <linux/in.h>
63#include <linux/ip.h>
Thomas Falconad7775d2016-04-01 17:20:34 -050064#include <linux/ipv6.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060065#include <linux/irq.h>
66#include <linux/kthread.h>
67#include <linux/seq_file.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060068#include <linux/interrupt.h>
69#include <net/net_namespace.h>
70#include <asm/hvcall.h>
71#include <linux/atomic.h>
72#include <asm/vio.h>
73#include <asm/iommu.h>
74#include <linux/uaccess.h>
75#include <asm/firmware.h>
Thomas Falcon65dc6892016-07-06 15:35:18 -050076#include <linux/workqueue.h>
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -040077#include <linux/if_vlan.h>
Thomas Falcon032c5e82015-12-21 11:26:06 -060078
79#include "ibmvnic.h"
80
81static const char ibmvnic_driver_name[] = "ibmvnic";
82static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
83
84MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
85MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
88
89static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90static int ibmvnic_remove(struct vio_dev *);
91static void release_sub_crqs(struct ibmvnic_adapter *);
92static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -050098static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
Thomas Falcon032c5e82015-12-21 11:26:06 -060099static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static int ibmvnic_poll(struct napi_struct *napi, int data);
109static void send_map_query(struct ibmvnic_adapter *adapter);
110static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111static void send_request_unmap(struct ibmvnic_adapter *, u8);
John Allenbd0b6722017-03-17 17:13:40 -0500112static void send_login(struct ibmvnic_adapter *adapter);
113static void send_cap_queries(struct ibmvnic_adapter *adapter);
114static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
John Allenea5509f2017-03-17 17:13:43 -0500115static int ibmvnic_init(struct ibmvnic_adapter *);
Nathan Fontenotf9928872017-03-30 02:48:54 -0400116static void release_crq_queue(struct ibmvnic_adapter *);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
154 unsigned long *irq)
155{
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
157 long rc;
158
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
160 *number = retbuf[0];
161 *irq = retbuf[1];
162
163 return rc;
164}
165
Thomas Falcon032c5e82015-12-21 11:26:06 -0600166static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
168{
169 struct device *dev = &adapter->vdev->dev;
170
171 ltb->size = size;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
173 GFP_KERNEL);
174
175 if (!ltb->buff) {
176 dev_err(dev, "Couldn't alloc long term buffer\n");
177 return -ENOMEM;
178 }
179 ltb->map_id = adapter->map_id;
180 adapter->map_id++;
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -0500181
182 init_completion(&adapter->fw_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600185 wait_for_completion(&adapter->fw_done);
186 return 0;
187}
188
189static void free_long_term_buff(struct ibmvnic_adapter *adapter,
190 struct ibmvnic_long_term_buff *ltb)
191{
192 struct device *dev = &adapter->vdev->dev;
193
Nathan Fontenotc657e322017-03-30 02:49:06 -0400194 if (!ltb->buff)
195 return;
196
Thomas Falcondfad09a2016-08-18 11:37:51 -0500197 if (!adapter->failover)
198 send_request_unmap(adapter, ltb->map_id);
Brian King59af56c2017-04-19 13:44:41 -0400199 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600200}
201
Thomas Falcon032c5e82015-12-21 11:26:06 -0600202static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
203 struct ibmvnic_rx_pool *pool)
204{
205 int count = pool->size - atomic_read(&pool->available);
206 struct device *dev = &adapter->vdev->dev;
207 int buffers_added = 0;
208 unsigned long lpar_rc;
209 union sub_crq sub_crq;
210 struct sk_buff *skb;
211 unsigned int offset;
212 dma_addr_t dma_addr;
213 unsigned char *dst;
214 u64 *handle_array;
215 int shift = 0;
216 int index;
217 int i;
218
219 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
220 be32_to_cpu(adapter->login_rsp_buf->
221 off_rxadd_subcrqs));
222
223 for (i = 0; i < count; ++i) {
224 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
225 if (!skb) {
226 dev_err(dev, "Couldn't replenish rx buff\n");
227 adapter->replenish_no_mem++;
228 break;
229 }
230
231 index = pool->free_map[pool->next_free];
232
233 if (pool->rx_buff[index].skb)
234 dev_err(dev, "Inconsistent free_map!\n");
235
236 /* Copy the skb to the long term mapped DMA buffer */
237 offset = index * pool->buff_size;
238 dst = pool->long_term_buff.buff + offset;
239 memset(dst, 0, pool->buff_size);
240 dma_addr = pool->long_term_buff.addr + offset;
241 pool->rx_buff[index].data = dst;
242
243 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
244 pool->rx_buff[index].dma = dma_addr;
245 pool->rx_buff[index].skb = skb;
246 pool->rx_buff[index].pool_index = pool->index;
247 pool->rx_buff[index].size = pool->buff_size;
248
249 memset(&sub_crq, 0, sizeof(sub_crq));
250 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
251 sub_crq.rx_add.correlator =
252 cpu_to_be64((u64)&pool->rx_buff[index]);
253 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
254 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
255
256 /* The length field of the sCRQ is defined to be 24 bits so the
257 * buffer size needs to be left shifted by a byte before it is
258 * converted to big endian to prevent the last byte from being
259 * truncated.
260 */
261#ifdef __LITTLE_ENDIAN__
262 shift = 8;
263#endif
264 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
265
266 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
267 &sub_crq);
268 if (lpar_rc != H_SUCCESS)
269 goto failure;
270
271 buffers_added++;
272 adapter->replenish_add_buff_success++;
273 pool->next_free = (pool->next_free + 1) % pool->size;
274 }
275 atomic_add(buffers_added, &pool->available);
276 return;
277
278failure:
279 dev_info(dev, "replenish pools failure\n");
280 pool->free_map[pool->next_free] = index;
281 pool->rx_buff[index].skb = NULL;
282 if (!dma_mapping_error(dev, dma_addr))
283 dma_unmap_single(dev, dma_addr, pool->buff_size,
284 DMA_FROM_DEVICE);
285
286 dev_kfree_skb_any(skb);
287 adapter->replenish_add_buff_failure++;
288 atomic_add(buffers_added, &pool->available);
289}
290
291static void replenish_pools(struct ibmvnic_adapter *adapter)
292{
293 int i;
294
295 if (adapter->migrated)
296 return;
297
298 adapter->replenish_task_cycles++;
299 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
300 i++) {
301 if (adapter->rx_pool[i].active)
302 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
303 }
304}
305
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -0400306static void release_stats_token(struct ibmvnic_adapter *adapter)
307{
308 struct device *dev = &adapter->vdev->dev;
309
310 if (!adapter->stats_token)
311 return;
312
313 dma_unmap_single(dev, adapter->stats_token,
314 sizeof(struct ibmvnic_statistics),
315 DMA_FROM_DEVICE);
316 adapter->stats_token = 0;
317}
318
319static int init_stats_token(struct ibmvnic_adapter *adapter)
320{
321 struct device *dev = &adapter->vdev->dev;
322 dma_addr_t stok;
323
324 stok = dma_map_single(dev, &adapter->stats,
325 sizeof(struct ibmvnic_statistics),
326 DMA_FROM_DEVICE);
327 if (dma_mapping_error(dev, stok)) {
328 dev_err(dev, "Couldn't map stats buffer\n");
329 return -1;
330 }
331
332 adapter->stats_token = stok;
333 return 0;
334}
335
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400336static void release_rx_pools(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600337{
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400338 struct ibmvnic_rx_pool *rx_pool;
339 int rx_scrqs;
340 int i, j;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600341
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400342 if (!adapter->rx_pool)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600343 return;
344
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400345 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
346 for (i = 0; i < rx_scrqs; i++) {
347 rx_pool = &adapter->rx_pool[i];
348
349 kfree(rx_pool->free_map);
350 free_long_term_buff(adapter, &rx_pool->long_term_buff);
351
352 if (!rx_pool->rx_buff)
353 continue;
354
355 for (j = 0; j < rx_pool->size; j++) {
356 if (rx_pool->rx_buff[j].skb) {
357 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
358 rx_pool->rx_buff[i].skb = NULL;
359 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600360 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400361
362 kfree(rx_pool->rx_buff);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600363 }
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400364
365 kfree(adapter->rx_pool);
366 adapter->rx_pool = NULL;
367}
368
369static int init_rx_pools(struct net_device *netdev)
370{
371 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
372 struct device *dev = &adapter->vdev->dev;
373 struct ibmvnic_rx_pool *rx_pool;
374 int rxadd_subcrqs;
375 u64 *size_array;
376 int i, j;
377
378 rxadd_subcrqs =
379 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
380 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
381 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
382
383 adapter->rx_pool = kcalloc(rxadd_subcrqs,
384 sizeof(struct ibmvnic_rx_pool),
385 GFP_KERNEL);
386 if (!adapter->rx_pool) {
387 dev_err(dev, "Failed to allocate rx pools\n");
388 return -1;
389 }
390
391 for (i = 0; i < rxadd_subcrqs; i++) {
392 rx_pool = &adapter->rx_pool[i];
393
394 netdev_dbg(adapter->netdev,
395 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
396 i, adapter->req_rx_add_entries_per_subcrq,
397 be64_to_cpu(size_array[i]));
398
399 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
400 rx_pool->index = i;
401 rx_pool->buff_size = be64_to_cpu(size_array[i]);
402 rx_pool->active = 1;
403
404 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
405 GFP_KERNEL);
406 if (!rx_pool->free_map) {
407 release_rx_pools(adapter);
408 return -1;
409 }
410
411 rx_pool->rx_buff = kcalloc(rx_pool->size,
412 sizeof(struct ibmvnic_rx_buff),
413 GFP_KERNEL);
414 if (!rx_pool->rx_buff) {
415 dev_err(dev, "Couldn't alloc rx buffers\n");
416 release_rx_pools(adapter);
417 return -1;
418 }
419
420 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
421 rx_pool->size * rx_pool->buff_size)) {
422 release_rx_pools(adapter);
423 return -1;
424 }
425
426 for (j = 0; j < rx_pool->size; ++j)
427 rx_pool->free_map[j] = j;
428
429 atomic_set(&rx_pool->available, 0);
430 rx_pool->next_alloc = 0;
431 rx_pool->next_free = 0;
432 }
433
434 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600435}
436
Nathan Fontenotc657e322017-03-30 02:49:06 -0400437static void release_tx_pools(struct ibmvnic_adapter *adapter)
438{
439 struct ibmvnic_tx_pool *tx_pool;
440 int i, tx_scrqs;
441
442 if (!adapter->tx_pool)
443 return;
444
445 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
446 for (i = 0; i < tx_scrqs; i++) {
447 tx_pool = &adapter->tx_pool[i];
448 kfree(tx_pool->tx_buff);
449 free_long_term_buff(adapter, &tx_pool->long_term_buff);
450 kfree(tx_pool->free_map);
451 }
452
453 kfree(adapter->tx_pool);
454 adapter->tx_pool = NULL;
455}
456
457static int init_tx_pools(struct net_device *netdev)
458{
459 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
460 struct device *dev = &adapter->vdev->dev;
461 struct ibmvnic_tx_pool *tx_pool;
462 int tx_subcrqs;
463 int i, j;
464
465 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
466 adapter->tx_pool = kcalloc(tx_subcrqs,
467 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
468 if (!adapter->tx_pool)
469 return -1;
470
471 for (i = 0; i < tx_subcrqs; i++) {
472 tx_pool = &adapter->tx_pool[i];
473 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
474 sizeof(struct ibmvnic_tx_buff),
475 GFP_KERNEL);
476 if (!tx_pool->tx_buff) {
477 dev_err(dev, "tx pool buffer allocation failed\n");
478 release_tx_pools(adapter);
479 return -1;
480 }
481
482 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
483 adapter->req_tx_entries_per_subcrq *
484 adapter->req_mtu)) {
485 release_tx_pools(adapter);
486 return -1;
487 }
488
489 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
490 sizeof(int), GFP_KERNEL);
491 if (!tx_pool->free_map) {
492 release_tx_pools(adapter);
493 return -1;
494 }
495
496 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
497 tx_pool->free_map[j] = j;
498
499 tx_pool->consumer_index = 0;
500 tx_pool->producer_index = 0;
501 }
502
503 return 0;
504}
505
Nathan Fontenot661a2622017-04-19 13:44:58 -0400506static void release_error_buffers(struct ibmvnic_adapter *adapter)
507{
508 struct device *dev = &adapter->vdev->dev;
509 struct ibmvnic_error_buff *error_buff, *tmp;
510 unsigned long flags;
511
512 spin_lock_irqsave(&adapter->error_list_lock, flags);
513 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
514 list_del(&error_buff->list);
515 dma_unmap_single(dev, error_buff->dma, error_buff->len,
516 DMA_FROM_DEVICE);
517 kfree(error_buff->buff);
518 kfree(error_buff);
519 }
520 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
521}
522
John Allena57a5d22017-03-17 17:13:41 -0500523static int ibmvnic_login(struct net_device *netdev)
Thomas Falcon032c5e82015-12-21 11:26:06 -0600524{
525 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
John Allenbd0b6722017-03-17 17:13:40 -0500526 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600527 struct device *dev = &adapter->vdev->dev;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600528
John Allenbd0b6722017-03-17 17:13:40 -0500529 do {
530 if (adapter->renegotiate) {
531 adapter->renegotiate = false;
Nathan Fontenotb5108882017-03-30 02:49:18 -0400532 release_sub_crqs(adapter);
John Allenbd0b6722017-03-17 17:13:40 -0500533
534 reinit_completion(&adapter->init_done);
535 send_cap_queries(adapter);
536 if (!wait_for_completion_timeout(&adapter->init_done,
537 timeout)) {
538 dev_err(dev, "Capabilities query timeout\n");
539 return -1;
540 }
541 }
542
543 reinit_completion(&adapter->init_done);
544 send_login(adapter);
545 if (!wait_for_completion_timeout(&adapter->init_done,
546 timeout)) {
547 dev_err(dev, "Login timeout\n");
548 return -1;
549 }
550 } while (adapter->renegotiate);
551
John Allena57a5d22017-03-17 17:13:41 -0500552 return 0;
553}
554
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400555static void release_resources(struct ibmvnic_adapter *adapter)
556{
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400557 release_tx_pools(adapter);
558 release_rx_pools(adapter);
559
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400560 release_stats_token(adapter);
Nathan Fontenot661a2622017-04-19 13:44:58 -0400561 release_error_buffers(adapter);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400562}
563
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400564static int set_real_num_queues(struct net_device *netdev)
565{
566 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
567 int rc;
568
569 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
570 if (rc) {
571 netdev_err(netdev, "failed to set the number of tx queues\n");
572 return rc;
573 }
574
575 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
576 if (rc)
577 netdev_err(netdev, "failed to set the number of rx queues\n");
578
579 return rc;
580}
581
John Allena57a5d22017-03-17 17:13:41 -0500582static int ibmvnic_open(struct net_device *netdev)
583{
584 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
585 struct device *dev = &adapter->vdev->dev;
John Allena57a5d22017-03-17 17:13:41 -0500586 union ibmvnic_crq crq;
John Allena57a5d22017-03-17 17:13:41 -0500587 int rc = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400588 int i;
John Allena57a5d22017-03-17 17:13:41 -0500589
John Allenea5509f2017-03-17 17:13:43 -0500590 if (adapter->is_closed) {
591 rc = ibmvnic_init(adapter);
592 if (rc)
593 return rc;
594 }
595
John Allena57a5d22017-03-17 17:13:41 -0500596 rc = ibmvnic_login(netdev);
597 if (rc)
598 return rc;
599
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400600 rc = set_real_num_queues(netdev);
601 if (rc)
602 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500603
604 rc = init_sub_crq_irqs(adapter);
605 if (rc) {
606 dev_err(dev, "failed to initialize sub crq irqs\n");
607 return -1;
608 }
609
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400610 rc = init_stats_token(adapter);
611 if (rc)
612 return rc;
613
Thomas Falcon032c5e82015-12-21 11:26:06 -0600614 adapter->map_id = 1;
615 adapter->napi = kcalloc(adapter->req_rx_queues,
616 sizeof(struct napi_struct), GFP_KERNEL);
617 if (!adapter->napi)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400618 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600619 for (i = 0; i < adapter->req_rx_queues; i++) {
620 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
621 NAPI_POLL_WEIGHT);
622 napi_enable(&adapter->napi[i]);
623 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600624
Thomas Falcon032c5e82015-12-21 11:26:06 -0600625 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400626
627 rc = init_rx_pools(netdev);
628 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400629 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600630
Nathan Fontenotc657e322017-03-30 02:49:06 -0400631 rc = init_tx_pools(netdev);
632 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400633 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600634
Thomas Falcon032c5e82015-12-21 11:26:06 -0600635 replenish_pools(adapter);
636
637 /* We're ready to receive frames, enable the sub-crq interrupts and
638 * set the logical link state to up
639 */
640 for (i = 0; i < adapter->req_rx_queues; i++)
641 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
642
643 for (i = 0; i < adapter->req_tx_queues; i++)
644 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
645
646 memset(&crq, 0, sizeof(crq));
647 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
648 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
649 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
650 ibmvnic_send_crq(adapter, &crq);
651
Thomas Falconb8efb892016-07-06 15:35:15 -0500652 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500653 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500654
Thomas Falcon032c5e82015-12-21 11:26:06 -0600655 return 0;
656
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400657ibmvnic_open_fail:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600658 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500659 napi_disable(&adapter->napi[i]);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400660 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600661 return -ENOMEM;
662}
663
Brian Kingdd9c20f2017-04-19 13:45:10 -0400664static void disable_sub_crqs(struct ibmvnic_adapter *adapter)
665{
666 int i;
667
668 if (adapter->tx_scrq) {
669 for (i = 0; i < adapter->req_tx_queues; i++)
670 if (adapter->tx_scrq[i])
671 disable_irq(adapter->tx_scrq[i]->irq);
672 }
673
674 if (adapter->rx_scrq) {
675 for (i = 0; i < adapter->req_rx_queues; i++)
676 if (adapter->rx_scrq[i])
677 disable_irq(adapter->rx_scrq[i]->irq);
678 }
679}
680
John Allenea5509f2017-03-17 17:13:43 -0500681static int ibmvnic_close(struct net_device *netdev)
682{
683 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
684 union ibmvnic_crq crq;
685 int i;
686
687 adapter->closing = true;
Brian Kingdd9c20f2017-04-19 13:45:10 -0400688 disable_sub_crqs(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500689
690 for (i = 0; i < adapter->req_rx_queues; i++)
691 napi_disable(&adapter->napi[i]);
692
693 if (!adapter->failover)
694 netif_tx_stop_all_queues(netdev);
695
Thomas Falcon032c5e82015-12-21 11:26:06 -0600696 memset(&crq, 0, sizeof(crq));
697 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
698 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
699 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
700 ibmvnic_send_crq(adapter, &crq);
701
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400702 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600703
John Allenea5509f2017-03-17 17:13:43 -0500704 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600705 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600706 return 0;
707}
708
Thomas Falconad7775d2016-04-01 17:20:34 -0500709/**
710 * build_hdr_data - creates L2/L3/L4 header data buffer
711 * @hdr_field - bitfield determining needed headers
712 * @skb - socket buffer
713 * @hdr_len - array of header lengths
714 * @tot_len - total length of data
715 *
716 * Reads hdr_field to determine which headers are needed by firmware.
717 * Builds a buffer containing these headers. Saves individual header
718 * lengths and total buffer length to be used to build descriptors.
719 */
720static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
721 int *hdr_len, u8 *hdr_data)
722{
723 int len = 0;
724 u8 *hdr;
725
726 hdr_len[0] = sizeof(struct ethhdr);
727
728 if (skb->protocol == htons(ETH_P_IP)) {
729 hdr_len[1] = ip_hdr(skb)->ihl * 4;
730 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
731 hdr_len[2] = tcp_hdrlen(skb);
732 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
733 hdr_len[2] = sizeof(struct udphdr);
734 } else if (skb->protocol == htons(ETH_P_IPV6)) {
735 hdr_len[1] = sizeof(struct ipv6hdr);
736 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
737 hdr_len[2] = tcp_hdrlen(skb);
738 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
739 hdr_len[2] = sizeof(struct udphdr);
740 }
741
742 memset(hdr_data, 0, 120);
743 if ((hdr_field >> 6) & 1) {
744 hdr = skb_mac_header(skb);
745 memcpy(hdr_data, hdr, hdr_len[0]);
746 len += hdr_len[0];
747 }
748
749 if ((hdr_field >> 5) & 1) {
750 hdr = skb_network_header(skb);
751 memcpy(hdr_data + len, hdr, hdr_len[1]);
752 len += hdr_len[1];
753 }
754
755 if ((hdr_field >> 4) & 1) {
756 hdr = skb_transport_header(skb);
757 memcpy(hdr_data + len, hdr, hdr_len[2]);
758 len += hdr_len[2];
759 }
760 return len;
761}
762
763/**
764 * create_hdr_descs - create header and header extension descriptors
765 * @hdr_field - bitfield determining needed headers
766 * @data - buffer containing header data
767 * @len - length of data buffer
768 * @hdr_len - array of individual header lengths
769 * @scrq_arr - descriptor array
770 *
771 * Creates header and, if needed, header extension descriptors and
772 * places them in a descriptor array, scrq_arr
773 */
774
775static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
776 union sub_crq *scrq_arr)
777{
778 union sub_crq hdr_desc;
779 int tmp_len = len;
780 u8 *data, *cur;
781 int tmp;
782
783 while (tmp_len > 0) {
784 cur = hdr_data + len - tmp_len;
785
786 memset(&hdr_desc, 0, sizeof(hdr_desc));
787 if (cur != hdr_data) {
788 data = hdr_desc.hdr_ext.data;
789 tmp = tmp_len > 29 ? 29 : tmp_len;
790 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
791 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
792 hdr_desc.hdr_ext.len = tmp;
793 } else {
794 data = hdr_desc.hdr.data;
795 tmp = tmp_len > 24 ? 24 : tmp_len;
796 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
797 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
798 hdr_desc.hdr.len = tmp;
799 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
800 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
801 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
802 hdr_desc.hdr.flag = hdr_field << 1;
803 }
804 memcpy(data, cur, tmp);
805 tmp_len -= tmp;
806 *scrq_arr = hdr_desc;
807 scrq_arr++;
808 }
809}
810
811/**
812 * build_hdr_descs_arr - build a header descriptor array
813 * @skb - socket buffer
814 * @num_entries - number of descriptors to be sent
815 * @subcrq - first TX descriptor
816 * @hdr_field - bit field determining which headers will be sent
817 *
818 * This function will build a TX descriptor array with applicable
819 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
820 */
821
822static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
823 int *num_entries, u8 hdr_field)
824{
825 int hdr_len[3] = {0, 0, 0};
826 int tot_len, len;
827 u8 *hdr_data = txbuff->hdr_data;
828
829 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
830 txbuff->hdr_data);
831 len = tot_len;
832 len -= 24;
833 if (len > 0)
834 num_entries += len % 29 ? len / 29 + 1 : len / 29;
835 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
836 txbuff->indir_arr + 1);
837}
838
Thomas Falcon032c5e82015-12-21 11:26:06 -0600839static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
840{
841 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
842 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500843 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600844 struct device *dev = &adapter->vdev->dev;
845 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600846 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600847 struct ibmvnic_tx_pool *tx_pool;
848 unsigned int tx_send_failed = 0;
849 unsigned int tx_map_failed = 0;
850 unsigned int tx_dropped = 0;
851 unsigned int tx_packets = 0;
852 unsigned int tx_bytes = 0;
853 dma_addr_t data_dma_addr;
854 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600855 unsigned long lpar_rc;
856 union sub_crq tx_crq;
857 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500858 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600859 unsigned char *dst;
860 u64 *handle_array;
861 int index = 0;
862 int ret = 0;
863
864 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600865 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600866 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
867 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
868 be32_to_cpu(adapter->login_rsp_buf->
869 off_txsubm_subcrqs));
870 if (adapter->migrated) {
871 tx_send_failed++;
872 tx_dropped++;
873 ret = NETDEV_TX_BUSY;
874 goto out;
875 }
876
877 index = tx_pool->free_map[tx_pool->consumer_index];
878 offset = index * adapter->req_mtu;
879 dst = tx_pool->long_term_buff.buff + offset;
880 memset(dst, 0, adapter->req_mtu);
881 skb_copy_from_linear_data(skb, dst, skb->len);
882 data_dma_addr = tx_pool->long_term_buff.addr + offset;
883
884 tx_pool->consumer_index =
885 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600886 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600887
888 tx_buff = &tx_pool->tx_buff[index];
889 tx_buff->skb = skb;
890 tx_buff->data_dma[0] = data_dma_addr;
891 tx_buff->data_len[0] = skb->len;
892 tx_buff->index = index;
893 tx_buff->pool_index = queue_num;
894 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600895
896 memset(&tx_crq, 0, sizeof(tx_crq));
897 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
898 tx_crq.v1.type = IBMVNIC_TX_DESC;
899 tx_crq.v1.n_crq_elem = 1;
900 tx_crq.v1.n_sge = 1;
901 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
902 tx_crq.v1.correlator = cpu_to_be32(index);
903 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
904 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
905 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
906
907 if (adapter->vlan_header_insertion) {
908 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
909 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
910 }
911
912 if (skb->protocol == htons(ETH_P_IP)) {
913 if (ip_hdr(skb)->version == 4)
914 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
915 else if (ip_hdr(skb)->version == 6)
916 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
917
918 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
919 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
920 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
921 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
922 }
923
Thomas Falconad7775d2016-04-01 17:20:34 -0500924 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600925 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500926 hdrs += 2;
927 }
928 /* determine if l2/3/4 headers are sent to firmware */
929 if ((*hdrs >> 7) & 1 &&
930 (skb->protocol == htons(ETH_P_IP) ||
931 skb->protocol == htons(ETH_P_IPV6))) {
932 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
933 tx_crq.v1.n_crq_elem = num_entries;
934 tx_buff->indir_arr[0] = tx_crq;
935 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
936 sizeof(tx_buff->indir_arr),
937 DMA_TO_DEVICE);
938 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
939 if (!firmware_has_feature(FW_FEATURE_CMO))
940 dev_err(dev, "tx: unable to map descriptor array\n");
941 tx_map_failed++;
942 tx_dropped++;
943 ret = NETDEV_TX_BUSY;
944 goto out;
945 }
John Allen498cd8e2016-04-06 11:49:55 -0500946 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500947 (u64)tx_buff->indir_dma,
948 (u64)num_entries);
949 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500950 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
951 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500952 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600953 if (lpar_rc != H_SUCCESS) {
954 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
955
956 if (tx_pool->consumer_index == 0)
957 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600958 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600959 else
960 tx_pool->consumer_index--;
961
962 tx_send_failed++;
963 tx_dropped++;
964 ret = NETDEV_TX_BUSY;
965 goto out;
966 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600967
Brian King58c8c0c2017-04-19 13:44:47 -0400968 if (atomic_inc_return(&tx_scrq->used)
969 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600970 netdev_info(netdev, "Stopping queue %d\n", queue_num);
971 netif_stop_subqueue(netdev, queue_num);
972 }
973
Thomas Falcon032c5e82015-12-21 11:26:06 -0600974 tx_packets++;
975 tx_bytes += skb->len;
976 txq->trans_start = jiffies;
977 ret = NETDEV_TX_OK;
978
979out:
980 netdev->stats.tx_dropped += tx_dropped;
981 netdev->stats.tx_bytes += tx_bytes;
982 netdev->stats.tx_packets += tx_packets;
983 adapter->tx_send_failed += tx_send_failed;
984 adapter->tx_map_failed += tx_map_failed;
985
986 return ret;
987}
988
989static void ibmvnic_set_multi(struct net_device *netdev)
990{
991 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
992 struct netdev_hw_addr *ha;
993 union ibmvnic_crq crq;
994
995 memset(&crq, 0, sizeof(crq));
996 crq.request_capability.first = IBMVNIC_CRQ_CMD;
997 crq.request_capability.cmd = REQUEST_CAPABILITY;
998
999 if (netdev->flags & IFF_PROMISC) {
1000 if (!adapter->promisc_supported)
1001 return;
1002 } else {
1003 if (netdev->flags & IFF_ALLMULTI) {
1004 /* Accept all multicast */
1005 memset(&crq, 0, sizeof(crq));
1006 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1007 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1008 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1009 ibmvnic_send_crq(adapter, &crq);
1010 } else if (netdev_mc_empty(netdev)) {
1011 /* Reject all multicast */
1012 memset(&crq, 0, sizeof(crq));
1013 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1014 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1015 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1016 ibmvnic_send_crq(adapter, &crq);
1017 } else {
1018 /* Accept one or more multicast(s) */
1019 netdev_for_each_mc_addr(ha, netdev) {
1020 memset(&crq, 0, sizeof(crq));
1021 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1022 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1023 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1024 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1025 ha->addr);
1026 ibmvnic_send_crq(adapter, &crq);
1027 }
1028 }
1029 }
1030}
1031
1032static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1033{
1034 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1035 struct sockaddr *addr = p;
1036 union ibmvnic_crq crq;
1037
1038 if (!is_valid_ether_addr(addr->sa_data))
1039 return -EADDRNOTAVAIL;
1040
1041 memset(&crq, 0, sizeof(crq));
1042 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1043 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1044 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1045 ibmvnic_send_crq(adapter, &crq);
1046 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1047 return 0;
1048}
1049
Thomas Falcon032c5e82015-12-21 11:26:06 -06001050static void ibmvnic_tx_timeout(struct net_device *dev)
1051{
1052 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1053 int rc;
1054
1055 /* Adapter timed out, resetting it */
1056 release_sub_crqs(adapter);
1057 rc = ibmvnic_reset_crq(adapter);
1058 if (rc)
1059 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1060 else
1061 ibmvnic_send_crq_init(adapter);
1062}
1063
1064static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1065 struct ibmvnic_rx_buff *rx_buff)
1066{
1067 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1068
1069 rx_buff->skb = NULL;
1070
1071 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1072 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1073
1074 atomic_dec(&pool->available);
1075}
1076
1077static int ibmvnic_poll(struct napi_struct *napi, int budget)
1078{
1079 struct net_device *netdev = napi->dev;
1080 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1081 int scrq_num = (int)(napi - adapter->napi);
1082 int frames_processed = 0;
1083restart_poll:
1084 while (frames_processed < budget) {
1085 struct sk_buff *skb;
1086 struct ibmvnic_rx_buff *rx_buff;
1087 union sub_crq *next;
1088 u32 length;
1089 u16 offset;
1090 u8 flags = 0;
1091
1092 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1093 break;
1094 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1095 rx_buff =
1096 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1097 rx_comp.correlator);
1098 /* do error checking */
1099 if (next->rx_comp.rc) {
1100 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1101 /* free the entry */
1102 next->rx_comp.first = 0;
1103 remove_buff_from_pool(adapter, rx_buff);
1104 break;
1105 }
1106
1107 length = be32_to_cpu(next->rx_comp.len);
1108 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1109 flags = next->rx_comp.flags;
1110 skb = rx_buff->skb;
1111 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1112 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001113
1114 /* VLAN Header has been stripped by the system firmware and
1115 * needs to be inserted by the driver
1116 */
1117 if (adapter->rx_vlan_header_insertion &&
1118 (flags & IBMVNIC_VLAN_STRIPPED))
1119 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1120 ntohs(next->rx_comp.vlan_tci));
1121
Thomas Falcon032c5e82015-12-21 11:26:06 -06001122 /* free the entry */
1123 next->rx_comp.first = 0;
1124 remove_buff_from_pool(adapter, rx_buff);
1125
1126 skb_put(skb, length);
1127 skb->protocol = eth_type_trans(skb, netdev);
1128
1129 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1130 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1131 skb->ip_summed = CHECKSUM_UNNECESSARY;
1132 }
1133
1134 length = skb->len;
1135 napi_gro_receive(napi, skb); /* send it up */
1136 netdev->stats.rx_packets++;
1137 netdev->stats.rx_bytes += length;
1138 frames_processed++;
1139 }
John Allen498cd8e2016-04-06 11:49:55 -05001140 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001141
1142 if (frames_processed < budget) {
1143 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001144 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001145 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1146 napi_reschedule(napi)) {
1147 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1148 goto restart_poll;
1149 }
1150 }
1151 return frames_processed;
1152}
1153
1154#ifdef CONFIG_NET_POLL_CONTROLLER
1155static void ibmvnic_netpoll_controller(struct net_device *dev)
1156{
1157 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1158 int i;
1159
1160 replenish_pools(netdev_priv(dev));
1161 for (i = 0; i < adapter->req_rx_queues; i++)
1162 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1163 adapter->rx_scrq[i]);
1164}
1165#endif
1166
1167static const struct net_device_ops ibmvnic_netdev_ops = {
1168 .ndo_open = ibmvnic_open,
1169 .ndo_stop = ibmvnic_close,
1170 .ndo_start_xmit = ibmvnic_xmit,
1171 .ndo_set_rx_mode = ibmvnic_set_multi,
1172 .ndo_set_mac_address = ibmvnic_set_mac,
1173 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001174 .ndo_tx_timeout = ibmvnic_tx_timeout,
1175#ifdef CONFIG_NET_POLL_CONTROLLER
1176 .ndo_poll_controller = ibmvnic_netpoll_controller,
1177#endif
1178};
1179
1180/* ethtool functions */
1181
Philippe Reynes8a433792017-01-07 22:37:29 +01001182static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1183 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001184{
Philippe Reynes8a433792017-01-07 22:37:29 +01001185 u32 supported, advertising;
1186
1187 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001188 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001189 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001190 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001191 cmd->base.speed = SPEED_1000;
1192 cmd->base.duplex = DUPLEX_FULL;
1193 cmd->base.port = PORT_FIBRE;
1194 cmd->base.phy_address = 0;
1195 cmd->base.autoneg = AUTONEG_ENABLE;
1196
1197 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1198 supported);
1199 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1200 advertising);
1201
Thomas Falcon032c5e82015-12-21 11:26:06 -06001202 return 0;
1203}
1204
1205static void ibmvnic_get_drvinfo(struct net_device *dev,
1206 struct ethtool_drvinfo *info)
1207{
1208 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1209 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1210}
1211
1212static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1213{
1214 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1215
1216 return adapter->msg_enable;
1217}
1218
1219static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1220{
1221 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1222
1223 adapter->msg_enable = data;
1224}
1225
1226static u32 ibmvnic_get_link(struct net_device *netdev)
1227{
1228 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1229
1230 /* Don't need to send a query because we request a logical link up at
1231 * init and then we wait for link state indications
1232 */
1233 return adapter->logical_link_state;
1234}
1235
1236static void ibmvnic_get_ringparam(struct net_device *netdev,
1237 struct ethtool_ringparam *ring)
1238{
1239 ring->rx_max_pending = 0;
1240 ring->tx_max_pending = 0;
1241 ring->rx_mini_max_pending = 0;
1242 ring->rx_jumbo_max_pending = 0;
1243 ring->rx_pending = 0;
1244 ring->tx_pending = 0;
1245 ring->rx_mini_pending = 0;
1246 ring->rx_jumbo_pending = 0;
1247}
1248
1249static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1250{
1251 int i;
1252
1253 if (stringset != ETH_SS_STATS)
1254 return;
1255
1256 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1257 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1258}
1259
1260static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1261{
1262 switch (sset) {
1263 case ETH_SS_STATS:
1264 return ARRAY_SIZE(ibmvnic_stats);
1265 default:
1266 return -EOPNOTSUPP;
1267 }
1268}
1269
1270static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1271 struct ethtool_stats *stats, u64 *data)
1272{
1273 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1274 union ibmvnic_crq crq;
1275 int i;
1276
1277 memset(&crq, 0, sizeof(crq));
1278 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1279 crq.request_statistics.cmd = REQUEST_STATISTICS;
1280 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1281 crq.request_statistics.len =
1282 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001283
1284 /* Wait for data to be written */
1285 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001286 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001287 wait_for_completion(&adapter->stats_done);
1288
1289 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1290 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1291}
1292
1293static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001294 .get_drvinfo = ibmvnic_get_drvinfo,
1295 .get_msglevel = ibmvnic_get_msglevel,
1296 .set_msglevel = ibmvnic_set_msglevel,
1297 .get_link = ibmvnic_get_link,
1298 .get_ringparam = ibmvnic_get_ringparam,
1299 .get_strings = ibmvnic_get_strings,
1300 .get_sset_count = ibmvnic_get_sset_count,
1301 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001302 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001303};
1304
1305/* Routines for managing CRQs/sCRQs */
1306
1307static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1308 struct ibmvnic_sub_crq_queue *scrq)
1309{
1310 struct device *dev = &adapter->vdev->dev;
1311 long rc;
1312
1313 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1314
1315 /* Close the sub-crqs */
1316 do {
1317 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1318 adapter->vdev->unit_address,
1319 scrq->crq_num);
1320 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1321
Thomas Falconffa73852017-04-19 13:44:29 -04001322 if (rc) {
1323 netdev_err(adapter->netdev,
1324 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1325 scrq->crq_num, rc);
1326 }
1327
Thomas Falcon032c5e82015-12-21 11:26:06 -06001328 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1329 DMA_BIDIRECTIONAL);
1330 free_pages((unsigned long)scrq->msgs, 2);
1331 kfree(scrq);
1332}
1333
1334static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1335 *adapter)
1336{
1337 struct device *dev = &adapter->vdev->dev;
1338 struct ibmvnic_sub_crq_queue *scrq;
1339 int rc;
1340
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001341 scrq = kzalloc(sizeof(*scrq), GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001342 if (!scrq)
1343 return NULL;
1344
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001345 scrq->msgs =
1346 (union sub_crq *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001347 if (!scrq->msgs) {
1348 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1349 goto zero_page_failed;
1350 }
1351
1352 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1353 DMA_BIDIRECTIONAL);
1354 if (dma_mapping_error(dev, scrq->msg_token)) {
1355 dev_warn(dev, "Couldn't map crq queue messages page\n");
1356 goto map_failed;
1357 }
1358
1359 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1360 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1361
1362 if (rc == H_RESOURCE)
1363 rc = ibmvnic_reset_crq(adapter);
1364
1365 if (rc == H_CLOSED) {
1366 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1367 } else if (rc) {
1368 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1369 goto reg_failed;
1370 }
1371
Thomas Falcon032c5e82015-12-21 11:26:06 -06001372 scrq->adapter = adapter;
1373 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001374 spin_lock_init(&scrq->lock);
1375
1376 netdev_dbg(adapter->netdev,
1377 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1378 scrq->crq_num, scrq->hw_irq, scrq->irq);
1379
1380 return scrq;
1381
Thomas Falcon032c5e82015-12-21 11:26:06 -06001382reg_failed:
1383 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1384 DMA_BIDIRECTIONAL);
1385map_failed:
1386 free_pages((unsigned long)scrq->msgs, 2);
1387zero_page_failed:
1388 kfree(scrq);
1389
1390 return NULL;
1391}
1392
1393static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1394{
1395 int i;
1396
1397 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001398 for (i = 0; i < adapter->req_tx_queues; i++) {
1399 if (!adapter->tx_scrq[i])
1400 continue;
1401
1402 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001403 free_irq(adapter->tx_scrq[i]->irq,
1404 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001405 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001406 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001407 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001408
1409 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1410 }
1411
Nathan Fontenot9501df32017-03-15 23:38:07 -04001412 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001413 adapter->tx_scrq = NULL;
1414 }
1415
1416 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001417 for (i = 0; i < adapter->req_rx_queues; i++) {
1418 if (!adapter->rx_scrq[i])
1419 continue;
1420
1421 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001422 free_irq(adapter->rx_scrq[i]->irq,
1423 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001424 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001425 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001426 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001427
1428 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1429 }
1430
Nathan Fontenot9501df32017-03-15 23:38:07 -04001431 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001432 adapter->rx_scrq = NULL;
1433 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001434}
1435
1436static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1437 struct ibmvnic_sub_crq_queue *scrq)
1438{
1439 struct device *dev = &adapter->vdev->dev;
1440 unsigned long rc;
1441
1442 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1443 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1444 if (rc)
1445 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1446 scrq->hw_irq, rc);
1447 return rc;
1448}
1449
1450static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1451 struct ibmvnic_sub_crq_queue *scrq)
1452{
1453 struct device *dev = &adapter->vdev->dev;
1454 unsigned long rc;
1455
1456 if (scrq->hw_irq > 0x100000000ULL) {
1457 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1458 return 1;
1459 }
1460
1461 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1462 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1463 if (rc)
1464 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1465 scrq->hw_irq, rc);
1466 return rc;
1467}
1468
1469static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1470 struct ibmvnic_sub_crq_queue *scrq)
1471{
1472 struct device *dev = &adapter->vdev->dev;
1473 struct ibmvnic_tx_buff *txbuff;
1474 union sub_crq *next;
1475 int index;
1476 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001477 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001478
1479restart_loop:
1480 while (pending_scrq(adapter, scrq)) {
1481 unsigned int pool = scrq->pool_index;
1482
1483 next = ibmvnic_next_scrq(adapter, scrq);
1484 for (i = 0; i < next->tx_comp.num_comps; i++) {
1485 if (next->tx_comp.rcs[i]) {
1486 dev_err(dev, "tx error %x\n",
1487 next->tx_comp.rcs[i]);
1488 continue;
1489 }
1490 index = be32_to_cpu(next->tx_comp.correlators[i]);
1491 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1492
1493 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1494 if (!txbuff->data_dma[j])
1495 continue;
1496
1497 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001498 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001499 /* if sub_crq was sent indirectly */
1500 first = txbuff->indir_arr[0].generic.first;
1501 if (first == IBMVNIC_CRQ_CMD) {
1502 dma_unmap_single(dev, txbuff->indir_dma,
1503 sizeof(txbuff->indir_arr),
1504 DMA_TO_DEVICE);
1505 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001506
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001507 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001508 if (atomic_sub_return(next->tx_comp.num_comps,
1509 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001510 (adapter->req_tx_entries_per_subcrq / 2) &&
1511 netif_subqueue_stopped(adapter->netdev,
1512 txbuff->skb)) {
1513 netif_wake_subqueue(adapter->netdev,
1514 scrq->pool_index);
1515 netdev_dbg(adapter->netdev,
1516 "Started queue %d\n",
1517 scrq->pool_index);
1518 }
1519
Thomas Falcon032c5e82015-12-21 11:26:06 -06001520 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001521 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001522
1523 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1524 producer_index] = index;
1525 adapter->tx_pool[pool].producer_index =
1526 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001527 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001528 }
1529 /* remove tx_comp scrq*/
1530 next->tx_comp.first = 0;
1531 }
1532
1533 enable_scrq_irq(adapter, scrq);
1534
1535 if (pending_scrq(adapter, scrq)) {
1536 disable_scrq_irq(adapter, scrq);
1537 goto restart_loop;
1538 }
1539
1540 return 0;
1541}
1542
1543static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1544{
1545 struct ibmvnic_sub_crq_queue *scrq = instance;
1546 struct ibmvnic_adapter *adapter = scrq->adapter;
1547
1548 disable_scrq_irq(adapter, scrq);
1549 ibmvnic_complete_tx(adapter, scrq);
1550
1551 return IRQ_HANDLED;
1552}
1553
1554static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1555{
1556 struct ibmvnic_sub_crq_queue *scrq = instance;
1557 struct ibmvnic_adapter *adapter = scrq->adapter;
1558
1559 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1560 disable_scrq_irq(adapter, scrq);
1561 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1562 }
1563
1564 return IRQ_HANDLED;
1565}
1566
Thomas Falconea22d512016-07-06 15:35:17 -05001567static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1568{
1569 struct device *dev = &adapter->vdev->dev;
1570 struct ibmvnic_sub_crq_queue *scrq;
1571 int i = 0, j = 0;
1572 int rc = 0;
1573
1574 for (i = 0; i < adapter->req_tx_queues; i++) {
1575 scrq = adapter->tx_scrq[i];
1576 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1577
Michael Ellerman99c17902016-09-10 19:59:05 +10001578 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001579 rc = -EINVAL;
1580 dev_err(dev, "Error mapping irq\n");
1581 goto req_tx_irq_failed;
1582 }
1583
1584 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1585 0, "ibmvnic_tx", scrq);
1586
1587 if (rc) {
1588 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1589 scrq->irq, rc);
1590 irq_dispose_mapping(scrq->irq);
1591 goto req_rx_irq_failed;
1592 }
1593 }
1594
1595 for (i = 0; i < adapter->req_rx_queues; i++) {
1596 scrq = adapter->rx_scrq[i];
1597 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001598 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001599 rc = -EINVAL;
1600 dev_err(dev, "Error mapping irq\n");
1601 goto req_rx_irq_failed;
1602 }
1603 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1604 0, "ibmvnic_rx", scrq);
1605 if (rc) {
1606 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1607 scrq->irq, rc);
1608 irq_dispose_mapping(scrq->irq);
1609 goto req_rx_irq_failed;
1610 }
1611 }
1612 return rc;
1613
1614req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001615 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001616 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1617 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001618 }
Thomas Falconea22d512016-07-06 15:35:17 -05001619 i = adapter->req_tx_queues;
1620req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001621 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001622 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1623 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001624 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001625 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001626 return rc;
1627}
1628
Thomas Falcon032c5e82015-12-21 11:26:06 -06001629static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1630{
1631 struct device *dev = &adapter->vdev->dev;
1632 struct ibmvnic_sub_crq_queue **allqueues;
1633 int registered_queues = 0;
1634 union ibmvnic_crq crq;
1635 int total_queues;
1636 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001637 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001638
1639 if (!retry) {
1640 /* Sub-CRQ entries are 32 byte long */
1641 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1642
1643 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1644 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1645 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1646 goto allqueues_failed;
1647 }
1648
1649 /* Get the minimum between the queried max and the entries
1650 * that fit in our PAGE_SIZE
1651 */
1652 adapter->req_tx_entries_per_subcrq =
1653 adapter->max_tx_entries_per_subcrq > entries_page ?
1654 entries_page : adapter->max_tx_entries_per_subcrq;
1655 adapter->req_rx_add_entries_per_subcrq =
1656 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1657 entries_page : adapter->max_rx_add_entries_per_subcrq;
1658
John Allen6dbcd8f2016-11-07 14:27:28 -06001659 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1660 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001661 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001662
Thomas Falconf39f0d12017-02-14 10:22:59 -06001663 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001664 }
1665
1666 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1667
1668 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1669 if (!allqueues)
1670 goto allqueues_failed;
1671
1672 for (i = 0; i < total_queues; i++) {
1673 allqueues[i] = init_sub_crq_queue(adapter);
1674 if (!allqueues[i]) {
1675 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1676 break;
1677 }
1678 registered_queues++;
1679 }
1680
1681 /* Make sure we were able to register the minimum number of queues */
1682 if (registered_queues <
1683 adapter->min_tx_queues + adapter->min_rx_queues) {
1684 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1685 goto tx_failed;
1686 }
1687
1688 /* Distribute the failed allocated queues*/
1689 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1690 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1691 switch (i % 3) {
1692 case 0:
1693 if (adapter->req_rx_queues > adapter->min_rx_queues)
1694 adapter->req_rx_queues--;
1695 else
1696 more++;
1697 break;
1698 case 1:
1699 if (adapter->req_tx_queues > adapter->min_tx_queues)
1700 adapter->req_tx_queues--;
1701 else
1702 more++;
1703 break;
1704 }
1705 }
1706
1707 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1708 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1709 if (!adapter->tx_scrq)
1710 goto tx_failed;
1711
1712 for (i = 0; i < adapter->req_tx_queues; i++) {
1713 adapter->tx_scrq[i] = allqueues[i];
1714 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001715 }
1716
1717 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1718 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1719 if (!adapter->rx_scrq)
1720 goto rx_failed;
1721
1722 for (i = 0; i < adapter->req_rx_queues; i++) {
1723 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1724 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001725 }
1726
1727 memset(&crq, 0, sizeof(crq));
1728 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1729 crq.request_capability.cmd = REQUEST_CAPABILITY;
1730
1731 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001732 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001733 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001734 ibmvnic_send_crq(adapter, &crq);
1735
1736 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001737 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001738 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001739 ibmvnic_send_crq(adapter, &crq);
1740
1741 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001742 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001743 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001744 ibmvnic_send_crq(adapter, &crq);
1745
1746 crq.request_capability.capability =
1747 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1748 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001749 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001750 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001751 ibmvnic_send_crq(adapter, &crq);
1752
1753 crq.request_capability.capability =
1754 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1755 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001756 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001757 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001758 ibmvnic_send_crq(adapter, &crq);
1759
1760 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001761 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001762 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001763 ibmvnic_send_crq(adapter, &crq);
1764
1765 if (adapter->netdev->flags & IFF_PROMISC) {
1766 if (adapter->promisc_supported) {
1767 crq.request_capability.capability =
1768 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001769 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001770 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001771 ibmvnic_send_crq(adapter, &crq);
1772 }
1773 } else {
1774 crq.request_capability.capability =
1775 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001776 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001777 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001778 ibmvnic_send_crq(adapter, &crq);
1779 }
1780
1781 kfree(allqueues);
1782
1783 return;
1784
Thomas Falcon032c5e82015-12-21 11:26:06 -06001785rx_failed:
1786 kfree(adapter->tx_scrq);
1787 adapter->tx_scrq = NULL;
1788tx_failed:
1789 for (i = 0; i < registered_queues; i++)
1790 release_sub_crq_queue(adapter, allqueues[i]);
1791 kfree(allqueues);
1792allqueues_failed:
1793 ibmvnic_remove(adapter->vdev);
1794}
1795
1796static int pending_scrq(struct ibmvnic_adapter *adapter,
1797 struct ibmvnic_sub_crq_queue *scrq)
1798{
1799 union sub_crq *entry = &scrq->msgs[scrq->cur];
1800
1801 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1802 return 1;
1803 else
1804 return 0;
1805}
1806
1807static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1808 struct ibmvnic_sub_crq_queue *scrq)
1809{
1810 union sub_crq *entry;
1811 unsigned long flags;
1812
1813 spin_lock_irqsave(&scrq->lock, flags);
1814 entry = &scrq->msgs[scrq->cur];
1815 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1816 if (++scrq->cur == scrq->size)
1817 scrq->cur = 0;
1818 } else {
1819 entry = NULL;
1820 }
1821 spin_unlock_irqrestore(&scrq->lock, flags);
1822
1823 return entry;
1824}
1825
1826static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1827{
1828 struct ibmvnic_crq_queue *queue = &adapter->crq;
1829 union ibmvnic_crq *crq;
1830
1831 crq = &queue->msgs[queue->cur];
1832 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1833 if (++queue->cur == queue->size)
1834 queue->cur = 0;
1835 } else {
1836 crq = NULL;
1837 }
1838
1839 return crq;
1840}
1841
1842static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1843 union sub_crq *sub_crq)
1844{
1845 unsigned int ua = adapter->vdev->unit_address;
1846 struct device *dev = &adapter->vdev->dev;
1847 u64 *u64_crq = (u64 *)sub_crq;
1848 int rc;
1849
1850 netdev_dbg(adapter->netdev,
1851 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1852 (unsigned long int)cpu_to_be64(remote_handle),
1853 (unsigned long int)cpu_to_be64(u64_crq[0]),
1854 (unsigned long int)cpu_to_be64(u64_crq[1]),
1855 (unsigned long int)cpu_to_be64(u64_crq[2]),
1856 (unsigned long int)cpu_to_be64(u64_crq[3]));
1857
1858 /* Make sure the hypervisor sees the complete request */
1859 mb();
1860
1861 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1862 cpu_to_be64(remote_handle),
1863 cpu_to_be64(u64_crq[0]),
1864 cpu_to_be64(u64_crq[1]),
1865 cpu_to_be64(u64_crq[2]),
1866 cpu_to_be64(u64_crq[3]));
1867
1868 if (rc) {
1869 if (rc == H_CLOSED)
1870 dev_warn(dev, "CRQ Queue closed\n");
1871 dev_err(dev, "Send error (rc=%d)\n", rc);
1872 }
1873
1874 return rc;
1875}
1876
Thomas Falconad7775d2016-04-01 17:20:34 -05001877static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1878 u64 remote_handle, u64 ioba, u64 num_entries)
1879{
1880 unsigned int ua = adapter->vdev->unit_address;
1881 struct device *dev = &adapter->vdev->dev;
1882 int rc;
1883
1884 /* Make sure the hypervisor sees the complete request */
1885 mb();
1886 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1887 cpu_to_be64(remote_handle),
1888 ioba, num_entries);
1889
1890 if (rc) {
1891 if (rc == H_CLOSED)
1892 dev_warn(dev, "CRQ Queue closed\n");
1893 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1894 }
1895
1896 return rc;
1897}
1898
Thomas Falcon032c5e82015-12-21 11:26:06 -06001899static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1900 union ibmvnic_crq *crq)
1901{
1902 unsigned int ua = adapter->vdev->unit_address;
1903 struct device *dev = &adapter->vdev->dev;
1904 u64 *u64_crq = (u64 *)crq;
1905 int rc;
1906
1907 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1908 (unsigned long int)cpu_to_be64(u64_crq[0]),
1909 (unsigned long int)cpu_to_be64(u64_crq[1]));
1910
1911 /* Make sure the hypervisor sees the complete request */
1912 mb();
1913
1914 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1915 cpu_to_be64(u64_crq[0]),
1916 cpu_to_be64(u64_crq[1]));
1917
1918 if (rc) {
1919 if (rc == H_CLOSED)
1920 dev_warn(dev, "CRQ Queue closed\n");
1921 dev_warn(dev, "Send error (rc=%d)\n", rc);
1922 }
1923
1924 return rc;
1925}
1926
1927static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1928{
1929 union ibmvnic_crq crq;
1930
1931 memset(&crq, 0, sizeof(crq));
1932 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1933 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1934 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1935
1936 return ibmvnic_send_crq(adapter, &crq);
1937}
1938
1939static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1940{
1941 union ibmvnic_crq crq;
1942
1943 memset(&crq, 0, sizeof(crq));
1944 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1945 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1946 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1947
1948 return ibmvnic_send_crq(adapter, &crq);
1949}
1950
1951static int send_version_xchg(struct ibmvnic_adapter *adapter)
1952{
1953 union ibmvnic_crq crq;
1954
1955 memset(&crq, 0, sizeof(crq));
1956 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1957 crq.version_exchange.cmd = VERSION_EXCHANGE;
1958 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1959
1960 return ibmvnic_send_crq(adapter, &crq);
1961}
1962
1963static void send_login(struct ibmvnic_adapter *adapter)
1964{
1965 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1966 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001967 struct device *dev = &adapter->vdev->dev;
1968 dma_addr_t rsp_buffer_token;
1969 dma_addr_t buffer_token;
1970 size_t rsp_buffer_size;
1971 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001972 size_t buffer_size;
1973 __be64 *tx_list_p;
1974 __be64 *rx_list_p;
1975 int i;
1976
1977 buffer_size =
1978 sizeof(struct ibmvnic_login_buffer) +
1979 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1980
1981 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1982 if (!login_buffer)
1983 goto buf_alloc_failed;
1984
1985 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1986 DMA_TO_DEVICE);
1987 if (dma_mapping_error(dev, buffer_token)) {
1988 dev_err(dev, "Couldn't map login buffer\n");
1989 goto buf_map_failed;
1990 }
1991
John Allen498cd8e2016-04-06 11:49:55 -05001992 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1993 sizeof(u64) * adapter->req_tx_queues +
1994 sizeof(u64) * adapter->req_rx_queues +
1995 sizeof(u64) * adapter->req_rx_queues +
1996 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001997
1998 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1999 if (!login_rsp_buffer)
2000 goto buf_rsp_alloc_failed;
2001
2002 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2003 rsp_buffer_size, DMA_FROM_DEVICE);
2004 if (dma_mapping_error(dev, rsp_buffer_token)) {
2005 dev_err(dev, "Couldn't map login rsp buffer\n");
2006 goto buf_rsp_map_failed;
2007 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002008
Thomas Falcon032c5e82015-12-21 11:26:06 -06002009 adapter->login_buf = login_buffer;
2010 adapter->login_buf_token = buffer_token;
2011 adapter->login_buf_sz = buffer_size;
2012 adapter->login_rsp_buf = login_rsp_buffer;
2013 adapter->login_rsp_buf_token = rsp_buffer_token;
2014 adapter->login_rsp_buf_sz = rsp_buffer_size;
2015
2016 login_buffer->len = cpu_to_be32(buffer_size);
2017 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2018 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2019 login_buffer->off_txcomp_subcrqs =
2020 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2021 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2022 login_buffer->off_rxcomp_subcrqs =
2023 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2024 sizeof(u64) * adapter->req_tx_queues);
2025 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2026 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2027
2028 tx_list_p = (__be64 *)((char *)login_buffer +
2029 sizeof(struct ibmvnic_login_buffer));
2030 rx_list_p = (__be64 *)((char *)login_buffer +
2031 sizeof(struct ibmvnic_login_buffer) +
2032 sizeof(u64) * adapter->req_tx_queues);
2033
2034 for (i = 0; i < adapter->req_tx_queues; i++) {
2035 if (adapter->tx_scrq[i]) {
2036 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2037 crq_num);
2038 }
2039 }
2040
2041 for (i = 0; i < adapter->req_rx_queues; i++) {
2042 if (adapter->rx_scrq[i]) {
2043 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2044 crq_num);
2045 }
2046 }
2047
2048 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2049 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2050 netdev_dbg(adapter->netdev, "%016lx\n",
2051 ((unsigned long int *)(adapter->login_buf))[i]);
2052 }
2053
2054 memset(&crq, 0, sizeof(crq));
2055 crq.login.first = IBMVNIC_CRQ_CMD;
2056 crq.login.cmd = LOGIN;
2057 crq.login.ioba = cpu_to_be32(buffer_token);
2058 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002059 ibmvnic_send_crq(adapter, &crq);
2060
2061 return;
2062
Thomas Falcon032c5e82015-12-21 11:26:06 -06002063buf_rsp_map_failed:
2064 kfree(login_rsp_buffer);
2065buf_rsp_alloc_failed:
2066 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2067buf_map_failed:
2068 kfree(login_buffer);
2069buf_alloc_failed:
2070 return;
2071}
2072
2073static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2074 u32 len, u8 map_id)
2075{
2076 union ibmvnic_crq crq;
2077
2078 memset(&crq, 0, sizeof(crq));
2079 crq.request_map.first = IBMVNIC_CRQ_CMD;
2080 crq.request_map.cmd = REQUEST_MAP;
2081 crq.request_map.map_id = map_id;
2082 crq.request_map.ioba = cpu_to_be32(addr);
2083 crq.request_map.len = cpu_to_be32(len);
2084 ibmvnic_send_crq(adapter, &crq);
2085}
2086
2087static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2088{
2089 union ibmvnic_crq crq;
2090
2091 memset(&crq, 0, sizeof(crq));
2092 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2093 crq.request_unmap.cmd = REQUEST_UNMAP;
2094 crq.request_unmap.map_id = map_id;
2095 ibmvnic_send_crq(adapter, &crq);
2096}
2097
2098static void send_map_query(struct ibmvnic_adapter *adapter)
2099{
2100 union ibmvnic_crq crq;
2101
2102 memset(&crq, 0, sizeof(crq));
2103 crq.query_map.first = IBMVNIC_CRQ_CMD;
2104 crq.query_map.cmd = QUERY_MAP;
2105 ibmvnic_send_crq(adapter, &crq);
2106}
2107
2108/* Send a series of CRQs requesting various capabilities of the VNIC server */
2109static void send_cap_queries(struct ibmvnic_adapter *adapter)
2110{
2111 union ibmvnic_crq crq;
2112
Thomas Falcon901e0402017-02-15 12:17:59 -06002113 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002114 memset(&crq, 0, sizeof(crq));
2115 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2116 crq.query_capability.cmd = QUERY_CAPABILITY;
2117
2118 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002119 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002120 ibmvnic_send_crq(adapter, &crq);
2121
2122 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002123 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002124 ibmvnic_send_crq(adapter, &crq);
2125
2126 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002127 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002128 ibmvnic_send_crq(adapter, &crq);
2129
2130 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002131 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002132 ibmvnic_send_crq(adapter, &crq);
2133
2134 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002135 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002136 ibmvnic_send_crq(adapter, &crq);
2137
2138 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002139 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002140 ibmvnic_send_crq(adapter, &crq);
2141
2142 crq.query_capability.capability =
2143 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002144 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002145 ibmvnic_send_crq(adapter, &crq);
2146
2147 crq.query_capability.capability =
2148 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002149 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002150 ibmvnic_send_crq(adapter, &crq);
2151
2152 crq.query_capability.capability =
2153 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002154 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002155 ibmvnic_send_crq(adapter, &crq);
2156
2157 crq.query_capability.capability =
2158 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002159 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002160 ibmvnic_send_crq(adapter, &crq);
2161
2162 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002163 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002164 ibmvnic_send_crq(adapter, &crq);
2165
2166 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002167 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002168 ibmvnic_send_crq(adapter, &crq);
2169
2170 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002171 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002172 ibmvnic_send_crq(adapter, &crq);
2173
2174 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002175 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002176 ibmvnic_send_crq(adapter, &crq);
2177
2178 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002179 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002180 ibmvnic_send_crq(adapter, &crq);
2181
2182 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002183 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002184 ibmvnic_send_crq(adapter, &crq);
2185
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002186 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2187 atomic_inc(&adapter->running_cap_crqs);
2188 ibmvnic_send_crq(adapter, &crq);
2189
Thomas Falcon032c5e82015-12-21 11:26:06 -06002190 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002191 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002192 ibmvnic_send_crq(adapter, &crq);
2193
2194 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002195 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002196 ibmvnic_send_crq(adapter, &crq);
2197
2198 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002199 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002200 ibmvnic_send_crq(adapter, &crq);
2201
2202 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002203 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002204 ibmvnic_send_crq(adapter, &crq);
2205
2206 crq.query_capability.capability =
2207 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002208 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002209 ibmvnic_send_crq(adapter, &crq);
2210
2211 crq.query_capability.capability =
2212 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002213 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002214 ibmvnic_send_crq(adapter, &crq);
2215
2216 crq.query_capability.capability =
2217 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002218 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002219 ibmvnic_send_crq(adapter, &crq);
2220
2221 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002222 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002223 ibmvnic_send_crq(adapter, &crq);
2224}
2225
2226static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2227{
2228 struct device *dev = &adapter->vdev->dev;
2229 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2230 union ibmvnic_crq crq;
2231 int i;
2232
2233 dma_unmap_single(dev, adapter->ip_offload_tok,
2234 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2235
2236 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2237 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2238 netdev_dbg(adapter->netdev, "%016lx\n",
2239 ((unsigned long int *)(buf))[i]);
2240
2241 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2242 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2243 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2244 buf->tcp_ipv4_chksum);
2245 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2246 buf->tcp_ipv6_chksum);
2247 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2248 buf->udp_ipv4_chksum);
2249 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2250 buf->udp_ipv6_chksum);
2251 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2252 buf->large_tx_ipv4);
2253 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2254 buf->large_tx_ipv6);
2255 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2256 buf->large_rx_ipv4);
2257 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2258 buf->large_rx_ipv6);
2259 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2260 buf->max_ipv4_header_size);
2261 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2262 buf->max_ipv6_header_size);
2263 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2264 buf->max_tcp_header_size);
2265 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2266 buf->max_udp_header_size);
2267 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2268 buf->max_large_tx_size);
2269 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2270 buf->max_large_rx_size);
2271 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2272 buf->ipv6_extension_header);
2273 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2274 buf->tcp_pseudosum_req);
2275 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2276 buf->num_ipv6_ext_headers);
2277 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2278 buf->off_ipv6_ext_headers);
2279
2280 adapter->ip_offload_ctrl_tok =
2281 dma_map_single(dev, &adapter->ip_offload_ctrl,
2282 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2283
2284 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2285 dev_err(dev, "Couldn't map ip offload control buffer\n");
2286 return;
2287 }
2288
2289 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2290 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2291 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2292 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2293 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2294
2295 /* large_tx/rx disabled for now, additional features needed */
2296 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2297 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2298 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2299 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2300
2301 adapter->netdev->features = NETIF_F_GSO;
2302
2303 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2304 adapter->netdev->features |= NETIF_F_IP_CSUM;
2305
2306 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2307 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2308
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002309 if ((adapter->netdev->features &
2310 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2311 adapter->netdev->features |= NETIF_F_RXCSUM;
2312
Thomas Falcon032c5e82015-12-21 11:26:06 -06002313 memset(&crq, 0, sizeof(crq));
2314 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2315 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2316 crq.control_ip_offload.len =
2317 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2318 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2319 ibmvnic_send_crq(adapter, &crq);
2320}
2321
2322static void handle_error_info_rsp(union ibmvnic_crq *crq,
2323 struct ibmvnic_adapter *adapter)
2324{
2325 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002326 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002327 unsigned long flags;
2328 bool found = false;
2329 int i;
2330
2331 if (!crq->request_error_rsp.rc.code) {
2332 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2333 crq->request_error_rsp.rc.code);
2334 return;
2335 }
2336
2337 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002338 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002339 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2340 found = true;
2341 list_del(&error_buff->list);
2342 break;
2343 }
2344 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2345
2346 if (!found) {
2347 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002348 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002349 return;
2350 }
2351
2352 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002353 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002354
2355 for (i = 0; i < error_buff->len; i++) {
2356 pr_cont("%02x", (int)error_buff->buff[i]);
2357 if (i % 8 == 7)
2358 pr_cont(" ");
2359 }
2360 pr_cont("\n");
2361
2362 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2363 DMA_FROM_DEVICE);
2364 kfree(error_buff->buff);
2365 kfree(error_buff);
2366}
2367
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002368static void request_error_information(struct ibmvnic_adapter *adapter,
2369 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002370{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002371 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002372 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002373 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002374 unsigned long timeout = msecs_to_jiffies(30000);
2375 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002376 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002377 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002378
2379 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2380 if (!error_buff)
2381 return;
2382
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002383 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002384 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2385 if (!error_buff->buff) {
2386 kfree(error_buff);
2387 return;
2388 }
2389
2390 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2391 DMA_FROM_DEVICE);
2392 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002393 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002394 kfree(error_buff->buff);
2395 kfree(error_buff);
2396 return;
2397 }
2398
Thomas Falcon032c5e82015-12-21 11:26:06 -06002399 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002400 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002401
2402 spin_lock_irqsave(&adapter->error_list_lock, flags);
2403 list_add_tail(&error_buff->list, &adapter->errors);
2404 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2405
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002406 memset(&crq, 0, sizeof(crq));
2407 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2408 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2409 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2410 crq.request_error_info.len = cpu_to_be32(detail_len);
2411 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2412
2413 rc = ibmvnic_send_crq(adapter, &crq);
2414 if (rc) {
2415 netdev_err(netdev, "failed to request error information\n");
2416 goto err_info_fail;
2417 }
2418
2419 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2420 netdev_err(netdev, "timeout waiting for error information\n");
2421 goto err_info_fail;
2422 }
2423
2424 return;
2425
2426err_info_fail:
2427 spin_lock_irqsave(&adapter->error_list_lock, flags);
2428 list_del(&error_buff->list);
2429 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2430
2431 kfree(error_buff->buff);
2432 kfree(error_buff);
2433}
2434
2435static void handle_error_indication(union ibmvnic_crq *crq,
2436 struct ibmvnic_adapter *adapter)
2437{
2438 struct device *dev = &adapter->vdev->dev;
2439
2440 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2441 crq->error_indication.flags
2442 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2443 be32_to_cpu(crq->error_indication.error_id),
2444 be16_to_cpu(crq->error_indication.error_cause));
2445
2446 if (be32_to_cpu(crq->error_indication.error_id))
2447 request_error_information(adapter, crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002448}
2449
2450static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2451 struct ibmvnic_adapter *adapter)
2452{
2453 struct net_device *netdev = adapter->netdev;
2454 struct device *dev = &adapter->vdev->dev;
2455 long rc;
2456
2457 rc = crq->change_mac_addr_rsp.rc.code;
2458 if (rc) {
2459 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2460 return;
2461 }
2462 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2463 ETH_ALEN);
2464}
2465
2466static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2467 struct ibmvnic_adapter *adapter)
2468{
2469 struct device *dev = &adapter->vdev->dev;
2470 u64 *req_value;
2471 char *name;
2472
Thomas Falcon901e0402017-02-15 12:17:59 -06002473 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002474 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2475 case REQ_TX_QUEUES:
2476 req_value = &adapter->req_tx_queues;
2477 name = "tx";
2478 break;
2479 case REQ_RX_QUEUES:
2480 req_value = &adapter->req_rx_queues;
2481 name = "rx";
2482 break;
2483 case REQ_RX_ADD_QUEUES:
2484 req_value = &adapter->req_rx_add_queues;
2485 name = "rx_add";
2486 break;
2487 case REQ_TX_ENTRIES_PER_SUBCRQ:
2488 req_value = &adapter->req_tx_entries_per_subcrq;
2489 name = "tx_entries_per_subcrq";
2490 break;
2491 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2492 req_value = &adapter->req_rx_add_entries_per_subcrq;
2493 name = "rx_add_entries_per_subcrq";
2494 break;
2495 case REQ_MTU:
2496 req_value = &adapter->req_mtu;
2497 name = "mtu";
2498 break;
2499 case PROMISC_REQUESTED:
2500 req_value = &adapter->promisc;
2501 name = "promisc";
2502 break;
2503 default:
2504 dev_err(dev, "Got invalid cap request rsp %d\n",
2505 crq->request_capability.capability);
2506 return;
2507 }
2508
2509 switch (crq->request_capability_rsp.rc.code) {
2510 case SUCCESS:
2511 break;
2512 case PARTIALSUCCESS:
2513 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2514 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002515 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002516 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002517 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002518 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002519 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002520 return;
2521 default:
2522 dev_err(dev, "Error %d in request cap rsp\n",
2523 crq->request_capability_rsp.rc.code);
2524 return;
2525 }
2526
2527 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002528 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002529 union ibmvnic_crq newcrq;
2530 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2531 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2532 &adapter->ip_offload_buf;
2533
Thomas Falcon249168a2017-02-15 12:18:00 -06002534 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002535 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2536 buf_sz,
2537 DMA_FROM_DEVICE);
2538
2539 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2540 if (!firmware_has_feature(FW_FEATURE_CMO))
2541 dev_err(dev, "Couldn't map offload buffer\n");
2542 return;
2543 }
2544
2545 memset(&newcrq, 0, sizeof(newcrq));
2546 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2547 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2548 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2549 newcrq.query_ip_offload.ioba =
2550 cpu_to_be32(adapter->ip_offload_tok);
2551
2552 ibmvnic_send_crq(adapter, &newcrq);
2553 }
2554}
2555
2556static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2557 struct ibmvnic_adapter *adapter)
2558{
2559 struct device *dev = &adapter->vdev->dev;
2560 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2561 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002562 int i;
2563
2564 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2565 DMA_BIDIRECTIONAL);
2566 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2567 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2568
John Allen498cd8e2016-04-06 11:49:55 -05002569 /* If the number of queues requested can't be allocated by the
2570 * server, the login response will return with code 1. We will need
2571 * to resend the login buffer with fewer queues requested.
2572 */
2573 if (login_rsp_crq->generic.rc.code) {
2574 adapter->renegotiate = true;
2575 complete(&adapter->init_done);
2576 return 0;
2577 }
2578
Thomas Falcon032c5e82015-12-21 11:26:06 -06002579 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2580 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2581 netdev_dbg(adapter->netdev, "%016lx\n",
2582 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2583 }
2584
2585 /* Sanity checks */
2586 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2587 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2588 adapter->req_rx_add_queues !=
2589 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2590 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2591 ibmvnic_remove(adapter->vdev);
2592 return -EIO;
2593 }
2594 complete(&adapter->init_done);
2595
Thomas Falcon032c5e82015-12-21 11:26:06 -06002596 return 0;
2597}
2598
2599static void handle_request_map_rsp(union ibmvnic_crq *crq,
2600 struct ibmvnic_adapter *adapter)
2601{
2602 struct device *dev = &adapter->vdev->dev;
2603 u8 map_id = crq->request_map_rsp.map_id;
2604 int tx_subcrqs;
2605 int rx_subcrqs;
2606 long rc;
2607 int i;
2608
2609 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2610 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2611
2612 rc = crq->request_map_rsp.rc.code;
2613 if (rc) {
2614 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2615 adapter->map_id--;
2616 /* need to find and zero tx/rx_pool map_id */
2617 for (i = 0; i < tx_subcrqs; i++) {
2618 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2619 adapter->tx_pool[i].long_term_buff.map_id = 0;
2620 }
2621 for (i = 0; i < rx_subcrqs; i++) {
2622 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2623 adapter->rx_pool[i].long_term_buff.map_id = 0;
2624 }
2625 }
2626 complete(&adapter->fw_done);
2627}
2628
2629static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2630 struct ibmvnic_adapter *adapter)
2631{
2632 struct device *dev = &adapter->vdev->dev;
2633 long rc;
2634
2635 rc = crq->request_unmap_rsp.rc.code;
2636 if (rc)
2637 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2638}
2639
2640static void handle_query_map_rsp(union ibmvnic_crq *crq,
2641 struct ibmvnic_adapter *adapter)
2642{
2643 struct net_device *netdev = adapter->netdev;
2644 struct device *dev = &adapter->vdev->dev;
2645 long rc;
2646
2647 rc = crq->query_map_rsp.rc.code;
2648 if (rc) {
2649 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2650 return;
2651 }
2652 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2653 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2654 crq->query_map_rsp.free_pages);
2655}
2656
2657static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2658 struct ibmvnic_adapter *adapter)
2659{
2660 struct net_device *netdev = adapter->netdev;
2661 struct device *dev = &adapter->vdev->dev;
2662 long rc;
2663
Thomas Falcon901e0402017-02-15 12:17:59 -06002664 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002665 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002666 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002667 rc = crq->query_capability.rc.code;
2668 if (rc) {
2669 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2670 goto out;
2671 }
2672
2673 switch (be16_to_cpu(crq->query_capability.capability)) {
2674 case MIN_TX_QUEUES:
2675 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002676 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002677 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2678 adapter->min_tx_queues);
2679 break;
2680 case MIN_RX_QUEUES:
2681 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002682 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002683 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2684 adapter->min_rx_queues);
2685 break;
2686 case MIN_RX_ADD_QUEUES:
2687 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002688 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002689 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2690 adapter->min_rx_add_queues);
2691 break;
2692 case MAX_TX_QUEUES:
2693 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002694 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002695 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2696 adapter->max_tx_queues);
2697 break;
2698 case MAX_RX_QUEUES:
2699 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002700 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002701 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2702 adapter->max_rx_queues);
2703 break;
2704 case MAX_RX_ADD_QUEUES:
2705 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002706 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2708 adapter->max_rx_add_queues);
2709 break;
2710 case MIN_TX_ENTRIES_PER_SUBCRQ:
2711 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002712 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002713 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2714 adapter->min_tx_entries_per_subcrq);
2715 break;
2716 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2717 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002718 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2720 adapter->min_rx_add_entries_per_subcrq);
2721 break;
2722 case MAX_TX_ENTRIES_PER_SUBCRQ:
2723 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002724 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2726 adapter->max_tx_entries_per_subcrq);
2727 break;
2728 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2729 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002730 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002731 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2732 adapter->max_rx_add_entries_per_subcrq);
2733 break;
2734 case TCP_IP_OFFLOAD:
2735 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002736 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002737 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2738 adapter->tcp_ip_offload);
2739 break;
2740 case PROMISC_SUPPORTED:
2741 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002742 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002743 netdev_dbg(netdev, "promisc_supported = %lld\n",
2744 adapter->promisc_supported);
2745 break;
2746 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002747 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002748 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002749 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2750 break;
2751 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002752 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002753 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002754 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2755 break;
2756 case MAX_MULTICAST_FILTERS:
2757 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002758 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002759 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2760 adapter->max_multicast_filters);
2761 break;
2762 case VLAN_HEADER_INSERTION:
2763 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002764 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002765 if (adapter->vlan_header_insertion)
2766 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2767 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2768 adapter->vlan_header_insertion);
2769 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002770 case RX_VLAN_HEADER_INSERTION:
2771 adapter->rx_vlan_header_insertion =
2772 be64_to_cpu(crq->query_capability.number);
2773 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
2774 adapter->rx_vlan_header_insertion);
2775 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002776 case MAX_TX_SG_ENTRIES:
2777 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002778 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002779 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2780 adapter->max_tx_sg_entries);
2781 break;
2782 case RX_SG_SUPPORTED:
2783 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002784 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2786 adapter->rx_sg_supported);
2787 break;
2788 case OPT_TX_COMP_SUB_QUEUES:
2789 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002790 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002791 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2792 adapter->opt_tx_comp_sub_queues);
2793 break;
2794 case OPT_RX_COMP_QUEUES:
2795 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002796 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002797 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2798 adapter->opt_rx_comp_queues);
2799 break;
2800 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2801 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002802 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002803 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2804 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2805 break;
2806 case OPT_TX_ENTRIES_PER_SUBCRQ:
2807 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002808 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2810 adapter->opt_tx_entries_per_subcrq);
2811 break;
2812 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2813 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002814 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002815 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2816 adapter->opt_rxba_entries_per_subcrq);
2817 break;
2818 case TX_RX_DESC_REQ:
2819 adapter->tx_rx_desc_req = crq->query_capability.number;
2820 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2821 adapter->tx_rx_desc_req);
2822 break;
2823
2824 default:
2825 netdev_err(netdev, "Got invalid cap rsp %d\n",
2826 crq->query_capability.capability);
2827 }
2828
2829out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002830 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2831 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002832 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002833 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002834 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002835}
2836
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002837static void ibmvnic_xport_event(struct work_struct *work)
2838{
2839 struct ibmvnic_adapter *adapter = container_of(work,
2840 struct ibmvnic_adapter,
2841 ibmvnic_xport);
2842 struct device *dev = &adapter->vdev->dev;
2843 long rc;
2844
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002845 release_sub_crqs(adapter);
2846 if (adapter->migrated) {
2847 rc = ibmvnic_reenable_crq_queue(adapter);
2848 if (rc)
2849 dev_err(dev, "Error after enable rc=%ld\n", rc);
2850 adapter->migrated = false;
2851 rc = ibmvnic_send_crq_init(adapter);
2852 if (rc)
2853 dev_err(dev, "Error sending init rc=%ld\n", rc);
2854 }
2855}
2856
Thomas Falcon032c5e82015-12-21 11:26:06 -06002857static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2858 struct ibmvnic_adapter *adapter)
2859{
2860 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2861 struct net_device *netdev = adapter->netdev;
2862 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002863 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002864 long rc;
2865
2866 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002867 (unsigned long int)cpu_to_be64(u64_crq[0]),
2868 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869 switch (gen_crq->first) {
2870 case IBMVNIC_CRQ_INIT_RSP:
2871 switch (gen_crq->cmd) {
2872 case IBMVNIC_CRQ_INIT:
2873 dev_info(dev, "Partner initialized\n");
2874 /* Send back a response */
2875 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002876 if (!rc)
2877 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002878 else
2879 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2880 break;
2881 case IBMVNIC_CRQ_INIT_COMPLETE:
2882 dev_info(dev, "Partner initialization complete\n");
2883 send_version_xchg(adapter);
2884 break;
2885 default:
2886 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2887 }
2888 return;
2889 case IBMVNIC_CRQ_XPORT_EVENT:
2890 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2891 dev_info(dev, "Re-enabling adapter\n");
2892 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002893 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002894 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2895 dev_info(dev, "Backing device failover detected\n");
2896 netif_carrier_off(netdev);
2897 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002898 } else {
2899 /* The adapter lost the connection */
2900 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2901 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002902 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002903 }
2904 return;
2905 case IBMVNIC_CRQ_CMD_RSP:
2906 break;
2907 default:
2908 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2909 gen_crq->first);
2910 return;
2911 }
2912
2913 switch (gen_crq->cmd) {
2914 case VERSION_EXCHANGE_RSP:
2915 rc = crq->version_exchange_rsp.rc.code;
2916 if (rc) {
2917 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2918 break;
2919 }
2920 dev_info(dev, "Partner protocol version is %d\n",
2921 crq->version_exchange_rsp.version);
2922 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2923 ibmvnic_version)
2924 ibmvnic_version =
2925 be16_to_cpu(crq->version_exchange_rsp.version);
2926 send_cap_queries(adapter);
2927 break;
2928 case QUERY_CAPABILITY_RSP:
2929 handle_query_cap_rsp(crq, adapter);
2930 break;
2931 case QUERY_MAP_RSP:
2932 handle_query_map_rsp(crq, adapter);
2933 break;
2934 case REQUEST_MAP_RSP:
2935 handle_request_map_rsp(crq, adapter);
2936 break;
2937 case REQUEST_UNMAP_RSP:
2938 handle_request_unmap_rsp(crq, adapter);
2939 break;
2940 case REQUEST_CAPABILITY_RSP:
2941 handle_request_cap_rsp(crq, adapter);
2942 break;
2943 case LOGIN_RSP:
2944 netdev_dbg(netdev, "Got Login Response\n");
2945 handle_login_rsp(crq, adapter);
2946 break;
2947 case LOGICAL_LINK_STATE_RSP:
2948 netdev_dbg(netdev, "Got Logical Link State Response\n");
2949 adapter->logical_link_state =
2950 crq->logical_link_state_rsp.link_state;
2951 break;
2952 case LINK_STATE_INDICATION:
2953 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2954 adapter->phys_link_state =
2955 crq->link_state_indication.phys_link_state;
2956 adapter->logical_link_state =
2957 crq->link_state_indication.logical_link_state;
2958 break;
2959 case CHANGE_MAC_ADDR_RSP:
2960 netdev_dbg(netdev, "Got MAC address change Response\n");
2961 handle_change_mac_rsp(crq, adapter);
2962 break;
2963 case ERROR_INDICATION:
2964 netdev_dbg(netdev, "Got Error Indication\n");
2965 handle_error_indication(crq, adapter);
2966 break;
2967 case REQUEST_ERROR_RSP:
2968 netdev_dbg(netdev, "Got Error Detail Response\n");
2969 handle_error_info_rsp(crq, adapter);
2970 break;
2971 case REQUEST_STATISTICS_RSP:
2972 netdev_dbg(netdev, "Got Statistics Response\n");
2973 complete(&adapter->stats_done);
2974 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975 case QUERY_IP_OFFLOAD_RSP:
2976 netdev_dbg(netdev, "Got Query IP offload Response\n");
2977 handle_query_ip_offload_rsp(adapter);
2978 break;
2979 case MULTICAST_CTRL_RSP:
2980 netdev_dbg(netdev, "Got multicast control Response\n");
2981 break;
2982 case CONTROL_IP_OFFLOAD_RSP:
2983 netdev_dbg(netdev, "Got Control IP offload Response\n");
2984 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
2985 sizeof(adapter->ip_offload_ctrl),
2986 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05002987 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002988 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002989 case COLLECT_FW_TRACE_RSP:
2990 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
2991 complete(&adapter->fw_done);
2992 break;
2993 default:
2994 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
2995 gen_crq->cmd);
2996 }
2997}
2998
2999static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3000{
3001 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003002
Thomas Falcon6c267b32017-02-15 12:17:58 -06003003 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003004 return IRQ_HANDLED;
3005}
3006
3007static void ibmvnic_tasklet(void *data)
3008{
3009 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003010 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003011 union ibmvnic_crq *crq;
3012 unsigned long flags;
3013 bool done = false;
3014
3015 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003016 while (!done) {
3017 /* Pull all the valid messages off the CRQ */
3018 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3019 ibmvnic_handle_crq(crq, adapter);
3020 crq->generic.first = 0;
3021 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003022
3023 /* remain in tasklet until all
3024 * capabilities responses are received
3025 */
3026 if (!adapter->wait_capability)
3027 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003028 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003029 /* if capabilities CRQ's were sent in this tasklet, the following
3030 * tasklet must wait until all responses are received
3031 */
3032 if (atomic_read(&adapter->running_cap_crqs) != 0)
3033 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003034 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003035}
3036
3037static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3038{
3039 struct vio_dev *vdev = adapter->vdev;
3040 int rc;
3041
3042 do {
3043 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3044 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3045
3046 if (rc)
3047 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3048
3049 return rc;
3050}
3051
3052static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3053{
3054 struct ibmvnic_crq_queue *crq = &adapter->crq;
3055 struct device *dev = &adapter->vdev->dev;
3056 struct vio_dev *vdev = adapter->vdev;
3057 int rc;
3058
3059 /* Close the CRQ */
3060 do {
3061 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3062 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3063
3064 /* Clean out the queue */
3065 memset(crq->msgs, 0, PAGE_SIZE);
3066 crq->cur = 0;
3067
3068 /* And re-open it again */
3069 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3070 crq->msg_token, PAGE_SIZE);
3071
3072 if (rc == H_CLOSED)
3073 /* Adapter is good, but other end is not ready */
3074 dev_warn(dev, "Partner adapter not ready\n");
3075 else if (rc != 0)
3076 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3077
3078 return rc;
3079}
3080
Nathan Fontenotf9928872017-03-30 02:48:54 -04003081static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003082{
3083 struct ibmvnic_crq_queue *crq = &adapter->crq;
3084 struct vio_dev *vdev = adapter->vdev;
3085 long rc;
3086
Nathan Fontenotf9928872017-03-30 02:48:54 -04003087 if (!crq->msgs)
3088 return;
3089
Thomas Falcon032c5e82015-12-21 11:26:06 -06003090 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3091 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003092 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003093 do {
3094 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3095 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3096
3097 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3098 DMA_BIDIRECTIONAL);
3099 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003100 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003101}
3102
Nathan Fontenotf9928872017-03-30 02:48:54 -04003103static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003104{
3105 struct ibmvnic_crq_queue *crq = &adapter->crq;
3106 struct device *dev = &adapter->vdev->dev;
3107 struct vio_dev *vdev = adapter->vdev;
3108 int rc, retrc = -ENOMEM;
3109
Nathan Fontenotf9928872017-03-30 02:48:54 -04003110 if (crq->msgs)
3111 return 0;
3112
Thomas Falcon032c5e82015-12-21 11:26:06 -06003113 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3114 /* Should we allocate more than one page? */
3115
3116 if (!crq->msgs)
3117 return -ENOMEM;
3118
3119 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3120 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3121 DMA_BIDIRECTIONAL);
3122 if (dma_mapping_error(dev, crq->msg_token))
3123 goto map_failed;
3124
3125 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3126 crq->msg_token, PAGE_SIZE);
3127
3128 if (rc == H_RESOURCE)
3129 /* maybe kexecing and resource is busy. try a reset */
3130 rc = ibmvnic_reset_crq(adapter);
3131 retrc = rc;
3132
3133 if (rc == H_CLOSED) {
3134 dev_warn(dev, "Partner adapter not ready\n");
3135 } else if (rc) {
3136 dev_warn(dev, "Error %d opening adapter\n", rc);
3137 goto reg_crq_failed;
3138 }
3139
3140 retrc = 0;
3141
Thomas Falcon6c267b32017-02-15 12:17:58 -06003142 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3143 (unsigned long)adapter);
3144
Thomas Falcon032c5e82015-12-21 11:26:06 -06003145 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3146 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3147 adapter);
3148 if (rc) {
3149 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3150 vdev->irq, rc);
3151 goto req_irq_failed;
3152 }
3153
3154 rc = vio_enable_interrupts(vdev);
3155 if (rc) {
3156 dev_err(dev, "Error %d enabling interrupts\n", rc);
3157 goto req_irq_failed;
3158 }
3159
3160 crq->cur = 0;
3161 spin_lock_init(&crq->lock);
3162
3163 return retrc;
3164
3165req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003166 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003167 do {
3168 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3169 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3170reg_crq_failed:
3171 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3172map_failed:
3173 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003174 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003175 return retrc;
3176}
3177
Thomas Falcon65dc6892016-07-06 15:35:18 -05003178static void handle_crq_init_rsp(struct work_struct *work)
3179{
3180 struct ibmvnic_adapter *adapter = container_of(work,
3181 struct ibmvnic_adapter,
3182 vnic_crq_init);
3183 struct device *dev = &adapter->vdev->dev;
3184 struct net_device *netdev = adapter->netdev;
3185 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003186 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003187 int rc;
3188
Thomas Falcondfad09a2016-08-18 11:37:51 -05003189 if (adapter->failover) {
3190 release_sub_crqs(adapter);
3191 if (netif_running(netdev)) {
3192 netif_tx_disable(netdev);
3193 ibmvnic_close(netdev);
3194 restart = true;
3195 }
3196 }
3197
Thomas Falcon65dc6892016-07-06 15:35:18 -05003198 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003199 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003200 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3201 dev_err(dev, "Passive init timeout\n");
3202 goto task_failed;
3203 }
3204
Thomas Falconf39f0d12017-02-14 10:22:59 -06003205 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003206
Thomas Falcondfad09a2016-08-18 11:37:51 -05003207 if (adapter->failover) {
3208 adapter->failover = false;
3209 if (restart) {
3210 rc = ibmvnic_open(netdev);
3211 if (rc)
3212 goto restart_failed;
3213 }
3214 netif_carrier_on(netdev);
3215 return;
3216 }
3217
Thomas Falcon65dc6892016-07-06 15:35:18 -05003218 rc = register_netdev(netdev);
3219 if (rc) {
3220 dev_err(dev,
3221 "failed to register netdev rc=%d\n", rc);
3222 goto register_failed;
3223 }
3224 dev_info(dev, "ibmvnic registered\n");
3225
3226 return;
3227
Thomas Falcondfad09a2016-08-18 11:37:51 -05003228restart_failed:
3229 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003230register_failed:
3231 release_sub_crqs(adapter);
3232task_failed:
3233 dev_err(dev, "Passive initialization was not successful\n");
3234}
3235
John Allenf6ef6402017-03-17 17:13:42 -05003236static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3237{
3238 struct device *dev = &adapter->vdev->dev;
3239 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003240 int rc;
3241
Nathan Fontenotf9928872017-03-30 02:48:54 -04003242 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003243 if (rc) {
3244 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3245 return rc;
3246 }
3247
John Allenf6ef6402017-03-17 17:13:42 -05003248 init_completion(&adapter->init_done);
3249 ibmvnic_send_crq_init(adapter);
3250 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3251 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003252 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003253 return -1;
3254 }
3255
3256 return 0;
3257}
3258
Thomas Falcon032c5e82015-12-21 11:26:06 -06003259static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3260{
3261 struct ibmvnic_adapter *adapter;
3262 struct net_device *netdev;
3263 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003264 int rc;
3265
3266 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3267 dev->unit_address);
3268
3269 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3270 VETH_MAC_ADDR, NULL);
3271 if (!mac_addr_p) {
3272 dev_err(&dev->dev,
3273 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3274 __FILE__, __LINE__);
3275 return 0;
3276 }
3277
3278 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3279 IBMVNIC_MAX_TX_QUEUES);
3280 if (!netdev)
3281 return -ENOMEM;
3282
3283 adapter = netdev_priv(netdev);
3284 dev_set_drvdata(&dev->dev, netdev);
3285 adapter->vdev = dev;
3286 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003287 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003288
3289 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3290 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3291 netdev->irq = dev->irq;
3292 netdev->netdev_ops = &ibmvnic_netdev_ops;
3293 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3294 SET_NETDEV_DEV(netdev, &dev->dev);
3295
Thomas Falcon65dc6892016-07-06 15:35:18 -05003296 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003297 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003298
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299 spin_lock_init(&adapter->stats_lock);
3300
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303
John Allenf6ef6402017-03-17 17:13:42 -05003304 rc = ibmvnic_init(adapter);
3305 if (rc) {
3306 free_netdev(netdev);
3307 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003308 }
3309
Thomas Falconf39f0d12017-02-14 10:22:59 -06003310 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003311 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003312
3313 rc = register_netdev(netdev);
3314 if (rc) {
3315 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003316 free_netdev(netdev);
3317 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003318 }
3319 dev_info(&dev->dev, "ibmvnic registered\n");
3320
3321 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003322}
3323
3324static int ibmvnic_remove(struct vio_dev *dev)
3325{
3326 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003327 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328
3329 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003330
3331 release_resources(adapter);
3332 release_sub_crqs(adapter);
3333 release_crq_queue(adapter);
3334
Thomas Falcon032c5e82015-12-21 11:26:06 -06003335 free_netdev(netdev);
3336 dev_set_drvdata(&dev->dev, NULL);
3337
3338 return 0;
3339}
3340
3341static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3342{
3343 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3344 struct ibmvnic_adapter *adapter;
3345 struct iommu_table *tbl;
3346 unsigned long ret = 0;
3347 int i;
3348
3349 tbl = get_iommu_table_base(&vdev->dev);
3350
3351 /* netdev inits at probe time along with the structures we need below*/
3352 if (!netdev)
3353 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3354
3355 adapter = netdev_priv(netdev);
3356
3357 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003358 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3359
3360 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3361 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3362
3363 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3364 i++)
3365 ret += adapter->rx_pool[i].size *
3366 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3367
3368 return ret;
3369}
3370
3371static int ibmvnic_resume(struct device *dev)
3372{
3373 struct net_device *netdev = dev_get_drvdata(dev);
3374 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3375 int i;
3376
3377 /* kick the interrupt handlers just in case we lost an interrupt */
3378 for (i = 0; i < adapter->req_rx_queues; i++)
3379 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3380 adapter->rx_scrq[i]);
3381
3382 return 0;
3383}
3384
3385static struct vio_device_id ibmvnic_device_table[] = {
3386 {"network", "IBM,vnic"},
3387 {"", "" }
3388};
3389MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3390
3391static const struct dev_pm_ops ibmvnic_pm_ops = {
3392 .resume = ibmvnic_resume
3393};
3394
3395static struct vio_driver ibmvnic_driver = {
3396 .id_table = ibmvnic_device_table,
3397 .probe = ibmvnic_probe,
3398 .remove = ibmvnic_remove,
3399 .get_desired_dma = ibmvnic_get_desired_dma,
3400 .name = ibmvnic_driver_name,
3401 .pm = &ibmvnic_pm_ops,
3402};
3403
3404/* module functions */
3405static int __init ibmvnic_module_init(void)
3406{
3407 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3408 IBMVNIC_DRIVER_VERSION);
3409
3410 return vio_register_driver(&ibmvnic_driver);
3411}
3412
3413static void __exit ibmvnic_module_exit(void)
3414{
3415 vio_unregister_driver(&ibmvnic_driver);
3416}
3417
3418module_init(ibmvnic_module_init);
3419module_exit(ibmvnic_module_exit);