blob: 5a916a2f91be1bee6ec8532b0a994e1d737d0e9a [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) {
911 tx_send_failed++;
912 tx_dropped++;
913 ret = NETDEV_TX_BUSY;
914 goto out;
915 }
916
917 index = tx_pool->free_map[tx_pool->consumer_index];
918 offset = index * adapter->req_mtu;
919 dst = tx_pool->long_term_buff.buff + offset;
920 memset(dst, 0, adapter->req_mtu);
921 skb_copy_from_linear_data(skb, dst, skb->len);
922 data_dma_addr = tx_pool->long_term_buff.addr + offset;
923
924 tx_pool->consumer_index =
925 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600926 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600927
928 tx_buff = &tx_pool->tx_buff[index];
929 tx_buff->skb = skb;
930 tx_buff->data_dma[0] = data_dma_addr;
931 tx_buff->data_len[0] = skb->len;
932 tx_buff->index = index;
933 tx_buff->pool_index = queue_num;
934 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600935
936 memset(&tx_crq, 0, sizeof(tx_crq));
937 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
938 tx_crq.v1.type = IBMVNIC_TX_DESC;
939 tx_crq.v1.n_crq_elem = 1;
940 tx_crq.v1.n_sge = 1;
941 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
942 tx_crq.v1.correlator = cpu_to_be32(index);
943 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
944 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
945 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
946
947 if (adapter->vlan_header_insertion) {
948 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
949 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
950 }
951
952 if (skb->protocol == htons(ETH_P_IP)) {
953 if (ip_hdr(skb)->version == 4)
954 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
955 else if (ip_hdr(skb)->version == 6)
956 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
957
958 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
959 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
960 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
961 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
962 }
963
Thomas Falconad7775d2016-04-01 17:20:34 -0500964 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600965 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500966 hdrs += 2;
967 }
968 /* determine if l2/3/4 headers are sent to firmware */
969 if ((*hdrs >> 7) & 1 &&
970 (skb->protocol == htons(ETH_P_IP) ||
971 skb->protocol == htons(ETH_P_IPV6))) {
972 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
973 tx_crq.v1.n_crq_elem = num_entries;
974 tx_buff->indir_arr[0] = tx_crq;
975 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
976 sizeof(tx_buff->indir_arr),
977 DMA_TO_DEVICE);
978 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
979 if (!firmware_has_feature(FW_FEATURE_CMO))
980 dev_err(dev, "tx: unable to map descriptor array\n");
981 tx_map_failed++;
982 tx_dropped++;
983 ret = NETDEV_TX_BUSY;
984 goto out;
985 }
John Allen498cd8e2016-04-06 11:49:55 -0500986 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500987 (u64)tx_buff->indir_dma,
988 (u64)num_entries);
989 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500990 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
991 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500992 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600993 if (lpar_rc != H_SUCCESS) {
994 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
995
996 if (tx_pool->consumer_index == 0)
997 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600998 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600999 else
1000 tx_pool->consumer_index--;
1001
1002 tx_send_failed++;
1003 tx_dropped++;
1004 ret = NETDEV_TX_BUSY;
1005 goto out;
1006 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001007
Brian King58c8c0c2017-04-19 13:44:47 -04001008 if (atomic_inc_return(&tx_scrq->used)
1009 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001010 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1011 netif_stop_subqueue(netdev, queue_num);
1012 }
1013
Thomas Falcon032c5e82015-12-21 11:26:06 -06001014 tx_packets++;
1015 tx_bytes += skb->len;
1016 txq->trans_start = jiffies;
1017 ret = NETDEV_TX_OK;
1018
1019out:
1020 netdev->stats.tx_dropped += tx_dropped;
1021 netdev->stats.tx_bytes += tx_bytes;
1022 netdev->stats.tx_packets += tx_packets;
1023 adapter->tx_send_failed += tx_send_failed;
1024 adapter->tx_map_failed += tx_map_failed;
1025
1026 return ret;
1027}
1028
1029static void ibmvnic_set_multi(struct net_device *netdev)
1030{
1031 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1032 struct netdev_hw_addr *ha;
1033 union ibmvnic_crq crq;
1034
1035 memset(&crq, 0, sizeof(crq));
1036 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1037 crq.request_capability.cmd = REQUEST_CAPABILITY;
1038
1039 if (netdev->flags & IFF_PROMISC) {
1040 if (!adapter->promisc_supported)
1041 return;
1042 } else {
1043 if (netdev->flags & IFF_ALLMULTI) {
1044 /* Accept all multicast */
1045 memset(&crq, 0, sizeof(crq));
1046 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1047 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1048 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1049 ibmvnic_send_crq(adapter, &crq);
1050 } else if (netdev_mc_empty(netdev)) {
1051 /* Reject all multicast */
1052 memset(&crq, 0, sizeof(crq));
1053 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1054 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1055 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1056 ibmvnic_send_crq(adapter, &crq);
1057 } else {
1058 /* Accept one or more multicast(s) */
1059 netdev_for_each_mc_addr(ha, netdev) {
1060 memset(&crq, 0, sizeof(crq));
1061 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1062 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1063 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1064 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1065 ha->addr);
1066 ibmvnic_send_crq(adapter, &crq);
1067 }
1068 }
1069 }
1070}
1071
1072static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1073{
1074 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1075 struct sockaddr *addr = p;
1076 union ibmvnic_crq crq;
1077
1078 if (!is_valid_ether_addr(addr->sa_data))
1079 return -EADDRNOTAVAIL;
1080
1081 memset(&crq, 0, sizeof(crq));
1082 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1083 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1084 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1085 ibmvnic_send_crq(adapter, &crq);
1086 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1087 return 0;
1088}
1089
Thomas Falcon032c5e82015-12-21 11:26:06 -06001090static void ibmvnic_tx_timeout(struct net_device *dev)
1091{
1092 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1093 int rc;
1094
1095 /* Adapter timed out, resetting it */
1096 release_sub_crqs(adapter);
1097 rc = ibmvnic_reset_crq(adapter);
1098 if (rc)
1099 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1100 else
1101 ibmvnic_send_crq_init(adapter);
1102}
1103
1104static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1105 struct ibmvnic_rx_buff *rx_buff)
1106{
1107 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1108
1109 rx_buff->skb = NULL;
1110
1111 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1112 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1113
1114 atomic_dec(&pool->available);
1115}
1116
1117static int ibmvnic_poll(struct napi_struct *napi, int budget)
1118{
1119 struct net_device *netdev = napi->dev;
1120 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1121 int scrq_num = (int)(napi - adapter->napi);
1122 int frames_processed = 0;
1123restart_poll:
1124 while (frames_processed < budget) {
1125 struct sk_buff *skb;
1126 struct ibmvnic_rx_buff *rx_buff;
1127 union sub_crq *next;
1128 u32 length;
1129 u16 offset;
1130 u8 flags = 0;
1131
1132 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1133 break;
1134 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1135 rx_buff =
1136 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1137 rx_comp.correlator);
1138 /* do error checking */
1139 if (next->rx_comp.rc) {
1140 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1141 /* free the entry */
1142 next->rx_comp.first = 0;
1143 remove_buff_from_pool(adapter, rx_buff);
1144 break;
1145 }
1146
1147 length = be32_to_cpu(next->rx_comp.len);
1148 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1149 flags = next->rx_comp.flags;
1150 skb = rx_buff->skb;
1151 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1152 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001153
1154 /* VLAN Header has been stripped by the system firmware and
1155 * needs to be inserted by the driver
1156 */
1157 if (adapter->rx_vlan_header_insertion &&
1158 (flags & IBMVNIC_VLAN_STRIPPED))
1159 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1160 ntohs(next->rx_comp.vlan_tci));
1161
Thomas Falcon032c5e82015-12-21 11:26:06 -06001162 /* free the entry */
1163 next->rx_comp.first = 0;
1164 remove_buff_from_pool(adapter, rx_buff);
1165
1166 skb_put(skb, length);
1167 skb->protocol = eth_type_trans(skb, netdev);
1168
1169 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1170 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1171 skb->ip_summed = CHECKSUM_UNNECESSARY;
1172 }
1173
1174 length = skb->len;
1175 napi_gro_receive(napi, skb); /* send it up */
1176 netdev->stats.rx_packets++;
1177 netdev->stats.rx_bytes += length;
1178 frames_processed++;
1179 }
John Allen498cd8e2016-04-06 11:49:55 -05001180 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001181
1182 if (frames_processed < budget) {
1183 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001184 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001185 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1186 napi_reschedule(napi)) {
1187 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1188 goto restart_poll;
1189 }
1190 }
1191 return frames_processed;
1192}
1193
1194#ifdef CONFIG_NET_POLL_CONTROLLER
1195static void ibmvnic_netpoll_controller(struct net_device *dev)
1196{
1197 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1198 int i;
1199
1200 replenish_pools(netdev_priv(dev));
1201 for (i = 0; i < adapter->req_rx_queues; i++)
1202 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1203 adapter->rx_scrq[i]);
1204}
1205#endif
1206
1207static const struct net_device_ops ibmvnic_netdev_ops = {
1208 .ndo_open = ibmvnic_open,
1209 .ndo_stop = ibmvnic_close,
1210 .ndo_start_xmit = ibmvnic_xmit,
1211 .ndo_set_rx_mode = ibmvnic_set_multi,
1212 .ndo_set_mac_address = ibmvnic_set_mac,
1213 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001214 .ndo_tx_timeout = ibmvnic_tx_timeout,
1215#ifdef CONFIG_NET_POLL_CONTROLLER
1216 .ndo_poll_controller = ibmvnic_netpoll_controller,
1217#endif
1218};
1219
1220/* ethtool functions */
1221
Philippe Reynes8a433792017-01-07 22:37:29 +01001222static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1223 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001224{
Philippe Reynes8a433792017-01-07 22:37:29 +01001225 u32 supported, advertising;
1226
1227 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001228 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001229 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001230 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001231 cmd->base.speed = SPEED_1000;
1232 cmd->base.duplex = DUPLEX_FULL;
1233 cmd->base.port = PORT_FIBRE;
1234 cmd->base.phy_address = 0;
1235 cmd->base.autoneg = AUTONEG_ENABLE;
1236
1237 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1238 supported);
1239 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1240 advertising);
1241
Thomas Falcon032c5e82015-12-21 11:26:06 -06001242 return 0;
1243}
1244
1245static void ibmvnic_get_drvinfo(struct net_device *dev,
1246 struct ethtool_drvinfo *info)
1247{
1248 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1249 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1250}
1251
1252static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1253{
1254 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1255
1256 return adapter->msg_enable;
1257}
1258
1259static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1260{
1261 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1262
1263 adapter->msg_enable = data;
1264}
1265
1266static u32 ibmvnic_get_link(struct net_device *netdev)
1267{
1268 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1269
1270 /* Don't need to send a query because we request a logical link up at
1271 * init and then we wait for link state indications
1272 */
1273 return adapter->logical_link_state;
1274}
1275
1276static void ibmvnic_get_ringparam(struct net_device *netdev,
1277 struct ethtool_ringparam *ring)
1278{
1279 ring->rx_max_pending = 0;
1280 ring->tx_max_pending = 0;
1281 ring->rx_mini_max_pending = 0;
1282 ring->rx_jumbo_max_pending = 0;
1283 ring->rx_pending = 0;
1284 ring->tx_pending = 0;
1285 ring->rx_mini_pending = 0;
1286 ring->rx_jumbo_pending = 0;
1287}
1288
1289static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1290{
1291 int i;
1292
1293 if (stringset != ETH_SS_STATS)
1294 return;
1295
1296 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1297 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1298}
1299
1300static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1301{
1302 switch (sset) {
1303 case ETH_SS_STATS:
1304 return ARRAY_SIZE(ibmvnic_stats);
1305 default:
1306 return -EOPNOTSUPP;
1307 }
1308}
1309
1310static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1311 struct ethtool_stats *stats, u64 *data)
1312{
1313 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1314 union ibmvnic_crq crq;
1315 int i;
1316
1317 memset(&crq, 0, sizeof(crq));
1318 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1319 crq.request_statistics.cmd = REQUEST_STATISTICS;
1320 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1321 crq.request_statistics.len =
1322 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001323
1324 /* Wait for data to be written */
1325 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001326 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001327 wait_for_completion(&adapter->stats_done);
1328
1329 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1330 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1331}
1332
1333static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001334 .get_drvinfo = ibmvnic_get_drvinfo,
1335 .get_msglevel = ibmvnic_get_msglevel,
1336 .set_msglevel = ibmvnic_set_msglevel,
1337 .get_link = ibmvnic_get_link,
1338 .get_ringparam = ibmvnic_get_ringparam,
1339 .get_strings = ibmvnic_get_strings,
1340 .get_sset_count = ibmvnic_get_sset_count,
1341 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001342 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001343};
1344
1345/* Routines for managing CRQs/sCRQs */
1346
1347static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1348 struct ibmvnic_sub_crq_queue *scrq)
1349{
1350 struct device *dev = &adapter->vdev->dev;
1351 long rc;
1352
1353 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1354
1355 /* Close the sub-crqs */
1356 do {
1357 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1358 adapter->vdev->unit_address,
1359 scrq->crq_num);
1360 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1361
Thomas Falconffa73852017-04-19 13:44:29 -04001362 if (rc) {
1363 netdev_err(adapter->netdev,
1364 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1365 scrq->crq_num, rc);
1366 }
1367
Thomas Falcon032c5e82015-12-21 11:26:06 -06001368 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1369 DMA_BIDIRECTIONAL);
1370 free_pages((unsigned long)scrq->msgs, 2);
1371 kfree(scrq);
1372}
1373
1374static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1375 *adapter)
1376{
1377 struct device *dev = &adapter->vdev->dev;
1378 struct ibmvnic_sub_crq_queue *scrq;
1379 int rc;
1380
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001381 scrq = kzalloc(sizeof(*scrq), GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001382 if (!scrq)
1383 return NULL;
1384
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001385 scrq->msgs =
1386 (union sub_crq *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001387 if (!scrq->msgs) {
1388 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1389 goto zero_page_failed;
1390 }
1391
1392 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1393 DMA_BIDIRECTIONAL);
1394 if (dma_mapping_error(dev, scrq->msg_token)) {
1395 dev_warn(dev, "Couldn't map crq queue messages page\n");
1396 goto map_failed;
1397 }
1398
1399 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1400 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1401
1402 if (rc == H_RESOURCE)
1403 rc = ibmvnic_reset_crq(adapter);
1404
1405 if (rc == H_CLOSED) {
1406 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1407 } else if (rc) {
1408 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1409 goto reg_failed;
1410 }
1411
Thomas Falcon032c5e82015-12-21 11:26:06 -06001412 scrq->adapter = adapter;
1413 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001414 spin_lock_init(&scrq->lock);
1415
1416 netdev_dbg(adapter->netdev,
1417 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1418 scrq->crq_num, scrq->hw_irq, scrq->irq);
1419
1420 return scrq;
1421
Thomas Falcon032c5e82015-12-21 11:26:06 -06001422reg_failed:
1423 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1424 DMA_BIDIRECTIONAL);
1425map_failed:
1426 free_pages((unsigned long)scrq->msgs, 2);
1427zero_page_failed:
1428 kfree(scrq);
1429
1430 return NULL;
1431}
1432
1433static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1434{
1435 int i;
1436
1437 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001438 for (i = 0; i < adapter->req_tx_queues; i++) {
1439 if (!adapter->tx_scrq[i])
1440 continue;
1441
1442 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001443 free_irq(adapter->tx_scrq[i]->irq,
1444 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001445 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001446 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001447 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001448
1449 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1450 }
1451
Nathan Fontenot9501df32017-03-15 23:38:07 -04001452 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001453 adapter->tx_scrq = NULL;
1454 }
1455
1456 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001457 for (i = 0; i < adapter->req_rx_queues; i++) {
1458 if (!adapter->rx_scrq[i])
1459 continue;
1460
1461 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001462 free_irq(adapter->rx_scrq[i]->irq,
1463 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001464 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001465 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001466 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001467
1468 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1469 }
1470
Nathan Fontenot9501df32017-03-15 23:38:07 -04001471 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001472 adapter->rx_scrq = NULL;
1473 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001474}
1475
1476static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1477 struct ibmvnic_sub_crq_queue *scrq)
1478{
1479 struct device *dev = &adapter->vdev->dev;
1480 unsigned long rc;
1481
1482 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1483 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1484 if (rc)
1485 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1486 scrq->hw_irq, rc);
1487 return rc;
1488}
1489
1490static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1491 struct ibmvnic_sub_crq_queue *scrq)
1492{
1493 struct device *dev = &adapter->vdev->dev;
1494 unsigned long rc;
1495
1496 if (scrq->hw_irq > 0x100000000ULL) {
1497 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1498 return 1;
1499 }
1500
1501 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1502 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1503 if (rc)
1504 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1505 scrq->hw_irq, rc);
1506 return rc;
1507}
1508
1509static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1510 struct ibmvnic_sub_crq_queue *scrq)
1511{
1512 struct device *dev = &adapter->vdev->dev;
1513 struct ibmvnic_tx_buff *txbuff;
1514 union sub_crq *next;
1515 int index;
1516 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001517 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001518
1519restart_loop:
1520 while (pending_scrq(adapter, scrq)) {
1521 unsigned int pool = scrq->pool_index;
1522
1523 next = ibmvnic_next_scrq(adapter, scrq);
1524 for (i = 0; i < next->tx_comp.num_comps; i++) {
1525 if (next->tx_comp.rcs[i]) {
1526 dev_err(dev, "tx error %x\n",
1527 next->tx_comp.rcs[i]);
1528 continue;
1529 }
1530 index = be32_to_cpu(next->tx_comp.correlators[i]);
1531 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1532
1533 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1534 if (!txbuff->data_dma[j])
1535 continue;
1536
1537 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001538 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001539 /* if sub_crq was sent indirectly */
1540 first = txbuff->indir_arr[0].generic.first;
1541 if (first == IBMVNIC_CRQ_CMD) {
1542 dma_unmap_single(dev, txbuff->indir_dma,
1543 sizeof(txbuff->indir_arr),
1544 DMA_TO_DEVICE);
1545 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001546
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001547 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001548 if (atomic_sub_return(next->tx_comp.num_comps,
1549 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001550 (adapter->req_tx_entries_per_subcrq / 2) &&
1551 netif_subqueue_stopped(adapter->netdev,
1552 txbuff->skb)) {
1553 netif_wake_subqueue(adapter->netdev,
1554 scrq->pool_index);
1555 netdev_dbg(adapter->netdev,
1556 "Started queue %d\n",
1557 scrq->pool_index);
1558 }
1559
Thomas Falcon032c5e82015-12-21 11:26:06 -06001560 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001561 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001562
1563 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1564 producer_index] = index;
1565 adapter->tx_pool[pool].producer_index =
1566 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001567 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001568 }
1569 /* remove tx_comp scrq*/
1570 next->tx_comp.first = 0;
1571 }
1572
1573 enable_scrq_irq(adapter, scrq);
1574
1575 if (pending_scrq(adapter, scrq)) {
1576 disable_scrq_irq(adapter, scrq);
1577 goto restart_loop;
1578 }
1579
1580 return 0;
1581}
1582
1583static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1584{
1585 struct ibmvnic_sub_crq_queue *scrq = instance;
1586 struct ibmvnic_adapter *adapter = scrq->adapter;
1587
1588 disable_scrq_irq(adapter, scrq);
1589 ibmvnic_complete_tx(adapter, scrq);
1590
1591 return IRQ_HANDLED;
1592}
1593
1594static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1595{
1596 struct ibmvnic_sub_crq_queue *scrq = instance;
1597 struct ibmvnic_adapter *adapter = scrq->adapter;
1598
1599 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1600 disable_scrq_irq(adapter, scrq);
1601 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1602 }
1603
1604 return IRQ_HANDLED;
1605}
1606
Thomas Falconea22d512016-07-06 15:35:17 -05001607static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1608{
1609 struct device *dev = &adapter->vdev->dev;
1610 struct ibmvnic_sub_crq_queue *scrq;
1611 int i = 0, j = 0;
1612 int rc = 0;
1613
1614 for (i = 0; i < adapter->req_tx_queues; i++) {
1615 scrq = adapter->tx_scrq[i];
1616 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1617
Michael Ellerman99c17902016-09-10 19:59:05 +10001618 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001619 rc = -EINVAL;
1620 dev_err(dev, "Error mapping irq\n");
1621 goto req_tx_irq_failed;
1622 }
1623
1624 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1625 0, "ibmvnic_tx", scrq);
1626
1627 if (rc) {
1628 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1629 scrq->irq, rc);
1630 irq_dispose_mapping(scrq->irq);
1631 goto req_rx_irq_failed;
1632 }
1633 }
1634
1635 for (i = 0; i < adapter->req_rx_queues; i++) {
1636 scrq = adapter->rx_scrq[i];
1637 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001638 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001639 rc = -EINVAL;
1640 dev_err(dev, "Error mapping irq\n");
1641 goto req_rx_irq_failed;
1642 }
1643 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1644 0, "ibmvnic_rx", scrq);
1645 if (rc) {
1646 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1647 scrq->irq, rc);
1648 irq_dispose_mapping(scrq->irq);
1649 goto req_rx_irq_failed;
1650 }
1651 }
1652 return rc;
1653
1654req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001655 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001656 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1657 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001658 }
Thomas Falconea22d512016-07-06 15:35:17 -05001659 i = adapter->req_tx_queues;
1660req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001661 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001662 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1663 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001664 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001665 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001666 return rc;
1667}
1668
Thomas Falcon032c5e82015-12-21 11:26:06 -06001669static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1670{
1671 struct device *dev = &adapter->vdev->dev;
1672 struct ibmvnic_sub_crq_queue **allqueues;
1673 int registered_queues = 0;
1674 union ibmvnic_crq crq;
1675 int total_queues;
1676 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001677 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001678
1679 if (!retry) {
1680 /* Sub-CRQ entries are 32 byte long */
1681 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1682
1683 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1684 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1685 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1686 goto allqueues_failed;
1687 }
1688
1689 /* Get the minimum between the queried max and the entries
1690 * that fit in our PAGE_SIZE
1691 */
1692 adapter->req_tx_entries_per_subcrq =
1693 adapter->max_tx_entries_per_subcrq > entries_page ?
1694 entries_page : adapter->max_tx_entries_per_subcrq;
1695 adapter->req_rx_add_entries_per_subcrq =
1696 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1697 entries_page : adapter->max_rx_add_entries_per_subcrq;
1698
John Allen6dbcd8f2016-11-07 14:27:28 -06001699 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1700 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001701 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001702
Thomas Falconf39f0d12017-02-14 10:22:59 -06001703 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001704 }
1705
1706 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1707
1708 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1709 if (!allqueues)
1710 goto allqueues_failed;
1711
1712 for (i = 0; i < total_queues; i++) {
1713 allqueues[i] = init_sub_crq_queue(adapter);
1714 if (!allqueues[i]) {
1715 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1716 break;
1717 }
1718 registered_queues++;
1719 }
1720
1721 /* Make sure we were able to register the minimum number of queues */
1722 if (registered_queues <
1723 adapter->min_tx_queues + adapter->min_rx_queues) {
1724 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1725 goto tx_failed;
1726 }
1727
1728 /* Distribute the failed allocated queues*/
1729 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1730 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1731 switch (i % 3) {
1732 case 0:
1733 if (adapter->req_rx_queues > adapter->min_rx_queues)
1734 adapter->req_rx_queues--;
1735 else
1736 more++;
1737 break;
1738 case 1:
1739 if (adapter->req_tx_queues > adapter->min_tx_queues)
1740 adapter->req_tx_queues--;
1741 else
1742 more++;
1743 break;
1744 }
1745 }
1746
1747 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1748 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1749 if (!adapter->tx_scrq)
1750 goto tx_failed;
1751
1752 for (i = 0; i < adapter->req_tx_queues; i++) {
1753 adapter->tx_scrq[i] = allqueues[i];
1754 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001755 }
1756
1757 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1758 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1759 if (!adapter->rx_scrq)
1760 goto rx_failed;
1761
1762 for (i = 0; i < adapter->req_rx_queues; i++) {
1763 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1764 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001765 }
1766
1767 memset(&crq, 0, sizeof(crq));
1768 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1769 crq.request_capability.cmd = REQUEST_CAPABILITY;
1770
1771 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001772 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001773 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001774 ibmvnic_send_crq(adapter, &crq);
1775
1776 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001777 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001778 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001779 ibmvnic_send_crq(adapter, &crq);
1780
1781 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001782 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001783 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001784 ibmvnic_send_crq(adapter, &crq);
1785
1786 crq.request_capability.capability =
1787 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1788 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001789 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001790 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001791 ibmvnic_send_crq(adapter, &crq);
1792
1793 crq.request_capability.capability =
1794 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1795 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001796 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001797 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001798 ibmvnic_send_crq(adapter, &crq);
1799
1800 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001801 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001802 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001803 ibmvnic_send_crq(adapter, &crq);
1804
1805 if (adapter->netdev->flags & IFF_PROMISC) {
1806 if (adapter->promisc_supported) {
1807 crq.request_capability.capability =
1808 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001809 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001810 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001811 ibmvnic_send_crq(adapter, &crq);
1812 }
1813 } else {
1814 crq.request_capability.capability =
1815 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001816 crq.request_capability.number = cpu_to_be64(0);
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
1821 kfree(allqueues);
1822
1823 return;
1824
Thomas Falcon032c5e82015-12-21 11:26:06 -06001825rx_failed:
1826 kfree(adapter->tx_scrq);
1827 adapter->tx_scrq = NULL;
1828tx_failed:
1829 for (i = 0; i < registered_queues; i++)
1830 release_sub_crq_queue(adapter, allqueues[i]);
1831 kfree(allqueues);
1832allqueues_failed:
1833 ibmvnic_remove(adapter->vdev);
1834}
1835
1836static int pending_scrq(struct ibmvnic_adapter *adapter,
1837 struct ibmvnic_sub_crq_queue *scrq)
1838{
1839 union sub_crq *entry = &scrq->msgs[scrq->cur];
1840
1841 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1842 return 1;
1843 else
1844 return 0;
1845}
1846
1847static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1848 struct ibmvnic_sub_crq_queue *scrq)
1849{
1850 union sub_crq *entry;
1851 unsigned long flags;
1852
1853 spin_lock_irqsave(&scrq->lock, flags);
1854 entry = &scrq->msgs[scrq->cur];
1855 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1856 if (++scrq->cur == scrq->size)
1857 scrq->cur = 0;
1858 } else {
1859 entry = NULL;
1860 }
1861 spin_unlock_irqrestore(&scrq->lock, flags);
1862
1863 return entry;
1864}
1865
1866static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1867{
1868 struct ibmvnic_crq_queue *queue = &adapter->crq;
1869 union ibmvnic_crq *crq;
1870
1871 crq = &queue->msgs[queue->cur];
1872 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1873 if (++queue->cur == queue->size)
1874 queue->cur = 0;
1875 } else {
1876 crq = NULL;
1877 }
1878
1879 return crq;
1880}
1881
1882static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1883 union sub_crq *sub_crq)
1884{
1885 unsigned int ua = adapter->vdev->unit_address;
1886 struct device *dev = &adapter->vdev->dev;
1887 u64 *u64_crq = (u64 *)sub_crq;
1888 int rc;
1889
1890 netdev_dbg(adapter->netdev,
1891 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1892 (unsigned long int)cpu_to_be64(remote_handle),
1893 (unsigned long int)cpu_to_be64(u64_crq[0]),
1894 (unsigned long int)cpu_to_be64(u64_crq[1]),
1895 (unsigned long int)cpu_to_be64(u64_crq[2]),
1896 (unsigned long int)cpu_to_be64(u64_crq[3]));
1897
1898 /* Make sure the hypervisor sees the complete request */
1899 mb();
1900
1901 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1902 cpu_to_be64(remote_handle),
1903 cpu_to_be64(u64_crq[0]),
1904 cpu_to_be64(u64_crq[1]),
1905 cpu_to_be64(u64_crq[2]),
1906 cpu_to_be64(u64_crq[3]));
1907
1908 if (rc) {
1909 if (rc == H_CLOSED)
1910 dev_warn(dev, "CRQ Queue closed\n");
1911 dev_err(dev, "Send error (rc=%d)\n", rc);
1912 }
1913
1914 return rc;
1915}
1916
Thomas Falconad7775d2016-04-01 17:20:34 -05001917static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1918 u64 remote_handle, u64 ioba, u64 num_entries)
1919{
1920 unsigned int ua = adapter->vdev->unit_address;
1921 struct device *dev = &adapter->vdev->dev;
1922 int rc;
1923
1924 /* Make sure the hypervisor sees the complete request */
1925 mb();
1926 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1927 cpu_to_be64(remote_handle),
1928 ioba, num_entries);
1929
1930 if (rc) {
1931 if (rc == H_CLOSED)
1932 dev_warn(dev, "CRQ Queue closed\n");
1933 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1934 }
1935
1936 return rc;
1937}
1938
Thomas Falcon032c5e82015-12-21 11:26:06 -06001939static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1940 union ibmvnic_crq *crq)
1941{
1942 unsigned int ua = adapter->vdev->unit_address;
1943 struct device *dev = &adapter->vdev->dev;
1944 u64 *u64_crq = (u64 *)crq;
1945 int rc;
1946
1947 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1948 (unsigned long int)cpu_to_be64(u64_crq[0]),
1949 (unsigned long int)cpu_to_be64(u64_crq[1]));
1950
1951 /* Make sure the hypervisor sees the complete request */
1952 mb();
1953
1954 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1955 cpu_to_be64(u64_crq[0]),
1956 cpu_to_be64(u64_crq[1]));
1957
1958 if (rc) {
1959 if (rc == H_CLOSED)
1960 dev_warn(dev, "CRQ Queue closed\n");
1961 dev_warn(dev, "Send error (rc=%d)\n", rc);
1962 }
1963
1964 return rc;
1965}
1966
1967static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1968{
1969 union ibmvnic_crq crq;
1970
1971 memset(&crq, 0, sizeof(crq));
1972 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1973 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1974 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1975
1976 return ibmvnic_send_crq(adapter, &crq);
1977}
1978
1979static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1980{
1981 union ibmvnic_crq crq;
1982
1983 memset(&crq, 0, sizeof(crq));
1984 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1985 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1986 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1987
1988 return ibmvnic_send_crq(adapter, &crq);
1989}
1990
1991static int send_version_xchg(struct ibmvnic_adapter *adapter)
1992{
1993 union ibmvnic_crq crq;
1994
1995 memset(&crq, 0, sizeof(crq));
1996 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1997 crq.version_exchange.cmd = VERSION_EXCHANGE;
1998 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1999
2000 return ibmvnic_send_crq(adapter, &crq);
2001}
2002
2003static void send_login(struct ibmvnic_adapter *adapter)
2004{
2005 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2006 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002007 struct device *dev = &adapter->vdev->dev;
2008 dma_addr_t rsp_buffer_token;
2009 dma_addr_t buffer_token;
2010 size_t rsp_buffer_size;
2011 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002012 size_t buffer_size;
2013 __be64 *tx_list_p;
2014 __be64 *rx_list_p;
2015 int i;
2016
2017 buffer_size =
2018 sizeof(struct ibmvnic_login_buffer) +
2019 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2020
2021 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2022 if (!login_buffer)
2023 goto buf_alloc_failed;
2024
2025 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2026 DMA_TO_DEVICE);
2027 if (dma_mapping_error(dev, buffer_token)) {
2028 dev_err(dev, "Couldn't map login buffer\n");
2029 goto buf_map_failed;
2030 }
2031
John Allen498cd8e2016-04-06 11:49:55 -05002032 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2033 sizeof(u64) * adapter->req_tx_queues +
2034 sizeof(u64) * adapter->req_rx_queues +
2035 sizeof(u64) * adapter->req_rx_queues +
2036 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002037
2038 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2039 if (!login_rsp_buffer)
2040 goto buf_rsp_alloc_failed;
2041
2042 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2043 rsp_buffer_size, DMA_FROM_DEVICE);
2044 if (dma_mapping_error(dev, rsp_buffer_token)) {
2045 dev_err(dev, "Couldn't map login rsp buffer\n");
2046 goto buf_rsp_map_failed;
2047 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002048
Thomas Falcon032c5e82015-12-21 11:26:06 -06002049 adapter->login_buf = login_buffer;
2050 adapter->login_buf_token = buffer_token;
2051 adapter->login_buf_sz = buffer_size;
2052 adapter->login_rsp_buf = login_rsp_buffer;
2053 adapter->login_rsp_buf_token = rsp_buffer_token;
2054 adapter->login_rsp_buf_sz = rsp_buffer_size;
2055
2056 login_buffer->len = cpu_to_be32(buffer_size);
2057 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2058 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2059 login_buffer->off_txcomp_subcrqs =
2060 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2061 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2062 login_buffer->off_rxcomp_subcrqs =
2063 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2064 sizeof(u64) * adapter->req_tx_queues);
2065 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2066 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2067
2068 tx_list_p = (__be64 *)((char *)login_buffer +
2069 sizeof(struct ibmvnic_login_buffer));
2070 rx_list_p = (__be64 *)((char *)login_buffer +
2071 sizeof(struct ibmvnic_login_buffer) +
2072 sizeof(u64) * adapter->req_tx_queues);
2073
2074 for (i = 0; i < adapter->req_tx_queues; i++) {
2075 if (adapter->tx_scrq[i]) {
2076 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2077 crq_num);
2078 }
2079 }
2080
2081 for (i = 0; i < adapter->req_rx_queues; i++) {
2082 if (adapter->rx_scrq[i]) {
2083 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2084 crq_num);
2085 }
2086 }
2087
2088 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2089 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2090 netdev_dbg(adapter->netdev, "%016lx\n",
2091 ((unsigned long int *)(adapter->login_buf))[i]);
2092 }
2093
2094 memset(&crq, 0, sizeof(crq));
2095 crq.login.first = IBMVNIC_CRQ_CMD;
2096 crq.login.cmd = LOGIN;
2097 crq.login.ioba = cpu_to_be32(buffer_token);
2098 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002099 ibmvnic_send_crq(adapter, &crq);
2100
2101 return;
2102
Thomas Falcon032c5e82015-12-21 11:26:06 -06002103buf_rsp_map_failed:
2104 kfree(login_rsp_buffer);
2105buf_rsp_alloc_failed:
2106 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2107buf_map_failed:
2108 kfree(login_buffer);
2109buf_alloc_failed:
2110 return;
2111}
2112
2113static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2114 u32 len, u8 map_id)
2115{
2116 union ibmvnic_crq crq;
2117
2118 memset(&crq, 0, sizeof(crq));
2119 crq.request_map.first = IBMVNIC_CRQ_CMD;
2120 crq.request_map.cmd = REQUEST_MAP;
2121 crq.request_map.map_id = map_id;
2122 crq.request_map.ioba = cpu_to_be32(addr);
2123 crq.request_map.len = cpu_to_be32(len);
2124 ibmvnic_send_crq(adapter, &crq);
2125}
2126
2127static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2128{
2129 union ibmvnic_crq crq;
2130
2131 memset(&crq, 0, sizeof(crq));
2132 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2133 crq.request_unmap.cmd = REQUEST_UNMAP;
2134 crq.request_unmap.map_id = map_id;
2135 ibmvnic_send_crq(adapter, &crq);
2136}
2137
2138static void send_map_query(struct ibmvnic_adapter *adapter)
2139{
2140 union ibmvnic_crq crq;
2141
2142 memset(&crq, 0, sizeof(crq));
2143 crq.query_map.first = IBMVNIC_CRQ_CMD;
2144 crq.query_map.cmd = QUERY_MAP;
2145 ibmvnic_send_crq(adapter, &crq);
2146}
2147
2148/* Send a series of CRQs requesting various capabilities of the VNIC server */
2149static void send_cap_queries(struct ibmvnic_adapter *adapter)
2150{
2151 union ibmvnic_crq crq;
2152
Thomas Falcon901e0402017-02-15 12:17:59 -06002153 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002154 memset(&crq, 0, sizeof(crq));
2155 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2156 crq.query_capability.cmd = QUERY_CAPABILITY;
2157
2158 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002159 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002160 ibmvnic_send_crq(adapter, &crq);
2161
2162 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002163 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002164 ibmvnic_send_crq(adapter, &crq);
2165
2166 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002167 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002168 ibmvnic_send_crq(adapter, &crq);
2169
2170 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002171 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002172 ibmvnic_send_crq(adapter, &crq);
2173
2174 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002175 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002176 ibmvnic_send_crq(adapter, &crq);
2177
2178 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_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 =
2183 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002184 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002185 ibmvnic_send_crq(adapter, &crq);
2186
2187 crq.query_capability.capability =
2188 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002189 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002190 ibmvnic_send_crq(adapter, &crq);
2191
2192 crq.query_capability.capability =
2193 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002194 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002195 ibmvnic_send_crq(adapter, &crq);
2196
2197 crq.query_capability.capability =
2198 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002199 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002200 ibmvnic_send_crq(adapter, &crq);
2201
2202 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002203 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002204 ibmvnic_send_crq(adapter, &crq);
2205
2206 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002207 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002208 ibmvnic_send_crq(adapter, &crq);
2209
2210 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002211 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002212 ibmvnic_send_crq(adapter, &crq);
2213
2214 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002215 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002216 ibmvnic_send_crq(adapter, &crq);
2217
2218 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
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(VLAN_HEADER_INSERTION);
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
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002226 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2227 atomic_inc(&adapter->running_cap_crqs);
2228 ibmvnic_send_crq(adapter, &crq);
2229
Thomas Falcon032c5e82015-12-21 11:26:06 -06002230 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
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(RX_SG_SUPPORTED);
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(OPT_TX_COMP_SUB_QUEUES);
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(OPT_RX_COMP_QUEUES);
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
2246 crq.query_capability.capability =
2247 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002248 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002249 ibmvnic_send_crq(adapter, &crq);
2250
2251 crq.query_capability.capability =
2252 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002253 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002254 ibmvnic_send_crq(adapter, &crq);
2255
2256 crq.query_capability.capability =
2257 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002258 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002259 ibmvnic_send_crq(adapter, &crq);
2260
2261 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002262 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002263 ibmvnic_send_crq(adapter, &crq);
2264}
2265
2266static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2267{
2268 struct device *dev = &adapter->vdev->dev;
2269 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2270 union ibmvnic_crq crq;
2271 int i;
2272
2273 dma_unmap_single(dev, adapter->ip_offload_tok,
2274 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2275
2276 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2277 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2278 netdev_dbg(adapter->netdev, "%016lx\n",
2279 ((unsigned long int *)(buf))[i]);
2280
2281 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2282 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2283 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2284 buf->tcp_ipv4_chksum);
2285 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2286 buf->tcp_ipv6_chksum);
2287 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2288 buf->udp_ipv4_chksum);
2289 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2290 buf->udp_ipv6_chksum);
2291 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2292 buf->large_tx_ipv4);
2293 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2294 buf->large_tx_ipv6);
2295 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2296 buf->large_rx_ipv4);
2297 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2298 buf->large_rx_ipv6);
2299 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2300 buf->max_ipv4_header_size);
2301 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2302 buf->max_ipv6_header_size);
2303 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2304 buf->max_tcp_header_size);
2305 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2306 buf->max_udp_header_size);
2307 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2308 buf->max_large_tx_size);
2309 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2310 buf->max_large_rx_size);
2311 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2312 buf->ipv6_extension_header);
2313 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2314 buf->tcp_pseudosum_req);
2315 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2316 buf->num_ipv6_ext_headers);
2317 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2318 buf->off_ipv6_ext_headers);
2319
2320 adapter->ip_offload_ctrl_tok =
2321 dma_map_single(dev, &adapter->ip_offload_ctrl,
2322 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2323
2324 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2325 dev_err(dev, "Couldn't map ip offload control buffer\n");
2326 return;
2327 }
2328
2329 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2330 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2331 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2332 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2333 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2334
2335 /* large_tx/rx disabled for now, additional features needed */
2336 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2337 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2338 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2339 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2340
2341 adapter->netdev->features = NETIF_F_GSO;
2342
2343 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2344 adapter->netdev->features |= NETIF_F_IP_CSUM;
2345
2346 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2347 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2348
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002349 if ((adapter->netdev->features &
2350 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2351 adapter->netdev->features |= NETIF_F_RXCSUM;
2352
Thomas Falcon032c5e82015-12-21 11:26:06 -06002353 memset(&crq, 0, sizeof(crq));
2354 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2355 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2356 crq.control_ip_offload.len =
2357 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2358 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2359 ibmvnic_send_crq(adapter, &crq);
2360}
2361
2362static void handle_error_info_rsp(union ibmvnic_crq *crq,
2363 struct ibmvnic_adapter *adapter)
2364{
2365 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002366 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002367 unsigned long flags;
2368 bool found = false;
2369 int i;
2370
2371 if (!crq->request_error_rsp.rc.code) {
2372 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2373 crq->request_error_rsp.rc.code);
2374 return;
2375 }
2376
2377 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002378 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002379 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2380 found = true;
2381 list_del(&error_buff->list);
2382 break;
2383 }
2384 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2385
2386 if (!found) {
2387 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002388 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002389 return;
2390 }
2391
2392 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002393 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002394
2395 for (i = 0; i < error_buff->len; i++) {
2396 pr_cont("%02x", (int)error_buff->buff[i]);
2397 if (i % 8 == 7)
2398 pr_cont(" ");
2399 }
2400 pr_cont("\n");
2401
2402 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2403 DMA_FROM_DEVICE);
2404 kfree(error_buff->buff);
2405 kfree(error_buff);
2406}
2407
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002408static void request_error_information(struct ibmvnic_adapter *adapter,
2409 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002410{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002411 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002412 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002413 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002414 unsigned long timeout = msecs_to_jiffies(30000);
2415 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002417 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002418
2419 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2420 if (!error_buff)
2421 return;
2422
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002423 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002424 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2425 if (!error_buff->buff) {
2426 kfree(error_buff);
2427 return;
2428 }
2429
2430 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2431 DMA_FROM_DEVICE);
2432 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002433 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002434 kfree(error_buff->buff);
2435 kfree(error_buff);
2436 return;
2437 }
2438
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002440 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002441
2442 spin_lock_irqsave(&adapter->error_list_lock, flags);
2443 list_add_tail(&error_buff->list, &adapter->errors);
2444 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2445
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002446 memset(&crq, 0, sizeof(crq));
2447 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2448 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2449 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2450 crq.request_error_info.len = cpu_to_be32(detail_len);
2451 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2452
2453 rc = ibmvnic_send_crq(adapter, &crq);
2454 if (rc) {
2455 netdev_err(netdev, "failed to request error information\n");
2456 goto err_info_fail;
2457 }
2458
2459 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2460 netdev_err(netdev, "timeout waiting for error information\n");
2461 goto err_info_fail;
2462 }
2463
2464 return;
2465
2466err_info_fail:
2467 spin_lock_irqsave(&adapter->error_list_lock, flags);
2468 list_del(&error_buff->list);
2469 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2470
2471 kfree(error_buff->buff);
2472 kfree(error_buff);
2473}
2474
2475static void handle_error_indication(union ibmvnic_crq *crq,
2476 struct ibmvnic_adapter *adapter)
2477{
2478 struct device *dev = &adapter->vdev->dev;
2479
2480 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2481 crq->error_indication.flags
2482 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2483 be32_to_cpu(crq->error_indication.error_id),
2484 be16_to_cpu(crq->error_indication.error_cause));
2485
2486 if (be32_to_cpu(crq->error_indication.error_id))
2487 request_error_information(adapter, crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002488}
2489
2490static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2491 struct ibmvnic_adapter *adapter)
2492{
2493 struct net_device *netdev = adapter->netdev;
2494 struct device *dev = &adapter->vdev->dev;
2495 long rc;
2496
2497 rc = crq->change_mac_addr_rsp.rc.code;
2498 if (rc) {
2499 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2500 return;
2501 }
2502 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2503 ETH_ALEN);
2504}
2505
2506static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2507 struct ibmvnic_adapter *adapter)
2508{
2509 struct device *dev = &adapter->vdev->dev;
2510 u64 *req_value;
2511 char *name;
2512
Thomas Falcon901e0402017-02-15 12:17:59 -06002513 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002514 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2515 case REQ_TX_QUEUES:
2516 req_value = &adapter->req_tx_queues;
2517 name = "tx";
2518 break;
2519 case REQ_RX_QUEUES:
2520 req_value = &adapter->req_rx_queues;
2521 name = "rx";
2522 break;
2523 case REQ_RX_ADD_QUEUES:
2524 req_value = &adapter->req_rx_add_queues;
2525 name = "rx_add";
2526 break;
2527 case REQ_TX_ENTRIES_PER_SUBCRQ:
2528 req_value = &adapter->req_tx_entries_per_subcrq;
2529 name = "tx_entries_per_subcrq";
2530 break;
2531 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2532 req_value = &adapter->req_rx_add_entries_per_subcrq;
2533 name = "rx_add_entries_per_subcrq";
2534 break;
2535 case REQ_MTU:
2536 req_value = &adapter->req_mtu;
2537 name = "mtu";
2538 break;
2539 case PROMISC_REQUESTED:
2540 req_value = &adapter->promisc;
2541 name = "promisc";
2542 break;
2543 default:
2544 dev_err(dev, "Got invalid cap request rsp %d\n",
2545 crq->request_capability.capability);
2546 return;
2547 }
2548
2549 switch (crq->request_capability_rsp.rc.code) {
2550 case SUCCESS:
2551 break;
2552 case PARTIALSUCCESS:
2553 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2554 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002555 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002556 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002557 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002558 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002559 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002560 return;
2561 default:
2562 dev_err(dev, "Error %d in request cap rsp\n",
2563 crq->request_capability_rsp.rc.code);
2564 return;
2565 }
2566
2567 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002568 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002569 union ibmvnic_crq newcrq;
2570 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2571 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2572 &adapter->ip_offload_buf;
2573
Thomas Falcon249168a2017-02-15 12:18:00 -06002574 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002575 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2576 buf_sz,
2577 DMA_FROM_DEVICE);
2578
2579 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2580 if (!firmware_has_feature(FW_FEATURE_CMO))
2581 dev_err(dev, "Couldn't map offload buffer\n");
2582 return;
2583 }
2584
2585 memset(&newcrq, 0, sizeof(newcrq));
2586 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2587 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2588 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2589 newcrq.query_ip_offload.ioba =
2590 cpu_to_be32(adapter->ip_offload_tok);
2591
2592 ibmvnic_send_crq(adapter, &newcrq);
2593 }
2594}
2595
2596static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2597 struct ibmvnic_adapter *adapter)
2598{
2599 struct device *dev = &adapter->vdev->dev;
2600 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2601 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002602 int i;
2603
2604 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2605 DMA_BIDIRECTIONAL);
2606 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2607 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2608
John Allen498cd8e2016-04-06 11:49:55 -05002609 /* If the number of queues requested can't be allocated by the
2610 * server, the login response will return with code 1. We will need
2611 * to resend the login buffer with fewer queues requested.
2612 */
2613 if (login_rsp_crq->generic.rc.code) {
2614 adapter->renegotiate = true;
2615 complete(&adapter->init_done);
2616 return 0;
2617 }
2618
Thomas Falcon032c5e82015-12-21 11:26:06 -06002619 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2620 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2621 netdev_dbg(adapter->netdev, "%016lx\n",
2622 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2623 }
2624
2625 /* Sanity checks */
2626 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2627 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2628 adapter->req_rx_add_queues !=
2629 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2630 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2631 ibmvnic_remove(adapter->vdev);
2632 return -EIO;
2633 }
2634 complete(&adapter->init_done);
2635
Thomas Falcon032c5e82015-12-21 11:26:06 -06002636 return 0;
2637}
2638
2639static void handle_request_map_rsp(union ibmvnic_crq *crq,
2640 struct ibmvnic_adapter *adapter)
2641{
2642 struct device *dev = &adapter->vdev->dev;
2643 u8 map_id = crq->request_map_rsp.map_id;
2644 int tx_subcrqs;
2645 int rx_subcrqs;
2646 long rc;
2647 int i;
2648
2649 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2650 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2651
2652 rc = crq->request_map_rsp.rc.code;
2653 if (rc) {
2654 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2655 adapter->map_id--;
2656 /* need to find and zero tx/rx_pool map_id */
2657 for (i = 0; i < tx_subcrqs; i++) {
2658 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2659 adapter->tx_pool[i].long_term_buff.map_id = 0;
2660 }
2661 for (i = 0; i < rx_subcrqs; i++) {
2662 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2663 adapter->rx_pool[i].long_term_buff.map_id = 0;
2664 }
2665 }
2666 complete(&adapter->fw_done);
2667}
2668
2669static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2670 struct ibmvnic_adapter *adapter)
2671{
2672 struct device *dev = &adapter->vdev->dev;
2673 long rc;
2674
2675 rc = crq->request_unmap_rsp.rc.code;
2676 if (rc)
2677 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2678}
2679
2680static void handle_query_map_rsp(union ibmvnic_crq *crq,
2681 struct ibmvnic_adapter *adapter)
2682{
2683 struct net_device *netdev = adapter->netdev;
2684 struct device *dev = &adapter->vdev->dev;
2685 long rc;
2686
2687 rc = crq->query_map_rsp.rc.code;
2688 if (rc) {
2689 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2690 return;
2691 }
2692 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2693 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2694 crq->query_map_rsp.free_pages);
2695}
2696
2697static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2698 struct ibmvnic_adapter *adapter)
2699{
2700 struct net_device *netdev = adapter->netdev;
2701 struct device *dev = &adapter->vdev->dev;
2702 long rc;
2703
Thomas Falcon901e0402017-02-15 12:17:59 -06002704 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002705 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002706 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002707 rc = crq->query_capability.rc.code;
2708 if (rc) {
2709 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2710 goto out;
2711 }
2712
2713 switch (be16_to_cpu(crq->query_capability.capability)) {
2714 case MIN_TX_QUEUES:
2715 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002716 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002717 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2718 adapter->min_tx_queues);
2719 break;
2720 case MIN_RX_QUEUES:
2721 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002722 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002723 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2724 adapter->min_rx_queues);
2725 break;
2726 case MIN_RX_ADD_QUEUES:
2727 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002728 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002729 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2730 adapter->min_rx_add_queues);
2731 break;
2732 case MAX_TX_QUEUES:
2733 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002734 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002735 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2736 adapter->max_tx_queues);
2737 break;
2738 case MAX_RX_QUEUES:
2739 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002740 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002741 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2742 adapter->max_rx_queues);
2743 break;
2744 case MAX_RX_ADD_QUEUES:
2745 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002746 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002747 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2748 adapter->max_rx_add_queues);
2749 break;
2750 case MIN_TX_ENTRIES_PER_SUBCRQ:
2751 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002752 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002753 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2754 adapter->min_tx_entries_per_subcrq);
2755 break;
2756 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2757 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002758 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002759 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2760 adapter->min_rx_add_entries_per_subcrq);
2761 break;
2762 case MAX_TX_ENTRIES_PER_SUBCRQ:
2763 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002764 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002765 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2766 adapter->max_tx_entries_per_subcrq);
2767 break;
2768 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2769 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002770 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002771 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2772 adapter->max_rx_add_entries_per_subcrq);
2773 break;
2774 case TCP_IP_OFFLOAD:
2775 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002776 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002777 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2778 adapter->tcp_ip_offload);
2779 break;
2780 case PROMISC_SUPPORTED:
2781 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002782 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002783 netdev_dbg(netdev, "promisc_supported = %lld\n",
2784 adapter->promisc_supported);
2785 break;
2786 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002787 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002788 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002789 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2790 break;
2791 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002792 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002793 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002794 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2795 break;
2796 case MAX_MULTICAST_FILTERS:
2797 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002798 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002799 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2800 adapter->max_multicast_filters);
2801 break;
2802 case VLAN_HEADER_INSERTION:
2803 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002804 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002805 if (adapter->vlan_header_insertion)
2806 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2807 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2808 adapter->vlan_header_insertion);
2809 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002810 case RX_VLAN_HEADER_INSERTION:
2811 adapter->rx_vlan_header_insertion =
2812 be64_to_cpu(crq->query_capability.number);
2813 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
2814 adapter->rx_vlan_header_insertion);
2815 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002816 case MAX_TX_SG_ENTRIES:
2817 adapter->max_tx_sg_entries =
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_tx_sg_entries = %lld\n",
2820 adapter->max_tx_sg_entries);
2821 break;
2822 case RX_SG_SUPPORTED:
2823 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002824 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002825 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2826 adapter->rx_sg_supported);
2827 break;
2828 case OPT_TX_COMP_SUB_QUEUES:
2829 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002830 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002831 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2832 adapter->opt_tx_comp_sub_queues);
2833 break;
2834 case OPT_RX_COMP_QUEUES:
2835 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002836 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002837 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2838 adapter->opt_rx_comp_queues);
2839 break;
2840 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2841 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002842 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002843 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2844 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2845 break;
2846 case OPT_TX_ENTRIES_PER_SUBCRQ:
2847 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002848 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002849 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2850 adapter->opt_tx_entries_per_subcrq);
2851 break;
2852 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2853 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002854 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002855 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2856 adapter->opt_rxba_entries_per_subcrq);
2857 break;
2858 case TX_RX_DESC_REQ:
2859 adapter->tx_rx_desc_req = crq->query_capability.number;
2860 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2861 adapter->tx_rx_desc_req);
2862 break;
2863
2864 default:
2865 netdev_err(netdev, "Got invalid cap rsp %d\n",
2866 crq->query_capability.capability);
2867 }
2868
2869out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002870 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2871 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002872 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002873 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002874 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002875}
2876
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002877static void ibmvnic_xport_event(struct work_struct *work)
2878{
2879 struct ibmvnic_adapter *adapter = container_of(work,
2880 struct ibmvnic_adapter,
2881 ibmvnic_xport);
2882 struct device *dev = &adapter->vdev->dev;
2883 long rc;
2884
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002885 release_sub_crqs(adapter);
2886 if (adapter->migrated) {
2887 rc = ibmvnic_reenable_crq_queue(adapter);
2888 if (rc)
2889 dev_err(dev, "Error after enable rc=%ld\n", rc);
2890 adapter->migrated = false;
2891 rc = ibmvnic_send_crq_init(adapter);
2892 if (rc)
2893 dev_err(dev, "Error sending init rc=%ld\n", rc);
2894 }
2895}
2896
Thomas Falcon032c5e82015-12-21 11:26:06 -06002897static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2898 struct ibmvnic_adapter *adapter)
2899{
2900 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2901 struct net_device *netdev = adapter->netdev;
2902 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002903 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002904 long rc;
2905
2906 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002907 (unsigned long int)cpu_to_be64(u64_crq[0]),
2908 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002909 switch (gen_crq->first) {
2910 case IBMVNIC_CRQ_INIT_RSP:
2911 switch (gen_crq->cmd) {
2912 case IBMVNIC_CRQ_INIT:
2913 dev_info(dev, "Partner initialized\n");
2914 /* Send back a response */
2915 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002916 if (!rc)
2917 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002918 else
2919 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2920 break;
2921 case IBMVNIC_CRQ_INIT_COMPLETE:
2922 dev_info(dev, "Partner initialization complete\n");
2923 send_version_xchg(adapter);
2924 break;
2925 default:
2926 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2927 }
2928 return;
2929 case IBMVNIC_CRQ_XPORT_EVENT:
2930 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2931 dev_info(dev, "Re-enabling adapter\n");
2932 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002933 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002934 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2935 dev_info(dev, "Backing device failover detected\n");
2936 netif_carrier_off(netdev);
2937 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002938 } else {
2939 /* The adapter lost the connection */
2940 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2941 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002942 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002943 }
2944 return;
2945 case IBMVNIC_CRQ_CMD_RSP:
2946 break;
2947 default:
2948 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2949 gen_crq->first);
2950 return;
2951 }
2952
2953 switch (gen_crq->cmd) {
2954 case VERSION_EXCHANGE_RSP:
2955 rc = crq->version_exchange_rsp.rc.code;
2956 if (rc) {
2957 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2958 break;
2959 }
2960 dev_info(dev, "Partner protocol version is %d\n",
2961 crq->version_exchange_rsp.version);
2962 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2963 ibmvnic_version)
2964 ibmvnic_version =
2965 be16_to_cpu(crq->version_exchange_rsp.version);
2966 send_cap_queries(adapter);
2967 break;
2968 case QUERY_CAPABILITY_RSP:
2969 handle_query_cap_rsp(crq, adapter);
2970 break;
2971 case QUERY_MAP_RSP:
2972 handle_query_map_rsp(crq, adapter);
2973 break;
2974 case REQUEST_MAP_RSP:
2975 handle_request_map_rsp(crq, adapter);
2976 break;
2977 case REQUEST_UNMAP_RSP:
2978 handle_request_unmap_rsp(crq, adapter);
2979 break;
2980 case REQUEST_CAPABILITY_RSP:
2981 handle_request_cap_rsp(crq, adapter);
2982 break;
2983 case LOGIN_RSP:
2984 netdev_dbg(netdev, "Got Login Response\n");
2985 handle_login_rsp(crq, adapter);
2986 break;
2987 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04002988 netdev_dbg(netdev,
2989 "Got Logical Link State Response, state: %d rc: %d\n",
2990 crq->logical_link_state_rsp.link_state,
2991 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002992 adapter->logical_link_state =
2993 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04002994 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
2995 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002996 break;
2997 case LINK_STATE_INDICATION:
2998 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2999 adapter->phys_link_state =
3000 crq->link_state_indication.phys_link_state;
3001 adapter->logical_link_state =
3002 crq->link_state_indication.logical_link_state;
3003 break;
3004 case CHANGE_MAC_ADDR_RSP:
3005 netdev_dbg(netdev, "Got MAC address change Response\n");
3006 handle_change_mac_rsp(crq, adapter);
3007 break;
3008 case ERROR_INDICATION:
3009 netdev_dbg(netdev, "Got Error Indication\n");
3010 handle_error_indication(crq, adapter);
3011 break;
3012 case REQUEST_ERROR_RSP:
3013 netdev_dbg(netdev, "Got Error Detail Response\n");
3014 handle_error_info_rsp(crq, adapter);
3015 break;
3016 case REQUEST_STATISTICS_RSP:
3017 netdev_dbg(netdev, "Got Statistics Response\n");
3018 complete(&adapter->stats_done);
3019 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003020 case QUERY_IP_OFFLOAD_RSP:
3021 netdev_dbg(netdev, "Got Query IP offload Response\n");
3022 handle_query_ip_offload_rsp(adapter);
3023 break;
3024 case MULTICAST_CTRL_RSP:
3025 netdev_dbg(netdev, "Got multicast control Response\n");
3026 break;
3027 case CONTROL_IP_OFFLOAD_RSP:
3028 netdev_dbg(netdev, "Got Control IP offload Response\n");
3029 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3030 sizeof(adapter->ip_offload_ctrl),
3031 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003032 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003033 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003034 case COLLECT_FW_TRACE_RSP:
3035 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3036 complete(&adapter->fw_done);
3037 break;
3038 default:
3039 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3040 gen_crq->cmd);
3041 }
3042}
3043
3044static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3045{
3046 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003047
Thomas Falcon6c267b32017-02-15 12:17:58 -06003048 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003049 return IRQ_HANDLED;
3050}
3051
3052static void ibmvnic_tasklet(void *data)
3053{
3054 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003055 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003056 union ibmvnic_crq *crq;
3057 unsigned long flags;
3058 bool done = false;
3059
3060 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003061 while (!done) {
3062 /* Pull all the valid messages off the CRQ */
3063 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3064 ibmvnic_handle_crq(crq, adapter);
3065 crq->generic.first = 0;
3066 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003067
3068 /* remain in tasklet until all
3069 * capabilities responses are received
3070 */
3071 if (!adapter->wait_capability)
3072 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003073 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003074 /* if capabilities CRQ's were sent in this tasklet, the following
3075 * tasklet must wait until all responses are received
3076 */
3077 if (atomic_read(&adapter->running_cap_crqs) != 0)
3078 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003079 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003080}
3081
3082static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3083{
3084 struct vio_dev *vdev = adapter->vdev;
3085 int rc;
3086
3087 do {
3088 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3089 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3090
3091 if (rc)
3092 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3093
3094 return rc;
3095}
3096
3097static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3098{
3099 struct ibmvnic_crq_queue *crq = &adapter->crq;
3100 struct device *dev = &adapter->vdev->dev;
3101 struct vio_dev *vdev = adapter->vdev;
3102 int rc;
3103
3104 /* Close the CRQ */
3105 do {
3106 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3107 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3108
3109 /* Clean out the queue */
3110 memset(crq->msgs, 0, PAGE_SIZE);
3111 crq->cur = 0;
3112
3113 /* And re-open it again */
3114 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3115 crq->msg_token, PAGE_SIZE);
3116
3117 if (rc == H_CLOSED)
3118 /* Adapter is good, but other end is not ready */
3119 dev_warn(dev, "Partner adapter not ready\n");
3120 else if (rc != 0)
3121 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3122
3123 return rc;
3124}
3125
Nathan Fontenotf9928872017-03-30 02:48:54 -04003126static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003127{
3128 struct ibmvnic_crq_queue *crq = &adapter->crq;
3129 struct vio_dev *vdev = adapter->vdev;
3130 long rc;
3131
Nathan Fontenotf9928872017-03-30 02:48:54 -04003132 if (!crq->msgs)
3133 return;
3134
Thomas Falcon032c5e82015-12-21 11:26:06 -06003135 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3136 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003137 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003138 do {
3139 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3140 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3141
3142 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3143 DMA_BIDIRECTIONAL);
3144 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003145 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003146}
3147
Nathan Fontenotf9928872017-03-30 02:48:54 -04003148static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003149{
3150 struct ibmvnic_crq_queue *crq = &adapter->crq;
3151 struct device *dev = &adapter->vdev->dev;
3152 struct vio_dev *vdev = adapter->vdev;
3153 int rc, retrc = -ENOMEM;
3154
Nathan Fontenotf9928872017-03-30 02:48:54 -04003155 if (crq->msgs)
3156 return 0;
3157
Thomas Falcon032c5e82015-12-21 11:26:06 -06003158 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3159 /* Should we allocate more than one page? */
3160
3161 if (!crq->msgs)
3162 return -ENOMEM;
3163
3164 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3165 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3166 DMA_BIDIRECTIONAL);
3167 if (dma_mapping_error(dev, crq->msg_token))
3168 goto map_failed;
3169
3170 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3171 crq->msg_token, PAGE_SIZE);
3172
3173 if (rc == H_RESOURCE)
3174 /* maybe kexecing and resource is busy. try a reset */
3175 rc = ibmvnic_reset_crq(adapter);
3176 retrc = rc;
3177
3178 if (rc == H_CLOSED) {
3179 dev_warn(dev, "Partner adapter not ready\n");
3180 } else if (rc) {
3181 dev_warn(dev, "Error %d opening adapter\n", rc);
3182 goto reg_crq_failed;
3183 }
3184
3185 retrc = 0;
3186
Thomas Falcon6c267b32017-02-15 12:17:58 -06003187 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3188 (unsigned long)adapter);
3189
Thomas Falcon032c5e82015-12-21 11:26:06 -06003190 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3191 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3192 adapter);
3193 if (rc) {
3194 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3195 vdev->irq, rc);
3196 goto req_irq_failed;
3197 }
3198
3199 rc = vio_enable_interrupts(vdev);
3200 if (rc) {
3201 dev_err(dev, "Error %d enabling interrupts\n", rc);
3202 goto req_irq_failed;
3203 }
3204
3205 crq->cur = 0;
3206 spin_lock_init(&crq->lock);
3207
3208 return retrc;
3209
3210req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003211 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003212 do {
3213 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3214 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3215reg_crq_failed:
3216 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3217map_failed:
3218 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003219 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003220 return retrc;
3221}
3222
Thomas Falcon65dc6892016-07-06 15:35:18 -05003223static void handle_crq_init_rsp(struct work_struct *work)
3224{
3225 struct ibmvnic_adapter *adapter = container_of(work,
3226 struct ibmvnic_adapter,
3227 vnic_crq_init);
3228 struct device *dev = &adapter->vdev->dev;
3229 struct net_device *netdev = adapter->netdev;
3230 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003231 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003232 int rc;
3233
Thomas Falcondfad09a2016-08-18 11:37:51 -05003234 if (adapter->failover) {
3235 release_sub_crqs(adapter);
3236 if (netif_running(netdev)) {
3237 netif_tx_disable(netdev);
3238 ibmvnic_close(netdev);
3239 restart = true;
3240 }
3241 }
3242
Thomas Falcon65dc6892016-07-06 15:35:18 -05003243 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003244 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003245 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3246 dev_err(dev, "Passive init timeout\n");
3247 goto task_failed;
3248 }
3249
Thomas Falconf39f0d12017-02-14 10:22:59 -06003250 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003251
Thomas Falcondfad09a2016-08-18 11:37:51 -05003252 if (adapter->failover) {
3253 adapter->failover = false;
3254 if (restart) {
3255 rc = ibmvnic_open(netdev);
3256 if (rc)
3257 goto restart_failed;
3258 }
3259 netif_carrier_on(netdev);
3260 return;
3261 }
3262
Thomas Falcon65dc6892016-07-06 15:35:18 -05003263 rc = register_netdev(netdev);
3264 if (rc) {
3265 dev_err(dev,
3266 "failed to register netdev rc=%d\n", rc);
3267 goto register_failed;
3268 }
3269 dev_info(dev, "ibmvnic registered\n");
3270
3271 return;
3272
Thomas Falcondfad09a2016-08-18 11:37:51 -05003273restart_failed:
3274 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003275register_failed:
3276 release_sub_crqs(adapter);
3277task_failed:
3278 dev_err(dev, "Passive initialization was not successful\n");
3279}
3280
John Allenf6ef6402017-03-17 17:13:42 -05003281static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3282{
3283 struct device *dev = &adapter->vdev->dev;
3284 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003285 int rc;
3286
Nathan Fontenotf9928872017-03-30 02:48:54 -04003287 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003288 if (rc) {
3289 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3290 return rc;
3291 }
3292
John Allenf6ef6402017-03-17 17:13:42 -05003293 init_completion(&adapter->init_done);
3294 ibmvnic_send_crq_init(adapter);
3295 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3296 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003297 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003298 return -1;
3299 }
3300
3301 return 0;
3302}
3303
Thomas Falcon032c5e82015-12-21 11:26:06 -06003304static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3305{
3306 struct ibmvnic_adapter *adapter;
3307 struct net_device *netdev;
3308 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003309 int rc;
3310
3311 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3312 dev->unit_address);
3313
3314 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3315 VETH_MAC_ADDR, NULL);
3316 if (!mac_addr_p) {
3317 dev_err(&dev->dev,
3318 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3319 __FILE__, __LINE__);
3320 return 0;
3321 }
3322
3323 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3324 IBMVNIC_MAX_TX_QUEUES);
3325 if (!netdev)
3326 return -ENOMEM;
3327
3328 adapter = netdev_priv(netdev);
3329 dev_set_drvdata(&dev->dev, netdev);
3330 adapter->vdev = dev;
3331 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003332 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003333
3334 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3335 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3336 netdev->irq = dev->irq;
3337 netdev->netdev_ops = &ibmvnic_netdev_ops;
3338 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3339 SET_NETDEV_DEV(netdev, &dev->dev);
3340
Thomas Falcon65dc6892016-07-06 15:35:18 -05003341 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003342 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003343
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344 spin_lock_init(&adapter->stats_lock);
3345
Thomas Falcon032c5e82015-12-21 11:26:06 -06003346 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003347 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003348
John Allenf6ef6402017-03-17 17:13:42 -05003349 rc = ibmvnic_init(adapter);
3350 if (rc) {
3351 free_netdev(netdev);
3352 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003353 }
3354
Thomas Falconf39f0d12017-02-14 10:22:59 -06003355 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003356 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003357
3358 rc = register_netdev(netdev);
3359 if (rc) {
3360 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003361 free_netdev(netdev);
3362 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003363 }
3364 dev_info(&dev->dev, "ibmvnic registered\n");
3365
3366 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003367}
3368
3369static int ibmvnic_remove(struct vio_dev *dev)
3370{
3371 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003372 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003373
3374 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003375
3376 release_resources(adapter);
3377 release_sub_crqs(adapter);
3378 release_crq_queue(adapter);
3379
Thomas Falcon032c5e82015-12-21 11:26:06 -06003380 free_netdev(netdev);
3381 dev_set_drvdata(&dev->dev, NULL);
3382
3383 return 0;
3384}
3385
3386static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3387{
3388 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3389 struct ibmvnic_adapter *adapter;
3390 struct iommu_table *tbl;
3391 unsigned long ret = 0;
3392 int i;
3393
3394 tbl = get_iommu_table_base(&vdev->dev);
3395
3396 /* netdev inits at probe time along with the structures we need below*/
3397 if (!netdev)
3398 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3399
3400 adapter = netdev_priv(netdev);
3401
3402 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003403 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3404
3405 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3406 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3407
3408 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3409 i++)
3410 ret += adapter->rx_pool[i].size *
3411 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3412
3413 return ret;
3414}
3415
3416static int ibmvnic_resume(struct device *dev)
3417{
3418 struct net_device *netdev = dev_get_drvdata(dev);
3419 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3420 int i;
3421
3422 /* kick the interrupt handlers just in case we lost an interrupt */
3423 for (i = 0; i < adapter->req_rx_queues; i++)
3424 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3425 adapter->rx_scrq[i]);
3426
3427 return 0;
3428}
3429
3430static struct vio_device_id ibmvnic_device_table[] = {
3431 {"network", "IBM,vnic"},
3432 {"", "" }
3433};
3434MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3435
3436static const struct dev_pm_ops ibmvnic_pm_ops = {
3437 .resume = ibmvnic_resume
3438};
3439
3440static struct vio_driver ibmvnic_driver = {
3441 .id_table = ibmvnic_device_table,
3442 .probe = ibmvnic_probe,
3443 .remove = ibmvnic_remove,
3444 .get_desired_dma = ibmvnic_get_desired_dma,
3445 .name = ibmvnic_driver_name,
3446 .pm = &ibmvnic_pm_ops,
3447};
3448
3449/* module functions */
3450static int __init ibmvnic_module_init(void)
3451{
3452 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3453 IBMVNIC_DRIVER_VERSION);
3454
3455 return vio_register_driver(&ibmvnic_driver);
3456}
3457
3458static void __exit ibmvnic_module_exit(void)
3459{
3460 vio_unregister_driver(&ibmvnic_driver);
3461}
3462
3463module_init(ibmvnic_module_init);
3464module_exit(ibmvnic_module_exit);