blob: 0f3595439293853512d6a946cc69f37e858d9889 [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
Thomas Falcon032c5e82015-12-21 11:26:06 -0600610 adapter->map_id = 1;
611 adapter->napi = kcalloc(adapter->req_rx_queues,
612 sizeof(struct napi_struct), GFP_KERNEL);
613 if (!adapter->napi)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400614 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600615 for (i = 0; i < adapter->req_rx_queues; i++) {
616 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
617 NAPI_POLL_WEIGHT);
618 napi_enable(&adapter->napi[i]);
619 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600620
Thomas Falcon032c5e82015-12-21 11:26:06 -0600621 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400622
623 rc = init_rx_pools(netdev);
624 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400625 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600626
Nathan Fontenotc657e322017-03-30 02:49:06 -0400627 rc = init_tx_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
Thomas Falcon032c5e82015-12-21 11:26:06 -0600631 replenish_pools(adapter);
632
633 /* We're ready to receive frames, enable the sub-crq interrupts and
634 * set the logical link state to up
635 */
636 for (i = 0; i < adapter->req_rx_queues; i++)
637 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
638
639 for (i = 0; i < adapter->req_tx_queues; i++)
640 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
641
642 memset(&crq, 0, sizeof(crq));
643 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
644 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
645 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
646 ibmvnic_send_crq(adapter, &crq);
647
Thomas Falconb8efb892016-07-06 15:35:15 -0500648 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500649 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500650
Thomas Falcon032c5e82015-12-21 11:26:06 -0600651 return 0;
652
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400653ibmvnic_open_fail:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600654 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500655 napi_disable(&adapter->napi[i]);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400656 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600657 return -ENOMEM;
658}
659
Brian Kingdd9c20f2017-04-19 13:45:10 -0400660static void disable_sub_crqs(struct ibmvnic_adapter *adapter)
661{
662 int i;
663
664 if (adapter->tx_scrq) {
665 for (i = 0; i < adapter->req_tx_queues; i++)
666 if (adapter->tx_scrq[i])
667 disable_irq(adapter->tx_scrq[i]->irq);
668 }
669
670 if (adapter->rx_scrq) {
671 for (i = 0; i < adapter->req_rx_queues; i++)
672 if (adapter->rx_scrq[i])
673 disable_irq(adapter->rx_scrq[i]->irq);
674 }
675}
676
John Allenea5509f2017-03-17 17:13:43 -0500677static int ibmvnic_close(struct net_device *netdev)
678{
679 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
680 union ibmvnic_crq crq;
681 int i;
682
683 adapter->closing = true;
Brian Kingdd9c20f2017-04-19 13:45:10 -0400684 disable_sub_crqs(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500685
686 for (i = 0; i < adapter->req_rx_queues; i++)
687 napi_disable(&adapter->napi[i]);
688
689 if (!adapter->failover)
690 netif_tx_stop_all_queues(netdev);
691
Thomas Falcon032c5e82015-12-21 11:26:06 -0600692 memset(&crq, 0, sizeof(crq));
693 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
694 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
695 crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
696 ibmvnic_send_crq(adapter, &crq);
697
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400698 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600699
John Allenea5509f2017-03-17 17:13:43 -0500700 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600701 adapter->closing = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600702 return 0;
703}
704
Thomas Falconad7775d2016-04-01 17:20:34 -0500705/**
706 * build_hdr_data - creates L2/L3/L4 header data buffer
707 * @hdr_field - bitfield determining needed headers
708 * @skb - socket buffer
709 * @hdr_len - array of header lengths
710 * @tot_len - total length of data
711 *
712 * Reads hdr_field to determine which headers are needed by firmware.
713 * Builds a buffer containing these headers. Saves individual header
714 * lengths and total buffer length to be used to build descriptors.
715 */
716static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
717 int *hdr_len, u8 *hdr_data)
718{
719 int len = 0;
720 u8 *hdr;
721
722 hdr_len[0] = sizeof(struct ethhdr);
723
724 if (skb->protocol == htons(ETH_P_IP)) {
725 hdr_len[1] = ip_hdr(skb)->ihl * 4;
726 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
727 hdr_len[2] = tcp_hdrlen(skb);
728 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
729 hdr_len[2] = sizeof(struct udphdr);
730 } else if (skb->protocol == htons(ETH_P_IPV6)) {
731 hdr_len[1] = sizeof(struct ipv6hdr);
732 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
733 hdr_len[2] = tcp_hdrlen(skb);
734 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
735 hdr_len[2] = sizeof(struct udphdr);
736 }
737
738 memset(hdr_data, 0, 120);
739 if ((hdr_field >> 6) & 1) {
740 hdr = skb_mac_header(skb);
741 memcpy(hdr_data, hdr, hdr_len[0]);
742 len += hdr_len[0];
743 }
744
745 if ((hdr_field >> 5) & 1) {
746 hdr = skb_network_header(skb);
747 memcpy(hdr_data + len, hdr, hdr_len[1]);
748 len += hdr_len[1];
749 }
750
751 if ((hdr_field >> 4) & 1) {
752 hdr = skb_transport_header(skb);
753 memcpy(hdr_data + len, hdr, hdr_len[2]);
754 len += hdr_len[2];
755 }
756 return len;
757}
758
759/**
760 * create_hdr_descs - create header and header extension descriptors
761 * @hdr_field - bitfield determining needed headers
762 * @data - buffer containing header data
763 * @len - length of data buffer
764 * @hdr_len - array of individual header lengths
765 * @scrq_arr - descriptor array
766 *
767 * Creates header and, if needed, header extension descriptors and
768 * places them in a descriptor array, scrq_arr
769 */
770
771static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
772 union sub_crq *scrq_arr)
773{
774 union sub_crq hdr_desc;
775 int tmp_len = len;
776 u8 *data, *cur;
777 int tmp;
778
779 while (tmp_len > 0) {
780 cur = hdr_data + len - tmp_len;
781
782 memset(&hdr_desc, 0, sizeof(hdr_desc));
783 if (cur != hdr_data) {
784 data = hdr_desc.hdr_ext.data;
785 tmp = tmp_len > 29 ? 29 : tmp_len;
786 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
787 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
788 hdr_desc.hdr_ext.len = tmp;
789 } else {
790 data = hdr_desc.hdr.data;
791 tmp = tmp_len > 24 ? 24 : tmp_len;
792 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
793 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
794 hdr_desc.hdr.len = tmp;
795 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
796 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
797 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
798 hdr_desc.hdr.flag = hdr_field << 1;
799 }
800 memcpy(data, cur, tmp);
801 tmp_len -= tmp;
802 *scrq_arr = hdr_desc;
803 scrq_arr++;
804 }
805}
806
807/**
808 * build_hdr_descs_arr - build a header descriptor array
809 * @skb - socket buffer
810 * @num_entries - number of descriptors to be sent
811 * @subcrq - first TX descriptor
812 * @hdr_field - bit field determining which headers will be sent
813 *
814 * This function will build a TX descriptor array with applicable
815 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
816 */
817
818static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
819 int *num_entries, u8 hdr_field)
820{
821 int hdr_len[3] = {0, 0, 0};
822 int tot_len, len;
823 u8 *hdr_data = txbuff->hdr_data;
824
825 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
826 txbuff->hdr_data);
827 len = tot_len;
828 len -= 24;
829 if (len > 0)
830 num_entries += len % 29 ? len / 29 + 1 : len / 29;
831 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
832 txbuff->indir_arr + 1);
833}
834
Thomas Falcon032c5e82015-12-21 11:26:06 -0600835static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
836{
837 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
838 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500839 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600840 struct device *dev = &adapter->vdev->dev;
841 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600842 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600843 struct ibmvnic_tx_pool *tx_pool;
844 unsigned int tx_send_failed = 0;
845 unsigned int tx_map_failed = 0;
846 unsigned int tx_dropped = 0;
847 unsigned int tx_packets = 0;
848 unsigned int tx_bytes = 0;
849 dma_addr_t data_dma_addr;
850 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600851 unsigned long lpar_rc;
852 union sub_crq tx_crq;
853 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500854 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600855 unsigned char *dst;
856 u64 *handle_array;
857 int index = 0;
858 int ret = 0;
859
860 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600861 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600862 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
863 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
864 be32_to_cpu(adapter->login_rsp_buf->
865 off_txsubm_subcrqs));
866 if (adapter->migrated) {
867 tx_send_failed++;
868 tx_dropped++;
869 ret = NETDEV_TX_BUSY;
870 goto out;
871 }
872
873 index = tx_pool->free_map[tx_pool->consumer_index];
874 offset = index * adapter->req_mtu;
875 dst = tx_pool->long_term_buff.buff + offset;
876 memset(dst, 0, adapter->req_mtu);
877 skb_copy_from_linear_data(skb, dst, skb->len);
878 data_dma_addr = tx_pool->long_term_buff.addr + offset;
879
880 tx_pool->consumer_index =
881 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600882 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600883
884 tx_buff = &tx_pool->tx_buff[index];
885 tx_buff->skb = skb;
886 tx_buff->data_dma[0] = data_dma_addr;
887 tx_buff->data_len[0] = skb->len;
888 tx_buff->index = index;
889 tx_buff->pool_index = queue_num;
890 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600891
892 memset(&tx_crq, 0, sizeof(tx_crq));
893 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
894 tx_crq.v1.type = IBMVNIC_TX_DESC;
895 tx_crq.v1.n_crq_elem = 1;
896 tx_crq.v1.n_sge = 1;
897 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
898 tx_crq.v1.correlator = cpu_to_be32(index);
899 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
900 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
901 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
902
903 if (adapter->vlan_header_insertion) {
904 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
905 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
906 }
907
908 if (skb->protocol == htons(ETH_P_IP)) {
909 if (ip_hdr(skb)->version == 4)
910 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
911 else if (ip_hdr(skb)->version == 6)
912 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
913
914 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
915 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
916 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
917 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
918 }
919
Thomas Falconad7775d2016-04-01 17:20:34 -0500920 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600921 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500922 hdrs += 2;
923 }
924 /* determine if l2/3/4 headers are sent to firmware */
925 if ((*hdrs >> 7) & 1 &&
926 (skb->protocol == htons(ETH_P_IP) ||
927 skb->protocol == htons(ETH_P_IPV6))) {
928 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
929 tx_crq.v1.n_crq_elem = num_entries;
930 tx_buff->indir_arr[0] = tx_crq;
931 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
932 sizeof(tx_buff->indir_arr),
933 DMA_TO_DEVICE);
934 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
935 if (!firmware_has_feature(FW_FEATURE_CMO))
936 dev_err(dev, "tx: unable to map descriptor array\n");
937 tx_map_failed++;
938 tx_dropped++;
939 ret = NETDEV_TX_BUSY;
940 goto out;
941 }
John Allen498cd8e2016-04-06 11:49:55 -0500942 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500943 (u64)tx_buff->indir_dma,
944 (u64)num_entries);
945 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500946 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
947 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500948 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600949 if (lpar_rc != H_SUCCESS) {
950 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
951
952 if (tx_pool->consumer_index == 0)
953 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600954 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600955 else
956 tx_pool->consumer_index--;
957
958 tx_send_failed++;
959 tx_dropped++;
960 ret = NETDEV_TX_BUSY;
961 goto out;
962 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600963
Brian King58c8c0c2017-04-19 13:44:47 -0400964 if (atomic_inc_return(&tx_scrq->used)
965 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600966 netdev_info(netdev, "Stopping queue %d\n", queue_num);
967 netif_stop_subqueue(netdev, queue_num);
968 }
969
Thomas Falcon032c5e82015-12-21 11:26:06 -0600970 tx_packets++;
971 tx_bytes += skb->len;
972 txq->trans_start = jiffies;
973 ret = NETDEV_TX_OK;
974
975out:
976 netdev->stats.tx_dropped += tx_dropped;
977 netdev->stats.tx_bytes += tx_bytes;
978 netdev->stats.tx_packets += tx_packets;
979 adapter->tx_send_failed += tx_send_failed;
980 adapter->tx_map_failed += tx_map_failed;
981
982 return ret;
983}
984
985static void ibmvnic_set_multi(struct net_device *netdev)
986{
987 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
988 struct netdev_hw_addr *ha;
989 union ibmvnic_crq crq;
990
991 memset(&crq, 0, sizeof(crq));
992 crq.request_capability.first = IBMVNIC_CRQ_CMD;
993 crq.request_capability.cmd = REQUEST_CAPABILITY;
994
995 if (netdev->flags & IFF_PROMISC) {
996 if (!adapter->promisc_supported)
997 return;
998 } else {
999 if (netdev->flags & IFF_ALLMULTI) {
1000 /* Accept all multicast */
1001 memset(&crq, 0, sizeof(crq));
1002 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1003 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1004 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1005 ibmvnic_send_crq(adapter, &crq);
1006 } else if (netdev_mc_empty(netdev)) {
1007 /* Reject all multicast */
1008 memset(&crq, 0, sizeof(crq));
1009 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1010 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1011 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1012 ibmvnic_send_crq(adapter, &crq);
1013 } else {
1014 /* Accept one or more multicast(s) */
1015 netdev_for_each_mc_addr(ha, netdev) {
1016 memset(&crq, 0, sizeof(crq));
1017 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1018 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1019 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1020 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1021 ha->addr);
1022 ibmvnic_send_crq(adapter, &crq);
1023 }
1024 }
1025 }
1026}
1027
1028static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1029{
1030 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1031 struct sockaddr *addr = p;
1032 union ibmvnic_crq crq;
1033
1034 if (!is_valid_ether_addr(addr->sa_data))
1035 return -EADDRNOTAVAIL;
1036
1037 memset(&crq, 0, sizeof(crq));
1038 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1039 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1040 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1041 ibmvnic_send_crq(adapter, &crq);
1042 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1043 return 0;
1044}
1045
Thomas Falcon032c5e82015-12-21 11:26:06 -06001046static void ibmvnic_tx_timeout(struct net_device *dev)
1047{
1048 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1049 int rc;
1050
1051 /* Adapter timed out, resetting it */
1052 release_sub_crqs(adapter);
1053 rc = ibmvnic_reset_crq(adapter);
1054 if (rc)
1055 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1056 else
1057 ibmvnic_send_crq_init(adapter);
1058}
1059
1060static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1061 struct ibmvnic_rx_buff *rx_buff)
1062{
1063 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1064
1065 rx_buff->skb = NULL;
1066
1067 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1068 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1069
1070 atomic_dec(&pool->available);
1071}
1072
1073static int ibmvnic_poll(struct napi_struct *napi, int budget)
1074{
1075 struct net_device *netdev = napi->dev;
1076 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1077 int scrq_num = (int)(napi - adapter->napi);
1078 int frames_processed = 0;
1079restart_poll:
1080 while (frames_processed < budget) {
1081 struct sk_buff *skb;
1082 struct ibmvnic_rx_buff *rx_buff;
1083 union sub_crq *next;
1084 u32 length;
1085 u16 offset;
1086 u8 flags = 0;
1087
1088 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1089 break;
1090 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1091 rx_buff =
1092 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1093 rx_comp.correlator);
1094 /* do error checking */
1095 if (next->rx_comp.rc) {
1096 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1097 /* free the entry */
1098 next->rx_comp.first = 0;
1099 remove_buff_from_pool(adapter, rx_buff);
1100 break;
1101 }
1102
1103 length = be32_to_cpu(next->rx_comp.len);
1104 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1105 flags = next->rx_comp.flags;
1106 skb = rx_buff->skb;
1107 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1108 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001109
1110 /* VLAN Header has been stripped by the system firmware and
1111 * needs to be inserted by the driver
1112 */
1113 if (adapter->rx_vlan_header_insertion &&
1114 (flags & IBMVNIC_VLAN_STRIPPED))
1115 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1116 ntohs(next->rx_comp.vlan_tci));
1117
Thomas Falcon032c5e82015-12-21 11:26:06 -06001118 /* free the entry */
1119 next->rx_comp.first = 0;
1120 remove_buff_from_pool(adapter, rx_buff);
1121
1122 skb_put(skb, length);
1123 skb->protocol = eth_type_trans(skb, netdev);
1124
1125 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1126 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1127 skb->ip_summed = CHECKSUM_UNNECESSARY;
1128 }
1129
1130 length = skb->len;
1131 napi_gro_receive(napi, skb); /* send it up */
1132 netdev->stats.rx_packets++;
1133 netdev->stats.rx_bytes += length;
1134 frames_processed++;
1135 }
John Allen498cd8e2016-04-06 11:49:55 -05001136 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001137
1138 if (frames_processed < budget) {
1139 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001140 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001141 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1142 napi_reschedule(napi)) {
1143 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1144 goto restart_poll;
1145 }
1146 }
1147 return frames_processed;
1148}
1149
1150#ifdef CONFIG_NET_POLL_CONTROLLER
1151static void ibmvnic_netpoll_controller(struct net_device *dev)
1152{
1153 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1154 int i;
1155
1156 replenish_pools(netdev_priv(dev));
1157 for (i = 0; i < adapter->req_rx_queues; i++)
1158 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1159 adapter->rx_scrq[i]);
1160}
1161#endif
1162
1163static const struct net_device_ops ibmvnic_netdev_ops = {
1164 .ndo_open = ibmvnic_open,
1165 .ndo_stop = ibmvnic_close,
1166 .ndo_start_xmit = ibmvnic_xmit,
1167 .ndo_set_rx_mode = ibmvnic_set_multi,
1168 .ndo_set_mac_address = ibmvnic_set_mac,
1169 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001170 .ndo_tx_timeout = ibmvnic_tx_timeout,
1171#ifdef CONFIG_NET_POLL_CONTROLLER
1172 .ndo_poll_controller = ibmvnic_netpoll_controller,
1173#endif
1174};
1175
1176/* ethtool functions */
1177
Philippe Reynes8a433792017-01-07 22:37:29 +01001178static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1179 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001180{
Philippe Reynes8a433792017-01-07 22:37:29 +01001181 u32 supported, advertising;
1182
1183 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001184 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001185 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001186 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001187 cmd->base.speed = SPEED_1000;
1188 cmd->base.duplex = DUPLEX_FULL;
1189 cmd->base.port = PORT_FIBRE;
1190 cmd->base.phy_address = 0;
1191 cmd->base.autoneg = AUTONEG_ENABLE;
1192
1193 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1194 supported);
1195 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1196 advertising);
1197
Thomas Falcon032c5e82015-12-21 11:26:06 -06001198 return 0;
1199}
1200
1201static void ibmvnic_get_drvinfo(struct net_device *dev,
1202 struct ethtool_drvinfo *info)
1203{
1204 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1205 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1206}
1207
1208static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1209{
1210 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1211
1212 return adapter->msg_enable;
1213}
1214
1215static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1216{
1217 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1218
1219 adapter->msg_enable = data;
1220}
1221
1222static u32 ibmvnic_get_link(struct net_device *netdev)
1223{
1224 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1225
1226 /* Don't need to send a query because we request a logical link up at
1227 * init and then we wait for link state indications
1228 */
1229 return adapter->logical_link_state;
1230}
1231
1232static void ibmvnic_get_ringparam(struct net_device *netdev,
1233 struct ethtool_ringparam *ring)
1234{
1235 ring->rx_max_pending = 0;
1236 ring->tx_max_pending = 0;
1237 ring->rx_mini_max_pending = 0;
1238 ring->rx_jumbo_max_pending = 0;
1239 ring->rx_pending = 0;
1240 ring->tx_pending = 0;
1241 ring->rx_mini_pending = 0;
1242 ring->rx_jumbo_pending = 0;
1243}
1244
1245static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1246{
1247 int i;
1248
1249 if (stringset != ETH_SS_STATS)
1250 return;
1251
1252 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1253 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1254}
1255
1256static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1257{
1258 switch (sset) {
1259 case ETH_SS_STATS:
1260 return ARRAY_SIZE(ibmvnic_stats);
1261 default:
1262 return -EOPNOTSUPP;
1263 }
1264}
1265
1266static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1267 struct ethtool_stats *stats, u64 *data)
1268{
1269 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1270 union ibmvnic_crq crq;
1271 int i;
1272
1273 memset(&crq, 0, sizeof(crq));
1274 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1275 crq.request_statistics.cmd = REQUEST_STATISTICS;
1276 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1277 crq.request_statistics.len =
1278 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001279
1280 /* Wait for data to be written */
1281 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001282 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001283 wait_for_completion(&adapter->stats_done);
1284
1285 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1286 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1287}
1288
1289static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001290 .get_drvinfo = ibmvnic_get_drvinfo,
1291 .get_msglevel = ibmvnic_get_msglevel,
1292 .set_msglevel = ibmvnic_set_msglevel,
1293 .get_link = ibmvnic_get_link,
1294 .get_ringparam = ibmvnic_get_ringparam,
1295 .get_strings = ibmvnic_get_strings,
1296 .get_sset_count = ibmvnic_get_sset_count,
1297 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001298 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001299};
1300
1301/* Routines for managing CRQs/sCRQs */
1302
1303static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1304 struct ibmvnic_sub_crq_queue *scrq)
1305{
1306 struct device *dev = &adapter->vdev->dev;
1307 long rc;
1308
1309 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1310
1311 /* Close the sub-crqs */
1312 do {
1313 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1314 adapter->vdev->unit_address,
1315 scrq->crq_num);
1316 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1317
Thomas Falconffa73852017-04-19 13:44:29 -04001318 if (rc) {
1319 netdev_err(adapter->netdev,
1320 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1321 scrq->crq_num, rc);
1322 }
1323
Thomas Falcon032c5e82015-12-21 11:26:06 -06001324 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1325 DMA_BIDIRECTIONAL);
1326 free_pages((unsigned long)scrq->msgs, 2);
1327 kfree(scrq);
1328}
1329
1330static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1331 *adapter)
1332{
1333 struct device *dev = &adapter->vdev->dev;
1334 struct ibmvnic_sub_crq_queue *scrq;
1335 int rc;
1336
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001337 scrq = kzalloc(sizeof(*scrq), GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001338 if (!scrq)
1339 return NULL;
1340
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001341 scrq->msgs =
1342 (union sub_crq *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001343 if (!scrq->msgs) {
1344 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1345 goto zero_page_failed;
1346 }
1347
1348 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1349 DMA_BIDIRECTIONAL);
1350 if (dma_mapping_error(dev, scrq->msg_token)) {
1351 dev_warn(dev, "Couldn't map crq queue messages page\n");
1352 goto map_failed;
1353 }
1354
1355 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1356 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1357
1358 if (rc == H_RESOURCE)
1359 rc = ibmvnic_reset_crq(adapter);
1360
1361 if (rc == H_CLOSED) {
1362 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1363 } else if (rc) {
1364 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1365 goto reg_failed;
1366 }
1367
Thomas Falcon032c5e82015-12-21 11:26:06 -06001368 scrq->adapter = adapter;
1369 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001370 spin_lock_init(&scrq->lock);
1371
1372 netdev_dbg(adapter->netdev,
1373 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1374 scrq->crq_num, scrq->hw_irq, scrq->irq);
1375
1376 return scrq;
1377
Thomas Falcon032c5e82015-12-21 11:26:06 -06001378reg_failed:
1379 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1380 DMA_BIDIRECTIONAL);
1381map_failed:
1382 free_pages((unsigned long)scrq->msgs, 2);
1383zero_page_failed:
1384 kfree(scrq);
1385
1386 return NULL;
1387}
1388
1389static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1390{
1391 int i;
1392
1393 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001394 for (i = 0; i < adapter->req_tx_queues; i++) {
1395 if (!adapter->tx_scrq[i])
1396 continue;
1397
1398 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001399 free_irq(adapter->tx_scrq[i]->irq,
1400 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001401 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001402 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001403 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001404
1405 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1406 }
1407
Nathan Fontenot9501df32017-03-15 23:38:07 -04001408 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001409 adapter->tx_scrq = NULL;
1410 }
1411
1412 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001413 for (i = 0; i < adapter->req_rx_queues; i++) {
1414 if (!adapter->rx_scrq[i])
1415 continue;
1416
1417 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001418 free_irq(adapter->rx_scrq[i]->irq,
1419 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001420 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001421 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001422 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001423
1424 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1425 }
1426
Nathan Fontenot9501df32017-03-15 23:38:07 -04001427 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001428 adapter->rx_scrq = NULL;
1429 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001430}
1431
1432static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1433 struct ibmvnic_sub_crq_queue *scrq)
1434{
1435 struct device *dev = &adapter->vdev->dev;
1436 unsigned long rc;
1437
1438 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1439 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1440 if (rc)
1441 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1442 scrq->hw_irq, rc);
1443 return rc;
1444}
1445
1446static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1447 struct ibmvnic_sub_crq_queue *scrq)
1448{
1449 struct device *dev = &adapter->vdev->dev;
1450 unsigned long rc;
1451
1452 if (scrq->hw_irq > 0x100000000ULL) {
1453 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1454 return 1;
1455 }
1456
1457 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1458 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1459 if (rc)
1460 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1461 scrq->hw_irq, rc);
1462 return rc;
1463}
1464
1465static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1466 struct ibmvnic_sub_crq_queue *scrq)
1467{
1468 struct device *dev = &adapter->vdev->dev;
1469 struct ibmvnic_tx_buff *txbuff;
1470 union sub_crq *next;
1471 int index;
1472 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001473 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001474
1475restart_loop:
1476 while (pending_scrq(adapter, scrq)) {
1477 unsigned int pool = scrq->pool_index;
1478
1479 next = ibmvnic_next_scrq(adapter, scrq);
1480 for (i = 0; i < next->tx_comp.num_comps; i++) {
1481 if (next->tx_comp.rcs[i]) {
1482 dev_err(dev, "tx error %x\n",
1483 next->tx_comp.rcs[i]);
1484 continue;
1485 }
1486 index = be32_to_cpu(next->tx_comp.correlators[i]);
1487 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1488
1489 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1490 if (!txbuff->data_dma[j])
1491 continue;
1492
1493 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001494 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001495 /* if sub_crq was sent indirectly */
1496 first = txbuff->indir_arr[0].generic.first;
1497 if (first == IBMVNIC_CRQ_CMD) {
1498 dma_unmap_single(dev, txbuff->indir_dma,
1499 sizeof(txbuff->indir_arr),
1500 DMA_TO_DEVICE);
1501 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001502
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001503 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001504 if (atomic_sub_return(next->tx_comp.num_comps,
1505 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001506 (adapter->req_tx_entries_per_subcrq / 2) &&
1507 netif_subqueue_stopped(adapter->netdev,
1508 txbuff->skb)) {
1509 netif_wake_subqueue(adapter->netdev,
1510 scrq->pool_index);
1511 netdev_dbg(adapter->netdev,
1512 "Started queue %d\n",
1513 scrq->pool_index);
1514 }
1515
Thomas Falcon032c5e82015-12-21 11:26:06 -06001516 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001517 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001518
1519 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1520 producer_index] = index;
1521 adapter->tx_pool[pool].producer_index =
1522 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001523 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001524 }
1525 /* remove tx_comp scrq*/
1526 next->tx_comp.first = 0;
1527 }
1528
1529 enable_scrq_irq(adapter, scrq);
1530
1531 if (pending_scrq(adapter, scrq)) {
1532 disable_scrq_irq(adapter, scrq);
1533 goto restart_loop;
1534 }
1535
1536 return 0;
1537}
1538
1539static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1540{
1541 struct ibmvnic_sub_crq_queue *scrq = instance;
1542 struct ibmvnic_adapter *adapter = scrq->adapter;
1543
1544 disable_scrq_irq(adapter, scrq);
1545 ibmvnic_complete_tx(adapter, scrq);
1546
1547 return IRQ_HANDLED;
1548}
1549
1550static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1551{
1552 struct ibmvnic_sub_crq_queue *scrq = instance;
1553 struct ibmvnic_adapter *adapter = scrq->adapter;
1554
1555 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1556 disable_scrq_irq(adapter, scrq);
1557 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1558 }
1559
1560 return IRQ_HANDLED;
1561}
1562
Thomas Falconea22d512016-07-06 15:35:17 -05001563static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1564{
1565 struct device *dev = &adapter->vdev->dev;
1566 struct ibmvnic_sub_crq_queue *scrq;
1567 int i = 0, j = 0;
1568 int rc = 0;
1569
1570 for (i = 0; i < adapter->req_tx_queues; i++) {
1571 scrq = adapter->tx_scrq[i];
1572 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1573
Michael Ellerman99c17902016-09-10 19:59:05 +10001574 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001575 rc = -EINVAL;
1576 dev_err(dev, "Error mapping irq\n");
1577 goto req_tx_irq_failed;
1578 }
1579
1580 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1581 0, "ibmvnic_tx", scrq);
1582
1583 if (rc) {
1584 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1585 scrq->irq, rc);
1586 irq_dispose_mapping(scrq->irq);
1587 goto req_rx_irq_failed;
1588 }
1589 }
1590
1591 for (i = 0; i < adapter->req_rx_queues; i++) {
1592 scrq = adapter->rx_scrq[i];
1593 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001594 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001595 rc = -EINVAL;
1596 dev_err(dev, "Error mapping irq\n");
1597 goto req_rx_irq_failed;
1598 }
1599 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1600 0, "ibmvnic_rx", scrq);
1601 if (rc) {
1602 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1603 scrq->irq, rc);
1604 irq_dispose_mapping(scrq->irq);
1605 goto req_rx_irq_failed;
1606 }
1607 }
1608 return rc;
1609
1610req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001611 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001612 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1613 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001614 }
Thomas Falconea22d512016-07-06 15:35:17 -05001615 i = adapter->req_tx_queues;
1616req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001617 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001618 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1619 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001620 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001621 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001622 return rc;
1623}
1624
Thomas Falcon032c5e82015-12-21 11:26:06 -06001625static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1626{
1627 struct device *dev = &adapter->vdev->dev;
1628 struct ibmvnic_sub_crq_queue **allqueues;
1629 int registered_queues = 0;
1630 union ibmvnic_crq crq;
1631 int total_queues;
1632 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001633 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001634
1635 if (!retry) {
1636 /* Sub-CRQ entries are 32 byte long */
1637 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1638
1639 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1640 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1641 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1642 goto allqueues_failed;
1643 }
1644
1645 /* Get the minimum between the queried max and the entries
1646 * that fit in our PAGE_SIZE
1647 */
1648 adapter->req_tx_entries_per_subcrq =
1649 adapter->max_tx_entries_per_subcrq > entries_page ?
1650 entries_page : adapter->max_tx_entries_per_subcrq;
1651 adapter->req_rx_add_entries_per_subcrq =
1652 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1653 entries_page : adapter->max_rx_add_entries_per_subcrq;
1654
John Allen6dbcd8f2016-11-07 14:27:28 -06001655 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1656 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001657 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001658
Thomas Falconf39f0d12017-02-14 10:22:59 -06001659 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001660 }
1661
1662 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1663
1664 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1665 if (!allqueues)
1666 goto allqueues_failed;
1667
1668 for (i = 0; i < total_queues; i++) {
1669 allqueues[i] = init_sub_crq_queue(adapter);
1670 if (!allqueues[i]) {
1671 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1672 break;
1673 }
1674 registered_queues++;
1675 }
1676
1677 /* Make sure we were able to register the minimum number of queues */
1678 if (registered_queues <
1679 adapter->min_tx_queues + adapter->min_rx_queues) {
1680 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1681 goto tx_failed;
1682 }
1683
1684 /* Distribute the failed allocated queues*/
1685 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1686 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1687 switch (i % 3) {
1688 case 0:
1689 if (adapter->req_rx_queues > adapter->min_rx_queues)
1690 adapter->req_rx_queues--;
1691 else
1692 more++;
1693 break;
1694 case 1:
1695 if (adapter->req_tx_queues > adapter->min_tx_queues)
1696 adapter->req_tx_queues--;
1697 else
1698 more++;
1699 break;
1700 }
1701 }
1702
1703 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1704 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1705 if (!adapter->tx_scrq)
1706 goto tx_failed;
1707
1708 for (i = 0; i < adapter->req_tx_queues; i++) {
1709 adapter->tx_scrq[i] = allqueues[i];
1710 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001711 }
1712
1713 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1714 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1715 if (!adapter->rx_scrq)
1716 goto rx_failed;
1717
1718 for (i = 0; i < adapter->req_rx_queues; i++) {
1719 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1720 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001721 }
1722
1723 memset(&crq, 0, sizeof(crq));
1724 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1725 crq.request_capability.cmd = REQUEST_CAPABILITY;
1726
1727 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001728 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001729 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001730 ibmvnic_send_crq(adapter, &crq);
1731
1732 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001733 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001734 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001735 ibmvnic_send_crq(adapter, &crq);
1736
1737 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001738 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001739 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001740 ibmvnic_send_crq(adapter, &crq);
1741
1742 crq.request_capability.capability =
1743 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1744 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001745 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001746 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001747 ibmvnic_send_crq(adapter, &crq);
1748
1749 crq.request_capability.capability =
1750 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1751 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001752 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001753 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001754 ibmvnic_send_crq(adapter, &crq);
1755
1756 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001757 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001758 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001759 ibmvnic_send_crq(adapter, &crq);
1760
1761 if (adapter->netdev->flags & IFF_PROMISC) {
1762 if (adapter->promisc_supported) {
1763 crq.request_capability.capability =
1764 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001765 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001766 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001767 ibmvnic_send_crq(adapter, &crq);
1768 }
1769 } else {
1770 crq.request_capability.capability =
1771 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001772 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001773 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774 ibmvnic_send_crq(adapter, &crq);
1775 }
1776
1777 kfree(allqueues);
1778
1779 return;
1780
Thomas Falcon032c5e82015-12-21 11:26:06 -06001781rx_failed:
1782 kfree(adapter->tx_scrq);
1783 adapter->tx_scrq = NULL;
1784tx_failed:
1785 for (i = 0; i < registered_queues; i++)
1786 release_sub_crq_queue(adapter, allqueues[i]);
1787 kfree(allqueues);
1788allqueues_failed:
1789 ibmvnic_remove(adapter->vdev);
1790}
1791
1792static int pending_scrq(struct ibmvnic_adapter *adapter,
1793 struct ibmvnic_sub_crq_queue *scrq)
1794{
1795 union sub_crq *entry = &scrq->msgs[scrq->cur];
1796
1797 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1798 return 1;
1799 else
1800 return 0;
1801}
1802
1803static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1804 struct ibmvnic_sub_crq_queue *scrq)
1805{
1806 union sub_crq *entry;
1807 unsigned long flags;
1808
1809 spin_lock_irqsave(&scrq->lock, flags);
1810 entry = &scrq->msgs[scrq->cur];
1811 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1812 if (++scrq->cur == scrq->size)
1813 scrq->cur = 0;
1814 } else {
1815 entry = NULL;
1816 }
1817 spin_unlock_irqrestore(&scrq->lock, flags);
1818
1819 return entry;
1820}
1821
1822static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1823{
1824 struct ibmvnic_crq_queue *queue = &adapter->crq;
1825 union ibmvnic_crq *crq;
1826
1827 crq = &queue->msgs[queue->cur];
1828 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1829 if (++queue->cur == queue->size)
1830 queue->cur = 0;
1831 } else {
1832 crq = NULL;
1833 }
1834
1835 return crq;
1836}
1837
1838static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1839 union sub_crq *sub_crq)
1840{
1841 unsigned int ua = adapter->vdev->unit_address;
1842 struct device *dev = &adapter->vdev->dev;
1843 u64 *u64_crq = (u64 *)sub_crq;
1844 int rc;
1845
1846 netdev_dbg(adapter->netdev,
1847 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1848 (unsigned long int)cpu_to_be64(remote_handle),
1849 (unsigned long int)cpu_to_be64(u64_crq[0]),
1850 (unsigned long int)cpu_to_be64(u64_crq[1]),
1851 (unsigned long int)cpu_to_be64(u64_crq[2]),
1852 (unsigned long int)cpu_to_be64(u64_crq[3]));
1853
1854 /* Make sure the hypervisor sees the complete request */
1855 mb();
1856
1857 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1858 cpu_to_be64(remote_handle),
1859 cpu_to_be64(u64_crq[0]),
1860 cpu_to_be64(u64_crq[1]),
1861 cpu_to_be64(u64_crq[2]),
1862 cpu_to_be64(u64_crq[3]));
1863
1864 if (rc) {
1865 if (rc == H_CLOSED)
1866 dev_warn(dev, "CRQ Queue closed\n");
1867 dev_err(dev, "Send error (rc=%d)\n", rc);
1868 }
1869
1870 return rc;
1871}
1872
Thomas Falconad7775d2016-04-01 17:20:34 -05001873static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1874 u64 remote_handle, u64 ioba, u64 num_entries)
1875{
1876 unsigned int ua = adapter->vdev->unit_address;
1877 struct device *dev = &adapter->vdev->dev;
1878 int rc;
1879
1880 /* Make sure the hypervisor sees the complete request */
1881 mb();
1882 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1883 cpu_to_be64(remote_handle),
1884 ioba, num_entries);
1885
1886 if (rc) {
1887 if (rc == H_CLOSED)
1888 dev_warn(dev, "CRQ Queue closed\n");
1889 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1890 }
1891
1892 return rc;
1893}
1894
Thomas Falcon032c5e82015-12-21 11:26:06 -06001895static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1896 union ibmvnic_crq *crq)
1897{
1898 unsigned int ua = adapter->vdev->unit_address;
1899 struct device *dev = &adapter->vdev->dev;
1900 u64 *u64_crq = (u64 *)crq;
1901 int rc;
1902
1903 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1904 (unsigned long int)cpu_to_be64(u64_crq[0]),
1905 (unsigned long int)cpu_to_be64(u64_crq[1]));
1906
1907 /* Make sure the hypervisor sees the complete request */
1908 mb();
1909
1910 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1911 cpu_to_be64(u64_crq[0]),
1912 cpu_to_be64(u64_crq[1]));
1913
1914 if (rc) {
1915 if (rc == H_CLOSED)
1916 dev_warn(dev, "CRQ Queue closed\n");
1917 dev_warn(dev, "Send error (rc=%d)\n", rc);
1918 }
1919
1920 return rc;
1921}
1922
1923static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1924{
1925 union ibmvnic_crq crq;
1926
1927 memset(&crq, 0, sizeof(crq));
1928 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1929 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1930 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1931
1932 return ibmvnic_send_crq(adapter, &crq);
1933}
1934
1935static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1936{
1937 union ibmvnic_crq crq;
1938
1939 memset(&crq, 0, sizeof(crq));
1940 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1941 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1942 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1943
1944 return ibmvnic_send_crq(adapter, &crq);
1945}
1946
1947static int send_version_xchg(struct ibmvnic_adapter *adapter)
1948{
1949 union ibmvnic_crq crq;
1950
1951 memset(&crq, 0, sizeof(crq));
1952 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1953 crq.version_exchange.cmd = VERSION_EXCHANGE;
1954 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1955
1956 return ibmvnic_send_crq(adapter, &crq);
1957}
1958
1959static void send_login(struct ibmvnic_adapter *adapter)
1960{
1961 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1962 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001963 struct device *dev = &adapter->vdev->dev;
1964 dma_addr_t rsp_buffer_token;
1965 dma_addr_t buffer_token;
1966 size_t rsp_buffer_size;
1967 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001968 size_t buffer_size;
1969 __be64 *tx_list_p;
1970 __be64 *rx_list_p;
1971 int i;
1972
1973 buffer_size =
1974 sizeof(struct ibmvnic_login_buffer) +
1975 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1976
1977 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1978 if (!login_buffer)
1979 goto buf_alloc_failed;
1980
1981 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1982 DMA_TO_DEVICE);
1983 if (dma_mapping_error(dev, buffer_token)) {
1984 dev_err(dev, "Couldn't map login buffer\n");
1985 goto buf_map_failed;
1986 }
1987
John Allen498cd8e2016-04-06 11:49:55 -05001988 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1989 sizeof(u64) * adapter->req_tx_queues +
1990 sizeof(u64) * adapter->req_rx_queues +
1991 sizeof(u64) * adapter->req_rx_queues +
1992 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001993
1994 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1995 if (!login_rsp_buffer)
1996 goto buf_rsp_alloc_failed;
1997
1998 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1999 rsp_buffer_size, DMA_FROM_DEVICE);
2000 if (dma_mapping_error(dev, rsp_buffer_token)) {
2001 dev_err(dev, "Couldn't map login rsp buffer\n");
2002 goto buf_rsp_map_failed;
2003 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002004
Thomas Falcon032c5e82015-12-21 11:26:06 -06002005 adapter->login_buf = login_buffer;
2006 adapter->login_buf_token = buffer_token;
2007 adapter->login_buf_sz = buffer_size;
2008 adapter->login_rsp_buf = login_rsp_buffer;
2009 adapter->login_rsp_buf_token = rsp_buffer_token;
2010 adapter->login_rsp_buf_sz = rsp_buffer_size;
2011
2012 login_buffer->len = cpu_to_be32(buffer_size);
2013 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2014 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2015 login_buffer->off_txcomp_subcrqs =
2016 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2017 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2018 login_buffer->off_rxcomp_subcrqs =
2019 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2020 sizeof(u64) * adapter->req_tx_queues);
2021 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2022 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2023
2024 tx_list_p = (__be64 *)((char *)login_buffer +
2025 sizeof(struct ibmvnic_login_buffer));
2026 rx_list_p = (__be64 *)((char *)login_buffer +
2027 sizeof(struct ibmvnic_login_buffer) +
2028 sizeof(u64) * adapter->req_tx_queues);
2029
2030 for (i = 0; i < adapter->req_tx_queues; i++) {
2031 if (adapter->tx_scrq[i]) {
2032 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2033 crq_num);
2034 }
2035 }
2036
2037 for (i = 0; i < adapter->req_rx_queues; i++) {
2038 if (adapter->rx_scrq[i]) {
2039 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2040 crq_num);
2041 }
2042 }
2043
2044 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2045 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2046 netdev_dbg(adapter->netdev, "%016lx\n",
2047 ((unsigned long int *)(adapter->login_buf))[i]);
2048 }
2049
2050 memset(&crq, 0, sizeof(crq));
2051 crq.login.first = IBMVNIC_CRQ_CMD;
2052 crq.login.cmd = LOGIN;
2053 crq.login.ioba = cpu_to_be32(buffer_token);
2054 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002055 ibmvnic_send_crq(adapter, &crq);
2056
2057 return;
2058
Thomas Falcon032c5e82015-12-21 11:26:06 -06002059buf_rsp_map_failed:
2060 kfree(login_rsp_buffer);
2061buf_rsp_alloc_failed:
2062 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2063buf_map_failed:
2064 kfree(login_buffer);
2065buf_alloc_failed:
2066 return;
2067}
2068
2069static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2070 u32 len, u8 map_id)
2071{
2072 union ibmvnic_crq crq;
2073
2074 memset(&crq, 0, sizeof(crq));
2075 crq.request_map.first = IBMVNIC_CRQ_CMD;
2076 crq.request_map.cmd = REQUEST_MAP;
2077 crq.request_map.map_id = map_id;
2078 crq.request_map.ioba = cpu_to_be32(addr);
2079 crq.request_map.len = cpu_to_be32(len);
2080 ibmvnic_send_crq(adapter, &crq);
2081}
2082
2083static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2084{
2085 union ibmvnic_crq crq;
2086
2087 memset(&crq, 0, sizeof(crq));
2088 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2089 crq.request_unmap.cmd = REQUEST_UNMAP;
2090 crq.request_unmap.map_id = map_id;
2091 ibmvnic_send_crq(adapter, &crq);
2092}
2093
2094static void send_map_query(struct ibmvnic_adapter *adapter)
2095{
2096 union ibmvnic_crq crq;
2097
2098 memset(&crq, 0, sizeof(crq));
2099 crq.query_map.first = IBMVNIC_CRQ_CMD;
2100 crq.query_map.cmd = QUERY_MAP;
2101 ibmvnic_send_crq(adapter, &crq);
2102}
2103
2104/* Send a series of CRQs requesting various capabilities of the VNIC server */
2105static void send_cap_queries(struct ibmvnic_adapter *adapter)
2106{
2107 union ibmvnic_crq crq;
2108
Thomas Falcon901e0402017-02-15 12:17:59 -06002109 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002110 memset(&crq, 0, sizeof(crq));
2111 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2112 crq.query_capability.cmd = QUERY_CAPABILITY;
2113
2114 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002115 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002116 ibmvnic_send_crq(adapter, &crq);
2117
2118 crq.query_capability.capability = cpu_to_be16(MIN_RX_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_ADD_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(MAX_TX_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_RX_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_ADD_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 =
2139 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002140 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002141 ibmvnic_send_crq(adapter, &crq);
2142
2143 crq.query_capability.capability =
2144 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002145 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002146 ibmvnic_send_crq(adapter, &crq);
2147
2148 crq.query_capability.capability =
2149 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002150 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002151 ibmvnic_send_crq(adapter, &crq);
2152
2153 crq.query_capability.capability =
2154 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002155 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002156 ibmvnic_send_crq(adapter, &crq);
2157
2158 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
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(PROMISC_SUPPORTED);
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(MIN_MTU);
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(MAX_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_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002182 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2183 atomic_inc(&adapter->running_cap_crqs);
2184 ibmvnic_send_crq(adapter, &crq);
2185
Thomas Falcon032c5e82015-12-21 11:26:06 -06002186 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002187 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188 ibmvnic_send_crq(adapter, &crq);
2189
2190 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
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_RX_COMP_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 =
2203 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002204 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002205 ibmvnic_send_crq(adapter, &crq);
2206
2207 crq.query_capability.capability =
2208 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002209 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002210 ibmvnic_send_crq(adapter, &crq);
2211
2212 crq.query_capability.capability =
2213 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002214 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002215 ibmvnic_send_crq(adapter, &crq);
2216
2217 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
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
2222static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2223{
2224 struct device *dev = &adapter->vdev->dev;
2225 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2226 union ibmvnic_crq crq;
2227 int i;
2228
2229 dma_unmap_single(dev, adapter->ip_offload_tok,
2230 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2231
2232 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2233 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2234 netdev_dbg(adapter->netdev, "%016lx\n",
2235 ((unsigned long int *)(buf))[i]);
2236
2237 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2238 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2239 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2240 buf->tcp_ipv4_chksum);
2241 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2242 buf->tcp_ipv6_chksum);
2243 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2244 buf->udp_ipv4_chksum);
2245 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2246 buf->udp_ipv6_chksum);
2247 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2248 buf->large_tx_ipv4);
2249 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2250 buf->large_tx_ipv6);
2251 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2252 buf->large_rx_ipv4);
2253 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2254 buf->large_rx_ipv6);
2255 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2256 buf->max_ipv4_header_size);
2257 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2258 buf->max_ipv6_header_size);
2259 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2260 buf->max_tcp_header_size);
2261 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2262 buf->max_udp_header_size);
2263 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2264 buf->max_large_tx_size);
2265 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2266 buf->max_large_rx_size);
2267 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2268 buf->ipv6_extension_header);
2269 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2270 buf->tcp_pseudosum_req);
2271 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2272 buf->num_ipv6_ext_headers);
2273 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2274 buf->off_ipv6_ext_headers);
2275
2276 adapter->ip_offload_ctrl_tok =
2277 dma_map_single(dev, &adapter->ip_offload_ctrl,
2278 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2279
2280 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2281 dev_err(dev, "Couldn't map ip offload control buffer\n");
2282 return;
2283 }
2284
2285 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2286 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2287 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2288 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2289 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2290
2291 /* large_tx/rx disabled for now, additional features needed */
2292 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2293 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2294 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2295 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2296
2297 adapter->netdev->features = NETIF_F_GSO;
2298
2299 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2300 adapter->netdev->features |= NETIF_F_IP_CSUM;
2301
2302 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2303 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2304
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002305 if ((adapter->netdev->features &
2306 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2307 adapter->netdev->features |= NETIF_F_RXCSUM;
2308
Thomas Falcon032c5e82015-12-21 11:26:06 -06002309 memset(&crq, 0, sizeof(crq));
2310 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2311 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2312 crq.control_ip_offload.len =
2313 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2314 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2315 ibmvnic_send_crq(adapter, &crq);
2316}
2317
2318static void handle_error_info_rsp(union ibmvnic_crq *crq,
2319 struct ibmvnic_adapter *adapter)
2320{
2321 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002322 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002323 unsigned long flags;
2324 bool found = false;
2325 int i;
2326
2327 if (!crq->request_error_rsp.rc.code) {
2328 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2329 crq->request_error_rsp.rc.code);
2330 return;
2331 }
2332
2333 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002334 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002335 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2336 found = true;
2337 list_del(&error_buff->list);
2338 break;
2339 }
2340 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2341
2342 if (!found) {
2343 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002344 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002345 return;
2346 }
2347
2348 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002349 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002350
2351 for (i = 0; i < error_buff->len; i++) {
2352 pr_cont("%02x", (int)error_buff->buff[i]);
2353 if (i % 8 == 7)
2354 pr_cont(" ");
2355 }
2356 pr_cont("\n");
2357
2358 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2359 DMA_FROM_DEVICE);
2360 kfree(error_buff->buff);
2361 kfree(error_buff);
2362}
2363
Thomas Falcon032c5e82015-12-21 11:26:06 -06002364static void handle_error_indication(union ibmvnic_crq *crq,
2365 struct ibmvnic_adapter *adapter)
2366{
2367 int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002368 struct device *dev = &adapter->vdev->dev;
2369 struct ibmvnic_error_buff *error_buff;
2370 union ibmvnic_crq new_crq;
2371 unsigned long flags;
2372
2373 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2374 crq->error_indication.
2375 flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
Thomas Falcon75224c92017-02-15 10:33:33 -06002376 be32_to_cpu(crq->error_indication.error_id),
2377 be16_to_cpu(crq->error_indication.error_cause));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002378
2379 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2380 if (!error_buff)
2381 return;
2382
2383 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2384 if (!error_buff->buff) {
2385 kfree(error_buff);
2386 return;
2387 }
2388
2389 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2390 DMA_FROM_DEVICE);
2391 if (dma_mapping_error(dev, error_buff->dma)) {
2392 if (!firmware_has_feature(FW_FEATURE_CMO))
2393 dev_err(dev, "Couldn't map error buffer\n");
2394 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;
2400 error_buff->error_id = crq->error_indication.error_id;
2401
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
2406 memset(&new_crq, 0, sizeof(new_crq));
2407 new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2408 new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2409 new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2410 new_crq.request_error_info.len = cpu_to_be32(detail_len);
2411 new_crq.request_error_info.error_id = crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002412 ibmvnic_send_crq(adapter, &new_crq);
2413}
2414
2415static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2416 struct ibmvnic_adapter *adapter)
2417{
2418 struct net_device *netdev = adapter->netdev;
2419 struct device *dev = &adapter->vdev->dev;
2420 long rc;
2421
2422 rc = crq->change_mac_addr_rsp.rc.code;
2423 if (rc) {
2424 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2425 return;
2426 }
2427 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2428 ETH_ALEN);
2429}
2430
2431static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2432 struct ibmvnic_adapter *adapter)
2433{
2434 struct device *dev = &adapter->vdev->dev;
2435 u64 *req_value;
2436 char *name;
2437
Thomas Falcon901e0402017-02-15 12:17:59 -06002438 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2440 case REQ_TX_QUEUES:
2441 req_value = &adapter->req_tx_queues;
2442 name = "tx";
2443 break;
2444 case REQ_RX_QUEUES:
2445 req_value = &adapter->req_rx_queues;
2446 name = "rx";
2447 break;
2448 case REQ_RX_ADD_QUEUES:
2449 req_value = &adapter->req_rx_add_queues;
2450 name = "rx_add";
2451 break;
2452 case REQ_TX_ENTRIES_PER_SUBCRQ:
2453 req_value = &adapter->req_tx_entries_per_subcrq;
2454 name = "tx_entries_per_subcrq";
2455 break;
2456 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2457 req_value = &adapter->req_rx_add_entries_per_subcrq;
2458 name = "rx_add_entries_per_subcrq";
2459 break;
2460 case REQ_MTU:
2461 req_value = &adapter->req_mtu;
2462 name = "mtu";
2463 break;
2464 case PROMISC_REQUESTED:
2465 req_value = &adapter->promisc;
2466 name = "promisc";
2467 break;
2468 default:
2469 dev_err(dev, "Got invalid cap request rsp %d\n",
2470 crq->request_capability.capability);
2471 return;
2472 }
2473
2474 switch (crq->request_capability_rsp.rc.code) {
2475 case SUCCESS:
2476 break;
2477 case PARTIALSUCCESS:
2478 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2479 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002480 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002481 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002482 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002483 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002484 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002485 return;
2486 default:
2487 dev_err(dev, "Error %d in request cap rsp\n",
2488 crq->request_capability_rsp.rc.code);
2489 return;
2490 }
2491
2492 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002493 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002494 union ibmvnic_crq newcrq;
2495 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2496 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2497 &adapter->ip_offload_buf;
2498
Thomas Falcon249168a2017-02-15 12:18:00 -06002499 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002500 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2501 buf_sz,
2502 DMA_FROM_DEVICE);
2503
2504 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2505 if (!firmware_has_feature(FW_FEATURE_CMO))
2506 dev_err(dev, "Couldn't map offload buffer\n");
2507 return;
2508 }
2509
2510 memset(&newcrq, 0, sizeof(newcrq));
2511 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2512 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2513 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2514 newcrq.query_ip_offload.ioba =
2515 cpu_to_be32(adapter->ip_offload_tok);
2516
2517 ibmvnic_send_crq(adapter, &newcrq);
2518 }
2519}
2520
2521static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2522 struct ibmvnic_adapter *adapter)
2523{
2524 struct device *dev = &adapter->vdev->dev;
2525 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2526 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002527 int i;
2528
2529 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2530 DMA_BIDIRECTIONAL);
2531 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2532 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2533
John Allen498cd8e2016-04-06 11:49:55 -05002534 /* If the number of queues requested can't be allocated by the
2535 * server, the login response will return with code 1. We will need
2536 * to resend the login buffer with fewer queues requested.
2537 */
2538 if (login_rsp_crq->generic.rc.code) {
2539 adapter->renegotiate = true;
2540 complete(&adapter->init_done);
2541 return 0;
2542 }
2543
Thomas Falcon032c5e82015-12-21 11:26:06 -06002544 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2545 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2546 netdev_dbg(adapter->netdev, "%016lx\n",
2547 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2548 }
2549
2550 /* Sanity checks */
2551 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2552 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2553 adapter->req_rx_add_queues !=
2554 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2555 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2556 ibmvnic_remove(adapter->vdev);
2557 return -EIO;
2558 }
2559 complete(&adapter->init_done);
2560
Thomas Falcon032c5e82015-12-21 11:26:06 -06002561 return 0;
2562}
2563
2564static void handle_request_map_rsp(union ibmvnic_crq *crq,
2565 struct ibmvnic_adapter *adapter)
2566{
2567 struct device *dev = &adapter->vdev->dev;
2568 u8 map_id = crq->request_map_rsp.map_id;
2569 int tx_subcrqs;
2570 int rx_subcrqs;
2571 long rc;
2572 int i;
2573
2574 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2575 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2576
2577 rc = crq->request_map_rsp.rc.code;
2578 if (rc) {
2579 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2580 adapter->map_id--;
2581 /* need to find and zero tx/rx_pool map_id */
2582 for (i = 0; i < tx_subcrqs; i++) {
2583 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2584 adapter->tx_pool[i].long_term_buff.map_id = 0;
2585 }
2586 for (i = 0; i < rx_subcrqs; i++) {
2587 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2588 adapter->rx_pool[i].long_term_buff.map_id = 0;
2589 }
2590 }
2591 complete(&adapter->fw_done);
2592}
2593
2594static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2595 struct ibmvnic_adapter *adapter)
2596{
2597 struct device *dev = &adapter->vdev->dev;
2598 long rc;
2599
2600 rc = crq->request_unmap_rsp.rc.code;
2601 if (rc)
2602 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2603}
2604
2605static void handle_query_map_rsp(union ibmvnic_crq *crq,
2606 struct ibmvnic_adapter *adapter)
2607{
2608 struct net_device *netdev = adapter->netdev;
2609 struct device *dev = &adapter->vdev->dev;
2610 long rc;
2611
2612 rc = crq->query_map_rsp.rc.code;
2613 if (rc) {
2614 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2615 return;
2616 }
2617 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2618 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2619 crq->query_map_rsp.free_pages);
2620}
2621
2622static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2623 struct ibmvnic_adapter *adapter)
2624{
2625 struct net_device *netdev = adapter->netdev;
2626 struct device *dev = &adapter->vdev->dev;
2627 long rc;
2628
Thomas Falcon901e0402017-02-15 12:17:59 -06002629 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002630 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002631 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002632 rc = crq->query_capability.rc.code;
2633 if (rc) {
2634 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2635 goto out;
2636 }
2637
2638 switch (be16_to_cpu(crq->query_capability.capability)) {
2639 case MIN_TX_QUEUES:
2640 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002641 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002642 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2643 adapter->min_tx_queues);
2644 break;
2645 case MIN_RX_QUEUES:
2646 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002647 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002648 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2649 adapter->min_rx_queues);
2650 break;
2651 case MIN_RX_ADD_QUEUES:
2652 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002653 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002654 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2655 adapter->min_rx_add_queues);
2656 break;
2657 case MAX_TX_QUEUES:
2658 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002659 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002660 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2661 adapter->max_tx_queues);
2662 break;
2663 case MAX_RX_QUEUES:
2664 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002665 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002666 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2667 adapter->max_rx_queues);
2668 break;
2669 case MAX_RX_ADD_QUEUES:
2670 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002671 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002672 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2673 adapter->max_rx_add_queues);
2674 break;
2675 case MIN_TX_ENTRIES_PER_SUBCRQ:
2676 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002677 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002678 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2679 adapter->min_tx_entries_per_subcrq);
2680 break;
2681 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2682 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002683 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002684 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2685 adapter->min_rx_add_entries_per_subcrq);
2686 break;
2687 case MAX_TX_ENTRIES_PER_SUBCRQ:
2688 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002689 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002690 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2691 adapter->max_tx_entries_per_subcrq);
2692 break;
2693 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2694 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002695 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002696 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2697 adapter->max_rx_add_entries_per_subcrq);
2698 break;
2699 case TCP_IP_OFFLOAD:
2700 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002701 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002702 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2703 adapter->tcp_ip_offload);
2704 break;
2705 case PROMISC_SUPPORTED:
2706 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002707 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002708 netdev_dbg(netdev, "promisc_supported = %lld\n",
2709 adapter->promisc_supported);
2710 break;
2711 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002712 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002713 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002714 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2715 break;
2716 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002717 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002718 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002719 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2720 break;
2721 case MAX_MULTICAST_FILTERS:
2722 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002723 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002724 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2725 adapter->max_multicast_filters);
2726 break;
2727 case VLAN_HEADER_INSERTION:
2728 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002729 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002730 if (adapter->vlan_header_insertion)
2731 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2732 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2733 adapter->vlan_header_insertion);
2734 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002735 case RX_VLAN_HEADER_INSERTION:
2736 adapter->rx_vlan_header_insertion =
2737 be64_to_cpu(crq->query_capability.number);
2738 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
2739 adapter->rx_vlan_header_insertion);
2740 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741 case MAX_TX_SG_ENTRIES:
2742 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002743 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002744 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2745 adapter->max_tx_sg_entries);
2746 break;
2747 case RX_SG_SUPPORTED:
2748 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002749 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2751 adapter->rx_sg_supported);
2752 break;
2753 case OPT_TX_COMP_SUB_QUEUES:
2754 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002755 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002756 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2757 adapter->opt_tx_comp_sub_queues);
2758 break;
2759 case OPT_RX_COMP_QUEUES:
2760 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002761 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002762 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2763 adapter->opt_rx_comp_queues);
2764 break;
2765 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2766 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002767 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002768 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2769 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2770 break;
2771 case OPT_TX_ENTRIES_PER_SUBCRQ:
2772 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002773 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002774 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2775 adapter->opt_tx_entries_per_subcrq);
2776 break;
2777 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2778 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002779 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002780 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2781 adapter->opt_rxba_entries_per_subcrq);
2782 break;
2783 case TX_RX_DESC_REQ:
2784 adapter->tx_rx_desc_req = crq->query_capability.number;
2785 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2786 adapter->tx_rx_desc_req);
2787 break;
2788
2789 default:
2790 netdev_err(netdev, "Got invalid cap rsp %d\n",
2791 crq->query_capability.capability);
2792 }
2793
2794out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002795 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2796 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002797 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002798 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002799 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002800}
2801
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002802static void ibmvnic_xport_event(struct work_struct *work)
2803{
2804 struct ibmvnic_adapter *adapter = container_of(work,
2805 struct ibmvnic_adapter,
2806 ibmvnic_xport);
2807 struct device *dev = &adapter->vdev->dev;
2808 long rc;
2809
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002810 release_sub_crqs(adapter);
2811 if (adapter->migrated) {
2812 rc = ibmvnic_reenable_crq_queue(adapter);
2813 if (rc)
2814 dev_err(dev, "Error after enable rc=%ld\n", rc);
2815 adapter->migrated = false;
2816 rc = ibmvnic_send_crq_init(adapter);
2817 if (rc)
2818 dev_err(dev, "Error sending init rc=%ld\n", rc);
2819 }
2820}
2821
Thomas Falcon032c5e82015-12-21 11:26:06 -06002822static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2823 struct ibmvnic_adapter *adapter)
2824{
2825 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2826 struct net_device *netdev = adapter->netdev;
2827 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002828 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002829 long rc;
2830
2831 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002832 (unsigned long int)cpu_to_be64(u64_crq[0]),
2833 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002834 switch (gen_crq->first) {
2835 case IBMVNIC_CRQ_INIT_RSP:
2836 switch (gen_crq->cmd) {
2837 case IBMVNIC_CRQ_INIT:
2838 dev_info(dev, "Partner initialized\n");
2839 /* Send back a response */
2840 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002841 if (!rc)
2842 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002843 else
2844 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2845 break;
2846 case IBMVNIC_CRQ_INIT_COMPLETE:
2847 dev_info(dev, "Partner initialization complete\n");
2848 send_version_xchg(adapter);
2849 break;
2850 default:
2851 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2852 }
2853 return;
2854 case IBMVNIC_CRQ_XPORT_EVENT:
2855 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2856 dev_info(dev, "Re-enabling adapter\n");
2857 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002858 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002859 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2860 dev_info(dev, "Backing device failover detected\n");
2861 netif_carrier_off(netdev);
2862 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002863 } else {
2864 /* The adapter lost the connection */
2865 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2866 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002867 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002868 }
2869 return;
2870 case IBMVNIC_CRQ_CMD_RSP:
2871 break;
2872 default:
2873 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2874 gen_crq->first);
2875 return;
2876 }
2877
2878 switch (gen_crq->cmd) {
2879 case VERSION_EXCHANGE_RSP:
2880 rc = crq->version_exchange_rsp.rc.code;
2881 if (rc) {
2882 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2883 break;
2884 }
2885 dev_info(dev, "Partner protocol version is %d\n",
2886 crq->version_exchange_rsp.version);
2887 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2888 ibmvnic_version)
2889 ibmvnic_version =
2890 be16_to_cpu(crq->version_exchange_rsp.version);
2891 send_cap_queries(adapter);
2892 break;
2893 case QUERY_CAPABILITY_RSP:
2894 handle_query_cap_rsp(crq, adapter);
2895 break;
2896 case QUERY_MAP_RSP:
2897 handle_query_map_rsp(crq, adapter);
2898 break;
2899 case REQUEST_MAP_RSP:
2900 handle_request_map_rsp(crq, adapter);
2901 break;
2902 case REQUEST_UNMAP_RSP:
2903 handle_request_unmap_rsp(crq, adapter);
2904 break;
2905 case REQUEST_CAPABILITY_RSP:
2906 handle_request_cap_rsp(crq, adapter);
2907 break;
2908 case LOGIN_RSP:
2909 netdev_dbg(netdev, "Got Login Response\n");
2910 handle_login_rsp(crq, adapter);
2911 break;
2912 case LOGICAL_LINK_STATE_RSP:
2913 netdev_dbg(netdev, "Got Logical Link State Response\n");
2914 adapter->logical_link_state =
2915 crq->logical_link_state_rsp.link_state;
2916 break;
2917 case LINK_STATE_INDICATION:
2918 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2919 adapter->phys_link_state =
2920 crq->link_state_indication.phys_link_state;
2921 adapter->logical_link_state =
2922 crq->link_state_indication.logical_link_state;
2923 break;
2924 case CHANGE_MAC_ADDR_RSP:
2925 netdev_dbg(netdev, "Got MAC address change Response\n");
2926 handle_change_mac_rsp(crq, adapter);
2927 break;
2928 case ERROR_INDICATION:
2929 netdev_dbg(netdev, "Got Error Indication\n");
2930 handle_error_indication(crq, adapter);
2931 break;
2932 case REQUEST_ERROR_RSP:
2933 netdev_dbg(netdev, "Got Error Detail Response\n");
2934 handle_error_info_rsp(crq, adapter);
2935 break;
2936 case REQUEST_STATISTICS_RSP:
2937 netdev_dbg(netdev, "Got Statistics Response\n");
2938 complete(&adapter->stats_done);
2939 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002940 case QUERY_IP_OFFLOAD_RSP:
2941 netdev_dbg(netdev, "Got Query IP offload Response\n");
2942 handle_query_ip_offload_rsp(adapter);
2943 break;
2944 case MULTICAST_CTRL_RSP:
2945 netdev_dbg(netdev, "Got multicast control Response\n");
2946 break;
2947 case CONTROL_IP_OFFLOAD_RSP:
2948 netdev_dbg(netdev, "Got Control IP offload Response\n");
2949 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
2950 sizeof(adapter->ip_offload_ctrl),
2951 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05002952 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002953 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002954 case COLLECT_FW_TRACE_RSP:
2955 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
2956 complete(&adapter->fw_done);
2957 break;
2958 default:
2959 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
2960 gen_crq->cmd);
2961 }
2962}
2963
2964static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
2965{
2966 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06002967
Thomas Falcon6c267b32017-02-15 12:17:58 -06002968 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06002969 return IRQ_HANDLED;
2970}
2971
2972static void ibmvnic_tasklet(void *data)
2973{
2974 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002975 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002976 union ibmvnic_crq *crq;
2977 unsigned long flags;
2978 bool done = false;
2979
2980 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002981 while (!done) {
2982 /* Pull all the valid messages off the CRQ */
2983 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
2984 ibmvnic_handle_crq(crq, adapter);
2985 crq->generic.first = 0;
2986 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04002987
2988 /* remain in tasklet until all
2989 * capabilities responses are received
2990 */
2991 if (!adapter->wait_capability)
2992 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002993 }
Thomas Falcon249168a2017-02-15 12:18:00 -06002994 /* if capabilities CRQ's were sent in this tasklet, the following
2995 * tasklet must wait until all responses are received
2996 */
2997 if (atomic_read(&adapter->running_cap_crqs) != 0)
2998 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002999 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003000}
3001
3002static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3003{
3004 struct vio_dev *vdev = adapter->vdev;
3005 int rc;
3006
3007 do {
3008 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3009 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3010
3011 if (rc)
3012 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3013
3014 return rc;
3015}
3016
3017static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3018{
3019 struct ibmvnic_crq_queue *crq = &adapter->crq;
3020 struct device *dev = &adapter->vdev->dev;
3021 struct vio_dev *vdev = adapter->vdev;
3022 int rc;
3023
3024 /* Close the CRQ */
3025 do {
3026 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3027 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3028
3029 /* Clean out the queue */
3030 memset(crq->msgs, 0, PAGE_SIZE);
3031 crq->cur = 0;
3032
3033 /* And re-open it again */
3034 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3035 crq->msg_token, PAGE_SIZE);
3036
3037 if (rc == H_CLOSED)
3038 /* Adapter is good, but other end is not ready */
3039 dev_warn(dev, "Partner adapter not ready\n");
3040 else if (rc != 0)
3041 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3042
3043 return rc;
3044}
3045
Nathan Fontenotf9928872017-03-30 02:48:54 -04003046static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003047{
3048 struct ibmvnic_crq_queue *crq = &adapter->crq;
3049 struct vio_dev *vdev = adapter->vdev;
3050 long rc;
3051
Nathan Fontenotf9928872017-03-30 02:48:54 -04003052 if (!crq->msgs)
3053 return;
3054
Thomas Falcon032c5e82015-12-21 11:26:06 -06003055 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3056 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003057 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003058 do {
3059 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3060 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3061
3062 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3063 DMA_BIDIRECTIONAL);
3064 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003065 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003066}
3067
Nathan Fontenotf9928872017-03-30 02:48:54 -04003068static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003069{
3070 struct ibmvnic_crq_queue *crq = &adapter->crq;
3071 struct device *dev = &adapter->vdev->dev;
3072 struct vio_dev *vdev = adapter->vdev;
3073 int rc, retrc = -ENOMEM;
3074
Nathan Fontenotf9928872017-03-30 02:48:54 -04003075 if (crq->msgs)
3076 return 0;
3077
Thomas Falcon032c5e82015-12-21 11:26:06 -06003078 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3079 /* Should we allocate more than one page? */
3080
3081 if (!crq->msgs)
3082 return -ENOMEM;
3083
3084 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3085 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3086 DMA_BIDIRECTIONAL);
3087 if (dma_mapping_error(dev, crq->msg_token))
3088 goto map_failed;
3089
3090 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3091 crq->msg_token, PAGE_SIZE);
3092
3093 if (rc == H_RESOURCE)
3094 /* maybe kexecing and resource is busy. try a reset */
3095 rc = ibmvnic_reset_crq(adapter);
3096 retrc = rc;
3097
3098 if (rc == H_CLOSED) {
3099 dev_warn(dev, "Partner adapter not ready\n");
3100 } else if (rc) {
3101 dev_warn(dev, "Error %d opening adapter\n", rc);
3102 goto reg_crq_failed;
3103 }
3104
3105 retrc = 0;
3106
Thomas Falcon6c267b32017-02-15 12:17:58 -06003107 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3108 (unsigned long)adapter);
3109
Thomas Falcon032c5e82015-12-21 11:26:06 -06003110 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3111 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3112 adapter);
3113 if (rc) {
3114 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3115 vdev->irq, rc);
3116 goto req_irq_failed;
3117 }
3118
3119 rc = vio_enable_interrupts(vdev);
3120 if (rc) {
3121 dev_err(dev, "Error %d enabling interrupts\n", rc);
3122 goto req_irq_failed;
3123 }
3124
3125 crq->cur = 0;
3126 spin_lock_init(&crq->lock);
3127
3128 return retrc;
3129
3130req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003131 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003132 do {
3133 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3134 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3135reg_crq_failed:
3136 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3137map_failed:
3138 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003139 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003140 return retrc;
3141}
3142
Thomas Falcon65dc6892016-07-06 15:35:18 -05003143static void handle_crq_init_rsp(struct work_struct *work)
3144{
3145 struct ibmvnic_adapter *adapter = container_of(work,
3146 struct ibmvnic_adapter,
3147 vnic_crq_init);
3148 struct device *dev = &adapter->vdev->dev;
3149 struct net_device *netdev = adapter->netdev;
3150 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003151 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003152 int rc;
3153
Thomas Falcondfad09a2016-08-18 11:37:51 -05003154 if (adapter->failover) {
3155 release_sub_crqs(adapter);
3156 if (netif_running(netdev)) {
3157 netif_tx_disable(netdev);
3158 ibmvnic_close(netdev);
3159 restart = true;
3160 }
3161 }
3162
Thomas Falcon65dc6892016-07-06 15:35:18 -05003163 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003164 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003165 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3166 dev_err(dev, "Passive init timeout\n");
3167 goto task_failed;
3168 }
3169
Thomas Falconf39f0d12017-02-14 10:22:59 -06003170 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003171
Thomas Falcondfad09a2016-08-18 11:37:51 -05003172 if (adapter->failover) {
3173 adapter->failover = false;
3174 if (restart) {
3175 rc = ibmvnic_open(netdev);
3176 if (rc)
3177 goto restart_failed;
3178 }
3179 netif_carrier_on(netdev);
3180 return;
3181 }
3182
Thomas Falcon65dc6892016-07-06 15:35:18 -05003183 rc = register_netdev(netdev);
3184 if (rc) {
3185 dev_err(dev,
3186 "failed to register netdev rc=%d\n", rc);
3187 goto register_failed;
3188 }
3189 dev_info(dev, "ibmvnic registered\n");
3190
3191 return;
3192
Thomas Falcondfad09a2016-08-18 11:37:51 -05003193restart_failed:
3194 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003195register_failed:
3196 release_sub_crqs(adapter);
3197task_failed:
3198 dev_err(dev, "Passive initialization was not successful\n");
3199}
3200
John Allenf6ef6402017-03-17 17:13:42 -05003201static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3202{
3203 struct device *dev = &adapter->vdev->dev;
3204 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003205 int rc;
3206
Nathan Fontenotf9928872017-03-30 02:48:54 -04003207 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003208 if (rc) {
3209 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3210 return rc;
3211 }
3212
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -04003213 rc = init_stats_token(adapter);
3214 if (rc) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003215 release_crq_queue(adapter);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -04003216 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003217 }
3218
John Allenf6ef6402017-03-17 17:13:42 -05003219 init_completion(&adapter->init_done);
3220 ibmvnic_send_crq_init(adapter);
3221 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3222 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003223 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003224 return -1;
3225 }
3226
3227 return 0;
3228}
3229
Thomas Falcon032c5e82015-12-21 11:26:06 -06003230static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3231{
3232 struct ibmvnic_adapter *adapter;
3233 struct net_device *netdev;
3234 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003235 int rc;
3236
3237 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3238 dev->unit_address);
3239
3240 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3241 VETH_MAC_ADDR, NULL);
3242 if (!mac_addr_p) {
3243 dev_err(&dev->dev,
3244 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3245 __FILE__, __LINE__);
3246 return 0;
3247 }
3248
3249 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3250 IBMVNIC_MAX_TX_QUEUES);
3251 if (!netdev)
3252 return -ENOMEM;
3253
3254 adapter = netdev_priv(netdev);
3255 dev_set_drvdata(&dev->dev, netdev);
3256 adapter->vdev = dev;
3257 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003258 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003259
3260 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3261 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3262 netdev->irq = dev->irq;
3263 netdev->netdev_ops = &ibmvnic_netdev_ops;
3264 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3265 SET_NETDEV_DEV(netdev, &dev->dev);
3266
Thomas Falcon65dc6892016-07-06 15:35:18 -05003267 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003268 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003269
Thomas Falcon032c5e82015-12-21 11:26:06 -06003270 spin_lock_init(&adapter->stats_lock);
3271
Thomas Falcon032c5e82015-12-21 11:26:06 -06003272 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003273 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003274
John Allenf6ef6402017-03-17 17:13:42 -05003275 rc = ibmvnic_init(adapter);
3276 if (rc) {
3277 free_netdev(netdev);
3278 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003279 }
3280
Thomas Falconf39f0d12017-02-14 10:22:59 -06003281 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003282 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003283
3284 rc = register_netdev(netdev);
3285 if (rc) {
3286 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003287 free_netdev(netdev);
3288 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003289 }
3290 dev_info(&dev->dev, "ibmvnic registered\n");
3291
3292 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003293}
3294
3295static int ibmvnic_remove(struct vio_dev *dev)
3296{
3297 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003298 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003299
3300 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003301
3302 release_resources(adapter);
3303 release_sub_crqs(adapter);
3304 release_crq_queue(adapter);
3305
Thomas Falcon032c5e82015-12-21 11:26:06 -06003306 free_netdev(netdev);
3307 dev_set_drvdata(&dev->dev, NULL);
3308
3309 return 0;
3310}
3311
3312static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3313{
3314 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3315 struct ibmvnic_adapter *adapter;
3316 struct iommu_table *tbl;
3317 unsigned long ret = 0;
3318 int i;
3319
3320 tbl = get_iommu_table_base(&vdev->dev);
3321
3322 /* netdev inits at probe time along with the structures we need below*/
3323 if (!netdev)
3324 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3325
3326 adapter = netdev_priv(netdev);
3327
3328 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003329 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3330
3331 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3332 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3333
3334 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3335 i++)
3336 ret += adapter->rx_pool[i].size *
3337 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3338
3339 return ret;
3340}
3341
3342static int ibmvnic_resume(struct device *dev)
3343{
3344 struct net_device *netdev = dev_get_drvdata(dev);
3345 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3346 int i;
3347
3348 /* kick the interrupt handlers just in case we lost an interrupt */
3349 for (i = 0; i < adapter->req_rx_queues; i++)
3350 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3351 adapter->rx_scrq[i]);
3352
3353 return 0;
3354}
3355
3356static struct vio_device_id ibmvnic_device_table[] = {
3357 {"network", "IBM,vnic"},
3358 {"", "" }
3359};
3360MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3361
3362static const struct dev_pm_ops ibmvnic_pm_ops = {
3363 .resume = ibmvnic_resume
3364};
3365
3366static struct vio_driver ibmvnic_driver = {
3367 .id_table = ibmvnic_device_table,
3368 .probe = ibmvnic_probe,
3369 .remove = ibmvnic_remove,
3370 .get_desired_dma = ibmvnic_get_desired_dma,
3371 .name = ibmvnic_driver_name,
3372 .pm = &ibmvnic_pm_ops,
3373};
3374
3375/* module functions */
3376static int __init ibmvnic_module_init(void)
3377{
3378 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3379 IBMVNIC_DRIVER_VERSION);
3380
3381 return vio_register_driver(&ibmvnic_driver);
3382}
3383
3384static void __exit ibmvnic_module_exit(void)
3385{
3386 vio_unregister_driver(&ibmvnic_driver);
3387}
3388
3389module_init(ibmvnic_module_init);
3390module_exit(ibmvnic_module_exit);