blob: 115f21694994f3ed8925b01ee0a107a19ff53073 [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
732 for (i = 0; i < adapter->req_rx_queues; i++)
733 napi_disable(&adapter->napi[i]);
734
735 if (!adapter->failover)
736 netif_tx_stop_all_queues(netdev);
737
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400738 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600739
Nathan Fontenot1b8955e2017-03-30 02:49:29 -0400740 release_resources(adapter);
Thomas Falcon032c5e82015-12-21 11:26:06 -0600741
John Allenea5509f2017-03-17 17:13:43 -0500742 adapter->is_closed = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600743 adapter->closing = false;
Nathan Fontenot53da09e2017-04-21 15:39:04 -0400744 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600745}
746
Thomas Falconad7775d2016-04-01 17:20:34 -0500747/**
748 * build_hdr_data - creates L2/L3/L4 header data buffer
749 * @hdr_field - bitfield determining needed headers
750 * @skb - socket buffer
751 * @hdr_len - array of header lengths
752 * @tot_len - total length of data
753 *
754 * Reads hdr_field to determine which headers are needed by firmware.
755 * Builds a buffer containing these headers. Saves individual header
756 * lengths and total buffer length to be used to build descriptors.
757 */
758static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
759 int *hdr_len, u8 *hdr_data)
760{
761 int len = 0;
762 u8 *hdr;
763
764 hdr_len[0] = sizeof(struct ethhdr);
765
766 if (skb->protocol == htons(ETH_P_IP)) {
767 hdr_len[1] = ip_hdr(skb)->ihl * 4;
768 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
769 hdr_len[2] = tcp_hdrlen(skb);
770 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
771 hdr_len[2] = sizeof(struct udphdr);
772 } else if (skb->protocol == htons(ETH_P_IPV6)) {
773 hdr_len[1] = sizeof(struct ipv6hdr);
774 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
775 hdr_len[2] = tcp_hdrlen(skb);
776 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
777 hdr_len[2] = sizeof(struct udphdr);
778 }
779
780 memset(hdr_data, 0, 120);
781 if ((hdr_field >> 6) & 1) {
782 hdr = skb_mac_header(skb);
783 memcpy(hdr_data, hdr, hdr_len[0]);
784 len += hdr_len[0];
785 }
786
787 if ((hdr_field >> 5) & 1) {
788 hdr = skb_network_header(skb);
789 memcpy(hdr_data + len, hdr, hdr_len[1]);
790 len += hdr_len[1];
791 }
792
793 if ((hdr_field >> 4) & 1) {
794 hdr = skb_transport_header(skb);
795 memcpy(hdr_data + len, hdr, hdr_len[2]);
796 len += hdr_len[2];
797 }
798 return len;
799}
800
801/**
802 * create_hdr_descs - create header and header extension descriptors
803 * @hdr_field - bitfield determining needed headers
804 * @data - buffer containing header data
805 * @len - length of data buffer
806 * @hdr_len - array of individual header lengths
807 * @scrq_arr - descriptor array
808 *
809 * Creates header and, if needed, header extension descriptors and
810 * places them in a descriptor array, scrq_arr
811 */
812
813static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
814 union sub_crq *scrq_arr)
815{
816 union sub_crq hdr_desc;
817 int tmp_len = len;
818 u8 *data, *cur;
819 int tmp;
820
821 while (tmp_len > 0) {
822 cur = hdr_data + len - tmp_len;
823
824 memset(&hdr_desc, 0, sizeof(hdr_desc));
825 if (cur != hdr_data) {
826 data = hdr_desc.hdr_ext.data;
827 tmp = tmp_len > 29 ? 29 : tmp_len;
828 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
829 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
830 hdr_desc.hdr_ext.len = tmp;
831 } else {
832 data = hdr_desc.hdr.data;
833 tmp = tmp_len > 24 ? 24 : tmp_len;
834 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
835 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
836 hdr_desc.hdr.len = tmp;
837 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
838 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
839 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
840 hdr_desc.hdr.flag = hdr_field << 1;
841 }
842 memcpy(data, cur, tmp);
843 tmp_len -= tmp;
844 *scrq_arr = hdr_desc;
845 scrq_arr++;
846 }
847}
848
849/**
850 * build_hdr_descs_arr - build a header descriptor array
851 * @skb - socket buffer
852 * @num_entries - number of descriptors to be sent
853 * @subcrq - first TX descriptor
854 * @hdr_field - bit field determining which headers will be sent
855 *
856 * This function will build a TX descriptor array with applicable
857 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
858 */
859
860static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
861 int *num_entries, u8 hdr_field)
862{
863 int hdr_len[3] = {0, 0, 0};
864 int tot_len, len;
865 u8 *hdr_data = txbuff->hdr_data;
866
867 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
868 txbuff->hdr_data);
869 len = tot_len;
870 len -= 24;
871 if (len > 0)
872 num_entries += len % 29 ? len / 29 + 1 : len / 29;
873 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
874 txbuff->indir_arr + 1);
875}
876
Thomas Falcon032c5e82015-12-21 11:26:06 -0600877static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
878{
879 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
880 int queue_num = skb_get_queue_mapping(skb);
Thomas Falconad7775d2016-04-01 17:20:34 -0500881 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600882 struct device *dev = &adapter->vdev->dev;
883 struct ibmvnic_tx_buff *tx_buff = NULL;
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600884 struct ibmvnic_sub_crq_queue *tx_scrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600885 struct ibmvnic_tx_pool *tx_pool;
886 unsigned int tx_send_failed = 0;
887 unsigned int tx_map_failed = 0;
888 unsigned int tx_dropped = 0;
889 unsigned int tx_packets = 0;
890 unsigned int tx_bytes = 0;
891 dma_addr_t data_dma_addr;
892 struct netdev_queue *txq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600893 unsigned long lpar_rc;
894 union sub_crq tx_crq;
895 unsigned int offset;
Thomas Falconad7775d2016-04-01 17:20:34 -0500896 int num_entries = 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600897 unsigned char *dst;
898 u64 *handle_array;
899 int index = 0;
900 int ret = 0;
901
902 tx_pool = &adapter->tx_pool[queue_num];
Thomas Falcon142c0ac2017-03-05 12:18:41 -0600903 tx_scrq = adapter->tx_scrq[queue_num];
Thomas Falcon032c5e82015-12-21 11:26:06 -0600904 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
905 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
906 be32_to_cpu(adapter->login_rsp_buf->
907 off_txsubm_subcrqs));
908 if (adapter->migrated) {
909 tx_send_failed++;
910 tx_dropped++;
911 ret = NETDEV_TX_BUSY;
912 goto out;
913 }
914
915 index = tx_pool->free_map[tx_pool->consumer_index];
916 offset = index * adapter->req_mtu;
917 dst = tx_pool->long_term_buff.buff + offset;
918 memset(dst, 0, adapter->req_mtu);
919 skb_copy_from_linear_data(skb, dst, skb->len);
920 data_dma_addr = tx_pool->long_term_buff.addr + offset;
921
922 tx_pool->consumer_index =
923 (tx_pool->consumer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -0600924 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600925
926 tx_buff = &tx_pool->tx_buff[index];
927 tx_buff->skb = skb;
928 tx_buff->data_dma[0] = data_dma_addr;
929 tx_buff->data_len[0] = skb->len;
930 tx_buff->index = index;
931 tx_buff->pool_index = queue_num;
932 tx_buff->last_frag = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600933
934 memset(&tx_crq, 0, sizeof(tx_crq));
935 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
936 tx_crq.v1.type = IBMVNIC_TX_DESC;
937 tx_crq.v1.n_crq_elem = 1;
938 tx_crq.v1.n_sge = 1;
939 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
940 tx_crq.v1.correlator = cpu_to_be32(index);
941 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
942 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
943 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
944
945 if (adapter->vlan_header_insertion) {
946 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
947 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
948 }
949
950 if (skb->protocol == htons(ETH_P_IP)) {
951 if (ip_hdr(skb)->version == 4)
952 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
953 else if (ip_hdr(skb)->version == 6)
954 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
955
956 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
957 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
958 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
959 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
960 }
961
Thomas Falconad7775d2016-04-01 17:20:34 -0500962 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Thomas Falcon032c5e82015-12-21 11:26:06 -0600963 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
Thomas Falconad7775d2016-04-01 17:20:34 -0500964 hdrs += 2;
965 }
966 /* determine if l2/3/4 headers are sent to firmware */
967 if ((*hdrs >> 7) & 1 &&
968 (skb->protocol == htons(ETH_P_IP) ||
969 skb->protocol == htons(ETH_P_IPV6))) {
970 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
971 tx_crq.v1.n_crq_elem = num_entries;
972 tx_buff->indir_arr[0] = tx_crq;
973 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
974 sizeof(tx_buff->indir_arr),
975 DMA_TO_DEVICE);
976 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
977 if (!firmware_has_feature(FW_FEATURE_CMO))
978 dev_err(dev, "tx: unable to map descriptor array\n");
979 tx_map_failed++;
980 tx_dropped++;
981 ret = NETDEV_TX_BUSY;
982 goto out;
983 }
John Allen498cd8e2016-04-06 11:49:55 -0500984 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
Thomas Falconad7775d2016-04-01 17:20:34 -0500985 (u64)tx_buff->indir_dma,
986 (u64)num_entries);
987 } else {
John Allen498cd8e2016-04-06 11:49:55 -0500988 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
989 &tx_crq);
Thomas Falconad7775d2016-04-01 17:20:34 -0500990 }
Thomas Falcon032c5e82015-12-21 11:26:06 -0600991 if (lpar_rc != H_SUCCESS) {
992 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
993
994 if (tx_pool->consumer_index == 0)
995 tx_pool->consumer_index =
Thomas Falcon068d9f92017-03-05 12:18:42 -0600996 adapter->req_tx_entries_per_subcrq - 1;
Thomas Falcon032c5e82015-12-21 11:26:06 -0600997 else
998 tx_pool->consumer_index--;
999
1000 tx_send_failed++;
1001 tx_dropped++;
1002 ret = NETDEV_TX_BUSY;
1003 goto out;
1004 }
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001005
Brian King58c8c0c2017-04-19 13:44:47 -04001006 if (atomic_inc_return(&tx_scrq->used)
1007 >= adapter->req_tx_entries_per_subcrq) {
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001008 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1009 netif_stop_subqueue(netdev, queue_num);
1010 }
1011
Thomas Falcon032c5e82015-12-21 11:26:06 -06001012 tx_packets++;
1013 tx_bytes += skb->len;
1014 txq->trans_start = jiffies;
1015 ret = NETDEV_TX_OK;
1016
1017out:
1018 netdev->stats.tx_dropped += tx_dropped;
1019 netdev->stats.tx_bytes += tx_bytes;
1020 netdev->stats.tx_packets += tx_packets;
1021 adapter->tx_send_failed += tx_send_failed;
1022 adapter->tx_map_failed += tx_map_failed;
1023
1024 return ret;
1025}
1026
1027static void ibmvnic_set_multi(struct net_device *netdev)
1028{
1029 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1030 struct netdev_hw_addr *ha;
1031 union ibmvnic_crq crq;
1032
1033 memset(&crq, 0, sizeof(crq));
1034 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1035 crq.request_capability.cmd = REQUEST_CAPABILITY;
1036
1037 if (netdev->flags & IFF_PROMISC) {
1038 if (!adapter->promisc_supported)
1039 return;
1040 } else {
1041 if (netdev->flags & IFF_ALLMULTI) {
1042 /* Accept all multicast */
1043 memset(&crq, 0, sizeof(crq));
1044 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1045 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1046 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1047 ibmvnic_send_crq(adapter, &crq);
1048 } else if (netdev_mc_empty(netdev)) {
1049 /* Reject all multicast */
1050 memset(&crq, 0, sizeof(crq));
1051 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1052 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1053 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1054 ibmvnic_send_crq(adapter, &crq);
1055 } else {
1056 /* Accept one or more multicast(s) */
1057 netdev_for_each_mc_addr(ha, netdev) {
1058 memset(&crq, 0, sizeof(crq));
1059 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1060 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1061 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1062 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1063 ha->addr);
1064 ibmvnic_send_crq(adapter, &crq);
1065 }
1066 }
1067 }
1068}
1069
1070static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1071{
1072 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1073 struct sockaddr *addr = p;
1074 union ibmvnic_crq crq;
1075
1076 if (!is_valid_ether_addr(addr->sa_data))
1077 return -EADDRNOTAVAIL;
1078
1079 memset(&crq, 0, sizeof(crq));
1080 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1081 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1082 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1083 ibmvnic_send_crq(adapter, &crq);
1084 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1085 return 0;
1086}
1087
Thomas Falcon032c5e82015-12-21 11:26:06 -06001088static void ibmvnic_tx_timeout(struct net_device *dev)
1089{
1090 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1091 int rc;
1092
1093 /* Adapter timed out, resetting it */
1094 release_sub_crqs(adapter);
1095 rc = ibmvnic_reset_crq(adapter);
1096 if (rc)
1097 dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1098 else
1099 ibmvnic_send_crq_init(adapter);
1100}
1101
1102static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1103 struct ibmvnic_rx_buff *rx_buff)
1104{
1105 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1106
1107 rx_buff->skb = NULL;
1108
1109 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1110 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1111
1112 atomic_dec(&pool->available);
1113}
1114
1115static int ibmvnic_poll(struct napi_struct *napi, int budget)
1116{
1117 struct net_device *netdev = napi->dev;
1118 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1119 int scrq_num = (int)(napi - adapter->napi);
1120 int frames_processed = 0;
1121restart_poll:
1122 while (frames_processed < budget) {
1123 struct sk_buff *skb;
1124 struct ibmvnic_rx_buff *rx_buff;
1125 union sub_crq *next;
1126 u32 length;
1127 u16 offset;
1128 u8 flags = 0;
1129
1130 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1131 break;
1132 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1133 rx_buff =
1134 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1135 rx_comp.correlator);
1136 /* do error checking */
1137 if (next->rx_comp.rc) {
1138 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1139 /* free the entry */
1140 next->rx_comp.first = 0;
1141 remove_buff_from_pool(adapter, rx_buff);
1142 break;
1143 }
1144
1145 length = be32_to_cpu(next->rx_comp.len);
1146 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1147 flags = next->rx_comp.flags;
1148 skb = rx_buff->skb;
1149 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1150 length);
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04001151
1152 /* VLAN Header has been stripped by the system firmware and
1153 * needs to be inserted by the driver
1154 */
1155 if (adapter->rx_vlan_header_insertion &&
1156 (flags & IBMVNIC_VLAN_STRIPPED))
1157 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1158 ntohs(next->rx_comp.vlan_tci));
1159
Thomas Falcon032c5e82015-12-21 11:26:06 -06001160 /* free the entry */
1161 next->rx_comp.first = 0;
1162 remove_buff_from_pool(adapter, rx_buff);
1163
1164 skb_put(skb, length);
1165 skb->protocol = eth_type_trans(skb, netdev);
1166
1167 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1168 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1169 skb->ip_summed = CHECKSUM_UNNECESSARY;
1170 }
1171
1172 length = skb->len;
1173 napi_gro_receive(napi, skb); /* send it up */
1174 netdev->stats.rx_packets++;
1175 netdev->stats.rx_bytes += length;
1176 frames_processed++;
1177 }
John Allen498cd8e2016-04-06 11:49:55 -05001178 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001179
1180 if (frames_processed < budget) {
1181 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
Eric Dumazet6ad20162017-01-30 08:22:01 -08001182 napi_complete_done(napi, frames_processed);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001183 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1184 napi_reschedule(napi)) {
1185 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1186 goto restart_poll;
1187 }
1188 }
1189 return frames_processed;
1190}
1191
1192#ifdef CONFIG_NET_POLL_CONTROLLER
1193static void ibmvnic_netpoll_controller(struct net_device *dev)
1194{
1195 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1196 int i;
1197
1198 replenish_pools(netdev_priv(dev));
1199 for (i = 0; i < adapter->req_rx_queues; i++)
1200 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1201 adapter->rx_scrq[i]);
1202}
1203#endif
1204
1205static const struct net_device_ops ibmvnic_netdev_ops = {
1206 .ndo_open = ibmvnic_open,
1207 .ndo_stop = ibmvnic_close,
1208 .ndo_start_xmit = ibmvnic_xmit,
1209 .ndo_set_rx_mode = ibmvnic_set_multi,
1210 .ndo_set_mac_address = ibmvnic_set_mac,
1211 .ndo_validate_addr = eth_validate_addr,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001212 .ndo_tx_timeout = ibmvnic_tx_timeout,
1213#ifdef CONFIG_NET_POLL_CONTROLLER
1214 .ndo_poll_controller = ibmvnic_netpoll_controller,
1215#endif
1216};
1217
1218/* ethtool functions */
1219
Philippe Reynes8a433792017-01-07 22:37:29 +01001220static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1221 struct ethtool_link_ksettings *cmd)
Thomas Falcon032c5e82015-12-21 11:26:06 -06001222{
Philippe Reynes8a433792017-01-07 22:37:29 +01001223 u32 supported, advertising;
1224
1225 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001226 SUPPORTED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001227 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
Thomas Falcon032c5e82015-12-21 11:26:06 -06001228 ADVERTISED_FIBRE);
Philippe Reynes8a433792017-01-07 22:37:29 +01001229 cmd->base.speed = SPEED_1000;
1230 cmd->base.duplex = DUPLEX_FULL;
1231 cmd->base.port = PORT_FIBRE;
1232 cmd->base.phy_address = 0;
1233 cmd->base.autoneg = AUTONEG_ENABLE;
1234
1235 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1236 supported);
1237 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1238 advertising);
1239
Thomas Falcon032c5e82015-12-21 11:26:06 -06001240 return 0;
1241}
1242
1243static void ibmvnic_get_drvinfo(struct net_device *dev,
1244 struct ethtool_drvinfo *info)
1245{
1246 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1247 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1248}
1249
1250static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1251{
1252 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1253
1254 return adapter->msg_enable;
1255}
1256
1257static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1258{
1259 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1260
1261 adapter->msg_enable = data;
1262}
1263
1264static u32 ibmvnic_get_link(struct net_device *netdev)
1265{
1266 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1267
1268 /* Don't need to send a query because we request a logical link up at
1269 * init and then we wait for link state indications
1270 */
1271 return adapter->logical_link_state;
1272}
1273
1274static void ibmvnic_get_ringparam(struct net_device *netdev,
1275 struct ethtool_ringparam *ring)
1276{
1277 ring->rx_max_pending = 0;
1278 ring->tx_max_pending = 0;
1279 ring->rx_mini_max_pending = 0;
1280 ring->rx_jumbo_max_pending = 0;
1281 ring->rx_pending = 0;
1282 ring->tx_pending = 0;
1283 ring->rx_mini_pending = 0;
1284 ring->rx_jumbo_pending = 0;
1285}
1286
1287static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1288{
1289 int i;
1290
1291 if (stringset != ETH_SS_STATS)
1292 return;
1293
1294 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1295 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1296}
1297
1298static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1299{
1300 switch (sset) {
1301 case ETH_SS_STATS:
1302 return ARRAY_SIZE(ibmvnic_stats);
1303 default:
1304 return -EOPNOTSUPP;
1305 }
1306}
1307
1308static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1309 struct ethtool_stats *stats, u64 *data)
1310{
1311 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1312 union ibmvnic_crq crq;
1313 int i;
1314
1315 memset(&crq, 0, sizeof(crq));
1316 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1317 crq.request_statistics.cmd = REQUEST_STATISTICS;
1318 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1319 crq.request_statistics.len =
1320 cpu_to_be32(sizeof(struct ibmvnic_statistics));
Thomas Falcon032c5e82015-12-21 11:26:06 -06001321
1322 /* Wait for data to be written */
1323 init_completion(&adapter->stats_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05001324 ibmvnic_send_crq(adapter, &crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001325 wait_for_completion(&adapter->stats_done);
1326
1327 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1328 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1329}
1330
1331static const struct ethtool_ops ibmvnic_ethtool_ops = {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001332 .get_drvinfo = ibmvnic_get_drvinfo,
1333 .get_msglevel = ibmvnic_get_msglevel,
1334 .set_msglevel = ibmvnic_set_msglevel,
1335 .get_link = ibmvnic_get_link,
1336 .get_ringparam = ibmvnic_get_ringparam,
1337 .get_strings = ibmvnic_get_strings,
1338 .get_sset_count = ibmvnic_get_sset_count,
1339 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
Philippe Reynes8a433792017-01-07 22:37:29 +01001340 .get_link_ksettings = ibmvnic_get_link_ksettings,
Thomas Falcon032c5e82015-12-21 11:26:06 -06001341};
1342
1343/* Routines for managing CRQs/sCRQs */
1344
1345static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1346 struct ibmvnic_sub_crq_queue *scrq)
1347{
1348 struct device *dev = &adapter->vdev->dev;
1349 long rc;
1350
1351 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1352
1353 /* Close the sub-crqs */
1354 do {
1355 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1356 adapter->vdev->unit_address,
1357 scrq->crq_num);
1358 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1359
Thomas Falconffa73852017-04-19 13:44:29 -04001360 if (rc) {
1361 netdev_err(adapter->netdev,
1362 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1363 scrq->crq_num, rc);
1364 }
1365
Thomas Falcon032c5e82015-12-21 11:26:06 -06001366 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1367 DMA_BIDIRECTIONAL);
1368 free_pages((unsigned long)scrq->msgs, 2);
1369 kfree(scrq);
1370}
1371
1372static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1373 *adapter)
1374{
1375 struct device *dev = &adapter->vdev->dev;
1376 struct ibmvnic_sub_crq_queue *scrq;
1377 int rc;
1378
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001379 scrq = kzalloc(sizeof(*scrq), GFP_ATOMIC);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001380 if (!scrq)
1381 return NULL;
1382
Nathan Fontenot7f7adc52017-04-19 13:45:16 -04001383 scrq->msgs =
1384 (union sub_crq *)__get_free_pages(GFP_ATOMIC | __GFP_ZERO, 2);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001385 if (!scrq->msgs) {
1386 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1387 goto zero_page_failed;
1388 }
1389
1390 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1391 DMA_BIDIRECTIONAL);
1392 if (dma_mapping_error(dev, scrq->msg_token)) {
1393 dev_warn(dev, "Couldn't map crq queue messages page\n");
1394 goto map_failed;
1395 }
1396
1397 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1398 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1399
1400 if (rc == H_RESOURCE)
1401 rc = ibmvnic_reset_crq(adapter);
1402
1403 if (rc == H_CLOSED) {
1404 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1405 } else if (rc) {
1406 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1407 goto reg_failed;
1408 }
1409
Thomas Falcon032c5e82015-12-21 11:26:06 -06001410 scrq->adapter = adapter;
1411 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001412 spin_lock_init(&scrq->lock);
1413
1414 netdev_dbg(adapter->netdev,
1415 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1416 scrq->crq_num, scrq->hw_irq, scrq->irq);
1417
1418 return scrq;
1419
Thomas Falcon032c5e82015-12-21 11:26:06 -06001420reg_failed:
1421 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1422 DMA_BIDIRECTIONAL);
1423map_failed:
1424 free_pages((unsigned long)scrq->msgs, 2);
1425zero_page_failed:
1426 kfree(scrq);
1427
1428 return NULL;
1429}
1430
1431static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1432{
1433 int i;
1434
1435 if (adapter->tx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001436 for (i = 0; i < adapter->req_tx_queues; i++) {
1437 if (!adapter->tx_scrq[i])
1438 continue;
1439
1440 if (adapter->tx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001441 free_irq(adapter->tx_scrq[i]->irq,
1442 adapter->tx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001443 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001444 adapter->tx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001445 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001446
1447 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1448 }
1449
Nathan Fontenot9501df32017-03-15 23:38:07 -04001450 kfree(adapter->tx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001451 adapter->tx_scrq = NULL;
1452 }
1453
1454 if (adapter->rx_scrq) {
Nathan Fontenotb5108882017-03-30 02:49:18 -04001455 for (i = 0; i < adapter->req_rx_queues; i++) {
1456 if (!adapter->rx_scrq[i])
1457 continue;
1458
1459 if (adapter->rx_scrq[i]->irq) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06001460 free_irq(adapter->rx_scrq[i]->irq,
1461 adapter->rx_scrq[i]);
Thomas Falcon88eb98a2016-07-06 15:35:16 -05001462 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
Nathan Fontenotb5108882017-03-30 02:49:18 -04001463 adapter->rx_scrq[i]->irq = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001464 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001465
1466 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1467 }
1468
Nathan Fontenot9501df32017-03-15 23:38:07 -04001469 kfree(adapter->rx_scrq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001470 adapter->rx_scrq = NULL;
1471 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001472}
1473
1474static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1475 struct ibmvnic_sub_crq_queue *scrq)
1476{
1477 struct device *dev = &adapter->vdev->dev;
1478 unsigned long rc;
1479
1480 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1481 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1482 if (rc)
1483 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1484 scrq->hw_irq, rc);
1485 return rc;
1486}
1487
1488static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1489 struct ibmvnic_sub_crq_queue *scrq)
1490{
1491 struct device *dev = &adapter->vdev->dev;
1492 unsigned long rc;
1493
1494 if (scrq->hw_irq > 0x100000000ULL) {
1495 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1496 return 1;
1497 }
1498
1499 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1500 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1501 if (rc)
1502 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1503 scrq->hw_irq, rc);
1504 return rc;
1505}
1506
1507static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1508 struct ibmvnic_sub_crq_queue *scrq)
1509{
1510 struct device *dev = &adapter->vdev->dev;
1511 struct ibmvnic_tx_buff *txbuff;
1512 union sub_crq *next;
1513 int index;
1514 int i, j;
Thomas Falconad7775d2016-04-01 17:20:34 -05001515 u8 first;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001516
1517restart_loop:
1518 while (pending_scrq(adapter, scrq)) {
1519 unsigned int pool = scrq->pool_index;
1520
1521 next = ibmvnic_next_scrq(adapter, scrq);
1522 for (i = 0; i < next->tx_comp.num_comps; i++) {
1523 if (next->tx_comp.rcs[i]) {
1524 dev_err(dev, "tx error %x\n",
1525 next->tx_comp.rcs[i]);
1526 continue;
1527 }
1528 index = be32_to_cpu(next->tx_comp.correlators[i]);
1529 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1530
1531 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1532 if (!txbuff->data_dma[j])
1533 continue;
1534
1535 txbuff->data_dma[j] = 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001536 }
Thomas Falconad7775d2016-04-01 17:20:34 -05001537 /* if sub_crq was sent indirectly */
1538 first = txbuff->indir_arr[0].generic.first;
1539 if (first == IBMVNIC_CRQ_CMD) {
1540 dma_unmap_single(dev, txbuff->indir_dma,
1541 sizeof(txbuff->indir_arr),
1542 DMA_TO_DEVICE);
1543 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001544
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001545 if (txbuff->last_frag) {
Brian King58c8c0c2017-04-19 13:44:47 -04001546 if (atomic_sub_return(next->tx_comp.num_comps,
1547 &scrq->used) <=
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001548 (adapter->req_tx_entries_per_subcrq / 2) &&
1549 netif_subqueue_stopped(adapter->netdev,
1550 txbuff->skb)) {
1551 netif_wake_subqueue(adapter->netdev,
1552 scrq->pool_index);
1553 netdev_dbg(adapter->netdev,
1554 "Started queue %d\n",
1555 scrq->pool_index);
1556 }
1557
Thomas Falcon032c5e82015-12-21 11:26:06 -06001558 dev_kfree_skb_any(txbuff->skb);
Thomas Falcon142c0ac2017-03-05 12:18:41 -06001559 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06001560
1561 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1562 producer_index] = index;
1563 adapter->tx_pool[pool].producer_index =
1564 (adapter->tx_pool[pool].producer_index + 1) %
Thomas Falcon068d9f92017-03-05 12:18:42 -06001565 adapter->req_tx_entries_per_subcrq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001566 }
1567 /* remove tx_comp scrq*/
1568 next->tx_comp.first = 0;
1569 }
1570
1571 enable_scrq_irq(adapter, scrq);
1572
1573 if (pending_scrq(adapter, scrq)) {
1574 disable_scrq_irq(adapter, scrq);
1575 goto restart_loop;
1576 }
1577
1578 return 0;
1579}
1580
1581static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1582{
1583 struct ibmvnic_sub_crq_queue *scrq = instance;
1584 struct ibmvnic_adapter *adapter = scrq->adapter;
1585
1586 disable_scrq_irq(adapter, scrq);
1587 ibmvnic_complete_tx(adapter, scrq);
1588
1589 return IRQ_HANDLED;
1590}
1591
1592static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1593{
1594 struct ibmvnic_sub_crq_queue *scrq = instance;
1595 struct ibmvnic_adapter *adapter = scrq->adapter;
1596
1597 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1598 disable_scrq_irq(adapter, scrq);
1599 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1600 }
1601
1602 return IRQ_HANDLED;
1603}
1604
Thomas Falconea22d512016-07-06 15:35:17 -05001605static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1606{
1607 struct device *dev = &adapter->vdev->dev;
1608 struct ibmvnic_sub_crq_queue *scrq;
1609 int i = 0, j = 0;
1610 int rc = 0;
1611
1612 for (i = 0; i < adapter->req_tx_queues; i++) {
1613 scrq = adapter->tx_scrq[i];
1614 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1615
Michael Ellerman99c17902016-09-10 19:59:05 +10001616 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001617 rc = -EINVAL;
1618 dev_err(dev, "Error mapping irq\n");
1619 goto req_tx_irq_failed;
1620 }
1621
1622 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1623 0, "ibmvnic_tx", scrq);
1624
1625 if (rc) {
1626 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1627 scrq->irq, rc);
1628 irq_dispose_mapping(scrq->irq);
1629 goto req_rx_irq_failed;
1630 }
1631 }
1632
1633 for (i = 0; i < adapter->req_rx_queues; i++) {
1634 scrq = adapter->rx_scrq[i];
1635 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
Michael Ellerman99c17902016-09-10 19:59:05 +10001636 if (!scrq->irq) {
Thomas Falconea22d512016-07-06 15:35:17 -05001637 rc = -EINVAL;
1638 dev_err(dev, "Error mapping irq\n");
1639 goto req_rx_irq_failed;
1640 }
1641 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1642 0, "ibmvnic_rx", scrq);
1643 if (rc) {
1644 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1645 scrq->irq, rc);
1646 irq_dispose_mapping(scrq->irq);
1647 goto req_rx_irq_failed;
1648 }
1649 }
1650 return rc;
1651
1652req_rx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001653 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001654 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1655 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001656 }
Thomas Falconea22d512016-07-06 15:35:17 -05001657 i = adapter->req_tx_queues;
1658req_tx_irq_failed:
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001659 for (j = 0; j < i; j++) {
Thomas Falconea22d512016-07-06 15:35:17 -05001660 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1661 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
Thomas Falcon8bf371e2016-10-27 12:28:52 -05001662 }
Nathan Fontenotb5108882017-03-30 02:49:18 -04001663 release_sub_crqs(adapter);
Thomas Falconea22d512016-07-06 15:35:17 -05001664 return rc;
1665}
1666
Thomas Falcon032c5e82015-12-21 11:26:06 -06001667static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1668{
1669 struct device *dev = &adapter->vdev->dev;
1670 struct ibmvnic_sub_crq_queue **allqueues;
1671 int registered_queues = 0;
1672 union ibmvnic_crq crq;
1673 int total_queues;
1674 int more = 0;
Thomas Falconea22d512016-07-06 15:35:17 -05001675 int i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001676
1677 if (!retry) {
1678 /* Sub-CRQ entries are 32 byte long */
1679 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1680
1681 if (adapter->min_tx_entries_per_subcrq > entries_page ||
1682 adapter->min_rx_add_entries_per_subcrq > entries_page) {
1683 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1684 goto allqueues_failed;
1685 }
1686
1687 /* Get the minimum between the queried max and the entries
1688 * that fit in our PAGE_SIZE
1689 */
1690 adapter->req_tx_entries_per_subcrq =
1691 adapter->max_tx_entries_per_subcrq > entries_page ?
1692 entries_page : adapter->max_tx_entries_per_subcrq;
1693 adapter->req_rx_add_entries_per_subcrq =
1694 adapter->max_rx_add_entries_per_subcrq > entries_page ?
1695 entries_page : adapter->max_rx_add_entries_per_subcrq;
1696
John Allen6dbcd8f2016-11-07 14:27:28 -06001697 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1698 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
John Allen498cd8e2016-04-06 11:49:55 -05001699 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001700
Thomas Falconf39f0d12017-02-14 10:22:59 -06001701 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001702 }
1703
1704 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1705
1706 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1707 if (!allqueues)
1708 goto allqueues_failed;
1709
1710 for (i = 0; i < total_queues; i++) {
1711 allqueues[i] = init_sub_crq_queue(adapter);
1712 if (!allqueues[i]) {
1713 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1714 break;
1715 }
1716 registered_queues++;
1717 }
1718
1719 /* Make sure we were able to register the minimum number of queues */
1720 if (registered_queues <
1721 adapter->min_tx_queues + adapter->min_rx_queues) {
1722 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1723 goto tx_failed;
1724 }
1725
1726 /* Distribute the failed allocated queues*/
1727 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1728 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1729 switch (i % 3) {
1730 case 0:
1731 if (adapter->req_rx_queues > adapter->min_rx_queues)
1732 adapter->req_rx_queues--;
1733 else
1734 more++;
1735 break;
1736 case 1:
1737 if (adapter->req_tx_queues > adapter->min_tx_queues)
1738 adapter->req_tx_queues--;
1739 else
1740 more++;
1741 break;
1742 }
1743 }
1744
1745 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1746 sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1747 if (!adapter->tx_scrq)
1748 goto tx_failed;
1749
1750 for (i = 0; i < adapter->req_tx_queues; i++) {
1751 adapter->tx_scrq[i] = allqueues[i];
1752 adapter->tx_scrq[i]->pool_index = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001753 }
1754
1755 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1756 sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1757 if (!adapter->rx_scrq)
1758 goto rx_failed;
1759
1760 for (i = 0; i < adapter->req_rx_queues; i++) {
1761 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1762 adapter->rx_scrq[i]->scrq_num = i;
Thomas Falcon032c5e82015-12-21 11:26:06 -06001763 }
1764
1765 memset(&crq, 0, sizeof(crq));
1766 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1767 crq.request_capability.cmd = REQUEST_CAPABILITY;
1768
1769 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001770 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001771 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001772 ibmvnic_send_crq(adapter, &crq);
1773
1774 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001775 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001776 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001777 ibmvnic_send_crq(adapter, &crq);
1778
1779 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
Thomas Falconde89e852016-03-01 10:20:09 -06001780 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
Thomas Falcon901e0402017-02-15 12:17:59 -06001781 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001782 ibmvnic_send_crq(adapter, &crq);
1783
1784 crq.request_capability.capability =
1785 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1786 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001787 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001788 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001789 ibmvnic_send_crq(adapter, &crq);
1790
1791 crq.request_capability.capability =
1792 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1793 crq.request_capability.number =
Thomas Falconde89e852016-03-01 10:20:09 -06001794 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
Thomas Falcon901e0402017-02-15 12:17:59 -06001795 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001796 ibmvnic_send_crq(adapter, &crq);
1797
1798 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
Thomas Falconde89e852016-03-01 10:20:09 -06001799 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
Thomas Falcon901e0402017-02-15 12:17:59 -06001800 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001801 ibmvnic_send_crq(adapter, &crq);
1802
1803 if (adapter->netdev->flags & IFF_PROMISC) {
1804 if (adapter->promisc_supported) {
1805 crq.request_capability.capability =
1806 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001807 crq.request_capability.number = cpu_to_be64(1);
Thomas Falcon901e0402017-02-15 12:17:59 -06001808 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001809 ibmvnic_send_crq(adapter, &crq);
1810 }
1811 } else {
1812 crq.request_capability.capability =
1813 cpu_to_be16(PROMISC_REQUESTED);
Thomas Falconde89e852016-03-01 10:20:09 -06001814 crq.request_capability.number = cpu_to_be64(0);
Thomas Falcon901e0402017-02-15 12:17:59 -06001815 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06001816 ibmvnic_send_crq(adapter, &crq);
1817 }
1818
1819 kfree(allqueues);
1820
1821 return;
1822
Thomas Falcon032c5e82015-12-21 11:26:06 -06001823rx_failed:
1824 kfree(adapter->tx_scrq);
1825 adapter->tx_scrq = NULL;
1826tx_failed:
1827 for (i = 0; i < registered_queues; i++)
1828 release_sub_crq_queue(adapter, allqueues[i]);
1829 kfree(allqueues);
1830allqueues_failed:
1831 ibmvnic_remove(adapter->vdev);
1832}
1833
1834static int pending_scrq(struct ibmvnic_adapter *adapter,
1835 struct ibmvnic_sub_crq_queue *scrq)
1836{
1837 union sub_crq *entry = &scrq->msgs[scrq->cur];
1838
1839 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1840 return 1;
1841 else
1842 return 0;
1843}
1844
1845static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1846 struct ibmvnic_sub_crq_queue *scrq)
1847{
1848 union sub_crq *entry;
1849 unsigned long flags;
1850
1851 spin_lock_irqsave(&scrq->lock, flags);
1852 entry = &scrq->msgs[scrq->cur];
1853 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1854 if (++scrq->cur == scrq->size)
1855 scrq->cur = 0;
1856 } else {
1857 entry = NULL;
1858 }
1859 spin_unlock_irqrestore(&scrq->lock, flags);
1860
1861 return entry;
1862}
1863
1864static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1865{
1866 struct ibmvnic_crq_queue *queue = &adapter->crq;
1867 union ibmvnic_crq *crq;
1868
1869 crq = &queue->msgs[queue->cur];
1870 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1871 if (++queue->cur == queue->size)
1872 queue->cur = 0;
1873 } else {
1874 crq = NULL;
1875 }
1876
1877 return crq;
1878}
1879
1880static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1881 union sub_crq *sub_crq)
1882{
1883 unsigned int ua = adapter->vdev->unit_address;
1884 struct device *dev = &adapter->vdev->dev;
1885 u64 *u64_crq = (u64 *)sub_crq;
1886 int rc;
1887
1888 netdev_dbg(adapter->netdev,
1889 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1890 (unsigned long int)cpu_to_be64(remote_handle),
1891 (unsigned long int)cpu_to_be64(u64_crq[0]),
1892 (unsigned long int)cpu_to_be64(u64_crq[1]),
1893 (unsigned long int)cpu_to_be64(u64_crq[2]),
1894 (unsigned long int)cpu_to_be64(u64_crq[3]));
1895
1896 /* Make sure the hypervisor sees the complete request */
1897 mb();
1898
1899 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1900 cpu_to_be64(remote_handle),
1901 cpu_to_be64(u64_crq[0]),
1902 cpu_to_be64(u64_crq[1]),
1903 cpu_to_be64(u64_crq[2]),
1904 cpu_to_be64(u64_crq[3]));
1905
1906 if (rc) {
1907 if (rc == H_CLOSED)
1908 dev_warn(dev, "CRQ Queue closed\n");
1909 dev_err(dev, "Send error (rc=%d)\n", rc);
1910 }
1911
1912 return rc;
1913}
1914
Thomas Falconad7775d2016-04-01 17:20:34 -05001915static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1916 u64 remote_handle, u64 ioba, u64 num_entries)
1917{
1918 unsigned int ua = adapter->vdev->unit_address;
1919 struct device *dev = &adapter->vdev->dev;
1920 int rc;
1921
1922 /* Make sure the hypervisor sees the complete request */
1923 mb();
1924 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1925 cpu_to_be64(remote_handle),
1926 ioba, num_entries);
1927
1928 if (rc) {
1929 if (rc == H_CLOSED)
1930 dev_warn(dev, "CRQ Queue closed\n");
1931 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1932 }
1933
1934 return rc;
1935}
1936
Thomas Falcon032c5e82015-12-21 11:26:06 -06001937static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1938 union ibmvnic_crq *crq)
1939{
1940 unsigned int ua = adapter->vdev->unit_address;
1941 struct device *dev = &adapter->vdev->dev;
1942 u64 *u64_crq = (u64 *)crq;
1943 int rc;
1944
1945 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1946 (unsigned long int)cpu_to_be64(u64_crq[0]),
1947 (unsigned long int)cpu_to_be64(u64_crq[1]));
1948
1949 /* Make sure the hypervisor sees the complete request */
1950 mb();
1951
1952 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1953 cpu_to_be64(u64_crq[0]),
1954 cpu_to_be64(u64_crq[1]));
1955
1956 if (rc) {
1957 if (rc == H_CLOSED)
1958 dev_warn(dev, "CRQ Queue closed\n");
1959 dev_warn(dev, "Send error (rc=%d)\n", rc);
1960 }
1961
1962 return rc;
1963}
1964
1965static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1966{
1967 union ibmvnic_crq crq;
1968
1969 memset(&crq, 0, sizeof(crq));
1970 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1971 crq.generic.cmd = IBMVNIC_CRQ_INIT;
1972 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1973
1974 return ibmvnic_send_crq(adapter, &crq);
1975}
1976
1977static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1978{
1979 union ibmvnic_crq crq;
1980
1981 memset(&crq, 0, sizeof(crq));
1982 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1983 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1984 netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1985
1986 return ibmvnic_send_crq(adapter, &crq);
1987}
1988
1989static int send_version_xchg(struct ibmvnic_adapter *adapter)
1990{
1991 union ibmvnic_crq crq;
1992
1993 memset(&crq, 0, sizeof(crq));
1994 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1995 crq.version_exchange.cmd = VERSION_EXCHANGE;
1996 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1997
1998 return ibmvnic_send_crq(adapter, &crq);
1999}
2000
2001static void send_login(struct ibmvnic_adapter *adapter)
2002{
2003 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2004 struct ibmvnic_login_buffer *login_buffer;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002005 struct device *dev = &adapter->vdev->dev;
2006 dma_addr_t rsp_buffer_token;
2007 dma_addr_t buffer_token;
2008 size_t rsp_buffer_size;
2009 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002010 size_t buffer_size;
2011 __be64 *tx_list_p;
2012 __be64 *rx_list_p;
2013 int i;
2014
2015 buffer_size =
2016 sizeof(struct ibmvnic_login_buffer) +
2017 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2018
2019 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2020 if (!login_buffer)
2021 goto buf_alloc_failed;
2022
2023 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2024 DMA_TO_DEVICE);
2025 if (dma_mapping_error(dev, buffer_token)) {
2026 dev_err(dev, "Couldn't map login buffer\n");
2027 goto buf_map_failed;
2028 }
2029
John Allen498cd8e2016-04-06 11:49:55 -05002030 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2031 sizeof(u64) * adapter->req_tx_queues +
2032 sizeof(u64) * adapter->req_rx_queues +
2033 sizeof(u64) * adapter->req_rx_queues +
2034 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002035
2036 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2037 if (!login_rsp_buffer)
2038 goto buf_rsp_alloc_failed;
2039
2040 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2041 rsp_buffer_size, DMA_FROM_DEVICE);
2042 if (dma_mapping_error(dev, rsp_buffer_token)) {
2043 dev_err(dev, "Couldn't map login rsp buffer\n");
2044 goto buf_rsp_map_failed;
2045 }
Nathan Fontenot661a2622017-04-19 13:44:58 -04002046
Thomas Falcon032c5e82015-12-21 11:26:06 -06002047 adapter->login_buf = login_buffer;
2048 adapter->login_buf_token = buffer_token;
2049 adapter->login_buf_sz = buffer_size;
2050 adapter->login_rsp_buf = login_rsp_buffer;
2051 adapter->login_rsp_buf_token = rsp_buffer_token;
2052 adapter->login_rsp_buf_sz = rsp_buffer_size;
2053
2054 login_buffer->len = cpu_to_be32(buffer_size);
2055 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2056 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2057 login_buffer->off_txcomp_subcrqs =
2058 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2059 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2060 login_buffer->off_rxcomp_subcrqs =
2061 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2062 sizeof(u64) * adapter->req_tx_queues);
2063 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2064 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2065
2066 tx_list_p = (__be64 *)((char *)login_buffer +
2067 sizeof(struct ibmvnic_login_buffer));
2068 rx_list_p = (__be64 *)((char *)login_buffer +
2069 sizeof(struct ibmvnic_login_buffer) +
2070 sizeof(u64) * adapter->req_tx_queues);
2071
2072 for (i = 0; i < adapter->req_tx_queues; i++) {
2073 if (adapter->tx_scrq[i]) {
2074 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2075 crq_num);
2076 }
2077 }
2078
2079 for (i = 0; i < adapter->req_rx_queues; i++) {
2080 if (adapter->rx_scrq[i]) {
2081 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2082 crq_num);
2083 }
2084 }
2085
2086 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2087 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2088 netdev_dbg(adapter->netdev, "%016lx\n",
2089 ((unsigned long int *)(adapter->login_buf))[i]);
2090 }
2091
2092 memset(&crq, 0, sizeof(crq));
2093 crq.login.first = IBMVNIC_CRQ_CMD;
2094 crq.login.cmd = LOGIN;
2095 crq.login.ioba = cpu_to_be32(buffer_token);
2096 crq.login.len = cpu_to_be32(buffer_size);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002097 ibmvnic_send_crq(adapter, &crq);
2098
2099 return;
2100
Thomas Falcon032c5e82015-12-21 11:26:06 -06002101buf_rsp_map_failed:
2102 kfree(login_rsp_buffer);
2103buf_rsp_alloc_failed:
2104 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2105buf_map_failed:
2106 kfree(login_buffer);
2107buf_alloc_failed:
2108 return;
2109}
2110
2111static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2112 u32 len, u8 map_id)
2113{
2114 union ibmvnic_crq crq;
2115
2116 memset(&crq, 0, sizeof(crq));
2117 crq.request_map.first = IBMVNIC_CRQ_CMD;
2118 crq.request_map.cmd = REQUEST_MAP;
2119 crq.request_map.map_id = map_id;
2120 crq.request_map.ioba = cpu_to_be32(addr);
2121 crq.request_map.len = cpu_to_be32(len);
2122 ibmvnic_send_crq(adapter, &crq);
2123}
2124
2125static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2126{
2127 union ibmvnic_crq crq;
2128
2129 memset(&crq, 0, sizeof(crq));
2130 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2131 crq.request_unmap.cmd = REQUEST_UNMAP;
2132 crq.request_unmap.map_id = map_id;
2133 ibmvnic_send_crq(adapter, &crq);
2134}
2135
2136static void send_map_query(struct ibmvnic_adapter *adapter)
2137{
2138 union ibmvnic_crq crq;
2139
2140 memset(&crq, 0, sizeof(crq));
2141 crq.query_map.first = IBMVNIC_CRQ_CMD;
2142 crq.query_map.cmd = QUERY_MAP;
2143 ibmvnic_send_crq(adapter, &crq);
2144}
2145
2146/* Send a series of CRQs requesting various capabilities of the VNIC server */
2147static void send_cap_queries(struct ibmvnic_adapter *adapter)
2148{
2149 union ibmvnic_crq crq;
2150
Thomas Falcon901e0402017-02-15 12:17:59 -06002151 atomic_set(&adapter->running_cap_crqs, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002152 memset(&crq, 0, sizeof(crq));
2153 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2154 crq.query_capability.cmd = QUERY_CAPABILITY;
2155
2156 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002157 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002158 ibmvnic_send_crq(adapter, &crq);
2159
2160 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002161 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002162 ibmvnic_send_crq(adapter, &crq);
2163
2164 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002165 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002166 ibmvnic_send_crq(adapter, &crq);
2167
2168 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002169 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002170 ibmvnic_send_crq(adapter, &crq);
2171
2172 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002173 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002174 ibmvnic_send_crq(adapter, &crq);
2175
2176 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002177 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002178 ibmvnic_send_crq(adapter, &crq);
2179
2180 crq.query_capability.capability =
2181 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002182 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002183 ibmvnic_send_crq(adapter, &crq);
2184
2185 crq.query_capability.capability =
2186 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002187 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002188 ibmvnic_send_crq(adapter, &crq);
2189
2190 crq.query_capability.capability =
2191 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002192 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002193 ibmvnic_send_crq(adapter, &crq);
2194
2195 crq.query_capability.capability =
2196 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002197 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002198 ibmvnic_send_crq(adapter, &crq);
2199
2200 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
Thomas Falcon901e0402017-02-15 12:17:59 -06002201 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002202 ibmvnic_send_crq(adapter, &crq);
2203
2204 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002205 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002206 ibmvnic_send_crq(adapter, &crq);
2207
2208 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002209 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002210 ibmvnic_send_crq(adapter, &crq);
2211
2212 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
Thomas Falcon901e0402017-02-15 12:17:59 -06002213 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002214 ibmvnic_send_crq(adapter, &crq);
2215
2216 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
Thomas Falcon901e0402017-02-15 12:17:59 -06002217 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002218 ibmvnic_send_crq(adapter, &crq);
2219
2220 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
Thomas Falcon901e0402017-02-15 12:17:59 -06002221 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002222 ibmvnic_send_crq(adapter, &crq);
2223
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002224 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2225 atomic_inc(&adapter->running_cap_crqs);
2226 ibmvnic_send_crq(adapter, &crq);
2227
Thomas Falcon032c5e82015-12-21 11:26:06 -06002228 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002229 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002230 ibmvnic_send_crq(adapter, &crq);
2231
2232 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
Thomas Falcon901e0402017-02-15 12:17:59 -06002233 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002234 ibmvnic_send_crq(adapter, &crq);
2235
2236 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002237 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002238 ibmvnic_send_crq(adapter, &crq);
2239
2240 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
Thomas Falcon901e0402017-02-15 12:17:59 -06002241 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002242 ibmvnic_send_crq(adapter, &crq);
2243
2244 crq.query_capability.capability =
2245 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
Thomas Falcon901e0402017-02-15 12:17:59 -06002246 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002247 ibmvnic_send_crq(adapter, &crq);
2248
2249 crq.query_capability.capability =
2250 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002251 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002252 ibmvnic_send_crq(adapter, &crq);
2253
2254 crq.query_capability.capability =
2255 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002256 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002257 ibmvnic_send_crq(adapter, &crq);
2258
2259 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
Thomas Falcon901e0402017-02-15 12:17:59 -06002260 atomic_inc(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002261 ibmvnic_send_crq(adapter, &crq);
2262}
2263
2264static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2265{
2266 struct device *dev = &adapter->vdev->dev;
2267 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2268 union ibmvnic_crq crq;
2269 int i;
2270
2271 dma_unmap_single(dev, adapter->ip_offload_tok,
2272 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2273
2274 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2275 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2276 netdev_dbg(adapter->netdev, "%016lx\n",
2277 ((unsigned long int *)(buf))[i]);
2278
2279 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2280 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2281 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2282 buf->tcp_ipv4_chksum);
2283 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2284 buf->tcp_ipv6_chksum);
2285 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2286 buf->udp_ipv4_chksum);
2287 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2288 buf->udp_ipv6_chksum);
2289 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2290 buf->large_tx_ipv4);
2291 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2292 buf->large_tx_ipv6);
2293 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2294 buf->large_rx_ipv4);
2295 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2296 buf->large_rx_ipv6);
2297 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2298 buf->max_ipv4_header_size);
2299 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2300 buf->max_ipv6_header_size);
2301 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2302 buf->max_tcp_header_size);
2303 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2304 buf->max_udp_header_size);
2305 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2306 buf->max_large_tx_size);
2307 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2308 buf->max_large_rx_size);
2309 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2310 buf->ipv6_extension_header);
2311 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2312 buf->tcp_pseudosum_req);
2313 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2314 buf->num_ipv6_ext_headers);
2315 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2316 buf->off_ipv6_ext_headers);
2317
2318 adapter->ip_offload_ctrl_tok =
2319 dma_map_single(dev, &adapter->ip_offload_ctrl,
2320 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2321
2322 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2323 dev_err(dev, "Couldn't map ip offload control buffer\n");
2324 return;
2325 }
2326
2327 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2328 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2329 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2330 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2331 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2332
2333 /* large_tx/rx disabled for now, additional features needed */
2334 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2335 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2336 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2337 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2338
2339 adapter->netdev->features = NETIF_F_GSO;
2340
2341 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2342 adapter->netdev->features |= NETIF_F_IP_CSUM;
2343
2344 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2345 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2346
Thomas Falcon9be02cd2016-04-01 17:20:35 -05002347 if ((adapter->netdev->features &
2348 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2349 adapter->netdev->features |= NETIF_F_RXCSUM;
2350
Thomas Falcon032c5e82015-12-21 11:26:06 -06002351 memset(&crq, 0, sizeof(crq));
2352 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2353 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2354 crq.control_ip_offload.len =
2355 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2356 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2357 ibmvnic_send_crq(adapter, &crq);
2358}
2359
2360static void handle_error_info_rsp(union ibmvnic_crq *crq,
2361 struct ibmvnic_adapter *adapter)
2362{
2363 struct device *dev = &adapter->vdev->dev;
Wei Yongjun96183182016-06-27 20:48:53 +08002364 struct ibmvnic_error_buff *error_buff, *tmp;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002365 unsigned long flags;
2366 bool found = false;
2367 int i;
2368
2369 if (!crq->request_error_rsp.rc.code) {
2370 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2371 crq->request_error_rsp.rc.code);
2372 return;
2373 }
2374
2375 spin_lock_irqsave(&adapter->error_list_lock, flags);
Wei Yongjun96183182016-06-27 20:48:53 +08002376 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002377 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2378 found = true;
2379 list_del(&error_buff->list);
2380 break;
2381 }
2382 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2383
2384 if (!found) {
2385 dev_err(dev, "Couldn't find error id %x\n",
Thomas Falcon75224c92017-02-15 10:33:33 -06002386 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002387 return;
2388 }
2389
2390 dev_err(dev, "Detailed info for error id %x:",
Thomas Falcon75224c92017-02-15 10:33:33 -06002391 be32_to_cpu(crq->request_error_rsp.error_id));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002392
2393 for (i = 0; i < error_buff->len; i++) {
2394 pr_cont("%02x", (int)error_buff->buff[i]);
2395 if (i % 8 == 7)
2396 pr_cont(" ");
2397 }
2398 pr_cont("\n");
2399
2400 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2401 DMA_FROM_DEVICE);
2402 kfree(error_buff->buff);
2403 kfree(error_buff);
2404}
2405
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002406static void request_error_information(struct ibmvnic_adapter *adapter,
2407 union ibmvnic_crq *err_crq)
Thomas Falcon032c5e82015-12-21 11:26:06 -06002408{
Thomas Falcon032c5e82015-12-21 11:26:06 -06002409 struct device *dev = &adapter->vdev->dev;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002410 struct net_device *netdev = adapter->netdev;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002411 struct ibmvnic_error_buff *error_buff;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002412 unsigned long timeout = msecs_to_jiffies(30000);
2413 union ibmvnic_crq crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002414 unsigned long flags;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002415 int rc, detail_len;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002416
2417 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2418 if (!error_buff)
2419 return;
2420
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002421 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002422 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2423 if (!error_buff->buff) {
2424 kfree(error_buff);
2425 return;
2426 }
2427
2428 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2429 DMA_FROM_DEVICE);
2430 if (dma_mapping_error(dev, error_buff->dma)) {
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002431 netdev_err(netdev, "Couldn't map error buffer\n");
Thomas Falcon032c5e82015-12-21 11:26:06 -06002432 kfree(error_buff->buff);
2433 kfree(error_buff);
2434 return;
2435 }
2436
Thomas Falcon032c5e82015-12-21 11:26:06 -06002437 error_buff->len = detail_len;
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002438 error_buff->error_id = err_crq->error_indication.error_id;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002439
2440 spin_lock_irqsave(&adapter->error_list_lock, flags);
2441 list_add_tail(&error_buff->list, &adapter->errors);
2442 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2443
Nathan Fontenot2f9de9b2017-04-21 15:38:52 -04002444 memset(&crq, 0, sizeof(crq));
2445 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2446 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2447 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2448 crq.request_error_info.len = cpu_to_be32(detail_len);
2449 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2450
2451 rc = ibmvnic_send_crq(adapter, &crq);
2452 if (rc) {
2453 netdev_err(netdev, "failed to request error information\n");
2454 goto err_info_fail;
2455 }
2456
2457 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2458 netdev_err(netdev, "timeout waiting for error information\n");
2459 goto err_info_fail;
2460 }
2461
2462 return;
2463
2464err_info_fail:
2465 spin_lock_irqsave(&adapter->error_list_lock, flags);
2466 list_del(&error_buff->list);
2467 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2468
2469 kfree(error_buff->buff);
2470 kfree(error_buff);
2471}
2472
2473static void handle_error_indication(union ibmvnic_crq *crq,
2474 struct ibmvnic_adapter *adapter)
2475{
2476 struct device *dev = &adapter->vdev->dev;
2477
2478 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2479 crq->error_indication.flags
2480 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2481 be32_to_cpu(crq->error_indication.error_id),
2482 be16_to_cpu(crq->error_indication.error_cause));
2483
2484 if (be32_to_cpu(crq->error_indication.error_id))
2485 request_error_information(adapter, crq);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002486}
2487
2488static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2489 struct ibmvnic_adapter *adapter)
2490{
2491 struct net_device *netdev = adapter->netdev;
2492 struct device *dev = &adapter->vdev->dev;
2493 long rc;
2494
2495 rc = crq->change_mac_addr_rsp.rc.code;
2496 if (rc) {
2497 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2498 return;
2499 }
2500 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2501 ETH_ALEN);
2502}
2503
2504static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2505 struct ibmvnic_adapter *adapter)
2506{
2507 struct device *dev = &adapter->vdev->dev;
2508 u64 *req_value;
2509 char *name;
2510
Thomas Falcon901e0402017-02-15 12:17:59 -06002511 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002512 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2513 case REQ_TX_QUEUES:
2514 req_value = &adapter->req_tx_queues;
2515 name = "tx";
2516 break;
2517 case REQ_RX_QUEUES:
2518 req_value = &adapter->req_rx_queues;
2519 name = "rx";
2520 break;
2521 case REQ_RX_ADD_QUEUES:
2522 req_value = &adapter->req_rx_add_queues;
2523 name = "rx_add";
2524 break;
2525 case REQ_TX_ENTRIES_PER_SUBCRQ:
2526 req_value = &adapter->req_tx_entries_per_subcrq;
2527 name = "tx_entries_per_subcrq";
2528 break;
2529 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2530 req_value = &adapter->req_rx_add_entries_per_subcrq;
2531 name = "rx_add_entries_per_subcrq";
2532 break;
2533 case REQ_MTU:
2534 req_value = &adapter->req_mtu;
2535 name = "mtu";
2536 break;
2537 case PROMISC_REQUESTED:
2538 req_value = &adapter->promisc;
2539 name = "promisc";
2540 break;
2541 default:
2542 dev_err(dev, "Got invalid cap request rsp %d\n",
2543 crq->request_capability.capability);
2544 return;
2545 }
2546
2547 switch (crq->request_capability_rsp.rc.code) {
2548 case SUCCESS:
2549 break;
2550 case PARTIALSUCCESS:
2551 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2552 *req_value,
Thomas Falcon28f4d162017-02-15 10:32:11 -06002553 (long int)be64_to_cpu(crq->request_capability_rsp.
Thomas Falcon032c5e82015-12-21 11:26:06 -06002554 number), name);
Nathan Fontenotb5108882017-03-30 02:49:18 -04002555 release_sub_crqs(adapter);
Thomas Falcon28f4d162017-02-15 10:32:11 -06002556 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
Thomas Falconea22d512016-07-06 15:35:17 -05002557 init_sub_crqs(adapter, 1);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002558 return;
2559 default:
2560 dev_err(dev, "Error %d in request cap rsp\n",
2561 crq->request_capability_rsp.rc.code);
2562 return;
2563 }
2564
2565 /* Done receiving requested capabilities, query IP offload support */
Thomas Falcon901e0402017-02-15 12:17:59 -06002566 if (atomic_read(&adapter->running_cap_crqs) == 0) {
Thomas Falcon032c5e82015-12-21 11:26:06 -06002567 union ibmvnic_crq newcrq;
2568 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2569 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2570 &adapter->ip_offload_buf;
2571
Thomas Falcon249168a2017-02-15 12:18:00 -06002572 adapter->wait_capability = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002573 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2574 buf_sz,
2575 DMA_FROM_DEVICE);
2576
2577 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2578 if (!firmware_has_feature(FW_FEATURE_CMO))
2579 dev_err(dev, "Couldn't map offload buffer\n");
2580 return;
2581 }
2582
2583 memset(&newcrq, 0, sizeof(newcrq));
2584 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2585 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2586 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2587 newcrq.query_ip_offload.ioba =
2588 cpu_to_be32(adapter->ip_offload_tok);
2589
2590 ibmvnic_send_crq(adapter, &newcrq);
2591 }
2592}
2593
2594static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2595 struct ibmvnic_adapter *adapter)
2596{
2597 struct device *dev = &adapter->vdev->dev;
2598 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2599 struct ibmvnic_login_buffer *login = adapter->login_buf;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002600 int i;
2601
2602 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2603 DMA_BIDIRECTIONAL);
2604 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2605 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2606
John Allen498cd8e2016-04-06 11:49:55 -05002607 /* If the number of queues requested can't be allocated by the
2608 * server, the login response will return with code 1. We will need
2609 * to resend the login buffer with fewer queues requested.
2610 */
2611 if (login_rsp_crq->generic.rc.code) {
2612 adapter->renegotiate = true;
2613 complete(&adapter->init_done);
2614 return 0;
2615 }
2616
Thomas Falcon032c5e82015-12-21 11:26:06 -06002617 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2618 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2619 netdev_dbg(adapter->netdev, "%016lx\n",
2620 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2621 }
2622
2623 /* Sanity checks */
2624 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2625 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2626 adapter->req_rx_add_queues !=
2627 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2628 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2629 ibmvnic_remove(adapter->vdev);
2630 return -EIO;
2631 }
2632 complete(&adapter->init_done);
2633
Thomas Falcon032c5e82015-12-21 11:26:06 -06002634 return 0;
2635}
2636
2637static void handle_request_map_rsp(union ibmvnic_crq *crq,
2638 struct ibmvnic_adapter *adapter)
2639{
2640 struct device *dev = &adapter->vdev->dev;
2641 u8 map_id = crq->request_map_rsp.map_id;
2642 int tx_subcrqs;
2643 int rx_subcrqs;
2644 long rc;
2645 int i;
2646
2647 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2648 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2649
2650 rc = crq->request_map_rsp.rc.code;
2651 if (rc) {
2652 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2653 adapter->map_id--;
2654 /* need to find and zero tx/rx_pool map_id */
2655 for (i = 0; i < tx_subcrqs; i++) {
2656 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2657 adapter->tx_pool[i].long_term_buff.map_id = 0;
2658 }
2659 for (i = 0; i < rx_subcrqs; i++) {
2660 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2661 adapter->rx_pool[i].long_term_buff.map_id = 0;
2662 }
2663 }
2664 complete(&adapter->fw_done);
2665}
2666
2667static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2668 struct ibmvnic_adapter *adapter)
2669{
2670 struct device *dev = &adapter->vdev->dev;
2671 long rc;
2672
2673 rc = crq->request_unmap_rsp.rc.code;
2674 if (rc)
2675 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2676}
2677
2678static void handle_query_map_rsp(union ibmvnic_crq *crq,
2679 struct ibmvnic_adapter *adapter)
2680{
2681 struct net_device *netdev = adapter->netdev;
2682 struct device *dev = &adapter->vdev->dev;
2683 long rc;
2684
2685 rc = crq->query_map_rsp.rc.code;
2686 if (rc) {
2687 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2688 return;
2689 }
2690 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2691 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2692 crq->query_map_rsp.free_pages);
2693}
2694
2695static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2696 struct ibmvnic_adapter *adapter)
2697{
2698 struct net_device *netdev = adapter->netdev;
2699 struct device *dev = &adapter->vdev->dev;
2700 long rc;
2701
Thomas Falcon901e0402017-02-15 12:17:59 -06002702 atomic_dec(&adapter->running_cap_crqs);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002703 netdev_dbg(netdev, "Outstanding queries: %d\n",
Thomas Falcon901e0402017-02-15 12:17:59 -06002704 atomic_read(&adapter->running_cap_crqs));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002705 rc = crq->query_capability.rc.code;
2706 if (rc) {
2707 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2708 goto out;
2709 }
2710
2711 switch (be16_to_cpu(crq->query_capability.capability)) {
2712 case MIN_TX_QUEUES:
2713 adapter->min_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002714 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002715 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2716 adapter->min_tx_queues);
2717 break;
2718 case MIN_RX_QUEUES:
2719 adapter->min_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002720 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002721 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2722 adapter->min_rx_queues);
2723 break;
2724 case MIN_RX_ADD_QUEUES:
2725 adapter->min_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002726 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002727 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2728 adapter->min_rx_add_queues);
2729 break;
2730 case MAX_TX_QUEUES:
2731 adapter->max_tx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002732 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002733 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2734 adapter->max_tx_queues);
2735 break;
2736 case MAX_RX_QUEUES:
2737 adapter->max_rx_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002738 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002739 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2740 adapter->max_rx_queues);
2741 break;
2742 case MAX_RX_ADD_QUEUES:
2743 adapter->max_rx_add_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002744 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002745 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2746 adapter->max_rx_add_queues);
2747 break;
2748 case MIN_TX_ENTRIES_PER_SUBCRQ:
2749 adapter->min_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002750 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002751 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2752 adapter->min_tx_entries_per_subcrq);
2753 break;
2754 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2755 adapter->min_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002756 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002757 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2758 adapter->min_rx_add_entries_per_subcrq);
2759 break;
2760 case MAX_TX_ENTRIES_PER_SUBCRQ:
2761 adapter->max_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002762 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002763 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2764 adapter->max_tx_entries_per_subcrq);
2765 break;
2766 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2767 adapter->max_rx_add_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002768 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002769 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2770 adapter->max_rx_add_entries_per_subcrq);
2771 break;
2772 case TCP_IP_OFFLOAD:
2773 adapter->tcp_ip_offload =
Thomas Falconde89e852016-03-01 10:20:09 -06002774 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002775 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2776 adapter->tcp_ip_offload);
2777 break;
2778 case PROMISC_SUPPORTED:
2779 adapter->promisc_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002780 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002781 netdev_dbg(netdev, "promisc_supported = %lld\n",
2782 adapter->promisc_supported);
2783 break;
2784 case MIN_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002785 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002786 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002787 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2788 break;
2789 case MAX_MTU:
Thomas Falconde89e852016-03-01 10:20:09 -06002790 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
Thomas Falconf39f0d12017-02-14 10:22:59 -06002791 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002792 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2793 break;
2794 case MAX_MULTICAST_FILTERS:
2795 adapter->max_multicast_filters =
Thomas Falconde89e852016-03-01 10:20:09 -06002796 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002797 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2798 adapter->max_multicast_filters);
2799 break;
2800 case VLAN_HEADER_INSERTION:
2801 adapter->vlan_header_insertion =
Thomas Falconde89e852016-03-01 10:20:09 -06002802 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002803 if (adapter->vlan_header_insertion)
2804 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2805 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2806 adapter->vlan_header_insertion);
2807 break;
Murilo Fossa Vicentini6052d5e2017-04-21 15:38:46 -04002808 case RX_VLAN_HEADER_INSERTION:
2809 adapter->rx_vlan_header_insertion =
2810 be64_to_cpu(crq->query_capability.number);
2811 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
2812 adapter->rx_vlan_header_insertion);
2813 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002814 case MAX_TX_SG_ENTRIES:
2815 adapter->max_tx_sg_entries =
Thomas Falconde89e852016-03-01 10:20:09 -06002816 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002817 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2818 adapter->max_tx_sg_entries);
2819 break;
2820 case RX_SG_SUPPORTED:
2821 adapter->rx_sg_supported =
Thomas Falconde89e852016-03-01 10:20:09 -06002822 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002823 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2824 adapter->rx_sg_supported);
2825 break;
2826 case OPT_TX_COMP_SUB_QUEUES:
2827 adapter->opt_tx_comp_sub_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002828 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002829 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2830 adapter->opt_tx_comp_sub_queues);
2831 break;
2832 case OPT_RX_COMP_QUEUES:
2833 adapter->opt_rx_comp_queues =
Thomas Falconde89e852016-03-01 10:20:09 -06002834 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002835 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2836 adapter->opt_rx_comp_queues);
2837 break;
2838 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2839 adapter->opt_rx_bufadd_q_per_rx_comp_q =
Thomas Falconde89e852016-03-01 10:20:09 -06002840 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002841 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2842 adapter->opt_rx_bufadd_q_per_rx_comp_q);
2843 break;
2844 case OPT_TX_ENTRIES_PER_SUBCRQ:
2845 adapter->opt_tx_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002846 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002847 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2848 adapter->opt_tx_entries_per_subcrq);
2849 break;
2850 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2851 adapter->opt_rxba_entries_per_subcrq =
Thomas Falconde89e852016-03-01 10:20:09 -06002852 be64_to_cpu(crq->query_capability.number);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002853 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2854 adapter->opt_rxba_entries_per_subcrq);
2855 break;
2856 case TX_RX_DESC_REQ:
2857 adapter->tx_rx_desc_req = crq->query_capability.number;
2858 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2859 adapter->tx_rx_desc_req);
2860 break;
2861
2862 default:
2863 netdev_err(netdev, "Got invalid cap rsp %d\n",
2864 crq->query_capability.capability);
2865 }
2866
2867out:
Thomas Falcon249168a2017-02-15 12:18:00 -06002868 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2869 adapter->wait_capability = false;
Thomas Falconea22d512016-07-06 15:35:17 -05002870 init_sub_crqs(adapter, 0);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002871 /* We're done querying the capabilities, initialize sub-crqs */
Thomas Falcon249168a2017-02-15 12:18:00 -06002872 }
Thomas Falcon032c5e82015-12-21 11:26:06 -06002873}
2874
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002875static void ibmvnic_xport_event(struct work_struct *work)
2876{
2877 struct ibmvnic_adapter *adapter = container_of(work,
2878 struct ibmvnic_adapter,
2879 ibmvnic_xport);
2880 struct device *dev = &adapter->vdev->dev;
2881 long rc;
2882
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002883 release_sub_crqs(adapter);
2884 if (adapter->migrated) {
2885 rc = ibmvnic_reenable_crq_queue(adapter);
2886 if (rc)
2887 dev_err(dev, "Error after enable rc=%ld\n", rc);
2888 adapter->migrated = false;
2889 rc = ibmvnic_send_crq_init(adapter);
2890 if (rc)
2891 dev_err(dev, "Error sending init rc=%ld\n", rc);
2892 }
2893}
2894
Thomas Falcon032c5e82015-12-21 11:26:06 -06002895static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2896 struct ibmvnic_adapter *adapter)
2897{
2898 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2899 struct net_device *netdev = adapter->netdev;
2900 struct device *dev = &adapter->vdev->dev;
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002901 u64 *u64_crq = (u64 *)crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002902 long rc;
2903
2904 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
Murilo Fossa Vicentini993a82b2017-04-19 13:44:35 -04002905 (unsigned long int)cpu_to_be64(u64_crq[0]),
2906 (unsigned long int)cpu_to_be64(u64_crq[1]));
Thomas Falcon032c5e82015-12-21 11:26:06 -06002907 switch (gen_crq->first) {
2908 case IBMVNIC_CRQ_INIT_RSP:
2909 switch (gen_crq->cmd) {
2910 case IBMVNIC_CRQ_INIT:
2911 dev_info(dev, "Partner initialized\n");
2912 /* Send back a response */
2913 rc = ibmvnic_send_crq_init_complete(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05002914 if (!rc)
2915 schedule_work(&adapter->vnic_crq_init);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002916 else
2917 dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2918 break;
2919 case IBMVNIC_CRQ_INIT_COMPLETE:
2920 dev_info(dev, "Partner initialization complete\n");
2921 send_version_xchg(adapter);
2922 break;
2923 default:
2924 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2925 }
2926 return;
2927 case IBMVNIC_CRQ_XPORT_EVENT:
2928 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2929 dev_info(dev, "Re-enabling adapter\n");
2930 adapter->migrated = true;
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002931 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcondfad09a2016-08-18 11:37:51 -05002932 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2933 dev_info(dev, "Backing device failover detected\n");
2934 netif_carrier_off(netdev);
2935 adapter->failover = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06002936 } else {
2937 /* The adapter lost the connection */
2938 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2939 gen_crq->cmd);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05002940 schedule_work(&adapter->ibmvnic_xport);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002941 }
2942 return;
2943 case IBMVNIC_CRQ_CMD_RSP:
2944 break;
2945 default:
2946 dev_err(dev, "Got an invalid msg type 0x%02x\n",
2947 gen_crq->first);
2948 return;
2949 }
2950
2951 switch (gen_crq->cmd) {
2952 case VERSION_EXCHANGE_RSP:
2953 rc = crq->version_exchange_rsp.rc.code;
2954 if (rc) {
2955 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2956 break;
2957 }
2958 dev_info(dev, "Partner protocol version is %d\n",
2959 crq->version_exchange_rsp.version);
2960 if (be16_to_cpu(crq->version_exchange_rsp.version) <
2961 ibmvnic_version)
2962 ibmvnic_version =
2963 be16_to_cpu(crq->version_exchange_rsp.version);
2964 send_cap_queries(adapter);
2965 break;
2966 case QUERY_CAPABILITY_RSP:
2967 handle_query_cap_rsp(crq, adapter);
2968 break;
2969 case QUERY_MAP_RSP:
2970 handle_query_map_rsp(crq, adapter);
2971 break;
2972 case REQUEST_MAP_RSP:
2973 handle_request_map_rsp(crq, adapter);
2974 break;
2975 case REQUEST_UNMAP_RSP:
2976 handle_request_unmap_rsp(crq, adapter);
2977 break;
2978 case REQUEST_CAPABILITY_RSP:
2979 handle_request_cap_rsp(crq, adapter);
2980 break;
2981 case LOGIN_RSP:
2982 netdev_dbg(netdev, "Got Login Response\n");
2983 handle_login_rsp(crq, adapter);
2984 break;
2985 case LOGICAL_LINK_STATE_RSP:
Nathan Fontenot53da09e2017-04-21 15:39:04 -04002986 netdev_dbg(netdev,
2987 "Got Logical Link State Response, state: %d rc: %d\n",
2988 crq->logical_link_state_rsp.link_state,
2989 crq->logical_link_state_rsp.rc.code);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002990 adapter->logical_link_state =
2991 crq->logical_link_state_rsp.link_state;
Nathan Fontenot53da09e2017-04-21 15:39:04 -04002992 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
2993 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06002994 break;
2995 case LINK_STATE_INDICATION:
2996 netdev_dbg(netdev, "Got Logical Link State Indication\n");
2997 adapter->phys_link_state =
2998 crq->link_state_indication.phys_link_state;
2999 adapter->logical_link_state =
3000 crq->link_state_indication.logical_link_state;
3001 break;
3002 case CHANGE_MAC_ADDR_RSP:
3003 netdev_dbg(netdev, "Got MAC address change Response\n");
3004 handle_change_mac_rsp(crq, adapter);
3005 break;
3006 case ERROR_INDICATION:
3007 netdev_dbg(netdev, "Got Error Indication\n");
3008 handle_error_indication(crq, adapter);
3009 break;
3010 case REQUEST_ERROR_RSP:
3011 netdev_dbg(netdev, "Got Error Detail Response\n");
3012 handle_error_info_rsp(crq, adapter);
3013 break;
3014 case REQUEST_STATISTICS_RSP:
3015 netdev_dbg(netdev, "Got Statistics Response\n");
3016 complete(&adapter->stats_done);
3017 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003018 case QUERY_IP_OFFLOAD_RSP:
3019 netdev_dbg(netdev, "Got Query IP offload Response\n");
3020 handle_query_ip_offload_rsp(adapter);
3021 break;
3022 case MULTICAST_CTRL_RSP:
3023 netdev_dbg(netdev, "Got multicast control Response\n");
3024 break;
3025 case CONTROL_IP_OFFLOAD_RSP:
3026 netdev_dbg(netdev, "Got Control IP offload Response\n");
3027 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3028 sizeof(adapter->ip_offload_ctrl),
3029 DMA_TO_DEVICE);
John Allenbd0b6722017-03-17 17:13:40 -05003030 complete(&adapter->init_done);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003031 break;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003032 case COLLECT_FW_TRACE_RSP:
3033 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3034 complete(&adapter->fw_done);
3035 break;
3036 default:
3037 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3038 gen_crq->cmd);
3039 }
3040}
3041
3042static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3043{
3044 struct ibmvnic_adapter *adapter = instance;
Thomas Falcon6c267b32017-02-15 12:17:58 -06003045
Thomas Falcon6c267b32017-02-15 12:17:58 -06003046 tasklet_schedule(&adapter->tasklet);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003047 return IRQ_HANDLED;
3048}
3049
3050static void ibmvnic_tasklet(void *data)
3051{
3052 struct ibmvnic_adapter *adapter = data;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003053 struct ibmvnic_crq_queue *queue = &adapter->crq;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003054 union ibmvnic_crq *crq;
3055 unsigned long flags;
3056 bool done = false;
3057
3058 spin_lock_irqsave(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003059 while (!done) {
3060 /* Pull all the valid messages off the CRQ */
3061 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3062 ibmvnic_handle_crq(crq, adapter);
3063 crq->generic.first = 0;
3064 }
Brian Kinged7ecbf2017-04-19 13:44:53 -04003065
3066 /* remain in tasklet until all
3067 * capabilities responses are received
3068 */
3069 if (!adapter->wait_capability)
3070 done = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003071 }
Thomas Falcon249168a2017-02-15 12:18:00 -06003072 /* if capabilities CRQ's were sent in this tasklet, the following
3073 * tasklet must wait until all responses are received
3074 */
3075 if (atomic_read(&adapter->running_cap_crqs) != 0)
3076 adapter->wait_capability = true;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003077 spin_unlock_irqrestore(&queue->lock, flags);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003078}
3079
3080static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3081{
3082 struct vio_dev *vdev = adapter->vdev;
3083 int rc;
3084
3085 do {
3086 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3087 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3088
3089 if (rc)
3090 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3091
3092 return rc;
3093}
3094
3095static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3096{
3097 struct ibmvnic_crq_queue *crq = &adapter->crq;
3098 struct device *dev = &adapter->vdev->dev;
3099 struct vio_dev *vdev = adapter->vdev;
3100 int rc;
3101
3102 /* Close the CRQ */
3103 do {
3104 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3105 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3106
3107 /* Clean out the queue */
3108 memset(crq->msgs, 0, PAGE_SIZE);
3109 crq->cur = 0;
3110
3111 /* And re-open it again */
3112 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3113 crq->msg_token, PAGE_SIZE);
3114
3115 if (rc == H_CLOSED)
3116 /* Adapter is good, but other end is not ready */
3117 dev_warn(dev, "Partner adapter not ready\n");
3118 else if (rc != 0)
3119 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3120
3121 return rc;
3122}
3123
Nathan Fontenotf9928872017-03-30 02:48:54 -04003124static void release_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003125{
3126 struct ibmvnic_crq_queue *crq = &adapter->crq;
3127 struct vio_dev *vdev = adapter->vdev;
3128 long rc;
3129
Nathan Fontenotf9928872017-03-30 02:48:54 -04003130 if (!crq->msgs)
3131 return;
3132
Thomas Falcon032c5e82015-12-21 11:26:06 -06003133 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3134 free_irq(vdev->irq, adapter);
Thomas Falcon6c267b32017-02-15 12:17:58 -06003135 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003136 do {
3137 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3138 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3139
3140 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3141 DMA_BIDIRECTIONAL);
3142 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003143 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003144}
3145
Nathan Fontenotf9928872017-03-30 02:48:54 -04003146static int init_crq_queue(struct ibmvnic_adapter *adapter)
Thomas Falcon032c5e82015-12-21 11:26:06 -06003147{
3148 struct ibmvnic_crq_queue *crq = &adapter->crq;
3149 struct device *dev = &adapter->vdev->dev;
3150 struct vio_dev *vdev = adapter->vdev;
3151 int rc, retrc = -ENOMEM;
3152
Nathan Fontenotf9928872017-03-30 02:48:54 -04003153 if (crq->msgs)
3154 return 0;
3155
Thomas Falcon032c5e82015-12-21 11:26:06 -06003156 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3157 /* Should we allocate more than one page? */
3158
3159 if (!crq->msgs)
3160 return -ENOMEM;
3161
3162 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3163 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3164 DMA_BIDIRECTIONAL);
3165 if (dma_mapping_error(dev, crq->msg_token))
3166 goto map_failed;
3167
3168 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3169 crq->msg_token, PAGE_SIZE);
3170
3171 if (rc == H_RESOURCE)
3172 /* maybe kexecing and resource is busy. try a reset */
3173 rc = ibmvnic_reset_crq(adapter);
3174 retrc = rc;
3175
3176 if (rc == H_CLOSED) {
3177 dev_warn(dev, "Partner adapter not ready\n");
3178 } else if (rc) {
3179 dev_warn(dev, "Error %d opening adapter\n", rc);
3180 goto reg_crq_failed;
3181 }
3182
3183 retrc = 0;
3184
Thomas Falcon6c267b32017-02-15 12:17:58 -06003185 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3186 (unsigned long)adapter);
3187
Thomas Falcon032c5e82015-12-21 11:26:06 -06003188 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3189 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3190 adapter);
3191 if (rc) {
3192 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3193 vdev->irq, rc);
3194 goto req_irq_failed;
3195 }
3196
3197 rc = vio_enable_interrupts(vdev);
3198 if (rc) {
3199 dev_err(dev, "Error %d enabling interrupts\n", rc);
3200 goto req_irq_failed;
3201 }
3202
3203 crq->cur = 0;
3204 spin_lock_init(&crq->lock);
3205
3206 return retrc;
3207
3208req_irq_failed:
Thomas Falcon6c267b32017-02-15 12:17:58 -06003209 tasklet_kill(&adapter->tasklet);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003210 do {
3211 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3212 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3213reg_crq_failed:
3214 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3215map_failed:
3216 free_page((unsigned long)crq->msgs);
Nathan Fontenotf9928872017-03-30 02:48:54 -04003217 crq->msgs = NULL;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003218 return retrc;
3219}
3220
Thomas Falcon65dc6892016-07-06 15:35:18 -05003221static void handle_crq_init_rsp(struct work_struct *work)
3222{
3223 struct ibmvnic_adapter *adapter = container_of(work,
3224 struct ibmvnic_adapter,
3225 vnic_crq_init);
3226 struct device *dev = &adapter->vdev->dev;
3227 struct net_device *netdev = adapter->netdev;
3228 unsigned long timeout = msecs_to_jiffies(30000);
Thomas Falcondfad09a2016-08-18 11:37:51 -05003229 bool restart = false;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003230 int rc;
3231
Thomas Falcondfad09a2016-08-18 11:37:51 -05003232 if (adapter->failover) {
3233 release_sub_crqs(adapter);
3234 if (netif_running(netdev)) {
3235 netif_tx_disable(netdev);
3236 ibmvnic_close(netdev);
3237 restart = true;
3238 }
3239 }
3240
Thomas Falcon65dc6892016-07-06 15:35:18 -05003241 reinit_completion(&adapter->init_done);
Nathan Fontenotdb5d0b52017-02-10 13:45:05 -05003242 send_version_xchg(adapter);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003243 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3244 dev_err(dev, "Passive init timeout\n");
3245 goto task_failed;
3246 }
3247
Thomas Falconf39f0d12017-02-14 10:22:59 -06003248 netdev->mtu = adapter->req_mtu - ETH_HLEN;
Thomas Falcon65dc6892016-07-06 15:35:18 -05003249
Thomas Falcondfad09a2016-08-18 11:37:51 -05003250 if (adapter->failover) {
3251 adapter->failover = false;
3252 if (restart) {
3253 rc = ibmvnic_open(netdev);
3254 if (rc)
3255 goto restart_failed;
3256 }
3257 netif_carrier_on(netdev);
3258 return;
3259 }
3260
Thomas Falcon65dc6892016-07-06 15:35:18 -05003261 rc = register_netdev(netdev);
3262 if (rc) {
3263 dev_err(dev,
3264 "failed to register netdev rc=%d\n", rc);
3265 goto register_failed;
3266 }
3267 dev_info(dev, "ibmvnic registered\n");
3268
3269 return;
3270
Thomas Falcondfad09a2016-08-18 11:37:51 -05003271restart_failed:
3272 dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003273register_failed:
3274 release_sub_crqs(adapter);
3275task_failed:
3276 dev_err(dev, "Passive initialization was not successful\n");
3277}
3278
John Allenf6ef6402017-03-17 17:13:42 -05003279static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3280{
3281 struct device *dev = &adapter->vdev->dev;
3282 unsigned long timeout = msecs_to_jiffies(30000);
John Allenf6ef6402017-03-17 17:13:42 -05003283 int rc;
3284
Nathan Fontenotf9928872017-03-30 02:48:54 -04003285 rc = init_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003286 if (rc) {
3287 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3288 return rc;
3289 }
3290
John Allenf6ef6402017-03-17 17:13:42 -05003291 init_completion(&adapter->init_done);
3292 ibmvnic_send_crq_init(adapter);
3293 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3294 dev_err(dev, "Initialization sequence timed out\n");
Nathan Fontenotf9928872017-03-30 02:48:54 -04003295 release_crq_queue(adapter);
John Allenf6ef6402017-03-17 17:13:42 -05003296 return -1;
3297 }
3298
3299 return 0;
3300}
3301
Thomas Falcon032c5e82015-12-21 11:26:06 -06003302static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3303{
3304 struct ibmvnic_adapter *adapter;
3305 struct net_device *netdev;
3306 unsigned char *mac_addr_p;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003307 int rc;
3308
3309 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3310 dev->unit_address);
3311
3312 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3313 VETH_MAC_ADDR, NULL);
3314 if (!mac_addr_p) {
3315 dev_err(&dev->dev,
3316 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3317 __FILE__, __LINE__);
3318 return 0;
3319 }
3320
3321 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3322 IBMVNIC_MAX_TX_QUEUES);
3323 if (!netdev)
3324 return -ENOMEM;
3325
3326 adapter = netdev_priv(netdev);
3327 dev_set_drvdata(&dev->dev, netdev);
3328 adapter->vdev = dev;
3329 adapter->netdev = netdev;
Thomas Falcondfad09a2016-08-18 11:37:51 -05003330 adapter->failover = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003331
3332 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3333 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3334 netdev->irq = dev->irq;
3335 netdev->netdev_ops = &ibmvnic_netdev_ops;
3336 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3337 SET_NETDEV_DEV(netdev, &dev->dev);
3338
Thomas Falcon65dc6892016-07-06 15:35:18 -05003339 INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
Thomas Falcon9888d7b2016-10-27 12:28:51 -05003340 INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
Thomas Falcon65dc6892016-07-06 15:35:18 -05003341
Thomas Falcon032c5e82015-12-21 11:26:06 -06003342 spin_lock_init(&adapter->stats_lock);
3343
Thomas Falcon032c5e82015-12-21 11:26:06 -06003344 INIT_LIST_HEAD(&adapter->errors);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003345 spin_lock_init(&adapter->error_list_lock);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003346
John Allenf6ef6402017-03-17 17:13:42 -05003347 rc = ibmvnic_init(adapter);
3348 if (rc) {
3349 free_netdev(netdev);
3350 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003351 }
3352
Thomas Falconf39f0d12017-02-14 10:22:59 -06003353 netdev->mtu = adapter->req_mtu - ETH_HLEN;
John Allenea5509f2017-03-17 17:13:43 -05003354 adapter->is_closed = false;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003355
3356 rc = register_netdev(netdev);
3357 if (rc) {
3358 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
John Allenf6ef6402017-03-17 17:13:42 -05003359 free_netdev(netdev);
3360 return rc;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003361 }
3362 dev_info(&dev->dev, "ibmvnic registered\n");
3363
3364 return 0;
Thomas Falcon032c5e82015-12-21 11:26:06 -06003365}
3366
3367static int ibmvnic_remove(struct vio_dev *dev)
3368{
3369 struct net_device *netdev = dev_get_drvdata(&dev->dev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003370 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
Thomas Falcon032c5e82015-12-21 11:26:06 -06003371
3372 unregister_netdev(netdev);
Nathan Fontenot37489052017-04-19 13:45:04 -04003373
3374 release_resources(adapter);
3375 release_sub_crqs(adapter);
3376 release_crq_queue(adapter);
3377
Thomas Falcon032c5e82015-12-21 11:26:06 -06003378 free_netdev(netdev);
3379 dev_set_drvdata(&dev->dev, NULL);
3380
3381 return 0;
3382}
3383
3384static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3385{
3386 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3387 struct ibmvnic_adapter *adapter;
3388 struct iommu_table *tbl;
3389 unsigned long ret = 0;
3390 int i;
3391
3392 tbl = get_iommu_table_base(&vdev->dev);
3393
3394 /* netdev inits at probe time along with the structures we need below*/
3395 if (!netdev)
3396 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3397
3398 adapter = netdev_priv(netdev);
3399
3400 ret += PAGE_SIZE; /* the crq message queue */
Thomas Falcon032c5e82015-12-21 11:26:06 -06003401 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3402
3403 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3404 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3405
3406 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3407 i++)
3408 ret += adapter->rx_pool[i].size *
3409 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3410
3411 return ret;
3412}
3413
3414static int ibmvnic_resume(struct device *dev)
3415{
3416 struct net_device *netdev = dev_get_drvdata(dev);
3417 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3418 int i;
3419
3420 /* kick the interrupt handlers just in case we lost an interrupt */
3421 for (i = 0; i < adapter->req_rx_queues; i++)
3422 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3423 adapter->rx_scrq[i]);
3424
3425 return 0;
3426}
3427
3428static struct vio_device_id ibmvnic_device_table[] = {
3429 {"network", "IBM,vnic"},
3430 {"", "" }
3431};
3432MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3433
3434static const struct dev_pm_ops ibmvnic_pm_ops = {
3435 .resume = ibmvnic_resume
3436};
3437
3438static struct vio_driver ibmvnic_driver = {
3439 .id_table = ibmvnic_device_table,
3440 .probe = ibmvnic_probe,
3441 .remove = ibmvnic_remove,
3442 .get_desired_dma = ibmvnic_get_desired_dma,
3443 .name = ibmvnic_driver_name,
3444 .pm = &ibmvnic_pm_ops,
3445};
3446
3447/* module functions */
3448static int __init ibmvnic_module_init(void)
3449{
3450 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3451 IBMVNIC_DRIVER_VERSION);
3452
3453 return vio_register_driver(&ibmvnic_driver);
3454}
3455
3456static void __exit ibmvnic_module_exit(void)
3457{
3458 vio_unregister_driver(&ibmvnic_driver);
3459}
3460
3461module_init(ibmvnic_module_init);
3462module_exit(ibmvnic_module_exit);