blob: c2e260ce45bbac1b0da8d8fb7c3bc70118d8b16f [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
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400564static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
565{
566 struct net_device *netdev = adapter->netdev;
567 unsigned long timeout = msecs_to_jiffies(30000);
568 union ibmvnic_crq crq;
569 bool resend;
570 int rc;
571
572 if (adapter->logical_link_state == link_state) {
573 netdev_dbg(netdev, "Link state already %d\n", link_state);
574 return 0;
575 }
576
577 netdev_err(netdev, "setting link state %d\n", link_state);
578 memset(&crq, 0, sizeof(crq));
579 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
580 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
581 crq.logical_link_state.link_state = link_state;
582
583 do {
584 resend = false;
585
586 reinit_completion(&adapter->init_done);
587 rc = ibmvnic_send_crq(adapter, &crq);
588 if (rc) {
589 netdev_err(netdev, "Failed to set link state\n");
590 return rc;
591 }
592
593 if (!wait_for_completion_timeout(&adapter->init_done,
594 timeout)) {
595 netdev_err(netdev, "timeout setting link state\n");
596 return -1;
597 }
598
599 if (adapter->init_done_rc == 1) {
600 /* Partuial success, delay and re-send */
601 mdelay(1000);
602 resend = true;
603 }
604 } while (resend);
605
606 return 0;
607}
608
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400609static int set_real_num_queues(struct net_device *netdev)
610{
611 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
612 int rc;
613
614 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
615 if (rc) {
616 netdev_err(netdev, "failed to set the number of tx queues\n");
617 return rc;
618 }
619
620 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
621 if (rc)
622 netdev_err(netdev, "failed to set the number of rx queues\n");
623
624 return rc;
625}
626
John Allena57a5d22017-03-17 17:13:41 -0500627static int ibmvnic_open(struct net_device *netdev)
628{
629 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
630 struct device *dev = &adapter->vdev->dev;
John Allena57a5d22017-03-17 17:13:41 -0500631 int rc = 0;
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400632 int i;
John Allena57a5d22017-03-17 17:13:41 -0500633
John Allenea5509f2017-03-17 17:13:43 -0500634 if (adapter->is_closed) {
635 rc = ibmvnic_init(adapter);
636 if (rc)
637 return rc;
638 }
639
John Allena57a5d22017-03-17 17:13:41 -0500640 rc = ibmvnic_login(netdev);
641 if (rc)
642 return rc;
643
Thomas Falcon7f3c6e62017-04-21 15:38:40 -0400644 rc = set_real_num_queues(netdev);
645 if (rc)
646 return rc;
John Allenbd0b6722017-03-17 17:13:40 -0500647
648 rc = init_sub_crq_irqs(adapter);
649 if (rc) {
650 dev_err(dev, "failed to initialize sub crq irqs\n");
651 return -1;
652 }
653
Nathan Fontenot5d5e84e2017-04-21 15:38:58 -0400654 rc = init_stats_token(adapter);
655 if (rc)
656 return rc;
657
Thomas Falcon032c5e82015-12-21 11:26:06 -0600658 adapter->map_id = 1;
659 adapter->napi = kcalloc(adapter->req_rx_queues,
660 sizeof(struct napi_struct), GFP_KERNEL);
661 if (!adapter->napi)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400662 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600663 for (i = 0; i < adapter->req_rx_queues; i++) {
664 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
665 NAPI_POLL_WEIGHT);
666 napi_enable(&adapter->napi[i]);
667 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600668
Thomas Falcon032c5e82015-12-21 11:26:06 -0600669 send_map_query(adapter);
Nathan Fontenot0ffe2cb2017-03-30 02:49:12 -0400670
671 rc = init_rx_pools(netdev);
672 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400673 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600674
Nathan Fontenotc657e322017-03-30 02:49:06 -0400675 rc = init_tx_pools(netdev);
676 if (rc)
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400677 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600678
Thomas Falcon032c5e82015-12-21 11:26:06 -0600679 replenish_pools(adapter);
680
681 /* We're ready to receive frames, enable the sub-crq interrupts and
682 * set the logical link state to up
683 */
684 for (i = 0; i < adapter->req_rx_queues; i++)
685 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
686
687 for (i = 0; i < adapter->req_tx_queues; i++)
688 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
689
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400690 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
691 if (rc)
692 goto ibmvnic_open_fail;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600693
Thomas Falconb8efb892016-07-06 15:35:15 -0500694 netif_tx_start_all_queues(netdev);
John Allenea5509f2017-03-17 17:13:43 -0500695 adapter->is_closed = false;
Thomas Falconb8efb892016-07-06 15:35:15 -0500696
Thomas Falcon032c5e82015-12-21 11:26:06 -0600697 return 0;
698
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400699ibmvnic_open_fail:
Thomas Falcon032c5e82015-12-21 11:26:06 -0600700 for (i = 0; i < adapter->req_rx_queues; i++)
Nathan Fontenote722af62017-02-10 13:29:06 -0500701 napi_disable(&adapter->napi[i]);
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400702 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600703 return -ENOMEM;
704}
705
Brian Kingdd9c20f2017-04-19 13:45:10 -0400706static void disable_sub_crqs(struct ibmvnic_adapter *adapter)
707{
708 int i;
709
710 if (adapter->tx_scrq) {
711 for (i = 0; i < adapter->req_tx_queues; i++)
712 if (adapter->tx_scrq[i])
713 disable_irq(adapter->tx_scrq[i]->irq);
714 }
715
716 if (adapter->rx_scrq) {
717 for (i = 0; i < adapter->req_rx_queues; i++)
718 if (adapter->rx_scrq[i])
719 disable_irq(adapter->rx_scrq[i]->irq);
720 }
721}
722
John Allenea5509f2017-03-17 17:13:43 -0500723static int ibmvnic_close(struct net_device *netdev)
724{
725 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400726 int rc = 0;
John Allenea5509f2017-03-17 17:13:43 -0500727 int i;
728
729 adapter->closing = true;
Brian Kingdd9c20f2017-04-19 13:45:10 -0400730 disable_sub_crqs(adapter);
John Allenea5509f2017-03-17 17:13:43 -0500731
Nathan Fontenot3ca19932017-04-21 15:39:10 -0400732 if (adapter->napi) {
733 for (i = 0; i < adapter->req_rx_queues; i++)
734 napi_disable(&adapter->napi[i]);
735 }
John Allenea5509f2017-03-17 17:13:43 -0500736
737 if (!adapter->failover)
738 netif_tx_stop_all_queues(netdev);
739
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400740 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600741
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400742 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600743
John Allenea5509f2017-03-17 17:13:43 -0500744 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600745 adapter->closing = false;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400746 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600747}
748
Thomas Falconad7775d2016-04-01 17:20:34 -0500749/**
750 * build_hdr_data - creates L2/L3/L4 header data buffer
751 * @hdr_field - bitfield determining needed headers
752 * @skb - socket buffer
753 * @hdr_len - array of header lengths
754 * @tot_len - total length of data
755 *
756 * Reads hdr_field to determine which headers are needed by firmware.
757 * Builds a buffer containing these headers. Saves individual header
758 * lengths and total buffer length to be used to build descriptors.
759 */
760static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
761 int *hdr_len, u8 *hdr_data)
762{
763 int len = 0;
764 u8 *hdr;
765
766 hdr_len[0] = sizeof(struct ethhdr);
767
768 if (skb->protocol == htons(ETH_P_IP)) {
769 hdr_len[1] = ip_hdr(skb)->ihl * 4;
770 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
771 hdr_len[2] = tcp_hdrlen(skb);
772 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
773 hdr_len[2] = sizeof(struct udphdr);
774 } else if (skb->protocol == htons(ETH_P_IPV6)) {
775 hdr_len[1] = sizeof(struct ipv6hdr);
776 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
777 hdr_len[2] = tcp_hdrlen(skb);
778 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
779 hdr_len[2] = sizeof(struct udphdr);
780 }
781
782 memset(hdr_data, 0, 120);
783 if ((hdr_field >> 6) & 1) {
784 hdr = skb_mac_header(skb);
785 memcpy(hdr_data, hdr, hdr_len[0]);
786 len += hdr_len[0];
787 }
788
789 if ((hdr_field >> 5) & 1) {
790 hdr = skb_network_header(skb);
791 memcpy(hdr_data + len, hdr, hdr_len[1]);
792 len += hdr_len[1];
793 }
794
795 if ((hdr_field >> 4) & 1) {
796 hdr = skb_transport_header(skb);
797 memcpy(hdr_data + len, hdr, hdr_len[2]);
798 len += hdr_len[2];
799 }
800 return len;
801}
802
803/**
804 * create_hdr_descs - create header and header extension descriptors
805 * @hdr_field - bitfield determining needed headers
806 * @data - buffer containing header data
807 * @len - length of data buffer
808 * @hdr_len - array of individual header lengths
809 * @scrq_arr - descriptor array
810 *
811 * Creates header and, if needed, header extension descriptors and
812 * places them in a descriptor array, scrq_arr
813 */
814
815static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
816 union sub_crq *scrq_arr)
817{
818 union sub_crq hdr_desc;
819 int tmp_len = len;
820 u8 *data, *cur;
821 int tmp;
822
823 while (tmp_len > 0) {
824 cur = hdr_data + len - tmp_len;
825
826 memset(&hdr_desc, 0, sizeof(hdr_desc));
827 if (cur != hdr_data) {
828 data = hdr_desc.hdr_ext.data;
829 tmp = tmp_len > 29 ? 29 : tmp_len;
830 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
831 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
832 hdr_desc.hdr_ext.len = tmp;
833 } else {
834 data = hdr_desc.hdr.data;
835 tmp = tmp_len > 24 ? 24 : tmp_len;
836 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
837 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
838 hdr_desc.hdr.len = tmp;
839 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
840 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
841 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
842 hdr_desc.hdr.flag = hdr_field << 1;
843 }
844 memcpy(data, cur, tmp);
845 tmp_len -= tmp;
846 *scrq_arr = hdr_desc;
847 scrq_arr++;
848 }
849}
850
851/**
852 * build_hdr_descs_arr - build a header descriptor array
853 * @skb - socket buffer
854 * @num_entries - number of descriptors to be sent
855 * @subcrq - first TX descriptor
856 * @hdr_field - bit field determining which headers will be sent
857 *
858 * This function will build a TX descriptor array with applicable
859 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
860 */
861
862static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
863 int *num_entries, u8 hdr_field)
864{
865 int hdr_len[3] = {0, 0, 0};
866 int tot_len, len;
867 u8 *hdr_data = txbuff->hdr_data;
868
869 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
870 txbuff->hdr_data);
871 len = tot_len;
872 len -= 24;
873 if (len > 0)
874 num_entries += len % 29 ? len / 29 + 1 : len / 29;
875 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
876 txbuff->indir_arr + 1);
877}
878
Thomas Falcon032c5e82015-12-21 11:26:06 -0600879static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
880{
881 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
882 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500883 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600884 struct device *dev = &adapter->vdev->dev;
885 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600886 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600887 struct ibmvnic_tx_pool *tx_pool;
888 unsigned int tx_send_failed = 0;
889 unsigned int tx_map_failed = 0;
890 unsigned int tx_dropped = 0;
891 unsigned int tx_packets = 0;
892 unsigned int tx_bytes = 0;
893 dma_addr_t data_dma_addr;
894 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600895 unsigned long lpar_rc;
896 union sub_crq tx_crq;
897 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500898 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600899 unsigned char *dst;
900 u64 *handle_array;
901 int index = 0;
902 int ret = 0;
903
904 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600905 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600906 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
907 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
908 be32_to_cpu(adapter->login_rsp_buf->
909 off_txsubm_subcrqs));
910 if (adapter->migrated) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -0400911 if (!netif_subqueue_stopped(netdev, skb))
912 netif_stop_subqueue(netdev, queue_num);
913 dev_kfree_skb_any(skb);
914
Thomas Falcon032c5e82015-12-21 11:26:06 -0600915 tx_send_failed++;
916 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -0400917 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600918 goto out;
919 }
920
921 index = tx_pool->free_map[tx_pool->consumer_index];
922 offset = index * adapter->req_mtu;
923 dst = tx_pool->long_term_buff.buff + offset;
924 memset(dst, 0, adapter->req_mtu);
925 skb_copy_from_linear_data(skb, dst, skb->len);
926 data_dma_addr = tx_pool->long_term_buff.addr + offset;
927
928 tx_pool->consumer_index =
929 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600930 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600931
932 tx_buff = &tx_pool->tx_buff[index];
933 tx_buff->skb = skb;
934 tx_buff->data_dma[0] = data_dma_addr;
935 tx_buff->data_len[0] = skb->len;
936 tx_buff->index = index;
937 tx_buff->pool_index = queue_num;
938 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600939
940 memset(&tx_crq, 0, sizeof(tx_crq));
941 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
942 tx_crq.v1.type = IBMVNIC_TX_DESC;
943 tx_crq.v1.n_crq_elem = 1;
944 tx_crq.v1.n_sge = 1;
945 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
946 tx_crq.v1.correlator = cpu_to_be32(index);
947 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
948 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
949 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
950
951 if (adapter->vlan_header_insertion) {
952 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
953 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
954 }
955
956 if (skb->protocol == htons(ETH_P_IP)) {
957 if (ip_hdr(skb)->version == 4)
958 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
959 else if (ip_hdr(skb)->version == 6)
960 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
961
962 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
963 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
964 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
965 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
966 }
967
Thomas Falconad7775d2016-04-01 17:20:34 -0500968 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600969 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500970 hdrs += 2;
971 }
972 /* determine if l2/3/4 headers are sent to firmware */
973 if ((*hdrs >> 7) & 1 &&
974 (skb->protocol == htons(ETH_P_IP) ||
975 skb->protocol == htons(ETH_P_IPV6))) {
976 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
977 tx_crq.v1.n_crq_elem = num_entries;
978 tx_buff->indir_arr[0] = tx_crq;
979 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
980 sizeof(tx_buff->indir_arr),
981 DMA_TO_DEVICE);
982 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
Thomas Falcon7f5b0302017-04-21 15:39:16 -0400983 dev_kfree_skb_any(skb);
984 tx_buff->skb = NULL;
Thomas Falconad7775d2016-04-01 17:20:34 -0500985 if (!firmware_has_feature(FW_FEATURE_CMO))
986 dev_err(dev, "tx: unable to map descriptor array\n");
987 tx_map_failed++;
988 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -0400989 ret = NETDEV_TX_OK;
Thomas Falconad7775d2016-04-01 17:20:34 -0500990 goto out;
991 }
John Allen498cd8e2016-04-06 11:49:55 -0500992 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500993 (u64)tx_buff->indir_dma,
994 (u64)num_entries);
995 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500996 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
997 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500998 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600999 if (lpar_rc != H_SUCCESS) {
1000 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1001
1002 if (tx_pool->consumer_index == 0)
1003 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -06001004 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001005 else
1006 tx_pool->consumer_index--;
1007
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001008 dev_kfree_skb_any(skb);
1009 tx_buff->skb = NULL;
1010
1011 if (lpar_rc == H_CLOSED)
1012 netif_stop_subqueue(netdev, queue_num);
1013
Thomas Falcon032c5e82015-12-21 11:26:06 -06001014 tx_send_failed++;
1015 tx_dropped++;
Thomas Falcon7f5b0302017-04-21 15:39:16 -04001016 ret = NETDEV_TX_OK;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001017 goto out;
1018 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001019
Brian King58c8c0c2017-04-19 13:44:47 -04001020 if (atomic_inc_return(&tx_scrq->used)
1021 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001022 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1023 netif_stop_subqueue(netdev, queue_num);
1024 }
1025
Thomas Falcon032c5e82015-12-21 11:26:06 -06001026 tx_packets++;
1027 tx_bytes += skb->len;
1028 txq->trans_start = jiffies;
1029 ret = NETDEV_TX_OK;
1030
1031out:
1032 netdev->stats.tx_dropped += tx_dropped;
1033 netdev->stats.tx_bytes += tx_bytes;
1034 netdev->stats.tx_packets += tx_packets;
1035 adapter->tx_send_failed += tx_send_failed;
1036 adapter->tx_map_failed += tx_map_failed;
1037
1038 return ret;
1039}
1040
1041static void ibmvnic_set_multi(struct net_device *netdev)
1042{
1043 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1044 struct netdev_hw_addr *ha;
1045 union ibmvnic_crq crq;
1046
1047 memset(&crq, 0, sizeof(crq));
1048 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1049 crq.request_capability.cmd = REQUEST_CAPABILITY;
1050
1051 if (netdev->flags & IFF_PROMISC) {
1052 if (!adapter->promisc_supported)
1053 return;
1054 } else {
1055 if (netdev->flags & IFF_ALLMULTI) {
1056 /* Accept all multicast */
1057 memset(&crq, 0, sizeof(crq));
1058 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1059 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1060 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1061 ibmvnic_send_crq(adapter, &crq);
1062 } else if (netdev_mc_empty(netdev)) {
1063 /* Reject all multicast */
1064 memset(&crq, 0, sizeof(crq));
1065 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1066 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1067 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1068 ibmvnic_send_crq(adapter, &crq);
1069 } else {
1070 /* Accept one or more multicast(s) */
1071 netdev_for_each_mc_addr(ha, netdev) {
1072 memset(&crq, 0, sizeof(crq));
1073 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1074 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1075 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1076 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1077 ha->addr);
1078 ibmvnic_send_crq(adapter, &crq);
1079 }
1080 }
1081 }
1082}
1083
1084static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1085{
1086 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1087 struct sockaddr *addr = p;
1088 union ibmvnic_crq crq;
1089
1090 if (!is_valid_ether_addr(addr->sa_data))
1091 return -EADDRNOTAVAIL;
1092
1093 memset(&crq, 0, sizeof(crq));
1094 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1095 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1096 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1097 ibmvnic_send_crq(adapter, &crq);
1098 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1099 return 0;
1100}
1101
Thomas Falcon032c5e82015-12-21 11:26:06 -06001102static void ibmvnic_tx_timeout(struct net_device *dev)
1103{
1104 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1105 int rc;
1106
1107 /* Adapter timed out, resetting it */
1108 release_sub_crqs(adapter);
1109 rc = ibmvnic_reset_crq(adapter);
1110 if (rc)
1111 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1112 else
1113 ibmvnic_send_crq_init(adapter);
1114}
1115
1116static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1117 struct ibmvnic_rx_buff *rx_buff)
1118{
1119 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1120
1121 rx_buff->skb = NULL;
1122
1123 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1124 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1125
1126 atomic_dec(&pool->available);
1127}
1128
1129static int ibmvnic_poll(struct napi_struct *napi, int budget)
1130{
1131 struct net_device *netdev = napi->dev;
1132 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1133 int scrq_num = (int)(napi - adapter->napi);
1134 int frames_processed = 0;
1135restart_poll:
1136 while (frames_processed < budget) {
1137 struct sk_buff *skb;
1138 struct ibmvnic_rx_buff *rx_buff;
1139 union sub_crq *next;
1140 u32 length;
1141 u16 offset;
1142 u8 flags = 0;
1143
1144 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1145 break;
1146 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1147 rx_buff =
1148 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1149 rx_comp.correlator);
1150 /* do error checking */
1151 if (next->rx_comp.rc) {
1152 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1153 /* free the entry */
1154 next->rx_comp.first = 0;
1155 remove_buff_from_pool(adapter, rx_buff);
1156 break;
1157 }
1158
1159 length = be32_to_cpu(next->rx_comp.len);
1160 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1161 flags = next->rx_comp.flags;
1162 skb = rx_buff->skb;
1163 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1164 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001165
1166 /* VLAN Header has been stripped by the system firmware and
1167 * needs to be inserted by the driver
1168 */
1169 if (adapter->rx_vlan_header_insertion &&
1170 (flags & IBMVNIC_VLAN_STRIPPED))
1171 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1172 ntohs(next->rx_comp.vlan_tci));
1173
Thomas Falcon032c5e82015-12-21 11:26:06 -06001174 /* free the entry */
1175 next->rx_comp.first = 0;
1176 remove_buff_from_pool(adapter, rx_buff);
1177
1178 skb_put(skb, length);
1179 skb->protocol = eth_type_trans(skb, netdev);
1180
1181 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1182 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1183 skb->ip_summed = CHECKSUM_UNNECESSARY;
1184 }
1185
1186 length = skb->len;
1187 napi_gro_receive(napi, skb); /* send it up */
1188 netdev->stats.rx_packets++;
1189 netdev->stats.rx_bytes += length;
1190 frames_processed++;
1191 }
John Allen498cd8e2016-04-06 11:49:55 -05001192 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001193
1194 if (frames_processed < budget) {
1195 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001196 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001197 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1198 napi_reschedule(napi)) {
1199 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1200 goto restart_poll;
1201 }
1202 }
1203 return frames_processed;
1204}
1205
1206#ifdef CONFIG_NET_POLL_CONTROLLER
1207static void ibmvnic_netpoll_controller(struct net_device *dev)
1208{
1209 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1210 int i;
1211
1212 replenish_pools(netdev_priv(dev));
1213 for (i = 0; i < adapter->req_rx_queues; i++)
1214 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1215 adapter->rx_scrq[i]);
1216}
1217#endif
1218
1219static const struct net_device_ops ibmvnic_netdev_ops = {
1220 .ndo_open = ibmvnic_open,
1221 .ndo_stop = ibmvnic_close,
1222 .ndo_start_xmit = ibmvnic_xmit,
1223 .ndo_set_rx_mode = ibmvnic_set_multi,
1224 .ndo_set_mac_address = ibmvnic_set_mac,
1225 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001226 .ndo_tx_timeout = ibmvnic_tx_timeout,
1227#ifdef CONFIG_NET_POLL_CONTROLLER
1228 .ndo_poll_controller = ibmvnic_netpoll_controller,
1229#endif
1230};
1231
1232/* ethtool functions */
1233
Philippe Reynes8a433792017-01-07 22:37:29 +01001234static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1235 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001236{
Philippe Reynes8a433792017-01-07 22:37:29 +01001237 u32 supported, advertising;
1238
1239 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001240 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001241 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001242 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001243 cmd->base.speed = SPEED_1000;
1244 cmd->base.duplex = DUPLEX_FULL;
1245 cmd->base.port = PORT_FIBRE;
1246 cmd->base.phy_address = 0;
1247 cmd->base.autoneg = AUTONEG_ENABLE;
1248
1249 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1250 supported);
1251 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1252 advertising);
1253
Thomas Falcon032c5e82015-12-21 11:26:06 -06001254 return 0;
1255}
1256
1257static void ibmvnic_get_drvinfo(struct net_device *dev,
1258 struct ethtool_drvinfo *info)
1259{
1260 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1261 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1262}
1263
1264static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1265{
1266 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1267
1268 return adapter->msg_enable;
1269}
1270
1271static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1272{
1273 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1274
1275 adapter->msg_enable = data;
1276}
1277
1278static u32 ibmvnic_get_link(struct net_device *netdev)
1279{
1280 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1281
1282 /* Don't need to send a query because we request a logical link up at
1283 * init and then we wait for link state indications
1284 */
1285 return adapter->logical_link_state;
1286}
1287
1288static void ibmvnic_get_ringparam(struct net_device *netdev,
1289 struct ethtool_ringparam *ring)
1290{
1291 ring->rx_max_pending = 0;
1292 ring->tx_max_pending = 0;
1293 ring->rx_mini_max_pending = 0;
1294 ring->rx_jumbo_max_pending = 0;
1295 ring->rx_pending = 0;
1296 ring->tx_pending = 0;
1297 ring->rx_mini_pending = 0;
1298 ring->rx_jumbo_pending = 0;
1299}
1300
1301static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1302{
1303 int i;
1304
1305 if (stringset != ETH_SS_STATS)
1306 return;
1307
1308 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1309 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1310}
1311
1312static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1313{
1314 switch (sset) {
1315 case ETH_SS_STATS:
1316 return ARRAY_SIZE(ibmvnic_stats);
1317 default:
1318 return -EOPNOTSUPP;
1319 }
1320}
1321
1322static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1323 struct ethtool_stats *stats, u64 *data)
1324{
1325 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1326 union ibmvnic_crq crq;
1327 int i;
1328
1329 memset(&crq, 0, sizeof(crq));
1330 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1331 crq.request_statistics.cmd = REQUEST_STATISTICS;
1332 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1333 crq.request_statistics.len =
1334 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001335
1336 /* Wait for data to be written */
1337 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001338 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001339 wait_for_completion(&adapter->stats_done);
1340
1341 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1342 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1343}
1344
1345static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001346 .get_drvinfo = ibmvnic_get_drvinfo,
1347 .get_msglevel = ibmvnic_get_msglevel,
1348 .set_msglevel = ibmvnic_set_msglevel,
1349 .get_link = ibmvnic_get_link,
1350 .get_ringparam = ibmvnic_get_ringparam,
1351 .get_strings = ibmvnic_get_strings,
1352 .get_sset_count = ibmvnic_get_sset_count,
1353 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001354 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001355};
1356
1357/* Routines for managing CRQs/sCRQs */
1358
1359static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1360 struct ibmvnic_sub_crq_queue *scrq)
1361{
1362 struct device *dev = &adapter->vdev->dev;
1363 long rc;
1364
1365 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1366
1367 /* Close the sub-crqs */
1368 do {
1369 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1370 adapter->vdev->unit_address,
1371 scrq->crq_num);
1372 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1373
Thomas Falconffa73852017-04-19 13:44:29 -04001374 if (rc) {
1375 netdev_err(adapter->netdev,
1376 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1377 scrq->crq_num, rc);
1378 }
1379
Thomas Falcon032c5e82015-12-21 11:26:06 -06001380 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1381 DMA_BIDIRECTIONAL);
1382 free_pages((unsigned long)scrq->msgs, 2);
1383 kfree(scrq);
1384}
1385
1386static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1387 *adapter)
1388{
1389 struct device *dev = &adapter->vdev->dev;
1390 struct ibmvnic_sub_crq_queue *scrq;
1391 int rc;
1392
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001393 scrq = kzalloc(sizeof(*scrq), GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001394 if (!scrq)
1395 return NULL;
1396
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001397 scrq->msgs =
1398 (union sub_crq *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001399 if (!scrq->msgs) {
1400 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1401 goto zero_page_failed;
1402 }
1403
1404 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1405 DMA_BIDIRECTIONAL);
1406 if (dma_mapping_error(dev, scrq->msg_token)) {
1407 dev_warn(dev, "Couldn't map crq queue messages page\n");
1408 goto map_failed;
1409 }
1410
1411 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1412 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1413
1414 if (rc == H_RESOURCE)
1415 rc = ibmvnic_reset_crq(adapter);
1416
1417 if (rc == H_CLOSED) {
1418 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1419 } else if (rc) {
1420 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1421 goto reg_failed;
1422 }
1423
Thomas Falcon032c5e82015-12-21 11:26:06 -06001424 scrq->adapter = adapter;
1425 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001426 spin_lock_init(&scrq->lock);
1427
1428 netdev_dbg(adapter->netdev,
1429 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1430 scrq->crq_num, scrq->hw_irq, scrq->irq);
1431
1432 return scrq;
1433
Thomas Falcon032c5e82015-12-21 11:26:06 -06001434reg_failed:
1435 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1436 DMA_BIDIRECTIONAL);
1437map_failed:
1438 free_pages((unsigned long)scrq->msgs, 2);
1439zero_page_failed:
1440 kfree(scrq);
1441
1442 return NULL;
1443}
1444
1445static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1446{
1447 int i;
1448
1449 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001450 for (i = 0; i < adapter->req_tx_queues; i++) {
1451 if (!adapter->tx_scrq[i])
1452 continue;
1453
1454 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001455 free_irq(adapter->tx_scrq[i]->irq,
1456 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001457 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001458 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001459 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001460
1461 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1462 }
1463
Nathan Fontenot9501df32017-03-15 23:38:07 -04001464 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001465 adapter->tx_scrq = NULL;
1466 }
1467
1468 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001469 for (i = 0; i < adapter->req_rx_queues; i++) {
1470 if (!adapter->rx_scrq[i])
1471 continue;
1472
1473 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001474 free_irq(adapter->rx_scrq[i]->irq,
1475 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001476 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001477 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001478 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001479
1480 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1481 }
1482
Nathan Fontenot9501df32017-03-15 23:38:07 -04001483 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001484 adapter->rx_scrq = NULL;
1485 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001486}
1487
1488static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1489 struct ibmvnic_sub_crq_queue *scrq)
1490{
1491 struct device *dev = &adapter->vdev->dev;
1492 unsigned long rc;
1493
1494 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1495 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1496 if (rc)
1497 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1498 scrq->hw_irq, rc);
1499 return rc;
1500}
1501
1502static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1503 struct ibmvnic_sub_crq_queue *scrq)
1504{
1505 struct device *dev = &adapter->vdev->dev;
1506 unsigned long rc;
1507
1508 if (scrq->hw_irq > 0x100000000ULL) {
1509 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1510 return 1;
1511 }
1512
1513 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1514 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1515 if (rc)
1516 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1517 scrq->hw_irq, rc);
1518 return rc;
1519}
1520
1521static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1522 struct ibmvnic_sub_crq_queue *scrq)
1523{
1524 struct device *dev = &adapter->vdev->dev;
1525 struct ibmvnic_tx_buff *txbuff;
1526 union sub_crq *next;
1527 int index;
1528 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001529 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001530
1531restart_loop:
1532 while (pending_scrq(adapter, scrq)) {
1533 unsigned int pool = scrq->pool_index;
1534
1535 next = ibmvnic_next_scrq(adapter, scrq);
1536 for (i = 0; i < next->tx_comp.num_comps; i++) {
1537 if (next->tx_comp.rcs[i]) {
1538 dev_err(dev, "tx error %x\n",
1539 next->tx_comp.rcs[i]);
1540 continue;
1541 }
1542 index = be32_to_cpu(next->tx_comp.correlators[i]);
1543 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1544
1545 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1546 if (!txbuff->data_dma[j])
1547 continue;
1548
1549 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001550 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001551 /* if sub_crq was sent indirectly */
1552 first = txbuff->indir_arr[0].generic.first;
1553 if (first == IBMVNIC_CRQ_CMD) {
1554 dma_unmap_single(dev, txbuff->indir_dma,
1555 sizeof(txbuff->indir_arr),
1556 DMA_TO_DEVICE);
1557 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001558
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001559 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001560 if (atomic_sub_return(next->tx_comp.num_comps,
1561 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001562 (adapter->req_tx_entries_per_subcrq / 2) &&
1563 netif_subqueue_stopped(adapter->netdev,
1564 txbuff->skb)) {
1565 netif_wake_subqueue(adapter->netdev,
1566 scrq->pool_index);
1567 netdev_dbg(adapter->netdev,
1568 "Started queue %d\n",
1569 scrq->pool_index);
1570 }
1571
Thomas Falcon032c5e82015-12-21 11:26:06 -06001572 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001573 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001574
1575 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1576 producer_index] = index;
1577 adapter->tx_pool[pool].producer_index =
1578 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001579 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001580 }
1581 /* remove tx_comp scrq*/
1582 next->tx_comp.first = 0;
1583 }
1584
1585 enable_scrq_irq(adapter, scrq);
1586
1587 if (pending_scrq(adapter, scrq)) {
1588 disable_scrq_irq(adapter, scrq);
1589 goto restart_loop;
1590 }
1591
1592 return 0;
1593}
1594
1595static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1596{
1597 struct ibmvnic_sub_crq_queue *scrq = instance;
1598 struct ibmvnic_adapter *adapter = scrq->adapter;
1599
1600 disable_scrq_irq(adapter, scrq);
1601 ibmvnic_complete_tx(adapter, scrq);
1602
1603 return IRQ_HANDLED;
1604}
1605
1606static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1607{
1608 struct ibmvnic_sub_crq_queue *scrq = instance;
1609 struct ibmvnic_adapter *adapter = scrq->adapter;
1610
1611 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1612 disable_scrq_irq(adapter, scrq);
1613 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1614 }
1615
1616 return IRQ_HANDLED;
1617}
1618
Thomas Falconea22d512016-07-06 15:35:17 -05001619static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1620{
1621 struct device *dev = &adapter->vdev->dev;
1622 struct ibmvnic_sub_crq_queue *scrq;
1623 int i = 0, j = 0;
1624 int rc = 0;
1625
1626 for (i = 0; i < adapter->req_tx_queues; i++) {
1627 scrq = adapter->tx_scrq[i];
1628 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1629
Michael Ellerman99c17902016-09-10 19:59:05 +10001630 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001631 rc = -EINVAL;
1632 dev_err(dev, "Error mapping irq\n");
1633 goto req_tx_irq_failed;
1634 }
1635
1636 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1637 0, "ibmvnic_tx", scrq);
1638
1639 if (rc) {
1640 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1641 scrq->irq, rc);
1642 irq_dispose_mapping(scrq->irq);
1643 goto req_rx_irq_failed;
1644 }
1645 }
1646
1647 for (i = 0; i < adapter->req_rx_queues; i++) {
1648 scrq = adapter->rx_scrq[i];
1649 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001650 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001651 rc = -EINVAL;
1652 dev_err(dev, "Error mapping irq\n");
1653 goto req_rx_irq_failed;
1654 }
1655 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1656 0, "ibmvnic_rx", scrq);
1657 if (rc) {
1658 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1659 scrq->irq, rc);
1660 irq_dispose_mapping(scrq->irq);
1661 goto req_rx_irq_failed;
1662 }
1663 }
1664 return rc;
1665
1666req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001667 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001668 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1669 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001670 }
Thomas Falconea22d512016-07-06 15:35:17 -05001671 i = adapter->req_tx_queues;
1672req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001673 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001674 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1675 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001676 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001677 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001678 return rc;
1679}
1680
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001681static int init_sub_crqs(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001682{
1683 struct device *dev = &adapter->vdev->dev;
1684 struct ibmvnic_sub_crq_queue **allqueues;
1685 int registered_queues = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001686 int total_queues;
1687 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001688 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001689
Thomas Falcon032c5e82015-12-21 11:26:06 -06001690 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1691
1692 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1693 if (!allqueues)
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001694 return -1;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001695
1696 for (i = 0; i < total_queues; i++) {
1697 allqueues[i] = init_sub_crq_queue(adapter);
1698 if (!allqueues[i]) {
1699 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1700 break;
1701 }
1702 registered_queues++;
1703 }
1704
1705 /* Make sure we were able to register the minimum number of queues */
1706 if (registered_queues <
1707 adapter->min_tx_queues + adapter->min_rx_queues) {
1708 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1709 goto tx_failed;
1710 }
1711
1712 /* Distribute the failed allocated queues*/
1713 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1714 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1715 switch (i % 3) {
1716 case 0:
1717 if (adapter->req_rx_queues > adapter->min_rx_queues)
1718 adapter->req_rx_queues--;
1719 else
1720 more++;
1721 break;
1722 case 1:
1723 if (adapter->req_tx_queues > adapter->min_tx_queues)
1724 adapter->req_tx_queues--;
1725 else
1726 more++;
1727 break;
1728 }
1729 }
1730
1731 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1732 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1733 if (!adapter->tx_scrq)
1734 goto tx_failed;
1735
1736 for (i = 0; i < adapter->req_tx_queues; i++) {
1737 adapter->tx_scrq[i] = allqueues[i];
1738 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001739 }
1740
1741 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1742 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1743 if (!adapter->rx_scrq)
1744 goto rx_failed;
1745
1746 for (i = 0; i < adapter->req_rx_queues; i++) {
1747 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1748 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001749 }
1750
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04001751 kfree(allqueues);
1752 return 0;
1753
1754rx_failed:
1755 kfree(adapter->tx_scrq);
1756 adapter->tx_scrq = NULL;
1757tx_failed:
1758 for (i = 0; i < registered_queues; i++)
1759 release_sub_crq_queue(adapter, allqueues[i]);
1760 kfree(allqueues);
1761 return -1;
1762}
1763
1764static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
1765{
1766 struct device *dev = &adapter->vdev->dev;
1767 union ibmvnic_crq crq;
1768 int rc;
1769
1770 if (!retry) {
1771 /* Sub-CRQ entries are 32 byte long */
1772 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1773
1774 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1775 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1776 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1777 return;
1778 }
1779
1780 /* Get the minimum between the queried max and the entries
1781 * that fit in our PAGE_SIZE
1782 */
1783 adapter->req_tx_entries_per_subcrq =
1784 adapter->max_tx_entries_per_subcrq > entries_page ?
1785 entries_page : adapter->max_tx_entries_per_subcrq;
1786 adapter->req_rx_add_entries_per_subcrq =
1787 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1788 entries_page : adapter->max_rx_add_entries_per_subcrq;
1789
1790 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1791 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
1792 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
1793
1794 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
1795 }
1796
1797 rc = init_sub_crqs(adapter);
1798 if (rc)
1799 return;
1800
Thomas Falcon032c5e82015-12-21 11:26:06 -06001801 memset(&crq, 0, sizeof(crq));
1802 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1803 crq.request_capability.cmd = REQUEST_CAPABILITY;
1804
1805 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001806 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001807 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001808 ibmvnic_send_crq(adapter, &crq);
1809
1810 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001811 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001812 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001813 ibmvnic_send_crq(adapter, &crq);
1814
1815 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001816 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001817 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001818 ibmvnic_send_crq(adapter, &crq);
1819
1820 crq.request_capability.capability =
1821 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1822 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001823 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001824 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001825 ibmvnic_send_crq(adapter, &crq);
1826
1827 crq.request_capability.capability =
1828 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1829 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001830 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001831 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001832 ibmvnic_send_crq(adapter, &crq);
1833
1834 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001835 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001836 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001837 ibmvnic_send_crq(adapter, &crq);
1838
1839 if (adapter->netdev->flags & IFF_PROMISC) {
1840 if (adapter->promisc_supported) {
1841 crq.request_capability.capability =
1842 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001843 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001844 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001845 ibmvnic_send_crq(adapter, &crq);
1846 }
1847 } else {
1848 crq.request_capability.capability =
1849 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001850 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001851 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001852 ibmvnic_send_crq(adapter, &crq);
1853 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001854}
1855
1856static int pending_scrq(struct ibmvnic_adapter *adapter,
1857 struct ibmvnic_sub_crq_queue *scrq)
1858{
1859 union sub_crq *entry = &scrq->msgs[scrq->cur];
1860
1861 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1862 return 1;
1863 else
1864 return 0;
1865}
1866
1867static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1868 struct ibmvnic_sub_crq_queue *scrq)
1869{
1870 union sub_crq *entry;
1871 unsigned long flags;
1872
1873 spin_lock_irqsave(&scrq->lock, flags);
1874 entry = &scrq->msgs[scrq->cur];
1875 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1876 if (++scrq->cur == scrq->size)
1877 scrq->cur = 0;
1878 } else {
1879 entry = NULL;
1880 }
1881 spin_unlock_irqrestore(&scrq->lock, flags);
1882
1883 return entry;
1884}
1885
1886static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1887{
1888 struct ibmvnic_crq_queue *queue = &adapter->crq;
1889 union ibmvnic_crq *crq;
1890
1891 crq = &queue->msgs[queue->cur];
1892 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1893 if (++queue->cur == queue->size)
1894 queue->cur = 0;
1895 } else {
1896 crq = NULL;
1897 }
1898
1899 return crq;
1900}
1901
1902static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1903 union sub_crq *sub_crq)
1904{
1905 unsigned int ua = adapter->vdev->unit_address;
1906 struct device *dev = &adapter->vdev->dev;
1907 u64 *u64_crq = (u64 *)sub_crq;
1908 int rc;
1909
1910 netdev_dbg(adapter->netdev,
1911 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1912 (unsigned long int)cpu_to_be64(remote_handle),
1913 (unsigned long int)cpu_to_be64(u64_crq[0]),
1914 (unsigned long int)cpu_to_be64(u64_crq[1]),
1915 (unsigned long int)cpu_to_be64(u64_crq[2]),
1916 (unsigned long int)cpu_to_be64(u64_crq[3]));
1917
1918 /* Make sure the hypervisor sees the complete request */
1919 mb();
1920
1921 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1922 cpu_to_be64(remote_handle),
1923 cpu_to_be64(u64_crq[0]),
1924 cpu_to_be64(u64_crq[1]),
1925 cpu_to_be64(u64_crq[2]),
1926 cpu_to_be64(u64_crq[3]));
1927
1928 if (rc) {
1929 if (rc == H_CLOSED)
1930 dev_warn(dev, "CRQ Queue closed\n");
1931 dev_err(dev, "Send error (rc=%d)\n", rc);
1932 }
1933
1934 return rc;
1935}
1936
Thomas Falconad7775d2016-04-01 17:20:34 -05001937static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1938 u64 remote_handle, u64 ioba, u64 num_entries)
1939{
1940 unsigned int ua = adapter->vdev->unit_address;
1941 struct device *dev = &adapter->vdev->dev;
1942 int rc;
1943
1944 /* Make sure the hypervisor sees the complete request */
1945 mb();
1946 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1947 cpu_to_be64(remote_handle),
1948 ioba, num_entries);
1949
1950 if (rc) {
1951 if (rc == H_CLOSED)
1952 dev_warn(dev, "CRQ Queue closed\n");
1953 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1954 }
1955
1956 return rc;
1957}
1958
Thomas Falcon032c5e82015-12-21 11:26:06 -06001959static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1960 union ibmvnic_crq *crq)
1961{
1962 unsigned int ua = adapter->vdev->unit_address;
1963 struct device *dev = &adapter->vdev->dev;
1964 u64 *u64_crq = (u64 *)crq;
1965 int rc;
1966
1967 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1968 (unsigned long int)cpu_to_be64(u64_crq[0]),
1969 (unsigned long int)cpu_to_be64(u64_crq[1]));
1970
1971 /* Make sure the hypervisor sees the complete request */
1972 mb();
1973
1974 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1975 cpu_to_be64(u64_crq[0]),
1976 cpu_to_be64(u64_crq[1]));
1977
1978 if (rc) {
1979 if (rc == H_CLOSED)
1980 dev_warn(dev, "CRQ Queue closed\n");
1981 dev_warn(dev, "Send error (rc=%d)\n", rc);
1982 }
1983
1984 return rc;
1985}
1986
1987static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1988{
1989 union ibmvnic_crq crq;
1990
1991 memset(&crq, 0, sizeof(crq));
1992 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1993 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1994 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1995
1996 return ibmvnic_send_crq(adapter, &crq);
1997}
1998
1999static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
2000{
2001 union ibmvnic_crq crq;
2002
2003 memset(&crq, 0, sizeof(crq));
2004 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2005 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
2006 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
2007
2008 return ibmvnic_send_crq(adapter, &crq);
2009}
2010
2011static int send_version_xchg(struct ibmvnic_adapter *adapter)
2012{
2013 union ibmvnic_crq crq;
2014
2015 memset(&crq, 0, sizeof(crq));
2016 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2017 crq.version_exchange.cmd = VERSION_EXCHANGE;
2018 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2019
2020 return ibmvnic_send_crq(adapter, &crq);
2021}
2022
2023static void send_login(struct ibmvnic_adapter *adapter)
2024{
2025 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2026 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002027 struct device *dev = &adapter->vdev->dev;
2028 dma_addr_t rsp_buffer_token;
2029 dma_addr_t buffer_token;
2030 size_t rsp_buffer_size;
2031 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002032 size_t buffer_size;
2033 __be64 *tx_list_p;
2034 __be64 *rx_list_p;
2035 int i;
2036
2037 buffer_size =
2038 sizeof(struct ibmvnic_login_buffer) +
2039 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2040
2041 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2042 if (!login_buffer)
2043 goto buf_alloc_failed;
2044
2045 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2046 DMA_TO_DEVICE);
2047 if (dma_mapping_error(dev, buffer_token)) {
2048 dev_err(dev, "Couldn't map login buffer\n");
2049 goto buf_map_failed;
2050 }
2051
John Allen498cd8e2016-04-06 11:49:55 -05002052 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2053 sizeof(u64) * adapter->req_tx_queues +
2054 sizeof(u64) * adapter->req_rx_queues +
2055 sizeof(u64) * adapter->req_rx_queues +
2056 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002057
2058 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2059 if (!login_rsp_buffer)
2060 goto buf_rsp_alloc_failed;
2061
2062 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2063 rsp_buffer_size, DMA_FROM_DEVICE);
2064 if (dma_mapping_error(dev, rsp_buffer_token)) {
2065 dev_err(dev, "Couldn't map login rsp buffer\n");
2066 goto buf_rsp_map_failed;
2067 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002068
Thomas Falcon032c5e82015-12-21 11:26:06 -06002069 adapter->login_buf = login_buffer;
2070 adapter->login_buf_token = buffer_token;
2071 adapter->login_buf_sz = buffer_size;
2072 adapter->login_rsp_buf = login_rsp_buffer;
2073 adapter->login_rsp_buf_token = rsp_buffer_token;
2074 adapter->login_rsp_buf_sz = rsp_buffer_size;
2075
2076 login_buffer->len = cpu_to_be32(buffer_size);
2077 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2078 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2079 login_buffer->off_txcomp_subcrqs =
2080 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2081 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2082 login_buffer->off_rxcomp_subcrqs =
2083 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2084 sizeof(u64) * adapter->req_tx_queues);
2085 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2086 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2087
2088 tx_list_p = (__be64 *)((char *)login_buffer +
2089 sizeof(struct ibmvnic_login_buffer));
2090 rx_list_p = (__be64 *)((char *)login_buffer +
2091 sizeof(struct ibmvnic_login_buffer) +
2092 sizeof(u64) * adapter->req_tx_queues);
2093
2094 for (i = 0; i < adapter->req_tx_queues; i++) {
2095 if (adapter->tx_scrq[i]) {
2096 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2097 crq_num);
2098 }
2099 }
2100
2101 for (i = 0; i < adapter->req_rx_queues; i++) {
2102 if (adapter->rx_scrq[i]) {
2103 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2104 crq_num);
2105 }
2106 }
2107
2108 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2109 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2110 netdev_dbg(adapter->netdev, "%016lx\n",
2111 ((unsigned long int *)(adapter->login_buf))[i]);
2112 }
2113
2114 memset(&crq, 0, sizeof(crq));
2115 crq.login.first = IBMVNIC_CRQ_CMD;
2116 crq.login.cmd = LOGIN;
2117 crq.login.ioba = cpu_to_be32(buffer_token);
2118 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002119 ibmvnic_send_crq(adapter, &crq);
2120
2121 return;
2122
Thomas Falcon032c5e82015-12-21 11:26:06 -06002123buf_rsp_map_failed:
2124 kfree(login_rsp_buffer);
2125buf_rsp_alloc_failed:
2126 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2127buf_map_failed:
2128 kfree(login_buffer);
2129buf_alloc_failed:
2130 return;
2131}
2132
2133static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2134 u32 len, u8 map_id)
2135{
2136 union ibmvnic_crq crq;
2137
2138 memset(&crq, 0, sizeof(crq));
2139 crq.request_map.first = IBMVNIC_CRQ_CMD;
2140 crq.request_map.cmd = REQUEST_MAP;
2141 crq.request_map.map_id = map_id;
2142 crq.request_map.ioba = cpu_to_be32(addr);
2143 crq.request_map.len = cpu_to_be32(len);
2144 ibmvnic_send_crq(adapter, &crq);
2145}
2146
2147static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2148{
2149 union ibmvnic_crq crq;
2150
2151 memset(&crq, 0, sizeof(crq));
2152 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2153 crq.request_unmap.cmd = REQUEST_UNMAP;
2154 crq.request_unmap.map_id = map_id;
2155 ibmvnic_send_crq(adapter, &crq);
2156}
2157
2158static void send_map_query(struct ibmvnic_adapter *adapter)
2159{
2160 union ibmvnic_crq crq;
2161
2162 memset(&crq, 0, sizeof(crq));
2163 crq.query_map.first = IBMVNIC_CRQ_CMD;
2164 crq.query_map.cmd = QUERY_MAP;
2165 ibmvnic_send_crq(adapter, &crq);
2166}
2167
2168/* Send a series of CRQs requesting various capabilities of the VNIC server */
2169static void send_cap_queries(struct ibmvnic_adapter *adapter)
2170{
2171 union ibmvnic_crq crq;
2172
Thomas Falcon901e0402017-02-15 12:17:59 -06002173 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002174 memset(&crq, 0, sizeof(crq));
2175 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2176 crq.query_capability.cmd = QUERY_CAPABILITY;
2177
2178 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002179 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002180 ibmvnic_send_crq(adapter, &crq);
2181
2182 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002183 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002184 ibmvnic_send_crq(adapter, &crq);
2185
2186 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
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(MAX_TX_QUEUES);
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(MAX_RX_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(MAX_RX_ADD_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(MIN_TX_ENTRIES_PER_SUBCRQ);
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(MIN_RX_ADD_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(MAX_TX_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 =
2218 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002219 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002220 ibmvnic_send_crq(adapter, &crq);
2221
2222 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002223 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002224 ibmvnic_send_crq(adapter, &crq);
2225
2226 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002227 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002228 ibmvnic_send_crq(adapter, &crq);
2229
2230 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002231 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002232 ibmvnic_send_crq(adapter, &crq);
2233
2234 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002235 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002236 ibmvnic_send_crq(adapter, &crq);
2237
2238 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002239 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002240 ibmvnic_send_crq(adapter, &crq);
2241
2242 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002243 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002244 ibmvnic_send_crq(adapter, &crq);
2245
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002246 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2247 atomic_inc(&adapter->running_cap_crqs);
2248 ibmvnic_send_crq(adapter, &crq);
2249
Thomas Falcon032c5e82015-12-21 11:26:06 -06002250 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002251 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002252 ibmvnic_send_crq(adapter, &crq);
2253
2254 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002255 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002256 ibmvnic_send_crq(adapter, &crq);
2257
2258 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002259 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002260 ibmvnic_send_crq(adapter, &crq);
2261
2262 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002263 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002264 ibmvnic_send_crq(adapter, &crq);
2265
2266 crq.query_capability.capability =
2267 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002268 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002269 ibmvnic_send_crq(adapter, &crq);
2270
2271 crq.query_capability.capability =
2272 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002273 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002274 ibmvnic_send_crq(adapter, &crq);
2275
2276 crq.query_capability.capability =
2277 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002278 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002279 ibmvnic_send_crq(adapter, &crq);
2280
2281 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002282 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002283 ibmvnic_send_crq(adapter, &crq);
2284}
2285
2286static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2287{
2288 struct device *dev = &adapter->vdev->dev;
2289 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2290 union ibmvnic_crq crq;
2291 int i;
2292
2293 dma_unmap_single(dev, adapter->ip_offload_tok,
2294 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2295
2296 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2297 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2298 netdev_dbg(adapter->netdev, "%016lx\n",
2299 ((unsigned long int *)(buf))[i]);
2300
2301 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2302 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2303 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2304 buf->tcp_ipv4_chksum);
2305 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2306 buf->tcp_ipv6_chksum);
2307 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2308 buf->udp_ipv4_chksum);
2309 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2310 buf->udp_ipv6_chksum);
2311 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2312 buf->large_tx_ipv4);
2313 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2314 buf->large_tx_ipv6);
2315 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2316 buf->large_rx_ipv4);
2317 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2318 buf->large_rx_ipv6);
2319 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2320 buf->max_ipv4_header_size);
2321 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2322 buf->max_ipv6_header_size);
2323 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2324 buf->max_tcp_header_size);
2325 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2326 buf->max_udp_header_size);
2327 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2328 buf->max_large_tx_size);
2329 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2330 buf->max_large_rx_size);
2331 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2332 buf->ipv6_extension_header);
2333 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2334 buf->tcp_pseudosum_req);
2335 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2336 buf->num_ipv6_ext_headers);
2337 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2338 buf->off_ipv6_ext_headers);
2339
2340 adapter->ip_offload_ctrl_tok =
2341 dma_map_single(dev, &adapter->ip_offload_ctrl,
2342 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2343
2344 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2345 dev_err(dev, "Couldn't map ip offload control buffer\n");
2346 return;
2347 }
2348
2349 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2350 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2351 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2352 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2353 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2354
2355 /* large_tx/rx disabled for now, additional features needed */
2356 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2357 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2358 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2359 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2360
2361 adapter->netdev->features = NETIF_F_GSO;
2362
2363 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2364 adapter->netdev->features |= NETIF_F_IP_CSUM;
2365
2366 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2367 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2368
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002369 if ((adapter->netdev->features &
2370 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2371 adapter->netdev->features |= NETIF_F_RXCSUM;
2372
Thomas Falcon032c5e82015-12-21 11:26:06 -06002373 memset(&crq, 0, sizeof(crq));
2374 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2375 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2376 crq.control_ip_offload.len =
2377 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2378 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2379 ibmvnic_send_crq(adapter, &crq);
2380}
2381
2382static void handle_error_info_rsp(union ibmvnic_crq *crq,
2383 struct ibmvnic_adapter *adapter)
2384{
2385 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002386 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002387 unsigned long flags;
2388 bool found = false;
2389 int i;
2390
2391 if (!crq->request_error_rsp.rc.code) {
2392 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2393 crq->request_error_rsp.rc.code);
2394 return;
2395 }
2396
2397 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002398 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002399 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2400 found = true;
2401 list_del(&error_buff->list);
2402 break;
2403 }
2404 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2405
2406 if (!found) {
2407 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002408 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002409 return;
2410 }
2411
2412 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002413 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002414
2415 for (i = 0; i < error_buff->len; i++) {
2416 pr_cont("%02x", (int)error_buff->buff[i]);
2417 if (i % 8 == 7)
2418 pr_cont(" ");
2419 }
2420 pr_cont("\n");
2421
2422 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2423 DMA_FROM_DEVICE);
2424 kfree(error_buff->buff);
2425 kfree(error_buff);
2426}
2427
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002428static void request_error_information(struct ibmvnic_adapter *adapter,
2429 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002430{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002431 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002432 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002433 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002434 unsigned long timeout = msecs_to_jiffies(30000);
2435 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002436 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002437 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002438
2439 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2440 if (!error_buff)
2441 return;
2442
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002443 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002444 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2445 if (!error_buff->buff) {
2446 kfree(error_buff);
2447 return;
2448 }
2449
2450 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2451 DMA_FROM_DEVICE);
2452 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002453 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002454 kfree(error_buff->buff);
2455 kfree(error_buff);
2456 return;
2457 }
2458
Thomas Falcon032c5e82015-12-21 11:26:06 -06002459 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002460 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002461
2462 spin_lock_irqsave(&adapter->error_list_lock, flags);
2463 list_add_tail(&error_buff->list, &adapter->errors);
2464 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2465
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002466 memset(&crq, 0, sizeof(crq));
2467 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2468 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2469 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2470 crq.request_error_info.len = cpu_to_be32(detail_len);
2471 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2472
2473 rc = ibmvnic_send_crq(adapter, &crq);
2474 if (rc) {
2475 netdev_err(netdev, "failed to request error information\n");
2476 goto err_info_fail;
2477 }
2478
2479 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2480 netdev_err(netdev, "timeout waiting for error information\n");
2481 goto err_info_fail;
2482 }
2483
2484 return;
2485
2486err_info_fail:
2487 spin_lock_irqsave(&adapter->error_list_lock, flags);
2488 list_del(&error_buff->list);
2489 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2490
2491 kfree(error_buff->buff);
2492 kfree(error_buff);
2493}
2494
2495static void handle_error_indication(union ibmvnic_crq *crq,
2496 struct ibmvnic_adapter *adapter)
2497{
2498 struct device *dev = &adapter->vdev->dev;
2499
2500 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2501 crq->error_indication.flags
2502 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2503 be32_to_cpu(crq->error_indication.error_id),
2504 be16_to_cpu(crq->error_indication.error_cause));
2505
2506 if (be32_to_cpu(crq->error_indication.error_id))
2507 request_error_information(adapter, crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002508}
2509
2510static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2511 struct ibmvnic_adapter *adapter)
2512{
2513 struct net_device *netdev = adapter->netdev;
2514 struct device *dev = &adapter->vdev->dev;
2515 long rc;
2516
2517 rc = crq->change_mac_addr_rsp.rc.code;
2518 if (rc) {
2519 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2520 return;
2521 }
2522 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2523 ETH_ALEN);
2524}
2525
2526static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2527 struct ibmvnic_adapter *adapter)
2528{
2529 struct device *dev = &adapter->vdev->dev;
2530 u64 *req_value;
2531 char *name;
2532
Thomas Falcon901e0402017-02-15 12:17:59 -06002533 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002534 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2535 case REQ_TX_QUEUES:
2536 req_value = &adapter->req_tx_queues;
2537 name = "tx";
2538 break;
2539 case REQ_RX_QUEUES:
2540 req_value = &adapter->req_rx_queues;
2541 name = "rx";
2542 break;
2543 case REQ_RX_ADD_QUEUES:
2544 req_value = &adapter->req_rx_add_queues;
2545 name = "rx_add";
2546 break;
2547 case REQ_TX_ENTRIES_PER_SUBCRQ:
2548 req_value = &adapter->req_tx_entries_per_subcrq;
2549 name = "tx_entries_per_subcrq";
2550 break;
2551 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2552 req_value = &adapter->req_rx_add_entries_per_subcrq;
2553 name = "rx_add_entries_per_subcrq";
2554 break;
2555 case REQ_MTU:
2556 req_value = &adapter->req_mtu;
2557 name = "mtu";
2558 break;
2559 case PROMISC_REQUESTED:
2560 req_value = &adapter->promisc;
2561 name = "promisc";
2562 break;
2563 default:
2564 dev_err(dev, "Got invalid cap request rsp %d\n",
2565 crq->request_capability.capability);
2566 return;
2567 }
2568
2569 switch (crq->request_capability_rsp.rc.code) {
2570 case SUCCESS:
2571 break;
2572 case PARTIALSUCCESS:
2573 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2574 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002575 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002576 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002577 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002578 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002579 ibmvnic_send_req_caps(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002580 return;
2581 default:
2582 dev_err(dev, "Error %d in request cap rsp\n",
2583 crq->request_capability_rsp.rc.code);
2584 return;
2585 }
2586
2587 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002588 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002589 union ibmvnic_crq newcrq;
2590 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2591 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2592 &adapter->ip_offload_buf;
2593
Thomas Falcon249168a2017-02-15 12:18:00 -06002594 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002595 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2596 buf_sz,
2597 DMA_FROM_DEVICE);
2598
2599 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2600 if (!firmware_has_feature(FW_FEATURE_CMO))
2601 dev_err(dev, "Couldn't map offload buffer\n");
2602 return;
2603 }
2604
2605 memset(&newcrq, 0, sizeof(newcrq));
2606 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2607 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2608 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2609 newcrq.query_ip_offload.ioba =
2610 cpu_to_be32(adapter->ip_offload_tok);
2611
2612 ibmvnic_send_crq(adapter, &newcrq);
2613 }
2614}
2615
2616static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2617 struct ibmvnic_adapter *adapter)
2618{
2619 struct device *dev = &adapter->vdev->dev;
2620 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2621 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002622 int i;
2623
2624 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2625 DMA_BIDIRECTIONAL);
2626 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2627 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2628
John Allen498cd8e2016-04-06 11:49:55 -05002629 /* If the number of queues requested can't be allocated by the
2630 * server, the login response will return with code 1. We will need
2631 * to resend the login buffer with fewer queues requested.
2632 */
2633 if (login_rsp_crq->generic.rc.code) {
2634 adapter->renegotiate = true;
2635 complete(&adapter->init_done);
2636 return 0;
2637 }
2638
Thomas Falcon032c5e82015-12-21 11:26:06 -06002639 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2640 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2641 netdev_dbg(adapter->netdev, "%016lx\n",
2642 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2643 }
2644
2645 /* Sanity checks */
2646 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2647 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2648 adapter->req_rx_add_queues !=
2649 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2650 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2651 ibmvnic_remove(adapter->vdev);
2652 return -EIO;
2653 }
2654 complete(&adapter->init_done);
2655
Thomas Falcon032c5e82015-12-21 11:26:06 -06002656 return 0;
2657}
2658
2659static void handle_request_map_rsp(union ibmvnic_crq *crq,
2660 struct ibmvnic_adapter *adapter)
2661{
2662 struct device *dev = &adapter->vdev->dev;
2663 u8 map_id = crq->request_map_rsp.map_id;
2664 int tx_subcrqs;
2665 int rx_subcrqs;
2666 long rc;
2667 int i;
2668
2669 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2670 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2671
2672 rc = crq->request_map_rsp.rc.code;
2673 if (rc) {
2674 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2675 adapter->map_id--;
2676 /* need to find and zero tx/rx_pool map_id */
2677 for (i = 0; i < tx_subcrqs; i++) {
2678 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2679 adapter->tx_pool[i].long_term_buff.map_id = 0;
2680 }
2681 for (i = 0; i < rx_subcrqs; i++) {
2682 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2683 adapter->rx_pool[i].long_term_buff.map_id = 0;
2684 }
2685 }
2686 complete(&adapter->fw_done);
2687}
2688
2689static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2690 struct ibmvnic_adapter *adapter)
2691{
2692 struct device *dev = &adapter->vdev->dev;
2693 long rc;
2694
2695 rc = crq->request_unmap_rsp.rc.code;
2696 if (rc)
2697 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2698}
2699
2700static void handle_query_map_rsp(union ibmvnic_crq *crq,
2701 struct ibmvnic_adapter *adapter)
2702{
2703 struct net_device *netdev = adapter->netdev;
2704 struct device *dev = &adapter->vdev->dev;
2705 long rc;
2706
2707 rc = crq->query_map_rsp.rc.code;
2708 if (rc) {
2709 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2710 return;
2711 }
2712 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2713 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2714 crq->query_map_rsp.free_pages);
2715}
2716
2717static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2718 struct ibmvnic_adapter *adapter)
2719{
2720 struct net_device *netdev = adapter->netdev;
2721 struct device *dev = &adapter->vdev->dev;
2722 long rc;
2723
Thomas Falcon901e0402017-02-15 12:17:59 -06002724 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002725 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002726 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 rc = crq->query_capability.rc.code;
2728 if (rc) {
2729 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2730 goto out;
2731 }
2732
2733 switch (be16_to_cpu(crq->query_capability.capability)) {
2734 case MIN_TX_QUEUES:
2735 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002736 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002737 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2738 adapter->min_tx_queues);
2739 break;
2740 case MIN_RX_QUEUES:
2741 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002742 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002743 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2744 adapter->min_rx_queues);
2745 break;
2746 case MIN_RX_ADD_QUEUES:
2747 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002748 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002749 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2750 adapter->min_rx_add_queues);
2751 break;
2752 case MAX_TX_QUEUES:
2753 adapter->max_tx_queues =
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_tx_queues = %lld\n",
2756 adapter->max_tx_queues);
2757 break;
2758 case MAX_RX_QUEUES:
2759 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002760 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002761 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2762 adapter->max_rx_queues);
2763 break;
2764 case MAX_RX_ADD_QUEUES:
2765 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002766 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002767 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2768 adapter->max_rx_add_queues);
2769 break;
2770 case MIN_TX_ENTRIES_PER_SUBCRQ:
2771 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002772 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002773 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2774 adapter->min_tx_entries_per_subcrq);
2775 break;
2776 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2777 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002778 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002779 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2780 adapter->min_rx_add_entries_per_subcrq);
2781 break;
2782 case MAX_TX_ENTRIES_PER_SUBCRQ:
2783 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002784 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002785 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2786 adapter->max_tx_entries_per_subcrq);
2787 break;
2788 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2789 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002790 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002791 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2792 adapter->max_rx_add_entries_per_subcrq);
2793 break;
2794 case TCP_IP_OFFLOAD:
2795 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002796 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002797 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2798 adapter->tcp_ip_offload);
2799 break;
2800 case PROMISC_SUPPORTED:
2801 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002802 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002803 netdev_dbg(netdev, "promisc_supported = %lld\n",
2804 adapter->promisc_supported);
2805 break;
2806 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002807 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002808 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002809 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2810 break;
2811 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002812 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002813 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2815 break;
2816 case MAX_MULTICAST_FILTERS:
2817 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002818 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002819 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2820 adapter->max_multicast_filters);
2821 break;
2822 case VLAN_HEADER_INSERTION:
2823 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002824 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002825 if (adapter->vlan_header_insertion)
2826 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2827 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2828 adapter->vlan_header_insertion);
2829 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002830 case RX_VLAN_HEADER_INSERTION:
2831 adapter->rx_vlan_header_insertion =
2832 be64_to_cpu(crq->query_capability.number);
2833 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
2834 adapter->rx_vlan_header_insertion);
2835 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002836 case MAX_TX_SG_ENTRIES:
2837 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002838 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002839 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2840 adapter->max_tx_sg_entries);
2841 break;
2842 case RX_SG_SUPPORTED:
2843 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002844 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002845 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2846 adapter->rx_sg_supported);
2847 break;
2848 case OPT_TX_COMP_SUB_QUEUES:
2849 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002850 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002851 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2852 adapter->opt_tx_comp_sub_queues);
2853 break;
2854 case OPT_RX_COMP_QUEUES:
2855 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002856 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002857 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2858 adapter->opt_rx_comp_queues);
2859 break;
2860 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2861 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002862 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002863 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2864 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2865 break;
2866 case OPT_TX_ENTRIES_PER_SUBCRQ:
2867 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002868 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002869 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2870 adapter->opt_tx_entries_per_subcrq);
2871 break;
2872 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2873 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002874 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002875 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2876 adapter->opt_rxba_entries_per_subcrq);
2877 break;
2878 case TX_RX_DESC_REQ:
2879 adapter->tx_rx_desc_req = crq->query_capability.number;
2880 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2881 adapter->tx_rx_desc_req);
2882 break;
2883
2884 default:
2885 netdev_err(netdev, "Got invalid cap rsp %d\n",
2886 crq->query_capability.capability);
2887 }
2888
2889out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002890 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2891 adapter->wait_capability = false;
Nathan Fontenotd346b9b2017-04-25 15:01:04 -04002892 ibmvnic_send_req_caps(adapter, 0);
Thomas Falcon249168a2017-02-15 12:18:00 -06002893 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002894}
2895
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002896static void ibmvnic_xport_event(struct work_struct *work)
2897{
2898 struct ibmvnic_adapter *adapter = container_of(work,
2899 struct ibmvnic_adapter,
2900 ibmvnic_xport);
2901 struct device *dev = &adapter->vdev->dev;
2902 long rc;
2903
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002904 release_sub_crqs(adapter);
2905 if (adapter->migrated) {
2906 rc = ibmvnic_reenable_crq_queue(adapter);
2907 if (rc)
2908 dev_err(dev, "Error after enable rc=%ld\n", rc);
2909 adapter->migrated = false;
2910 rc = ibmvnic_send_crq_init(adapter);
2911 if (rc)
2912 dev_err(dev, "Error sending init rc=%ld\n", rc);
2913 }
2914}
2915
Thomas Falcon032c5e82015-12-21 11:26:06 -06002916static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2917 struct ibmvnic_adapter *adapter)
2918{
2919 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2920 struct net_device *netdev = adapter->netdev;
2921 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002922 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002923 long rc;
2924
2925 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002926 (unsigned long int)cpu_to_be64(u64_crq[0]),
2927 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002928 switch (gen_crq->first) {
2929 case IBMVNIC_CRQ_INIT_RSP:
2930 switch (gen_crq->cmd) {
2931 case IBMVNIC_CRQ_INIT:
2932 dev_info(dev, "Partner initialized\n");
2933 /* Send back a response */
2934 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002935 if (!rc)
2936 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002937 else
2938 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2939 break;
2940 case IBMVNIC_CRQ_INIT_COMPLETE:
2941 dev_info(dev, "Partner initialization complete\n");
2942 send_version_xchg(adapter);
2943 break;
2944 default:
2945 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2946 }
2947 return;
2948 case IBMVNIC_CRQ_XPORT_EVENT:
2949 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2950 dev_info(dev, "Re-enabling adapter\n");
2951 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002952 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002953 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2954 dev_info(dev, "Backing device failover detected\n");
2955 netif_carrier_off(netdev);
2956 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002957 } else {
2958 /* The adapter lost the connection */
2959 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2960 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002961 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002962 }
2963 return;
2964 case IBMVNIC_CRQ_CMD_RSP:
2965 break;
2966 default:
2967 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2968 gen_crq->first);
2969 return;
2970 }
2971
2972 switch (gen_crq->cmd) {
2973 case VERSION_EXCHANGE_RSP:
2974 rc = crq->version_exchange_rsp.rc.code;
2975 if (rc) {
2976 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2977 break;
2978 }
2979 dev_info(dev, "Partner protocol version is %d\n",
2980 crq->version_exchange_rsp.version);
2981 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2982 ibmvnic_version)
2983 ibmvnic_version =
2984 be16_to_cpu(crq->version_exchange_rsp.version);
2985 send_cap_queries(adapter);
2986 break;
2987 case QUERY_CAPABILITY_RSP:
2988 handle_query_cap_rsp(crq, adapter);
2989 break;
2990 case QUERY_MAP_RSP:
2991 handle_query_map_rsp(crq, adapter);
2992 break;
2993 case REQUEST_MAP_RSP:
2994 handle_request_map_rsp(crq, adapter);
2995 break;
2996 case REQUEST_UNMAP_RSP:
2997 handle_request_unmap_rsp(crq, adapter);
2998 break;
2999 case REQUEST_CAPABILITY_RSP:
3000 handle_request_cap_rsp(crq, adapter);
3001 break;
3002 case LOGIN_RSP:
3003 netdev_dbg(netdev, "Got Login Response\n");
3004 handle_login_rsp(crq, adapter);
3005 break;
3006 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003007 netdev_dbg(netdev,
3008 "Got Logical Link State Response, state: %d rc: %d\n",
3009 crq->logical_link_state_rsp.link_state,
3010 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003011 adapter->logical_link_state =
3012 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04003013 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3014 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003015 break;
3016 case LINK_STATE_INDICATION:
3017 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3018 adapter->phys_link_state =
3019 crq->link_state_indication.phys_link_state;
3020 adapter->logical_link_state =
3021 crq->link_state_indication.logical_link_state;
3022 break;
3023 case CHANGE_MAC_ADDR_RSP:
3024 netdev_dbg(netdev, "Got MAC address change Response\n");
3025 handle_change_mac_rsp(crq, adapter);
3026 break;
3027 case ERROR_INDICATION:
3028 netdev_dbg(netdev, "Got Error Indication\n");
3029 handle_error_indication(crq, adapter);
3030 break;
3031 case REQUEST_ERROR_RSP:
3032 netdev_dbg(netdev, "Got Error Detail Response\n");
3033 handle_error_info_rsp(crq, adapter);
3034 break;
3035 case REQUEST_STATISTICS_RSP:
3036 netdev_dbg(netdev, "Got Statistics Response\n");
3037 complete(&adapter->stats_done);
3038 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003039 case QUERY_IP_OFFLOAD_RSP:
3040 netdev_dbg(netdev, "Got Query IP offload Response\n");
3041 handle_query_ip_offload_rsp(adapter);
3042 break;
3043 case MULTICAST_CTRL_RSP:
3044 netdev_dbg(netdev, "Got multicast control Response\n");
3045 break;
3046 case CONTROL_IP_OFFLOAD_RSP:
3047 netdev_dbg(netdev, "Got Control IP offload Response\n");
3048 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3049 sizeof(adapter->ip_offload_ctrl),
3050 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003051 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003052 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 case COLLECT_FW_TRACE_RSP:
3054 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3055 complete(&adapter->fw_done);
3056 break;
3057 default:
3058 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3059 gen_crq->cmd);
3060 }
3061}
3062
3063static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3064{
3065 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003066
Thomas Falcon6c267b32017-02-15 12:17:58 -06003067 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003068 return IRQ_HANDLED;
3069}
3070
3071static void ibmvnic_tasklet(void *data)
3072{
3073 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003074 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003075 union ibmvnic_crq *crq;
3076 unsigned long flags;
3077 bool done = false;
3078
3079 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003080 while (!done) {
3081 /* Pull all the valid messages off the CRQ */
3082 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3083 ibmvnic_handle_crq(crq, adapter);
3084 crq->generic.first = 0;
3085 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003086
3087 /* remain in tasklet until all
3088 * capabilities responses are received
3089 */
3090 if (!adapter->wait_capability)
3091 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003092 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003093 /* if capabilities CRQ's were sent in this tasklet, the following
3094 * tasklet must wait until all responses are received
3095 */
3096 if (atomic_read(&adapter->running_cap_crqs) != 0)
3097 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003098 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003099}
3100
3101static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3102{
3103 struct vio_dev *vdev = adapter->vdev;
3104 int rc;
3105
3106 do {
3107 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3108 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3109
3110 if (rc)
3111 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3112
3113 return rc;
3114}
3115
3116static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3117{
3118 struct ibmvnic_crq_queue *crq = &adapter->crq;
3119 struct device *dev = &adapter->vdev->dev;
3120 struct vio_dev *vdev = adapter->vdev;
3121 int rc;
3122
3123 /* Close the CRQ */
3124 do {
3125 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3126 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3127
3128 /* Clean out the queue */
3129 memset(crq->msgs, 0, PAGE_SIZE);
3130 crq->cur = 0;
3131
3132 /* And re-open it again */
3133 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3134 crq->msg_token, PAGE_SIZE);
3135
3136 if (rc == H_CLOSED)
3137 /* Adapter is good, but other end is not ready */
3138 dev_warn(dev, "Partner adapter not ready\n");
3139 else if (rc != 0)
3140 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3141
3142 return rc;
3143}
3144
Nathan Fontenotf9928872017-03-30 02:48:54 -04003145static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003146{
3147 struct ibmvnic_crq_queue *crq = &adapter->crq;
3148 struct vio_dev *vdev = adapter->vdev;
3149 long rc;
3150
Nathan Fontenotf9928872017-03-30 02:48:54 -04003151 if (!crq->msgs)
3152 return;
3153
Thomas Falcon032c5e82015-12-21 11:26:06 -06003154 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3155 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003156 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003157 do {
3158 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3159 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3160
3161 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3162 DMA_BIDIRECTIONAL);
3163 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003164 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003165}
3166
Nathan Fontenotf9928872017-03-30 02:48:54 -04003167static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003168{
3169 struct ibmvnic_crq_queue *crq = &adapter->crq;
3170 struct device *dev = &adapter->vdev->dev;
3171 struct vio_dev *vdev = adapter->vdev;
3172 int rc, retrc = -ENOMEM;
3173
Nathan Fontenotf9928872017-03-30 02:48:54 -04003174 if (crq->msgs)
3175 return 0;
3176
Thomas Falcon032c5e82015-12-21 11:26:06 -06003177 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3178 /* Should we allocate more than one page? */
3179
3180 if (!crq->msgs)
3181 return -ENOMEM;
3182
3183 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3184 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3185 DMA_BIDIRECTIONAL);
3186 if (dma_mapping_error(dev, crq->msg_token))
3187 goto map_failed;
3188
3189 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3190 crq->msg_token, PAGE_SIZE);
3191
3192 if (rc == H_RESOURCE)
3193 /* maybe kexecing and resource is busy. try a reset */
3194 rc = ibmvnic_reset_crq(adapter);
3195 retrc = rc;
3196
3197 if (rc == H_CLOSED) {
3198 dev_warn(dev, "Partner adapter not ready\n");
3199 } else if (rc) {
3200 dev_warn(dev, "Error %d opening adapter\n", rc);
3201 goto reg_crq_failed;
3202 }
3203
3204 retrc = 0;
3205
Thomas Falcon6c267b32017-02-15 12:17:58 -06003206 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3207 (unsigned long)adapter);
3208
Thomas Falcon032c5e82015-12-21 11:26:06 -06003209 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3210 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3211 adapter);
3212 if (rc) {
3213 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3214 vdev->irq, rc);
3215 goto req_irq_failed;
3216 }
3217
3218 rc = vio_enable_interrupts(vdev);
3219 if (rc) {
3220 dev_err(dev, "Error %d enabling interrupts\n", rc);
3221 goto req_irq_failed;
3222 }
3223
3224 crq->cur = 0;
3225 spin_lock_init(&crq->lock);
3226
3227 return retrc;
3228
3229req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003230 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003231 do {
3232 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3233 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3234reg_crq_failed:
3235 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3236map_failed:
3237 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003238 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003239 return retrc;
3240}
3241
Thomas Falcon65dc6892016-07-06 15:35:18 -05003242static void handle_crq_init_rsp(struct work_struct *work)
3243{
3244 struct ibmvnic_adapter *adapter = container_of(work,
3245 struct ibmvnic_adapter,
3246 vnic_crq_init);
3247 struct device *dev = &adapter->vdev->dev;
3248 struct net_device *netdev = adapter->netdev;
3249 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003250 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003251 int rc;
3252
Thomas Falcondfad09a2016-08-18 11:37:51 -05003253 if (adapter->failover) {
3254 release_sub_crqs(adapter);
3255 if (netif_running(netdev)) {
3256 netif_tx_disable(netdev);
3257 ibmvnic_close(netdev);
3258 restart = true;
3259 }
3260 }
3261
Thomas Falcon65dc6892016-07-06 15:35:18 -05003262 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003263 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003264 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3265 dev_err(dev, "Passive init timeout\n");
3266 goto task_failed;
3267 }
3268
Thomas Falconf39f0d12017-02-14 10:22:59 -06003269 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003270
Thomas Falcondfad09a2016-08-18 11:37:51 -05003271 if (adapter->failover) {
3272 adapter->failover = false;
3273 if (restart) {
3274 rc = ibmvnic_open(netdev);
3275 if (rc)
3276 goto restart_failed;
3277 }
3278 netif_carrier_on(netdev);
3279 return;
3280 }
3281
Thomas Falcon65dc6892016-07-06 15:35:18 -05003282 rc = register_netdev(netdev);
3283 if (rc) {
3284 dev_err(dev,
3285 "failed to register netdev rc=%d\n", rc);
3286 goto register_failed;
3287 }
3288 dev_info(dev, "ibmvnic registered\n");
3289
3290 return;
3291
Thomas Falcondfad09a2016-08-18 11:37:51 -05003292restart_failed:
3293 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003294register_failed:
3295 release_sub_crqs(adapter);
3296task_failed:
3297 dev_err(dev, "Passive initialization was not successful\n");
3298}
3299
John Allenf6ef6402017-03-17 17:13:42 -05003300static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3301{
3302 struct device *dev = &adapter->vdev->dev;
3303 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003304 int rc;
3305
Nathan Fontenotf9928872017-03-30 02:48:54 -04003306 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003307 if (rc) {
3308 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3309 return rc;
3310 }
3311
John Allenf6ef6402017-03-17 17:13:42 -05003312 init_completion(&adapter->init_done);
3313 ibmvnic_send_crq_init(adapter);
3314 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3315 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003316 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003317 return -1;
3318 }
3319
3320 return 0;
3321}
3322
Thomas Falcon032c5e82015-12-21 11:26:06 -06003323static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3324{
3325 struct ibmvnic_adapter *adapter;
3326 struct net_device *netdev;
3327 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003328 int rc;
3329
3330 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3331 dev->unit_address);
3332
3333 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3334 VETH_MAC_ADDR, NULL);
3335 if (!mac_addr_p) {
3336 dev_err(&dev->dev,
3337 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3338 __FILE__, __LINE__);
3339 return 0;
3340 }
3341
3342 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3343 IBMVNIC_MAX_TX_QUEUES);
3344 if (!netdev)
3345 return -ENOMEM;
3346
3347 adapter = netdev_priv(netdev);
3348 dev_set_drvdata(&dev->dev, netdev);
3349 adapter->vdev = dev;
3350 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003351 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003352
3353 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3354 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3355 netdev->irq = dev->irq;
3356 netdev->netdev_ops = &ibmvnic_netdev_ops;
3357 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3358 SET_NETDEV_DEV(netdev, &dev->dev);
3359
Thomas Falcon65dc6892016-07-06 15:35:18 -05003360 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003361 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003362
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 spin_lock_init(&adapter->stats_lock);
3364
Thomas Falcon032c5e82015-12-21 11:26:06 -06003365 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003366 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003367
John Allenf6ef6402017-03-17 17:13:42 -05003368 rc = ibmvnic_init(adapter);
3369 if (rc) {
3370 free_netdev(netdev);
3371 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003372 }
3373
Thomas Falconf39f0d12017-02-14 10:22:59 -06003374 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003375 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003376
3377 rc = register_netdev(netdev);
3378 if (rc) {
3379 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003380 free_netdev(netdev);
3381 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003382 }
3383 dev_info(&dev->dev, "ibmvnic registered\n");
3384
3385 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003386}
3387
3388static int ibmvnic_remove(struct vio_dev *dev)
3389{
3390 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003391 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003392
3393 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003394
3395 release_resources(adapter);
3396 release_sub_crqs(adapter);
3397 release_crq_queue(adapter);
3398
Thomas Falcon032c5e82015-12-21 11:26:06 -06003399 free_netdev(netdev);
3400 dev_set_drvdata(&dev->dev, NULL);
3401
3402 return 0;
3403}
3404
3405static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3406{
3407 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3408 struct ibmvnic_adapter *adapter;
3409 struct iommu_table *tbl;
3410 unsigned long ret = 0;
3411 int i;
3412
3413 tbl = get_iommu_table_base(&vdev->dev);
3414
3415 /* netdev inits at probe time along with the structures we need below*/
3416 if (!netdev)
3417 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3418
3419 adapter = netdev_priv(netdev);
3420
3421 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003422 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3423
3424 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3425 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3426
3427 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3428 i++)
3429 ret += adapter->rx_pool[i].size *
3430 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3431
3432 return ret;
3433}
3434
3435static int ibmvnic_resume(struct device *dev)
3436{
3437 struct net_device *netdev = dev_get_drvdata(dev);
3438 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3439 int i;
3440
3441 /* kick the interrupt handlers just in case we lost an interrupt */
3442 for (i = 0; i < adapter->req_rx_queues; i++)
3443 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3444 adapter->rx_scrq[i]);
3445
3446 return 0;
3447}
3448
3449static struct vio_device_id ibmvnic_device_table[] = {
3450 {"network", "IBM,vnic"},
3451 {"", "" }
3452};
3453MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3454
3455static const struct dev_pm_ops ibmvnic_pm_ops = {
3456 .resume = ibmvnic_resume
3457};
3458
3459static struct vio_driver ibmvnic_driver = {
3460 .id_table = ibmvnic_device_table,
3461 .probe = ibmvnic_probe,
3462 .remove = ibmvnic_remove,
3463 .get_desired_dma = ibmvnic_get_desired_dma,
3464 .name = ibmvnic_driver_name,
3465 .pm = &ibmvnic_pm_ops,
3466};
3467
3468/* module functions */
3469static int __init ibmvnic_module_init(void)
3470{
3471 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3472 IBMVNIC_DRIVER_VERSION);
3473
3474 return vio_register_driver(&ibmvnic_driver);
3475}
3476
3477static void __exit ibmvnic_module_exit(void)
3478{
3479 vio_unregister_driver(&ibmvnic_driver);
3480}
3481
3482module_init(ibmvnic_module_init);
3483module_exit(ibmvnic_module_exit);