blob: cc34bf9be4051df640d441787d741bd0c54c15ac [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
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002364static void request_error_information(struct ibmvnic_adapter *adapter,
2365 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002366{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002367 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002368 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002369 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002370 unsigned long timeout = msecs_to_jiffies(30000);
2371 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002372 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002373 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002374
2375 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2376 if (!error_buff)
2377 return;
2378
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002379 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002380 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2381 if (!error_buff->buff) {
2382 kfree(error_buff);
2383 return;
2384 }
2385
2386 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2387 DMA_FROM_DEVICE);
2388 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002389 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002390 kfree(error_buff->buff);
2391 kfree(error_buff);
2392 return;
2393 }
2394
Thomas Falcon032c5e82015-12-21 11:26:06 -06002395 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002396 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002397
2398 spin_lock_irqsave(&adapter->error_list_lock, flags);
2399 list_add_tail(&error_buff->list, &adapter->errors);
2400 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2401
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002402 memset(&crq, 0, sizeof(crq));
2403 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2404 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2405 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2406 crq.request_error_info.len = cpu_to_be32(detail_len);
2407 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2408
2409 rc = ibmvnic_send_crq(adapter, &crq);
2410 if (rc) {
2411 netdev_err(netdev, "failed to request error information\n");
2412 goto err_info_fail;
2413 }
2414
2415 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2416 netdev_err(netdev, "timeout waiting for error information\n");
2417 goto err_info_fail;
2418 }
2419
2420 return;
2421
2422err_info_fail:
2423 spin_lock_irqsave(&adapter->error_list_lock, flags);
2424 list_del(&error_buff->list);
2425 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2426
2427 kfree(error_buff->buff);
2428 kfree(error_buff);
2429}
2430
2431static void handle_error_indication(union ibmvnic_crq *crq,
2432 struct ibmvnic_adapter *adapter)
2433{
2434 struct device *dev = &adapter->vdev->dev;
2435
2436 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2437 crq->error_indication.flags
2438 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2439 be32_to_cpu(crq->error_indication.error_id),
2440 be16_to_cpu(crq->error_indication.error_cause));
2441
2442 if (be32_to_cpu(crq->error_indication.error_id))
2443 request_error_information(adapter, crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002444}
2445
2446static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2447 struct ibmvnic_adapter *adapter)
2448{
2449 struct net_device *netdev = adapter->netdev;
2450 struct device *dev = &adapter->vdev->dev;
2451 long rc;
2452
2453 rc = crq->change_mac_addr_rsp.rc.code;
2454 if (rc) {
2455 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2456 return;
2457 }
2458 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2459 ETH_ALEN);
2460}
2461
2462static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2463 struct ibmvnic_adapter *adapter)
2464{
2465 struct device *dev = &adapter->vdev->dev;
2466 u64 *req_value;
2467 char *name;
2468
Thomas Falcon901e0402017-02-15 12:17:59 -06002469 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002470 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2471 case REQ_TX_QUEUES:
2472 req_value = &adapter->req_tx_queues;
2473 name = "tx";
2474 break;
2475 case REQ_RX_QUEUES:
2476 req_value = &adapter->req_rx_queues;
2477 name = "rx";
2478 break;
2479 case REQ_RX_ADD_QUEUES:
2480 req_value = &adapter->req_rx_add_queues;
2481 name = "rx_add";
2482 break;
2483 case REQ_TX_ENTRIES_PER_SUBCRQ:
2484 req_value = &adapter->req_tx_entries_per_subcrq;
2485 name = "tx_entries_per_subcrq";
2486 break;
2487 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2488 req_value = &adapter->req_rx_add_entries_per_subcrq;
2489 name = "rx_add_entries_per_subcrq";
2490 break;
2491 case REQ_MTU:
2492 req_value = &adapter->req_mtu;
2493 name = "mtu";
2494 break;
2495 case PROMISC_REQUESTED:
2496 req_value = &adapter->promisc;
2497 name = "promisc";
2498 break;
2499 default:
2500 dev_err(dev, "Got invalid cap request rsp %d\n",
2501 crq->request_capability.capability);
2502 return;
2503 }
2504
2505 switch (crq->request_capability_rsp.rc.code) {
2506 case SUCCESS:
2507 break;
2508 case PARTIALSUCCESS:
2509 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2510 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002511 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002512 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002513 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002514 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002515 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002516 return;
2517 default:
2518 dev_err(dev, "Error %d in request cap rsp\n",
2519 crq->request_capability_rsp.rc.code);
2520 return;
2521 }
2522
2523 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002524 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002525 union ibmvnic_crq newcrq;
2526 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2527 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2528 &adapter->ip_offload_buf;
2529
Thomas Falcon249168a2017-02-15 12:18:00 -06002530 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002531 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2532 buf_sz,
2533 DMA_FROM_DEVICE);
2534
2535 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2536 if (!firmware_has_feature(FW_FEATURE_CMO))
2537 dev_err(dev, "Couldn't map offload buffer\n");
2538 return;
2539 }
2540
2541 memset(&newcrq, 0, sizeof(newcrq));
2542 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2543 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2544 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2545 newcrq.query_ip_offload.ioba =
2546 cpu_to_be32(adapter->ip_offload_tok);
2547
2548 ibmvnic_send_crq(adapter, &newcrq);
2549 }
2550}
2551
2552static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2553 struct ibmvnic_adapter *adapter)
2554{
2555 struct device *dev = &adapter->vdev->dev;
2556 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2557 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002558 int i;
2559
2560 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2561 DMA_BIDIRECTIONAL);
2562 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2563 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2564
John Allen498cd8e2016-04-06 11:49:55 -05002565 /* If the number of queues requested can't be allocated by the
2566 * server, the login response will return with code 1. We will need
2567 * to resend the login buffer with fewer queues requested.
2568 */
2569 if (login_rsp_crq->generic.rc.code) {
2570 adapter->renegotiate = true;
2571 complete(&adapter->init_done);
2572 return 0;
2573 }
2574
Thomas Falcon032c5e82015-12-21 11:26:06 -06002575 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2576 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2577 netdev_dbg(adapter->netdev, "%016lx\n",
2578 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2579 }
2580
2581 /* Sanity checks */
2582 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2583 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2584 adapter->req_rx_add_queues !=
2585 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2586 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2587 ibmvnic_remove(adapter->vdev);
2588 return -EIO;
2589 }
2590 complete(&adapter->init_done);
2591
Thomas Falcon032c5e82015-12-21 11:26:06 -06002592 return 0;
2593}
2594
2595static void handle_request_map_rsp(union ibmvnic_crq *crq,
2596 struct ibmvnic_adapter *adapter)
2597{
2598 struct device *dev = &adapter->vdev->dev;
2599 u8 map_id = crq->request_map_rsp.map_id;
2600 int tx_subcrqs;
2601 int rx_subcrqs;
2602 long rc;
2603 int i;
2604
2605 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2606 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2607
2608 rc = crq->request_map_rsp.rc.code;
2609 if (rc) {
2610 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2611 adapter->map_id--;
2612 /* need to find and zero tx/rx_pool map_id */
2613 for (i = 0; i < tx_subcrqs; i++) {
2614 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2615 adapter->tx_pool[i].long_term_buff.map_id = 0;
2616 }
2617 for (i = 0; i < rx_subcrqs; i++) {
2618 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2619 adapter->rx_pool[i].long_term_buff.map_id = 0;
2620 }
2621 }
2622 complete(&adapter->fw_done);
2623}
2624
2625static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2626 struct ibmvnic_adapter *adapter)
2627{
2628 struct device *dev = &adapter->vdev->dev;
2629 long rc;
2630
2631 rc = crq->request_unmap_rsp.rc.code;
2632 if (rc)
2633 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2634}
2635
2636static void handle_query_map_rsp(union ibmvnic_crq *crq,
2637 struct ibmvnic_adapter *adapter)
2638{
2639 struct net_device *netdev = adapter->netdev;
2640 struct device *dev = &adapter->vdev->dev;
2641 long rc;
2642
2643 rc = crq->query_map_rsp.rc.code;
2644 if (rc) {
2645 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2646 return;
2647 }
2648 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2649 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2650 crq->query_map_rsp.free_pages);
2651}
2652
2653static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2654 struct ibmvnic_adapter *adapter)
2655{
2656 struct net_device *netdev = adapter->netdev;
2657 struct device *dev = &adapter->vdev->dev;
2658 long rc;
2659
Thomas Falcon901e0402017-02-15 12:17:59 -06002660 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002661 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002662 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002663 rc = crq->query_capability.rc.code;
2664 if (rc) {
2665 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2666 goto out;
2667 }
2668
2669 switch (be16_to_cpu(crq->query_capability.capability)) {
2670 case MIN_TX_QUEUES:
2671 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002672 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002673 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2674 adapter->min_tx_queues);
2675 break;
2676 case MIN_RX_QUEUES:
2677 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002678 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002679 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2680 adapter->min_rx_queues);
2681 break;
2682 case MIN_RX_ADD_QUEUES:
2683 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002684 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002685 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2686 adapter->min_rx_add_queues);
2687 break;
2688 case MAX_TX_QUEUES:
2689 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002690 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002691 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2692 adapter->max_tx_queues);
2693 break;
2694 case MAX_RX_QUEUES:
2695 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002696 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002697 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2698 adapter->max_rx_queues);
2699 break;
2700 case MAX_RX_ADD_QUEUES:
2701 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002702 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2704 adapter->max_rx_add_queues);
2705 break;
2706 case MIN_TX_ENTRIES_PER_SUBCRQ:
2707 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002708 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002709 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2710 adapter->min_tx_entries_per_subcrq);
2711 break;
2712 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2713 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002714 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2716 adapter->min_rx_add_entries_per_subcrq);
2717 break;
2718 case MAX_TX_ENTRIES_PER_SUBCRQ:
2719 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002720 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002721 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2722 adapter->max_tx_entries_per_subcrq);
2723 break;
2724 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2725 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002726 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2728 adapter->max_rx_add_entries_per_subcrq);
2729 break;
2730 case TCP_IP_OFFLOAD:
2731 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002732 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002733 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2734 adapter->tcp_ip_offload);
2735 break;
2736 case PROMISC_SUPPORTED:
2737 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002738 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002739 netdev_dbg(netdev, "promisc_supported = %lld\n",
2740 adapter->promisc_supported);
2741 break;
2742 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002743 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002744 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002745 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2746 break;
2747 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002748 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002749 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002750 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2751 break;
2752 case MAX_MULTICAST_FILTERS:
2753 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002754 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002755 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2756 adapter->max_multicast_filters);
2757 break;
2758 case VLAN_HEADER_INSERTION:
2759 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002760 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002761 if (adapter->vlan_header_insertion)
2762 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2763 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2764 adapter->vlan_header_insertion);
2765 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002766 case RX_VLAN_HEADER_INSERTION:
2767 adapter->rx_vlan_header_insertion =
2768 be64_to_cpu(crq->query_capability.number);
2769 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
2770 adapter->rx_vlan_header_insertion);
2771 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002772 case MAX_TX_SG_ENTRIES:
2773 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002774 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002775 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2776 adapter->max_tx_sg_entries);
2777 break;
2778 case RX_SG_SUPPORTED:
2779 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002780 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002781 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2782 adapter->rx_sg_supported);
2783 break;
2784 case OPT_TX_COMP_SUB_QUEUES:
2785 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002786 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2788 adapter->opt_tx_comp_sub_queues);
2789 break;
2790 case OPT_RX_COMP_QUEUES:
2791 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002792 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002793 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2794 adapter->opt_rx_comp_queues);
2795 break;
2796 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2797 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002798 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2800 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2801 break;
2802 case OPT_TX_ENTRIES_PER_SUBCRQ:
2803 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002804 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2806 adapter->opt_tx_entries_per_subcrq);
2807 break;
2808 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2809 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002810 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002811 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2812 adapter->opt_rxba_entries_per_subcrq);
2813 break;
2814 case TX_RX_DESC_REQ:
2815 adapter->tx_rx_desc_req = crq->query_capability.number;
2816 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2817 adapter->tx_rx_desc_req);
2818 break;
2819
2820 default:
2821 netdev_err(netdev, "Got invalid cap rsp %d\n",
2822 crq->query_capability.capability);
2823 }
2824
2825out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002826 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2827 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002828 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002829 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002830 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002831}
2832
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002833static void ibmvnic_xport_event(struct work_struct *work)
2834{
2835 struct ibmvnic_adapter *adapter = container_of(work,
2836 struct ibmvnic_adapter,
2837 ibmvnic_xport);
2838 struct device *dev = &adapter->vdev->dev;
2839 long rc;
2840
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002841 release_sub_crqs(adapter);
2842 if (adapter->migrated) {
2843 rc = ibmvnic_reenable_crq_queue(adapter);
2844 if (rc)
2845 dev_err(dev, "Error after enable rc=%ld\n", rc);
2846 adapter->migrated = false;
2847 rc = ibmvnic_send_crq_init(adapter);
2848 if (rc)
2849 dev_err(dev, "Error sending init rc=%ld\n", rc);
2850 }
2851}
2852
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2854 struct ibmvnic_adapter *adapter)
2855{
2856 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2857 struct net_device *netdev = adapter->netdev;
2858 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002859 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002860 long rc;
2861
2862 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002863 (unsigned long int)cpu_to_be64(u64_crq[0]),
2864 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002865 switch (gen_crq->first) {
2866 case IBMVNIC_CRQ_INIT_RSP:
2867 switch (gen_crq->cmd) {
2868 case IBMVNIC_CRQ_INIT:
2869 dev_info(dev, "Partner initialized\n");
2870 /* Send back a response */
2871 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002872 if (!rc)
2873 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002874 else
2875 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2876 break;
2877 case IBMVNIC_CRQ_INIT_COMPLETE:
2878 dev_info(dev, "Partner initialization complete\n");
2879 send_version_xchg(adapter);
2880 break;
2881 default:
2882 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2883 }
2884 return;
2885 case IBMVNIC_CRQ_XPORT_EVENT:
2886 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2887 dev_info(dev, "Re-enabling adapter\n");
2888 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002889 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002890 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2891 dev_info(dev, "Backing device failover detected\n");
2892 netif_carrier_off(netdev);
2893 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002894 } else {
2895 /* The adapter lost the connection */
2896 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2897 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002898 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002899 }
2900 return;
2901 case IBMVNIC_CRQ_CMD_RSP:
2902 break;
2903 default:
2904 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2905 gen_crq->first);
2906 return;
2907 }
2908
2909 switch (gen_crq->cmd) {
2910 case VERSION_EXCHANGE_RSP:
2911 rc = crq->version_exchange_rsp.rc.code;
2912 if (rc) {
2913 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2914 break;
2915 }
2916 dev_info(dev, "Partner protocol version is %d\n",
2917 crq->version_exchange_rsp.version);
2918 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2919 ibmvnic_version)
2920 ibmvnic_version =
2921 be16_to_cpu(crq->version_exchange_rsp.version);
2922 send_cap_queries(adapter);
2923 break;
2924 case QUERY_CAPABILITY_RSP:
2925 handle_query_cap_rsp(crq, adapter);
2926 break;
2927 case QUERY_MAP_RSP:
2928 handle_query_map_rsp(crq, adapter);
2929 break;
2930 case REQUEST_MAP_RSP:
2931 handle_request_map_rsp(crq, adapter);
2932 break;
2933 case REQUEST_UNMAP_RSP:
2934 handle_request_unmap_rsp(crq, adapter);
2935 break;
2936 case REQUEST_CAPABILITY_RSP:
2937 handle_request_cap_rsp(crq, adapter);
2938 break;
2939 case LOGIN_RSP:
2940 netdev_dbg(netdev, "Got Login Response\n");
2941 handle_login_rsp(crq, adapter);
2942 break;
2943 case LOGICAL_LINK_STATE_RSP:
2944 netdev_dbg(netdev, "Got Logical Link State Response\n");
2945 adapter->logical_link_state =
2946 crq->logical_link_state_rsp.link_state;
2947 break;
2948 case LINK_STATE_INDICATION:
2949 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2950 adapter->phys_link_state =
2951 crq->link_state_indication.phys_link_state;
2952 adapter->logical_link_state =
2953 crq->link_state_indication.logical_link_state;
2954 break;
2955 case CHANGE_MAC_ADDR_RSP:
2956 netdev_dbg(netdev, "Got MAC address change Response\n");
2957 handle_change_mac_rsp(crq, adapter);
2958 break;
2959 case ERROR_INDICATION:
2960 netdev_dbg(netdev, "Got Error Indication\n");
2961 handle_error_indication(crq, adapter);
2962 break;
2963 case REQUEST_ERROR_RSP:
2964 netdev_dbg(netdev, "Got Error Detail Response\n");
2965 handle_error_info_rsp(crq, adapter);
2966 break;
2967 case REQUEST_STATISTICS_RSP:
2968 netdev_dbg(netdev, "Got Statistics Response\n");
2969 complete(&adapter->stats_done);
2970 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002971 case QUERY_IP_OFFLOAD_RSP:
2972 netdev_dbg(netdev, "Got Query IP offload Response\n");
2973 handle_query_ip_offload_rsp(adapter);
2974 break;
2975 case MULTICAST_CTRL_RSP:
2976 netdev_dbg(netdev, "Got multicast control Response\n");
2977 break;
2978 case CONTROL_IP_OFFLOAD_RSP:
2979 netdev_dbg(netdev, "Got Control IP offload Response\n");
2980 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
2981 sizeof(adapter->ip_offload_ctrl),
2982 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05002983 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002984 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002985 case COLLECT_FW_TRACE_RSP:
2986 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
2987 complete(&adapter->fw_done);
2988 break;
2989 default:
2990 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
2991 gen_crq->cmd);
2992 }
2993}
2994
2995static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
2996{
2997 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06002998
Thomas Falcon6c267b32017-02-15 12:17:58 -06002999 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003000 return IRQ_HANDLED;
3001}
3002
3003static void ibmvnic_tasklet(void *data)
3004{
3005 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003006 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003007 union ibmvnic_crq *crq;
3008 unsigned long flags;
3009 bool done = false;
3010
3011 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003012 while (!done) {
3013 /* Pull all the valid messages off the CRQ */
3014 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3015 ibmvnic_handle_crq(crq, adapter);
3016 crq->generic.first = 0;
3017 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003018
3019 /* remain in tasklet until all
3020 * capabilities responses are received
3021 */
3022 if (!adapter->wait_capability)
3023 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003024 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003025 /* if capabilities CRQ's were sent in this tasklet, the following
3026 * tasklet must wait until all responses are received
3027 */
3028 if (atomic_read(&adapter->running_cap_crqs) != 0)
3029 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003030 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003031}
3032
3033static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3034{
3035 struct vio_dev *vdev = adapter->vdev;
3036 int rc;
3037
3038 do {
3039 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3040 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3041
3042 if (rc)
3043 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3044
3045 return rc;
3046}
3047
3048static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3049{
3050 struct ibmvnic_crq_queue *crq = &adapter->crq;
3051 struct device *dev = &adapter->vdev->dev;
3052 struct vio_dev *vdev = adapter->vdev;
3053 int rc;
3054
3055 /* Close the CRQ */
3056 do {
3057 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3058 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3059
3060 /* Clean out the queue */
3061 memset(crq->msgs, 0, PAGE_SIZE);
3062 crq->cur = 0;
3063
3064 /* And re-open it again */
3065 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3066 crq->msg_token, PAGE_SIZE);
3067
3068 if (rc == H_CLOSED)
3069 /* Adapter is good, but other end is not ready */
3070 dev_warn(dev, "Partner adapter not ready\n");
3071 else if (rc != 0)
3072 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3073
3074 return rc;
3075}
3076
Nathan Fontenotf9928872017-03-30 02:48:54 -04003077static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003078{
3079 struct ibmvnic_crq_queue *crq = &adapter->crq;
3080 struct vio_dev *vdev = adapter->vdev;
3081 long rc;
3082
Nathan Fontenotf9928872017-03-30 02:48:54 -04003083 if (!crq->msgs)
3084 return;
3085
Thomas Falcon032c5e82015-12-21 11:26:06 -06003086 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3087 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003088 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003089 do {
3090 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3091 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3092
3093 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3094 DMA_BIDIRECTIONAL);
3095 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003096 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003097}
3098
Nathan Fontenotf9928872017-03-30 02:48:54 -04003099static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003100{
3101 struct ibmvnic_crq_queue *crq = &adapter->crq;
3102 struct device *dev = &adapter->vdev->dev;
3103 struct vio_dev *vdev = adapter->vdev;
3104 int rc, retrc = -ENOMEM;
3105
Nathan Fontenotf9928872017-03-30 02:48:54 -04003106 if (crq->msgs)
3107 return 0;
3108
Thomas Falcon032c5e82015-12-21 11:26:06 -06003109 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3110 /* Should we allocate more than one page? */
3111
3112 if (!crq->msgs)
3113 return -ENOMEM;
3114
3115 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3116 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3117 DMA_BIDIRECTIONAL);
3118 if (dma_mapping_error(dev, crq->msg_token))
3119 goto map_failed;
3120
3121 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3122 crq->msg_token, PAGE_SIZE);
3123
3124 if (rc == H_RESOURCE)
3125 /* maybe kexecing and resource is busy. try a reset */
3126 rc = ibmvnic_reset_crq(adapter);
3127 retrc = rc;
3128
3129 if (rc == H_CLOSED) {
3130 dev_warn(dev, "Partner adapter not ready\n");
3131 } else if (rc) {
3132 dev_warn(dev, "Error %d opening adapter\n", rc);
3133 goto reg_crq_failed;
3134 }
3135
3136 retrc = 0;
3137
Thomas Falcon6c267b32017-02-15 12:17:58 -06003138 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3139 (unsigned long)adapter);
3140
Thomas Falcon032c5e82015-12-21 11:26:06 -06003141 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3142 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3143 adapter);
3144 if (rc) {
3145 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3146 vdev->irq, rc);
3147 goto req_irq_failed;
3148 }
3149
3150 rc = vio_enable_interrupts(vdev);
3151 if (rc) {
3152 dev_err(dev, "Error %d enabling interrupts\n", rc);
3153 goto req_irq_failed;
3154 }
3155
3156 crq->cur = 0;
3157 spin_lock_init(&crq->lock);
3158
3159 return retrc;
3160
3161req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003162 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003163 do {
3164 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3165 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3166reg_crq_failed:
3167 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3168map_failed:
3169 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003170 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003171 return retrc;
3172}
3173
Thomas Falcon65dc6892016-07-06 15:35:18 -05003174static void handle_crq_init_rsp(struct work_struct *work)
3175{
3176 struct ibmvnic_adapter *adapter = container_of(work,
3177 struct ibmvnic_adapter,
3178 vnic_crq_init);
3179 struct device *dev = &adapter->vdev->dev;
3180 struct net_device *netdev = adapter->netdev;
3181 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003182 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003183 int rc;
3184
Thomas Falcondfad09a2016-08-18 11:37:51 -05003185 if (adapter->failover) {
3186 release_sub_crqs(adapter);
3187 if (netif_running(netdev)) {
3188 netif_tx_disable(netdev);
3189 ibmvnic_close(netdev);
3190 restart = true;
3191 }
3192 }
3193
Thomas Falcon65dc6892016-07-06 15:35:18 -05003194 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003195 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003196 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3197 dev_err(dev, "Passive init timeout\n");
3198 goto task_failed;
3199 }
3200
Thomas Falconf39f0d12017-02-14 10:22:59 -06003201 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003202
Thomas Falcondfad09a2016-08-18 11:37:51 -05003203 if (adapter->failover) {
3204 adapter->failover = false;
3205 if (restart) {
3206 rc = ibmvnic_open(netdev);
3207 if (rc)
3208 goto restart_failed;
3209 }
3210 netif_carrier_on(netdev);
3211 return;
3212 }
3213
Thomas Falcon65dc6892016-07-06 15:35:18 -05003214 rc = register_netdev(netdev);
3215 if (rc) {
3216 dev_err(dev,
3217 "failed to register netdev rc=%d\n", rc);
3218 goto register_failed;
3219 }
3220 dev_info(dev, "ibmvnic registered\n");
3221
3222 return;
3223
Thomas Falcondfad09a2016-08-18 11:37:51 -05003224restart_failed:
3225 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003226register_failed:
3227 release_sub_crqs(adapter);
3228task_failed:
3229 dev_err(dev, "Passive initialization was not successful\n");
3230}
3231
John Allenf6ef6402017-03-17 17:13:42 -05003232static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3233{
3234 struct device *dev = &adapter->vdev->dev;
3235 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003236 int rc;
3237
Nathan Fontenotf9928872017-03-30 02:48:54 -04003238 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003239 if (rc) {
3240 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3241 return rc;
3242 }
3243
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -04003244 rc = init_stats_token(adapter);
3245 if (rc) {
Nathan Fontenotf9928872017-03-30 02:48:54 -04003246 release_crq_queue(adapter);
Nathan Fontenot7bbc27a2017-03-30 02:49:23 -04003247 return rc;
John Allenf6ef6402017-03-17 17:13:42 -05003248 }
3249
John Allenf6ef6402017-03-17 17:13:42 -05003250 init_completion(&adapter->init_done);
3251 ibmvnic_send_crq_init(adapter);
3252 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3253 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003254 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003255 return -1;
3256 }
3257
3258 return 0;
3259}
3260
Thomas Falcon032c5e82015-12-21 11:26:06 -06003261static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3262{
3263 struct ibmvnic_adapter *adapter;
3264 struct net_device *netdev;
3265 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003266 int rc;
3267
3268 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3269 dev->unit_address);
3270
3271 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3272 VETH_MAC_ADDR, NULL);
3273 if (!mac_addr_p) {
3274 dev_err(&dev->dev,
3275 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3276 __FILE__, __LINE__);
3277 return 0;
3278 }
3279
3280 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3281 IBMVNIC_MAX_TX_QUEUES);
3282 if (!netdev)
3283 return -ENOMEM;
3284
3285 adapter = netdev_priv(netdev);
3286 dev_set_drvdata(&dev->dev, netdev);
3287 adapter->vdev = dev;
3288 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003289 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003290
3291 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3292 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3293 netdev->irq = dev->irq;
3294 netdev->netdev_ops = &ibmvnic_netdev_ops;
3295 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3296 SET_NETDEV_DEV(netdev, &dev->dev);
3297
Thomas Falcon65dc6892016-07-06 15:35:18 -05003298 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003299 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003300
Thomas Falcon032c5e82015-12-21 11:26:06 -06003301 spin_lock_init(&adapter->stats_lock);
3302
Thomas Falcon032c5e82015-12-21 11:26:06 -06003303 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003305
John Allenf6ef6402017-03-17 17:13:42 -05003306 rc = ibmvnic_init(adapter);
3307 if (rc) {
3308 free_netdev(netdev);
3309 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003310 }
3311
Thomas Falconf39f0d12017-02-14 10:22:59 -06003312 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003313 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003314
3315 rc = register_netdev(netdev);
3316 if (rc) {
3317 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003318 free_netdev(netdev);
3319 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003320 }
3321 dev_info(&dev->dev, "ibmvnic registered\n");
3322
3323 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003324}
3325
3326static int ibmvnic_remove(struct vio_dev *dev)
3327{
3328 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003329 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003330
3331 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003332
3333 release_resources(adapter);
3334 release_sub_crqs(adapter);
3335 release_crq_queue(adapter);
3336
Thomas Falcon032c5e82015-12-21 11:26:06 -06003337 free_netdev(netdev);
3338 dev_set_drvdata(&dev->dev, NULL);
3339
3340 return 0;
3341}
3342
3343static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3344{
3345 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3346 struct ibmvnic_adapter *adapter;
3347 struct iommu_table *tbl;
3348 unsigned long ret = 0;
3349 int i;
3350
3351 tbl = get_iommu_table_base(&vdev->dev);
3352
3353 /* netdev inits at probe time along with the structures we need below*/
3354 if (!netdev)
3355 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3356
3357 adapter = netdev_priv(netdev);
3358
3359 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003360 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3361
3362 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3363 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3364
3365 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3366 i++)
3367 ret += adapter->rx_pool[i].size *
3368 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3369
3370 return ret;
3371}
3372
3373static int ibmvnic_resume(struct device *dev)
3374{
3375 struct net_device *netdev = dev_get_drvdata(dev);
3376 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3377 int i;
3378
3379 /* kick the interrupt handlers just in case we lost an interrupt */
3380 for (i = 0; i < adapter->req_rx_queues; i++)
3381 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3382 adapter->rx_scrq[i]);
3383
3384 return 0;
3385}
3386
3387static struct vio_device_id ibmvnic_device_table[] = {
3388 {"network", "IBM,vnic"},
3389 {"", "" }
3390};
3391MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3392
3393static const struct dev_pm_ops ibmvnic_pm_ops = {
3394 .resume = ibmvnic_resume
3395};
3396
3397static struct vio_driver ibmvnic_driver = {
3398 .id_table = ibmvnic_device_table,
3399 .probe = ibmvnic_probe,
3400 .remove = ibmvnic_remove,
3401 .get_desired_dma = ibmvnic_get_desired_dma,
3402 .name = ibmvnic_driver_name,
3403 .pm = &ibmvnic_pm_ops,
3404};
3405
3406/* module functions */
3407static int __init ibmvnic_module_init(void)
3408{
3409 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3410 IBMVNIC_DRIVER_VERSION);
3411
3412 return vio_register_driver(&ibmvnic_driver);
3413}
3414
3415static void __exit ibmvnic_module_exit(void)
3416{
3417 vio_unregister_driver(&ibmvnic_driver);
3418}
3419
3420module_init(ibmvnic_module_init);
3421module_exit(ibmvnic_module_exit);