ibmvnic: Update driver queues after change in ring size support
[sfrench/cifs-2.6.git] / drivers / net / ethernet / ibm / ibmvnic.c
1 /**************************************************************************/
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/if_arp.h>
63 #include <linux/in.h>
64 #include <linux/ip.h>
65 #include <linux/ipv6.h>
66 #include <linux/irq.h>
67 #include <linux/kthread.h>
68 #include <linux/seq_file.h>
69 #include <linux/interrupt.h>
70 #include <net/net_namespace.h>
71 #include <asm/hvcall.h>
72 #include <linux/atomic.h>
73 #include <asm/vio.h>
74 #include <asm/iommu.h>
75 #include <linux/uaccess.h>
76 #include <asm/firmware.h>
77 #include <linux/workqueue.h>
78 #include <linux/if_vlan.h>
79 #include <linux/utsname.h>
80
81 #include "ibmvnic.h"
82
83 static const char ibmvnic_driver_name[] = "ibmvnic";
84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
85
86 MODULE_AUTHOR("Santiago Leon");
87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
90
91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92 static int ibmvnic_remove(struct vio_dev *);
93 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99                        union sub_crq *sub_crq);
100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102 static int enable_scrq_irq(struct ibmvnic_adapter *,
103                            struct ibmvnic_sub_crq_queue *);
104 static int disable_scrq_irq(struct ibmvnic_adapter *,
105                             struct ibmvnic_sub_crq_queue *);
106 static int pending_scrq(struct ibmvnic_adapter *,
107                         struct ibmvnic_sub_crq_queue *);
108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109                                         struct ibmvnic_sub_crq_queue *);
110 static int ibmvnic_poll(struct napi_struct *napi, int data);
111 static void send_map_query(struct ibmvnic_adapter *adapter);
112 static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static int send_request_unmap(struct ibmvnic_adapter *, u8);
114 static int send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static int ibmvnic_reset_init(struct ibmvnic_adapter *);
120 static void release_crq_queue(struct ibmvnic_adapter *);
121 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
122 static int init_crq_queue(struct ibmvnic_adapter *adapter);
123
124 struct ibmvnic_stat {
125         char name[ETH_GSTRING_LEN];
126         int offset;
127 };
128
129 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
130                              offsetof(struct ibmvnic_statistics, stat))
131 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
132
133 static const struct ibmvnic_stat ibmvnic_stats[] = {
134         {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
135         {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
136         {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
137         {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
138         {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
139         {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
140         {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
141         {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
142         {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
143         {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
144         {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
145         {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
146         {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
147         {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
148         {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
149         {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
150         {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
151         {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
152         {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
153         {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
154         {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
155         {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156 };
157
158 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
159                           unsigned long length, unsigned long *number,
160                           unsigned long *irq)
161 {
162         unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
163         long rc;
164
165         rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
166         *number = retbuf[0];
167         *irq = retbuf[1];
168
169         return rc;
170 }
171
172 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
173                                 struct ibmvnic_long_term_buff *ltb, int size)
174 {
175         struct device *dev = &adapter->vdev->dev;
176         int rc;
177
178         ltb->size = size;
179         ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
180                                        GFP_KERNEL);
181
182         if (!ltb->buff) {
183                 dev_err(dev, "Couldn't alloc long term buffer\n");
184                 return -ENOMEM;
185         }
186         ltb->map_id = adapter->map_id;
187         adapter->map_id++;
188
189         init_completion(&adapter->fw_done);
190         rc = send_request_map(adapter, ltb->addr,
191                               ltb->size, ltb->map_id);
192         if (rc) {
193                 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
194                 return rc;
195         }
196         wait_for_completion(&adapter->fw_done);
197
198         if (adapter->fw_done_rc) {
199                 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
200                         adapter->fw_done_rc);
201                 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
202                 return -1;
203         }
204         return 0;
205 }
206
207 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
208                                 struct ibmvnic_long_term_buff *ltb)
209 {
210         struct device *dev = &adapter->vdev->dev;
211
212         if (!ltb->buff)
213                 return;
214
215         if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
216             adapter->reset_reason != VNIC_RESET_MOBILITY)
217                 send_request_unmap(adapter, ltb->map_id);
218         dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
219 }
220
221 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
222                                 struct ibmvnic_long_term_buff *ltb)
223 {
224         int rc;
225
226         memset(ltb->buff, 0, ltb->size);
227
228         init_completion(&adapter->fw_done);
229         rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
230         if (rc)
231                 return rc;
232         wait_for_completion(&adapter->fw_done);
233
234         if (adapter->fw_done_rc) {
235                 dev_info(&adapter->vdev->dev,
236                          "Reset failed, attempting to free and reallocate buffer\n");
237                 free_long_term_buff(adapter, ltb);
238                 return alloc_long_term_buff(adapter, ltb, ltb->size);
239         }
240         return 0;
241 }
242
243 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
244 {
245         int i;
246
247         for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
248              i++)
249                 adapter->rx_pool[i].active = 0;
250 }
251
252 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
253                               struct ibmvnic_rx_pool *pool)
254 {
255         int count = pool->size - atomic_read(&pool->available);
256         struct device *dev = &adapter->vdev->dev;
257         int buffers_added = 0;
258         unsigned long lpar_rc;
259         union sub_crq sub_crq;
260         struct sk_buff *skb;
261         unsigned int offset;
262         dma_addr_t dma_addr;
263         unsigned char *dst;
264         u64 *handle_array;
265         int shift = 0;
266         int index;
267         int i;
268
269         if (!pool->active)
270                 return;
271
272         handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
273                                       be32_to_cpu(adapter->login_rsp_buf->
274                                       off_rxadd_subcrqs));
275
276         for (i = 0; i < count; ++i) {
277                 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
278                 if (!skb) {
279                         dev_err(dev, "Couldn't replenish rx buff\n");
280                         adapter->replenish_no_mem++;
281                         break;
282                 }
283
284                 index = pool->free_map[pool->next_free];
285
286                 if (pool->rx_buff[index].skb)
287                         dev_err(dev, "Inconsistent free_map!\n");
288
289                 /* Copy the skb to the long term mapped DMA buffer */
290                 offset = index * pool->buff_size;
291                 dst = pool->long_term_buff.buff + offset;
292                 memset(dst, 0, pool->buff_size);
293                 dma_addr = pool->long_term_buff.addr + offset;
294                 pool->rx_buff[index].data = dst;
295
296                 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
297                 pool->rx_buff[index].dma = dma_addr;
298                 pool->rx_buff[index].skb = skb;
299                 pool->rx_buff[index].pool_index = pool->index;
300                 pool->rx_buff[index].size = pool->buff_size;
301
302                 memset(&sub_crq, 0, sizeof(sub_crq));
303                 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
304                 sub_crq.rx_add.correlator =
305                     cpu_to_be64((u64)&pool->rx_buff[index]);
306                 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
307                 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
308
309                 /* The length field of the sCRQ is defined to be 24 bits so the
310                  * buffer size needs to be left shifted by a byte before it is
311                  * converted to big endian to prevent the last byte from being
312                  * truncated.
313                  */
314 #ifdef __LITTLE_ENDIAN__
315                 shift = 8;
316 #endif
317                 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
318
319                 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
320                                       &sub_crq);
321                 if (lpar_rc != H_SUCCESS)
322                         goto failure;
323
324                 buffers_added++;
325                 adapter->replenish_add_buff_success++;
326                 pool->next_free = (pool->next_free + 1) % pool->size;
327         }
328         atomic_add(buffers_added, &pool->available);
329         return;
330
331 failure:
332         if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
333                 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
334         pool->free_map[pool->next_free] = index;
335         pool->rx_buff[index].skb = NULL;
336
337         dev_kfree_skb_any(skb);
338         adapter->replenish_add_buff_failure++;
339         atomic_add(buffers_added, &pool->available);
340
341         if (lpar_rc == H_CLOSED || adapter->failover_pending) {
342                 /* Disable buffer pool replenishment and report carrier off if
343                  * queue is closed or pending failover.
344                  * Firmware guarantees that a signal will be sent to the
345                  * driver, triggering a reset.
346                  */
347                 deactivate_rx_pools(adapter);
348                 netif_carrier_off(adapter->netdev);
349         }
350 }
351
352 static void replenish_pools(struct ibmvnic_adapter *adapter)
353 {
354         int i;
355
356         adapter->replenish_task_cycles++;
357         for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
358              i++) {
359                 if (adapter->rx_pool[i].active)
360                         replenish_rx_pool(adapter, &adapter->rx_pool[i]);
361         }
362 }
363
364 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
365 {
366         kfree(adapter->tx_stats_buffers);
367         kfree(adapter->rx_stats_buffers);
368         adapter->tx_stats_buffers = NULL;
369         adapter->rx_stats_buffers = NULL;
370 }
371
372 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
373 {
374         adapter->tx_stats_buffers =
375                                 kcalloc(IBMVNIC_MAX_QUEUES,
376                                         sizeof(struct ibmvnic_tx_queue_stats),
377                                         GFP_KERNEL);
378         if (!adapter->tx_stats_buffers)
379                 return -ENOMEM;
380
381         adapter->rx_stats_buffers =
382                                 kcalloc(IBMVNIC_MAX_QUEUES,
383                                         sizeof(struct ibmvnic_rx_queue_stats),
384                                         GFP_KERNEL);
385         if (!adapter->rx_stats_buffers)
386                 return -ENOMEM;
387
388         return 0;
389 }
390
391 static void release_stats_token(struct ibmvnic_adapter *adapter)
392 {
393         struct device *dev = &adapter->vdev->dev;
394
395         if (!adapter->stats_token)
396                 return;
397
398         dma_unmap_single(dev, adapter->stats_token,
399                          sizeof(struct ibmvnic_statistics),
400                          DMA_FROM_DEVICE);
401         adapter->stats_token = 0;
402 }
403
404 static int init_stats_token(struct ibmvnic_adapter *adapter)
405 {
406         struct device *dev = &adapter->vdev->dev;
407         dma_addr_t stok;
408
409         stok = dma_map_single(dev, &adapter->stats,
410                               sizeof(struct ibmvnic_statistics),
411                               DMA_FROM_DEVICE);
412         if (dma_mapping_error(dev, stok)) {
413                 dev_err(dev, "Couldn't map stats buffer\n");
414                 return -1;
415         }
416
417         adapter->stats_token = stok;
418         netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
419         return 0;
420 }
421
422 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
423 {
424         struct ibmvnic_rx_pool *rx_pool;
425         int rx_scrqs;
426         int i, j, rc;
427         u64 *size_array;
428
429         size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
430                 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
431
432         rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
433         for (i = 0; i < rx_scrqs; i++) {
434                 rx_pool = &adapter->rx_pool[i];
435
436                 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
437
438                 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
439                         free_long_term_buff(adapter, &rx_pool->long_term_buff);
440                         rx_pool->buff_size = be64_to_cpu(size_array[i]);
441                         alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
442                                              rx_pool->size *
443                                              rx_pool->buff_size);
444                 } else {
445                         rc = reset_long_term_buff(adapter,
446                                                   &rx_pool->long_term_buff);
447                 }
448
449                 if (rc)
450                         return rc;
451
452                 for (j = 0; j < rx_pool->size; j++)
453                         rx_pool->free_map[j] = j;
454
455                 memset(rx_pool->rx_buff, 0,
456                        rx_pool->size * sizeof(struct ibmvnic_rx_buff));
457
458                 atomic_set(&rx_pool->available, 0);
459                 rx_pool->next_alloc = 0;
460                 rx_pool->next_free = 0;
461                 rx_pool->active = 1;
462         }
463
464         return 0;
465 }
466
467 static void release_rx_pools(struct ibmvnic_adapter *adapter)
468 {
469         struct ibmvnic_rx_pool *rx_pool;
470         int i, j;
471
472         if (!adapter->rx_pool)
473                 return;
474
475         for (i = 0; i < adapter->num_active_rx_pools; i++) {
476                 rx_pool = &adapter->rx_pool[i];
477
478                 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
479
480                 kfree(rx_pool->free_map);
481                 free_long_term_buff(adapter, &rx_pool->long_term_buff);
482
483                 if (!rx_pool->rx_buff)
484                         continue;
485
486                 for (j = 0; j < rx_pool->size; j++) {
487                         if (rx_pool->rx_buff[j].skb) {
488                                 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
489                                 rx_pool->rx_buff[j].skb = NULL;
490                         }
491                 }
492
493                 kfree(rx_pool->rx_buff);
494         }
495
496         kfree(adapter->rx_pool);
497         adapter->rx_pool = NULL;
498         adapter->num_active_rx_pools = 0;
499 }
500
501 static int init_rx_pools(struct net_device *netdev)
502 {
503         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
504         struct device *dev = &adapter->vdev->dev;
505         struct ibmvnic_rx_pool *rx_pool;
506         int rxadd_subcrqs;
507         u64 *size_array;
508         int i, j;
509
510         rxadd_subcrqs =
511                 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
512         size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
513                 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
514
515         adapter->rx_pool = kcalloc(rxadd_subcrqs,
516                                    sizeof(struct ibmvnic_rx_pool),
517                                    GFP_KERNEL);
518         if (!adapter->rx_pool) {
519                 dev_err(dev, "Failed to allocate rx pools\n");
520                 return -1;
521         }
522
523         adapter->num_active_rx_pools = rxadd_subcrqs;
524
525         for (i = 0; i < rxadd_subcrqs; i++) {
526                 rx_pool = &adapter->rx_pool[i];
527
528                 netdev_dbg(adapter->netdev,
529                            "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
530                            i, adapter->req_rx_add_entries_per_subcrq,
531                            be64_to_cpu(size_array[i]));
532
533                 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
534                 rx_pool->index = i;
535                 rx_pool->buff_size = be64_to_cpu(size_array[i]);
536                 rx_pool->active = 1;
537
538                 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
539                                             GFP_KERNEL);
540                 if (!rx_pool->free_map) {
541                         release_rx_pools(adapter);
542                         return -1;
543                 }
544
545                 rx_pool->rx_buff = kcalloc(rx_pool->size,
546                                            sizeof(struct ibmvnic_rx_buff),
547                                            GFP_KERNEL);
548                 if (!rx_pool->rx_buff) {
549                         dev_err(dev, "Couldn't alloc rx buffers\n");
550                         release_rx_pools(adapter);
551                         return -1;
552                 }
553
554                 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
555                                          rx_pool->size * rx_pool->buff_size)) {
556                         release_rx_pools(adapter);
557                         return -1;
558                 }
559
560                 for (j = 0; j < rx_pool->size; ++j)
561                         rx_pool->free_map[j] = j;
562
563                 atomic_set(&rx_pool->available, 0);
564                 rx_pool->next_alloc = 0;
565                 rx_pool->next_free = 0;
566         }
567
568         return 0;
569 }
570
571 static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
572                              struct ibmvnic_tx_pool *tx_pool)
573 {
574         int rc, i;
575
576         rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
577         if (rc)
578                 return rc;
579
580         memset(tx_pool->tx_buff, 0,
581                tx_pool->num_buffers *
582                sizeof(struct ibmvnic_tx_buff));
583
584         for (i = 0; i < tx_pool->num_buffers; i++)
585                 tx_pool->free_map[i] = i;
586
587         tx_pool->consumer_index = 0;
588         tx_pool->producer_index = 0;
589
590         return 0;
591 }
592
593 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
594 {
595         int tx_scrqs;
596         int i, rc;
597
598         tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
599         for (i = 0; i < tx_scrqs; i++) {
600                 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
601                 if (rc)
602                         return rc;
603                 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
604                 if (rc)
605                         return rc;
606         }
607
608         return 0;
609 }
610
611 static void release_vpd_data(struct ibmvnic_adapter *adapter)
612 {
613         if (!adapter->vpd)
614                 return;
615
616         kfree(adapter->vpd->buff);
617         kfree(adapter->vpd);
618
619         adapter->vpd = NULL;
620 }
621
622 static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
623                                 struct ibmvnic_tx_pool *tx_pool)
624 {
625         kfree(tx_pool->tx_buff);
626         kfree(tx_pool->free_map);
627         free_long_term_buff(adapter, &tx_pool->long_term_buff);
628 }
629
630 static void release_tx_pools(struct ibmvnic_adapter *adapter)
631 {
632         int i;
633
634         if (!adapter->tx_pool)
635                 return;
636
637         for (i = 0; i < adapter->num_active_tx_pools; i++) {
638                 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
639                 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
640         }
641
642         kfree(adapter->tx_pool);
643         adapter->tx_pool = NULL;
644         kfree(adapter->tso_pool);
645         adapter->tso_pool = NULL;
646         adapter->num_active_tx_pools = 0;
647 }
648
649 static int init_one_tx_pool(struct net_device *netdev,
650                             struct ibmvnic_tx_pool *tx_pool,
651                             int num_entries, int buf_size)
652 {
653         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
654         int i;
655
656         tx_pool->tx_buff = kcalloc(num_entries,
657                                    sizeof(struct ibmvnic_tx_buff),
658                                    GFP_KERNEL);
659         if (!tx_pool->tx_buff)
660                 return -1;
661
662         if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
663                                  num_entries * buf_size))
664                 return -1;
665
666         tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
667         if (!tx_pool->free_map)
668                 return -1;
669
670         for (i = 0; i < num_entries; i++)
671                 tx_pool->free_map[i] = i;
672
673         tx_pool->consumer_index = 0;
674         tx_pool->producer_index = 0;
675         tx_pool->num_buffers = num_entries;
676         tx_pool->buf_size = buf_size;
677
678         return 0;
679 }
680
681 static int init_tx_pools(struct net_device *netdev)
682 {
683         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
684         int tx_subcrqs;
685         int i, rc;
686
687         tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
688         adapter->tx_pool = kcalloc(tx_subcrqs,
689                                    sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
690         if (!adapter->tx_pool)
691                 return -1;
692
693         adapter->tso_pool = kcalloc(tx_subcrqs,
694                                     sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
695         if (!adapter->tso_pool)
696                 return -1;
697
698         adapter->num_active_tx_pools = tx_subcrqs;
699
700         for (i = 0; i < tx_subcrqs; i++) {
701                 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
702                                       adapter->req_tx_entries_per_subcrq,
703                                       adapter->req_mtu + VLAN_HLEN);
704                 if (rc) {
705                         release_tx_pools(adapter);
706                         return rc;
707                 }
708
709                 init_one_tx_pool(netdev, &adapter->tso_pool[i],
710                                  IBMVNIC_TSO_BUFS,
711                                  IBMVNIC_TSO_BUF_SZ);
712                 if (rc) {
713                         release_tx_pools(adapter);
714                         return rc;
715                 }
716         }
717
718         return 0;
719 }
720
721 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
722 {
723         int i;
724
725         if (adapter->napi_enabled)
726                 return;
727
728         for (i = 0; i < adapter->req_rx_queues; i++)
729                 napi_enable(&adapter->napi[i]);
730
731         adapter->napi_enabled = true;
732 }
733
734 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
735 {
736         int i;
737
738         if (!adapter->napi_enabled)
739                 return;
740
741         for (i = 0; i < adapter->req_rx_queues; i++) {
742                 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
743                 napi_disable(&adapter->napi[i]);
744         }
745
746         adapter->napi_enabled = false;
747 }
748
749 static int init_napi(struct ibmvnic_adapter *adapter)
750 {
751         int i;
752
753         adapter->napi = kcalloc(adapter->req_rx_queues,
754                                 sizeof(struct napi_struct), GFP_KERNEL);
755         if (!adapter->napi)
756                 return -ENOMEM;
757
758         for (i = 0; i < adapter->req_rx_queues; i++) {
759                 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
760                 netif_napi_add(adapter->netdev, &adapter->napi[i],
761                                ibmvnic_poll, NAPI_POLL_WEIGHT);
762         }
763
764         adapter->num_active_rx_napi = adapter->req_rx_queues;
765         return 0;
766 }
767
768 static void release_napi(struct ibmvnic_adapter *adapter)
769 {
770         int i;
771
772         if (!adapter->napi)
773                 return;
774
775         for (i = 0; i < adapter->num_active_rx_napi; i++) {
776                 if (&adapter->napi[i]) {
777                         netdev_dbg(adapter->netdev,
778                                    "Releasing napi[%d]\n", i);
779                         netif_napi_del(&adapter->napi[i]);
780                 }
781         }
782
783         kfree(adapter->napi);
784         adapter->napi = NULL;
785         adapter->num_active_rx_napi = 0;
786         adapter->napi_enabled = false;
787 }
788
789 static int ibmvnic_login(struct net_device *netdev)
790 {
791         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
792         unsigned long timeout = msecs_to_jiffies(30000);
793         int retry_count = 0;
794         bool retry;
795         int rc;
796
797         do {
798                 retry = false;
799                 if (retry_count > IBMVNIC_MAX_QUEUES) {
800                         netdev_warn(netdev, "Login attempts exceeded\n");
801                         return -1;
802                 }
803
804                 adapter->init_done_rc = 0;
805                 reinit_completion(&adapter->init_done);
806                 rc = send_login(adapter);
807                 if (rc) {
808                         netdev_warn(netdev, "Unable to login\n");
809                         return rc;
810                 }
811
812                 if (!wait_for_completion_timeout(&adapter->init_done,
813                                                  timeout)) {
814                         netdev_warn(netdev, "Login timed out\n");
815                         return -1;
816                 }
817
818                 if (adapter->init_done_rc == PARTIALSUCCESS) {
819                         retry_count++;
820                         release_sub_crqs(adapter, 1);
821
822                         retry = true;
823                         netdev_dbg(netdev,
824                                    "Received partial success, retrying...\n");
825                         adapter->init_done_rc = 0;
826                         reinit_completion(&adapter->init_done);
827                         send_cap_queries(adapter);
828                         if (!wait_for_completion_timeout(&adapter->init_done,
829                                                          timeout)) {
830                                 netdev_warn(netdev,
831                                             "Capabilities query timed out\n");
832                                 return -1;
833                         }
834
835                         rc = init_sub_crqs(adapter);
836                         if (rc) {
837                                 netdev_warn(netdev,
838                                             "SCRQ initialization failed\n");
839                                 return -1;
840                         }
841
842                         rc = init_sub_crq_irqs(adapter);
843                         if (rc) {
844                                 netdev_warn(netdev,
845                                             "SCRQ irq initialization failed\n");
846                                 return -1;
847                         }
848                 } else if (adapter->init_done_rc) {
849                         netdev_warn(netdev, "Adapter login failed\n");
850                         return -1;
851                 }
852         } while (retry);
853
854         /* handle pending MAC address changes after successful login */
855         if (adapter->mac_change_pending) {
856                 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
857                 adapter->mac_change_pending = false;
858         }
859
860         return 0;
861 }
862
863 static void release_login_buffer(struct ibmvnic_adapter *adapter)
864 {
865         kfree(adapter->login_buf);
866         adapter->login_buf = NULL;
867 }
868
869 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
870 {
871         kfree(adapter->login_rsp_buf);
872         adapter->login_rsp_buf = NULL;
873 }
874
875 static void release_resources(struct ibmvnic_adapter *adapter)
876 {
877         release_vpd_data(adapter);
878
879         release_tx_pools(adapter);
880         release_rx_pools(adapter);
881
882         release_napi(adapter);
883         release_login_rsp_buffer(adapter);
884 }
885
886 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
887 {
888         struct net_device *netdev = adapter->netdev;
889         unsigned long timeout = msecs_to_jiffies(30000);
890         union ibmvnic_crq crq;
891         bool resend;
892         int rc;
893
894         netdev_dbg(netdev, "setting link state %d\n", link_state);
895
896         memset(&crq, 0, sizeof(crq));
897         crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
898         crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
899         crq.logical_link_state.link_state = link_state;
900
901         do {
902                 resend = false;
903
904                 reinit_completion(&adapter->init_done);
905                 rc = ibmvnic_send_crq(adapter, &crq);
906                 if (rc) {
907                         netdev_err(netdev, "Failed to set link state\n");
908                         return rc;
909                 }
910
911                 if (!wait_for_completion_timeout(&adapter->init_done,
912                                                  timeout)) {
913                         netdev_err(netdev, "timeout setting link state\n");
914                         return -1;
915                 }
916
917                 if (adapter->init_done_rc == 1) {
918                         /* Partuial success, delay and re-send */
919                         mdelay(1000);
920                         resend = true;
921                 } else if (adapter->init_done_rc) {
922                         netdev_warn(netdev, "Unable to set link state, rc=%d\n",
923                                     adapter->init_done_rc);
924                         return adapter->init_done_rc;
925                 }
926         } while (resend);
927
928         return 0;
929 }
930
931 static int set_real_num_queues(struct net_device *netdev)
932 {
933         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
934         int rc;
935
936         netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
937                    adapter->req_tx_queues, adapter->req_rx_queues);
938
939         rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
940         if (rc) {
941                 netdev_err(netdev, "failed to set the number of tx queues\n");
942                 return rc;
943         }
944
945         rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
946         if (rc)
947                 netdev_err(netdev, "failed to set the number of rx queues\n");
948
949         return rc;
950 }
951
952 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
953 {
954         struct device *dev = &adapter->vdev->dev;
955         union ibmvnic_crq crq;
956         int len = 0;
957         int rc;
958
959         if (adapter->vpd->buff)
960                 len = adapter->vpd->len;
961
962         init_completion(&adapter->fw_done);
963         crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
964         crq.get_vpd_size.cmd = GET_VPD_SIZE;
965         rc = ibmvnic_send_crq(adapter, &crq);
966         if (rc)
967                 return rc;
968         wait_for_completion(&adapter->fw_done);
969
970         if (!adapter->vpd->len)
971                 return -ENODATA;
972
973         if (!adapter->vpd->buff)
974                 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
975         else if (adapter->vpd->len != len)
976                 adapter->vpd->buff =
977                         krealloc(adapter->vpd->buff,
978                                  adapter->vpd->len, GFP_KERNEL);
979
980         if (!adapter->vpd->buff) {
981                 dev_err(dev, "Could allocate VPD buffer\n");
982                 return -ENOMEM;
983         }
984
985         adapter->vpd->dma_addr =
986                 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
987                                DMA_FROM_DEVICE);
988         if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
989                 dev_err(dev, "Could not map VPD buffer\n");
990                 kfree(adapter->vpd->buff);
991                 adapter->vpd->buff = NULL;
992                 return -ENOMEM;
993         }
994
995         reinit_completion(&adapter->fw_done);
996         crq.get_vpd.first = IBMVNIC_CRQ_CMD;
997         crq.get_vpd.cmd = GET_VPD;
998         crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
999         crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1000         rc = ibmvnic_send_crq(adapter, &crq);
1001         if (rc) {
1002                 kfree(adapter->vpd->buff);
1003                 adapter->vpd->buff = NULL;
1004                 return rc;
1005         }
1006         wait_for_completion(&adapter->fw_done);
1007
1008         return 0;
1009 }
1010
1011 static int init_resources(struct ibmvnic_adapter *adapter)
1012 {
1013         struct net_device *netdev = adapter->netdev;
1014         int rc;
1015
1016         rc = set_real_num_queues(netdev);
1017         if (rc)
1018                 return rc;
1019
1020         adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1021         if (!adapter->vpd)
1022                 return -ENOMEM;
1023
1024         /* Vital Product Data (VPD) */
1025         rc = ibmvnic_get_vpd(adapter);
1026         if (rc) {
1027                 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1028                 return rc;
1029         }
1030
1031         adapter->map_id = 1;
1032
1033         rc = init_napi(adapter);
1034         if (rc)
1035                 return rc;
1036
1037         send_map_query(adapter);
1038
1039         rc = init_rx_pools(netdev);
1040         if (rc)
1041                 return rc;
1042
1043         rc = init_tx_pools(netdev);
1044         return rc;
1045 }
1046
1047 static int __ibmvnic_open(struct net_device *netdev)
1048 {
1049         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1050         enum vnic_state prev_state = adapter->state;
1051         int i, rc;
1052
1053         adapter->state = VNIC_OPENING;
1054         replenish_pools(adapter);
1055         ibmvnic_napi_enable(adapter);
1056
1057         /* We're ready to receive frames, enable the sub-crq interrupts and
1058          * set the logical link state to up
1059          */
1060         for (i = 0; i < adapter->req_rx_queues; i++) {
1061                 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1062                 if (prev_state == VNIC_CLOSED)
1063                         enable_irq(adapter->rx_scrq[i]->irq);
1064                 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1065         }
1066
1067         for (i = 0; i < adapter->req_tx_queues; i++) {
1068                 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1069                 if (prev_state == VNIC_CLOSED)
1070                         enable_irq(adapter->tx_scrq[i]->irq);
1071                 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1072         }
1073
1074         rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1075         if (rc) {
1076                 for (i = 0; i < adapter->req_rx_queues; i++)
1077                         napi_disable(&adapter->napi[i]);
1078                 release_resources(adapter);
1079                 return rc;
1080         }
1081
1082         netif_tx_start_all_queues(netdev);
1083
1084         if (prev_state == VNIC_CLOSED) {
1085                 for (i = 0; i < adapter->req_rx_queues; i++)
1086                         napi_schedule(&adapter->napi[i]);
1087         }
1088
1089         adapter->state = VNIC_OPEN;
1090         return rc;
1091 }
1092
1093 static int ibmvnic_open(struct net_device *netdev)
1094 {
1095         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1096         int rc;
1097
1098         /* If device failover is pending, just set device state and return.
1099          * Device operation will be handled by reset routine.
1100          */
1101         if (adapter->failover_pending) {
1102                 adapter->state = VNIC_OPEN;
1103                 return 0;
1104         }
1105
1106         if (adapter->state != VNIC_CLOSED) {
1107                 rc = ibmvnic_login(netdev);
1108                 if (rc)
1109                         return rc;
1110
1111                 rc = init_resources(adapter);
1112                 if (rc) {
1113                         netdev_err(netdev, "failed to initialize resources\n");
1114                         release_resources(adapter);
1115                         return rc;
1116                 }
1117         }
1118
1119         rc = __ibmvnic_open(netdev);
1120         netif_carrier_on(netdev);
1121
1122         return rc;
1123 }
1124
1125 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1126 {
1127         struct ibmvnic_rx_pool *rx_pool;
1128         struct ibmvnic_rx_buff *rx_buff;
1129         u64 rx_entries;
1130         int rx_scrqs;
1131         int i, j;
1132
1133         if (!adapter->rx_pool)
1134                 return;
1135
1136         rx_scrqs = adapter->num_active_rx_pools;
1137         rx_entries = adapter->req_rx_add_entries_per_subcrq;
1138
1139         /* Free any remaining skbs in the rx buffer pools */
1140         for (i = 0; i < rx_scrqs; i++) {
1141                 rx_pool = &adapter->rx_pool[i];
1142                 if (!rx_pool || !rx_pool->rx_buff)
1143                         continue;
1144
1145                 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1146                 for (j = 0; j < rx_entries; j++) {
1147                         rx_buff = &rx_pool->rx_buff[j];
1148                         if (rx_buff && rx_buff->skb) {
1149                                 dev_kfree_skb_any(rx_buff->skb);
1150                                 rx_buff->skb = NULL;
1151                         }
1152                 }
1153         }
1154 }
1155
1156 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1157                               struct ibmvnic_tx_pool *tx_pool)
1158 {
1159         struct ibmvnic_tx_buff *tx_buff;
1160         u64 tx_entries;
1161         int i;
1162
1163         if (!tx_pool || !tx_pool->tx_buff)
1164                 return;
1165
1166         tx_entries = tx_pool->num_buffers;
1167
1168         for (i = 0; i < tx_entries; i++) {
1169                 tx_buff = &tx_pool->tx_buff[i];
1170                 if (tx_buff && tx_buff->skb) {
1171                         dev_kfree_skb_any(tx_buff->skb);
1172                         tx_buff->skb = NULL;
1173                 }
1174         }
1175 }
1176
1177 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1178 {
1179         int tx_scrqs;
1180         int i;
1181
1182         if (!adapter->tx_pool || !adapter->tso_pool)
1183                 return;
1184
1185         tx_scrqs = adapter->num_active_tx_pools;
1186
1187         /* Free any remaining skbs in the tx buffer pools */
1188         for (i = 0; i < tx_scrqs; i++) {
1189                 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1190                 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1191                 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1192         }
1193 }
1194
1195 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1196 {
1197         struct net_device *netdev = adapter->netdev;
1198         int i;
1199
1200         if (adapter->tx_scrq) {
1201                 for (i = 0; i < adapter->req_tx_queues; i++)
1202                         if (adapter->tx_scrq[i]->irq) {
1203                                 netdev_dbg(netdev,
1204                                            "Disabling tx_scrq[%d] irq\n", i);
1205                                 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1206                                 disable_irq(adapter->tx_scrq[i]->irq);
1207                         }
1208         }
1209
1210         if (adapter->rx_scrq) {
1211                 for (i = 0; i < adapter->req_rx_queues; i++) {
1212                         if (adapter->rx_scrq[i]->irq) {
1213                                 netdev_dbg(netdev,
1214                                            "Disabling rx_scrq[%d] irq\n", i);
1215                                 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1216                                 disable_irq(adapter->rx_scrq[i]->irq);
1217                         }
1218                 }
1219         }
1220 }
1221
1222 static void ibmvnic_cleanup(struct net_device *netdev)
1223 {
1224         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1225
1226         /* ensure that transmissions are stopped if called by do_reset */
1227         if (adapter->resetting)
1228                 netif_tx_disable(netdev);
1229         else
1230                 netif_tx_stop_all_queues(netdev);
1231
1232         ibmvnic_napi_disable(adapter);
1233         ibmvnic_disable_irqs(adapter);
1234
1235         clean_rx_pools(adapter);
1236         clean_tx_pools(adapter);
1237 }
1238
1239 static int __ibmvnic_close(struct net_device *netdev)
1240 {
1241         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1242         int rc = 0;
1243
1244         adapter->state = VNIC_CLOSING;
1245         rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1246         if (rc)
1247                 return rc;
1248         adapter->state = VNIC_CLOSED;
1249         return 0;
1250 }
1251
1252 static int ibmvnic_close(struct net_device *netdev)
1253 {
1254         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1255         int rc;
1256
1257         /* If device failover is pending, just set device state and return.
1258          * Device operation will be handled by reset routine.
1259          */
1260         if (adapter->failover_pending) {
1261                 adapter->state = VNIC_CLOSED;
1262                 return 0;
1263         }
1264
1265         rc = __ibmvnic_close(netdev);
1266         ibmvnic_cleanup(netdev);
1267
1268         return rc;
1269 }
1270
1271 /**
1272  * build_hdr_data - creates L2/L3/L4 header data buffer
1273  * @hdr_field - bitfield determining needed headers
1274  * @skb - socket buffer
1275  * @hdr_len - array of header lengths
1276  * @tot_len - total length of data
1277  *
1278  * Reads hdr_field to determine which headers are needed by firmware.
1279  * Builds a buffer containing these headers.  Saves individual header
1280  * lengths and total buffer length to be used to build descriptors.
1281  */
1282 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1283                           int *hdr_len, u8 *hdr_data)
1284 {
1285         int len = 0;
1286         u8 *hdr;
1287
1288         if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1289                 hdr_len[0] = sizeof(struct vlan_ethhdr);
1290         else
1291                 hdr_len[0] = sizeof(struct ethhdr);
1292
1293         if (skb->protocol == htons(ETH_P_IP)) {
1294                 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1295                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1296                         hdr_len[2] = tcp_hdrlen(skb);
1297                 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1298                         hdr_len[2] = sizeof(struct udphdr);
1299         } else if (skb->protocol == htons(ETH_P_IPV6)) {
1300                 hdr_len[1] = sizeof(struct ipv6hdr);
1301                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1302                         hdr_len[2] = tcp_hdrlen(skb);
1303                 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1304                         hdr_len[2] = sizeof(struct udphdr);
1305         } else if (skb->protocol == htons(ETH_P_ARP)) {
1306                 hdr_len[1] = arp_hdr_len(skb->dev);
1307                 hdr_len[2] = 0;
1308         }
1309
1310         memset(hdr_data, 0, 120);
1311         if ((hdr_field >> 6) & 1) {
1312                 hdr = skb_mac_header(skb);
1313                 memcpy(hdr_data, hdr, hdr_len[0]);
1314                 len += hdr_len[0];
1315         }
1316
1317         if ((hdr_field >> 5) & 1) {
1318                 hdr = skb_network_header(skb);
1319                 memcpy(hdr_data + len, hdr, hdr_len[1]);
1320                 len += hdr_len[1];
1321         }
1322
1323         if ((hdr_field >> 4) & 1) {
1324                 hdr = skb_transport_header(skb);
1325                 memcpy(hdr_data + len, hdr, hdr_len[2]);
1326                 len += hdr_len[2];
1327         }
1328         return len;
1329 }
1330
1331 /**
1332  * create_hdr_descs - create header and header extension descriptors
1333  * @hdr_field - bitfield determining needed headers
1334  * @data - buffer containing header data
1335  * @len - length of data buffer
1336  * @hdr_len - array of individual header lengths
1337  * @scrq_arr - descriptor array
1338  *
1339  * Creates header and, if needed, header extension descriptors and
1340  * places them in a descriptor array, scrq_arr
1341  */
1342
1343 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1344                             union sub_crq *scrq_arr)
1345 {
1346         union sub_crq hdr_desc;
1347         int tmp_len = len;
1348         int num_descs = 0;
1349         u8 *data, *cur;
1350         int tmp;
1351
1352         while (tmp_len > 0) {
1353                 cur = hdr_data + len - tmp_len;
1354
1355                 memset(&hdr_desc, 0, sizeof(hdr_desc));
1356                 if (cur != hdr_data) {
1357                         data = hdr_desc.hdr_ext.data;
1358                         tmp = tmp_len > 29 ? 29 : tmp_len;
1359                         hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1360                         hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1361                         hdr_desc.hdr_ext.len = tmp;
1362                 } else {
1363                         data = hdr_desc.hdr.data;
1364                         tmp = tmp_len > 24 ? 24 : tmp_len;
1365                         hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1366                         hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1367                         hdr_desc.hdr.len = tmp;
1368                         hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1369                         hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1370                         hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1371                         hdr_desc.hdr.flag = hdr_field << 1;
1372                 }
1373                 memcpy(data, cur, tmp);
1374                 tmp_len -= tmp;
1375                 *scrq_arr = hdr_desc;
1376                 scrq_arr++;
1377                 num_descs++;
1378         }
1379
1380         return num_descs;
1381 }
1382
1383 /**
1384  * build_hdr_descs_arr - build a header descriptor array
1385  * @skb - socket buffer
1386  * @num_entries - number of descriptors to be sent
1387  * @subcrq - first TX descriptor
1388  * @hdr_field - bit field determining which headers will be sent
1389  *
1390  * This function will build a TX descriptor array with applicable
1391  * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1392  */
1393
1394 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1395                                 int *num_entries, u8 hdr_field)
1396 {
1397         int hdr_len[3] = {0, 0, 0};
1398         int tot_len;
1399         u8 *hdr_data = txbuff->hdr_data;
1400
1401         tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1402                                  txbuff->hdr_data);
1403         *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1404                          txbuff->indir_arr + 1);
1405 }
1406
1407 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1408                                     struct net_device *netdev)
1409 {
1410         /* For some backing devices, mishandling of small packets
1411          * can result in a loss of connection or TX stall. Device
1412          * architects recommend that no packet should be smaller
1413          * than the minimum MTU value provided to the driver, so
1414          * pad any packets to that length
1415          */
1416         if (skb->len < netdev->min_mtu)
1417                 return skb_put_padto(skb, netdev->min_mtu);
1418
1419         return 0;
1420 }
1421
1422 static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1423 {
1424         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1425         int queue_num = skb_get_queue_mapping(skb);
1426         u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1427         struct device *dev = &adapter->vdev->dev;
1428         struct ibmvnic_tx_buff *tx_buff = NULL;
1429         struct ibmvnic_sub_crq_queue *tx_scrq;
1430         struct ibmvnic_tx_pool *tx_pool;
1431         unsigned int tx_send_failed = 0;
1432         unsigned int tx_map_failed = 0;
1433         unsigned int tx_dropped = 0;
1434         unsigned int tx_packets = 0;
1435         unsigned int tx_bytes = 0;
1436         dma_addr_t data_dma_addr;
1437         struct netdev_queue *txq;
1438         unsigned long lpar_rc;
1439         union sub_crq tx_crq;
1440         unsigned int offset;
1441         int num_entries = 1;
1442         unsigned char *dst;
1443         u64 *handle_array;
1444         int index = 0;
1445         u8 proto = 0;
1446         netdev_tx_t ret = NETDEV_TX_OK;
1447
1448         if (adapter->resetting) {
1449                 if (!netif_subqueue_stopped(netdev, skb))
1450                         netif_stop_subqueue(netdev, queue_num);
1451                 dev_kfree_skb_any(skb);
1452
1453                 tx_send_failed++;
1454                 tx_dropped++;
1455                 ret = NETDEV_TX_OK;
1456                 goto out;
1457         }
1458
1459         if (ibmvnic_xmit_workarounds(skb, netdev)) {
1460                 tx_dropped++;
1461                 tx_send_failed++;
1462                 ret = NETDEV_TX_OK;
1463                 goto out;
1464         }
1465         if (skb_is_gso(skb))
1466                 tx_pool = &adapter->tso_pool[queue_num];
1467         else
1468                 tx_pool = &adapter->tx_pool[queue_num];
1469
1470         tx_scrq = adapter->tx_scrq[queue_num];
1471         txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1472         handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1473                 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1474
1475         index = tx_pool->free_map[tx_pool->consumer_index];
1476
1477         if (index == IBMVNIC_INVALID_MAP) {
1478                 dev_kfree_skb_any(skb);
1479                 tx_send_failed++;
1480                 tx_dropped++;
1481                 ret = NETDEV_TX_OK;
1482                 goto out;
1483         }
1484
1485         tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1486
1487         offset = index * tx_pool->buf_size;
1488         dst = tx_pool->long_term_buff.buff + offset;
1489         memset(dst, 0, tx_pool->buf_size);
1490         data_dma_addr = tx_pool->long_term_buff.addr + offset;
1491
1492         if (skb_shinfo(skb)->nr_frags) {
1493                 int cur, i;
1494
1495                 /* Copy the head */
1496                 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1497                 cur = skb_headlen(skb);
1498
1499                 /* Copy the frags */
1500                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1501                         const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1502
1503                         memcpy(dst + cur,
1504                                page_address(skb_frag_page(frag)) +
1505                                frag->page_offset, skb_frag_size(frag));
1506                         cur += skb_frag_size(frag);
1507                 }
1508         } else {
1509                 skb_copy_from_linear_data(skb, dst, skb->len);
1510         }
1511
1512         tx_pool->consumer_index =
1513             (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1514
1515         tx_buff = &tx_pool->tx_buff[index];
1516         tx_buff->skb = skb;
1517         tx_buff->data_dma[0] = data_dma_addr;
1518         tx_buff->data_len[0] = skb->len;
1519         tx_buff->index = index;
1520         tx_buff->pool_index = queue_num;
1521         tx_buff->last_frag = true;
1522
1523         memset(&tx_crq, 0, sizeof(tx_crq));
1524         tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1525         tx_crq.v1.type = IBMVNIC_TX_DESC;
1526         tx_crq.v1.n_crq_elem = 1;
1527         tx_crq.v1.n_sge = 1;
1528         tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1529
1530         if (skb_is_gso(skb))
1531                 tx_crq.v1.correlator =
1532                         cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1533         else
1534                 tx_crq.v1.correlator = cpu_to_be32(index);
1535         tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1536         tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1537         tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1538
1539         if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
1540                 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1541                 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1542         }
1543
1544         if (skb->protocol == htons(ETH_P_IP)) {
1545                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1546                 proto = ip_hdr(skb)->protocol;
1547         } else if (skb->protocol == htons(ETH_P_IPV6)) {
1548                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1549                 proto = ipv6_hdr(skb)->nexthdr;
1550         }
1551
1552         if (proto == IPPROTO_TCP)
1553                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1554         else if (proto == IPPROTO_UDP)
1555                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1556
1557         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1558                 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1559                 hdrs += 2;
1560         }
1561         if (skb_is_gso(skb)) {
1562                 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1563                 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1564                 hdrs += 2;
1565         }
1566         /* determine if l2/3/4 headers are sent to firmware */
1567         if ((*hdrs >> 7) & 1) {
1568                 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1569                 tx_crq.v1.n_crq_elem = num_entries;
1570                 tx_buff->num_entries = num_entries;
1571                 tx_buff->indir_arr[0] = tx_crq;
1572                 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1573                                                     sizeof(tx_buff->indir_arr),
1574                                                     DMA_TO_DEVICE);
1575                 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1576                         dev_kfree_skb_any(skb);
1577                         tx_buff->skb = NULL;
1578                         if (!firmware_has_feature(FW_FEATURE_CMO))
1579                                 dev_err(dev, "tx: unable to map descriptor array\n");
1580                         tx_map_failed++;
1581                         tx_dropped++;
1582                         ret = NETDEV_TX_OK;
1583                         goto tx_err_out;
1584                 }
1585                 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1586                                                (u64)tx_buff->indir_dma,
1587                                                (u64)num_entries);
1588         } else {
1589                 tx_buff->num_entries = num_entries;
1590                 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1591                                       &tx_crq);
1592         }
1593         if (lpar_rc != H_SUCCESS) {
1594                 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1595                         dev_err_ratelimited(dev, "tx: send failed\n");
1596                 dev_kfree_skb_any(skb);
1597                 tx_buff->skb = NULL;
1598
1599                 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1600                         /* Disable TX and report carrier off if queue is closed
1601                          * or pending failover.
1602                          * Firmware guarantees that a signal will be sent to the
1603                          * driver, triggering a reset or some other action.
1604                          */
1605                         netif_tx_stop_all_queues(netdev);
1606                         netif_carrier_off(netdev);
1607                 }
1608
1609                 tx_send_failed++;
1610                 tx_dropped++;
1611                 ret = NETDEV_TX_OK;
1612                 goto tx_err_out;
1613         }
1614
1615         if (atomic_add_return(num_entries, &tx_scrq->used)
1616                                         >= adapter->req_tx_entries_per_subcrq) {
1617                 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1618                 netif_stop_subqueue(netdev, queue_num);
1619         }
1620
1621         tx_packets++;
1622         tx_bytes += skb->len;
1623         txq->trans_start = jiffies;
1624         ret = NETDEV_TX_OK;
1625         goto out;
1626
1627 tx_err_out:
1628         /* roll back consumer index and map array*/
1629         if (tx_pool->consumer_index == 0)
1630                 tx_pool->consumer_index =
1631                         tx_pool->num_buffers - 1;
1632         else
1633                 tx_pool->consumer_index--;
1634         tx_pool->free_map[tx_pool->consumer_index] = index;
1635 out:
1636         netdev->stats.tx_dropped += tx_dropped;
1637         netdev->stats.tx_bytes += tx_bytes;
1638         netdev->stats.tx_packets += tx_packets;
1639         adapter->tx_send_failed += tx_send_failed;
1640         adapter->tx_map_failed += tx_map_failed;
1641         adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1642         adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1643         adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1644
1645         return ret;
1646 }
1647
1648 static void ibmvnic_set_multi(struct net_device *netdev)
1649 {
1650         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1651         struct netdev_hw_addr *ha;
1652         union ibmvnic_crq crq;
1653
1654         memset(&crq, 0, sizeof(crq));
1655         crq.request_capability.first = IBMVNIC_CRQ_CMD;
1656         crq.request_capability.cmd = REQUEST_CAPABILITY;
1657
1658         if (netdev->flags & IFF_PROMISC) {
1659                 if (!adapter->promisc_supported)
1660                         return;
1661         } else {
1662                 if (netdev->flags & IFF_ALLMULTI) {
1663                         /* Accept all multicast */
1664                         memset(&crq, 0, sizeof(crq));
1665                         crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1666                         crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1667                         crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1668                         ibmvnic_send_crq(adapter, &crq);
1669                 } else if (netdev_mc_empty(netdev)) {
1670                         /* Reject all multicast */
1671                         memset(&crq, 0, sizeof(crq));
1672                         crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1673                         crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1674                         crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1675                         ibmvnic_send_crq(adapter, &crq);
1676                 } else {
1677                         /* Accept one or more multicast(s) */
1678                         netdev_for_each_mc_addr(ha, netdev) {
1679                                 memset(&crq, 0, sizeof(crq));
1680                                 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1681                                 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1682                                 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1683                                 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1684                                                 ha->addr);
1685                                 ibmvnic_send_crq(adapter, &crq);
1686                         }
1687                 }
1688         }
1689 }
1690
1691 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1692 {
1693         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1694         struct sockaddr *addr = p;
1695         union ibmvnic_crq crq;
1696         int rc;
1697
1698         if (!is_valid_ether_addr(addr->sa_data))
1699                 return -EADDRNOTAVAIL;
1700
1701         memset(&crq, 0, sizeof(crq));
1702         crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1703         crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1704         ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1705
1706         init_completion(&adapter->fw_done);
1707         rc = ibmvnic_send_crq(adapter, &crq);
1708         if (rc)
1709                 return rc;
1710         wait_for_completion(&adapter->fw_done);
1711         /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1712         return adapter->fw_done_rc ? -EIO : 0;
1713 }
1714
1715 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1716 {
1717         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1718         struct sockaddr *addr = p;
1719         int rc;
1720
1721         if (adapter->state == VNIC_PROBED) {
1722                 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1723                 adapter->mac_change_pending = true;
1724                 return 0;
1725         }
1726
1727         rc = __ibmvnic_set_mac(netdev, addr);
1728
1729         return rc;
1730 }
1731
1732 /**
1733  * do_reset returns zero if we are able to keep processing reset events, or
1734  * non-zero if we hit a fatal error and must halt.
1735  */
1736 static int do_reset(struct ibmvnic_adapter *adapter,
1737                     struct ibmvnic_rwi *rwi, u32 reset_state)
1738 {
1739         u64 old_num_rx_queues, old_num_tx_queues;
1740         u64 old_num_rx_slots, old_num_tx_slots;
1741         struct net_device *netdev = adapter->netdev;
1742         int i, rc;
1743
1744         netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1745                    rwi->reset_reason);
1746
1747         netif_carrier_off(netdev);
1748         adapter->reset_reason = rwi->reset_reason;
1749
1750         old_num_rx_queues = adapter->req_rx_queues;
1751         old_num_tx_queues = adapter->req_tx_queues;
1752         old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1753         old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
1754
1755         ibmvnic_cleanup(netdev);
1756
1757         if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1758             adapter->reset_reason != VNIC_RESET_FAILOVER) {
1759                 rc = __ibmvnic_close(netdev);
1760                 if (rc)
1761                         return rc;
1762         }
1763
1764         if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1765             adapter->wait_for_reset) {
1766                 release_resources(adapter);
1767                 release_sub_crqs(adapter, 1);
1768                 release_crq_queue(adapter);
1769         }
1770
1771         if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1772                 /* remove the closed state so when we call open it appears
1773                  * we are coming from the probed state.
1774                  */
1775                 adapter->state = VNIC_PROBED;
1776
1777                 if (adapter->wait_for_reset) {
1778                         rc = init_crq_queue(adapter);
1779                 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1780                         rc = ibmvnic_reenable_crq_queue(adapter);
1781                         release_sub_crqs(adapter, 1);
1782                 } else {
1783                         rc = ibmvnic_reset_crq(adapter);
1784                         if (!rc)
1785                                 rc = vio_enable_interrupts(adapter->vdev);
1786                 }
1787
1788                 if (rc) {
1789                         netdev_err(adapter->netdev,
1790                                    "Couldn't initialize crq. rc=%d\n", rc);
1791                         return rc;
1792                 }
1793
1794                 rc = ibmvnic_reset_init(adapter);
1795                 if (rc)
1796                         return IBMVNIC_INIT_FAILED;
1797
1798                 /* If the adapter was in PROBE state prior to the reset,
1799                  * exit here.
1800                  */
1801                 if (reset_state == VNIC_PROBED)
1802                         return 0;
1803
1804                 rc = ibmvnic_login(netdev);
1805                 if (rc) {
1806                         adapter->state = reset_state;
1807                         return rc;
1808                 }
1809
1810                 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1811                     adapter->wait_for_reset) {
1812                         rc = init_resources(adapter);
1813                         if (rc)
1814                                 return rc;
1815                 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1816                            adapter->req_tx_queues != old_num_tx_queues ||
1817                            adapter->req_rx_add_entries_per_subcrq !=
1818                                                         old_num_rx_slots ||
1819                            adapter->req_tx_entries_per_subcrq !=
1820                                                         old_num_tx_slots) {
1821                         release_rx_pools(adapter);
1822                         release_tx_pools(adapter);
1823                         release_napi(adapter);
1824                         release_vpd_data(adapter);
1825
1826                         rc = init_resources(adapter);
1827                         if (rc)
1828                                 return rc;
1829
1830                 } else {
1831                         rc = reset_tx_pools(adapter);
1832                         if (rc)
1833                                 return rc;
1834
1835                         rc = reset_rx_pools(adapter);
1836                         if (rc)
1837                                 return rc;
1838                 }
1839                 ibmvnic_disable_irqs(adapter);
1840         }
1841         adapter->state = VNIC_CLOSED;
1842
1843         if (reset_state == VNIC_CLOSED)
1844                 return 0;
1845
1846         rc = __ibmvnic_open(netdev);
1847         if (rc) {
1848                 if (list_empty(&adapter->rwi_list))
1849                         adapter->state = VNIC_CLOSED;
1850                 else
1851                         adapter->state = reset_state;
1852
1853                 return 0;
1854         }
1855
1856         /* kick napi */
1857         for (i = 0; i < adapter->req_rx_queues; i++)
1858                 napi_schedule(&adapter->napi[i]);
1859
1860         if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1861             adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1862                 netdev_notify_peers(netdev);
1863
1864         netif_carrier_on(netdev);
1865
1866         return 0;
1867 }
1868
1869 static int do_hard_reset(struct ibmvnic_adapter *adapter,
1870                          struct ibmvnic_rwi *rwi, u32 reset_state)
1871 {
1872         struct net_device *netdev = adapter->netdev;
1873         int rc;
1874
1875         netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
1876                    rwi->reset_reason);
1877
1878         netif_carrier_off(netdev);
1879         adapter->reset_reason = rwi->reset_reason;
1880
1881         ibmvnic_cleanup(netdev);
1882         release_resources(adapter);
1883         release_sub_crqs(adapter, 0);
1884         release_crq_queue(adapter);
1885
1886         /* remove the closed state so when we call open it appears
1887          * we are coming from the probed state.
1888          */
1889         adapter->state = VNIC_PROBED;
1890
1891         rc = init_crq_queue(adapter);
1892         if (rc) {
1893                 netdev_err(adapter->netdev,
1894                            "Couldn't initialize crq. rc=%d\n", rc);
1895                 return rc;
1896         }
1897
1898         rc = ibmvnic_init(adapter);
1899         if (rc)
1900                 return rc;
1901
1902         /* If the adapter was in PROBE state prior to the reset,
1903          * exit here.
1904          */
1905         if (reset_state == VNIC_PROBED)
1906                 return 0;
1907
1908         rc = ibmvnic_login(netdev);
1909         if (rc) {
1910                 adapter->state = VNIC_PROBED;
1911                 return 0;
1912         }
1913
1914         rc = init_resources(adapter);
1915         if (rc)
1916                 return rc;
1917
1918         ibmvnic_disable_irqs(adapter);
1919         adapter->state = VNIC_CLOSED;
1920
1921         if (reset_state == VNIC_CLOSED)
1922                 return 0;
1923
1924         rc = __ibmvnic_open(netdev);
1925         if (rc) {
1926                 if (list_empty(&adapter->rwi_list))
1927                         adapter->state = VNIC_CLOSED;
1928                 else
1929                         adapter->state = reset_state;
1930
1931                 return 0;
1932         }
1933
1934         netif_carrier_on(netdev);
1935
1936         return 0;
1937 }
1938
1939 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1940 {
1941         struct ibmvnic_rwi *rwi;
1942
1943         mutex_lock(&adapter->rwi_lock);
1944
1945         if (!list_empty(&adapter->rwi_list)) {
1946                 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1947                                        list);
1948                 list_del(&rwi->list);
1949         } else {
1950                 rwi = NULL;
1951         }
1952
1953         mutex_unlock(&adapter->rwi_lock);
1954         return rwi;
1955 }
1956
1957 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1958 {
1959         struct ibmvnic_rwi *rwi;
1960
1961         rwi = get_next_rwi(adapter);
1962         while (rwi) {
1963                 kfree(rwi);
1964                 rwi = get_next_rwi(adapter);
1965         }
1966 }
1967
1968 static void __ibmvnic_reset(struct work_struct *work)
1969 {
1970         struct ibmvnic_rwi *rwi;
1971         struct ibmvnic_adapter *adapter;
1972         struct net_device *netdev;
1973         bool we_lock_rtnl = false;
1974         u32 reset_state;
1975         int rc = 0;
1976
1977         adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1978         netdev = adapter->netdev;
1979
1980         /* netif_set_real_num_xx_queues needs to take rtnl lock here
1981          * unless wait_for_reset is set, in which case the rtnl lock
1982          * has already been taken before initializing the reset
1983          */
1984         if (!adapter->wait_for_reset) {
1985                 rtnl_lock();
1986                 we_lock_rtnl = true;
1987         }
1988         reset_state = adapter->state;
1989
1990         rwi = get_next_rwi(adapter);
1991         while (rwi) {
1992                 if (adapter->force_reset_recovery) {
1993                         adapter->force_reset_recovery = false;
1994                         rc = do_hard_reset(adapter, rwi, reset_state);
1995                 } else {
1996                         rc = do_reset(adapter, rwi, reset_state);
1997                 }
1998                 kfree(rwi);
1999                 if (rc && rc != IBMVNIC_INIT_FAILED &&
2000                     !adapter->force_reset_recovery)
2001                         break;
2002
2003                 rwi = get_next_rwi(adapter);
2004         }
2005
2006         if (adapter->wait_for_reset) {
2007                 adapter->wait_for_reset = false;
2008                 adapter->reset_done_rc = rc;
2009                 complete(&adapter->reset_done);
2010         }
2011
2012         if (rc) {
2013                 netdev_dbg(adapter->netdev, "Reset failed\n");
2014                 free_all_rwi(adapter);
2015         }
2016
2017         adapter->resetting = false;
2018         if (we_lock_rtnl)
2019                 rtnl_unlock();
2020 }
2021
2022 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2023                          enum ibmvnic_reset_reason reason)
2024 {
2025         struct list_head *entry, *tmp_entry;
2026         struct ibmvnic_rwi *rwi, *tmp;
2027         struct net_device *netdev = adapter->netdev;
2028         int ret;
2029
2030         if (adapter->state == VNIC_REMOVING ||
2031             adapter->state == VNIC_REMOVED ||
2032             adapter->failover_pending) {
2033                 ret = EBUSY;
2034                 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
2035                 goto err;
2036         }
2037
2038         if (adapter->state == VNIC_PROBING) {
2039                 netdev_warn(netdev, "Adapter reset during probe\n");
2040                 ret = adapter->init_done_rc = EAGAIN;
2041                 goto err;
2042         }
2043
2044         mutex_lock(&adapter->rwi_lock);
2045
2046         list_for_each(entry, &adapter->rwi_list) {
2047                 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2048                 if (tmp->reset_reason == reason) {
2049                         netdev_dbg(netdev, "Skipping matching reset\n");
2050                         mutex_unlock(&adapter->rwi_lock);
2051                         ret = EBUSY;
2052                         goto err;
2053                 }
2054         }
2055
2056         rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
2057         if (!rwi) {
2058                 mutex_unlock(&adapter->rwi_lock);
2059                 ibmvnic_close(netdev);
2060                 ret = ENOMEM;
2061                 goto err;
2062         }
2063         /* if we just received a transport event,
2064          * flush reset queue and process this reset
2065          */
2066         if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2067                 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2068                         list_del(entry);
2069         }
2070         rwi->reset_reason = reason;
2071         list_add_tail(&rwi->list, &adapter->rwi_list);
2072         mutex_unlock(&adapter->rwi_lock);
2073         adapter->resetting = true;
2074         netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
2075         schedule_work(&adapter->ibmvnic_reset);
2076
2077         return 0;
2078 err:
2079         if (adapter->wait_for_reset)
2080                 adapter->wait_for_reset = false;
2081         return -ret;
2082 }
2083
2084 static void ibmvnic_tx_timeout(struct net_device *dev)
2085 {
2086         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2087
2088         ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2089 }
2090
2091 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2092                                   struct ibmvnic_rx_buff *rx_buff)
2093 {
2094         struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2095
2096         rx_buff->skb = NULL;
2097
2098         pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2099         pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2100
2101         atomic_dec(&pool->available);
2102 }
2103
2104 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2105 {
2106         struct net_device *netdev = napi->dev;
2107         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2108         int scrq_num = (int)(napi - adapter->napi);
2109         int frames_processed = 0;
2110
2111 restart_poll:
2112         while (frames_processed < budget) {
2113                 struct sk_buff *skb;
2114                 struct ibmvnic_rx_buff *rx_buff;
2115                 union sub_crq *next;
2116                 u32 length;
2117                 u16 offset;
2118                 u8 flags = 0;
2119
2120                 if (unlikely(adapter->resetting &&
2121                              adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2122                         enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2123                         napi_complete_done(napi, frames_processed);
2124                         return frames_processed;
2125                 }
2126
2127                 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2128                         break;
2129                 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2130                 rx_buff =
2131                     (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2132                                                           rx_comp.correlator);
2133                 /* do error checking */
2134                 if (next->rx_comp.rc) {
2135                         netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2136                                    be16_to_cpu(next->rx_comp.rc));
2137                         /* free the entry */
2138                         next->rx_comp.first = 0;
2139                         dev_kfree_skb_any(rx_buff->skb);
2140                         remove_buff_from_pool(adapter, rx_buff);
2141                         continue;
2142                 } else if (!rx_buff->skb) {
2143                         /* free the entry */
2144                         next->rx_comp.first = 0;
2145                         remove_buff_from_pool(adapter, rx_buff);
2146                         continue;
2147                 }
2148
2149                 length = be32_to_cpu(next->rx_comp.len);
2150                 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2151                 flags = next->rx_comp.flags;
2152                 skb = rx_buff->skb;
2153                 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2154                                         length);
2155
2156                 /* VLAN Header has been stripped by the system firmware and
2157                  * needs to be inserted by the driver
2158                  */
2159                 if (adapter->rx_vlan_header_insertion &&
2160                     (flags & IBMVNIC_VLAN_STRIPPED))
2161                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2162                                                ntohs(next->rx_comp.vlan_tci));
2163
2164                 /* free the entry */
2165                 next->rx_comp.first = 0;
2166                 remove_buff_from_pool(adapter, rx_buff);
2167
2168                 skb_put(skb, length);
2169                 skb->protocol = eth_type_trans(skb, netdev);
2170                 skb_record_rx_queue(skb, scrq_num);
2171
2172                 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2173                     flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2174                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2175                 }
2176
2177                 length = skb->len;
2178                 napi_gro_receive(napi, skb); /* send it up */
2179                 netdev->stats.rx_packets++;
2180                 netdev->stats.rx_bytes += length;
2181                 adapter->rx_stats_buffers[scrq_num].packets++;
2182                 adapter->rx_stats_buffers[scrq_num].bytes += length;
2183                 frames_processed++;
2184         }
2185
2186         if (adapter->state != VNIC_CLOSING)
2187                 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2188
2189         if (frames_processed < budget) {
2190                 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2191                 napi_complete_done(napi, frames_processed);
2192                 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2193                     napi_reschedule(napi)) {
2194                         disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2195                         goto restart_poll;
2196                 }
2197         }
2198         return frames_processed;
2199 }
2200
2201 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2202 {
2203         int rc, ret;
2204
2205         adapter->fallback.mtu = adapter->req_mtu;
2206         adapter->fallback.rx_queues = adapter->req_rx_queues;
2207         adapter->fallback.tx_queues = adapter->req_tx_queues;
2208         adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2209         adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2210
2211         init_completion(&adapter->reset_done);
2212         adapter->wait_for_reset = true;
2213         rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2214         if (rc)
2215                 return rc;
2216         wait_for_completion(&adapter->reset_done);
2217
2218         ret = 0;
2219         if (adapter->reset_done_rc) {
2220                 ret = -EIO;
2221                 adapter->desired.mtu = adapter->fallback.mtu;
2222                 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2223                 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2224                 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2225                 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2226
2227                 init_completion(&adapter->reset_done);
2228                 adapter->wait_for_reset = true;
2229                 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2230                 if (rc)
2231                         return ret;
2232                 wait_for_completion(&adapter->reset_done);
2233         }
2234         adapter->wait_for_reset = false;
2235
2236         return ret;
2237 }
2238
2239 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2240 {
2241         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2242
2243         adapter->desired.mtu = new_mtu + ETH_HLEN;
2244
2245         return wait_for_reset(adapter);
2246 }
2247
2248 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2249                                                 struct net_device *dev,
2250                                                 netdev_features_t features)
2251 {
2252         /* Some backing hardware adapters can not
2253          * handle packets with a MSS less than 224
2254          * or with only one segment.
2255          */
2256         if (skb_is_gso(skb)) {
2257                 if (skb_shinfo(skb)->gso_size < 224 ||
2258                     skb_shinfo(skb)->gso_segs == 1)
2259                         features &= ~NETIF_F_GSO_MASK;
2260         }
2261
2262         return features;
2263 }
2264
2265 static const struct net_device_ops ibmvnic_netdev_ops = {
2266         .ndo_open               = ibmvnic_open,
2267         .ndo_stop               = ibmvnic_close,
2268         .ndo_start_xmit         = ibmvnic_xmit,
2269         .ndo_set_rx_mode        = ibmvnic_set_multi,
2270         .ndo_set_mac_address    = ibmvnic_set_mac,
2271         .ndo_validate_addr      = eth_validate_addr,
2272         .ndo_tx_timeout         = ibmvnic_tx_timeout,
2273         .ndo_change_mtu         = ibmvnic_change_mtu,
2274         .ndo_features_check     = ibmvnic_features_check,
2275 };
2276
2277 /* ethtool functions */
2278
2279 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2280                                       struct ethtool_link_ksettings *cmd)
2281 {
2282         u32 supported, advertising;
2283
2284         supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2285                           SUPPORTED_FIBRE);
2286         advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2287                             ADVERTISED_FIBRE);
2288         cmd->base.speed = SPEED_1000;
2289         cmd->base.duplex = DUPLEX_FULL;
2290         cmd->base.port = PORT_FIBRE;
2291         cmd->base.phy_address = 0;
2292         cmd->base.autoneg = AUTONEG_ENABLE;
2293
2294         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2295                                                 supported);
2296         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2297                                                 advertising);
2298
2299         return 0;
2300 }
2301
2302 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2303                                 struct ethtool_drvinfo *info)
2304 {
2305         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2306
2307         strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2308         strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2309         strlcpy(info->fw_version, adapter->fw_version,
2310                 sizeof(info->fw_version));
2311 }
2312
2313 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2314 {
2315         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2316
2317         return adapter->msg_enable;
2318 }
2319
2320 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2321 {
2322         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2323
2324         adapter->msg_enable = data;
2325 }
2326
2327 static u32 ibmvnic_get_link(struct net_device *netdev)
2328 {
2329         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2330
2331         /* Don't need to send a query because we request a logical link up at
2332          * init and then we wait for link state indications
2333          */
2334         return adapter->logical_link_state;
2335 }
2336
2337 static void ibmvnic_get_ringparam(struct net_device *netdev,
2338                                   struct ethtool_ringparam *ring)
2339 {
2340         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2341
2342         if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2343                 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2344                 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2345         } else {
2346                 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2347                 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2348         }
2349         ring->rx_mini_max_pending = 0;
2350         ring->rx_jumbo_max_pending = 0;
2351         ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2352         ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2353         ring->rx_mini_pending = 0;
2354         ring->rx_jumbo_pending = 0;
2355 }
2356
2357 static int ibmvnic_set_ringparam(struct net_device *netdev,
2358                                  struct ethtool_ringparam *ring)
2359 {
2360         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2361         int ret;
2362
2363         ret = 0;
2364         adapter->desired.rx_entries = ring->rx_pending;
2365         adapter->desired.tx_entries = ring->tx_pending;
2366
2367         ret = wait_for_reset(adapter);
2368
2369         if (!ret &&
2370             (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2371              adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2372                 netdev_info(netdev,
2373                             "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2374                             ring->rx_pending, ring->tx_pending,
2375                             adapter->req_rx_add_entries_per_subcrq,
2376                             adapter->req_tx_entries_per_subcrq);
2377         return ret;
2378 }
2379
2380 static void ibmvnic_get_channels(struct net_device *netdev,
2381                                  struct ethtool_channels *channels)
2382 {
2383         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2384
2385         if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2386                 channels->max_rx = adapter->max_rx_queues;
2387                 channels->max_tx = adapter->max_tx_queues;
2388         } else {
2389                 channels->max_rx = IBMVNIC_MAX_QUEUES;
2390                 channels->max_tx = IBMVNIC_MAX_QUEUES;
2391         }
2392
2393         channels->max_other = 0;
2394         channels->max_combined = 0;
2395         channels->rx_count = adapter->req_rx_queues;
2396         channels->tx_count = adapter->req_tx_queues;
2397         channels->other_count = 0;
2398         channels->combined_count = 0;
2399 }
2400
2401 static int ibmvnic_set_channels(struct net_device *netdev,
2402                                 struct ethtool_channels *channels)
2403 {
2404         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2405         int ret;
2406
2407         ret = 0;
2408         adapter->desired.rx_queues = channels->rx_count;
2409         adapter->desired.tx_queues = channels->tx_count;
2410
2411         ret = wait_for_reset(adapter);
2412
2413         if (!ret &&
2414             (adapter->req_rx_queues != channels->rx_count ||
2415              adapter->req_tx_queues != channels->tx_count))
2416                 netdev_info(netdev,
2417                             "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2418                             channels->rx_count, channels->tx_count,
2419                             adapter->req_rx_queues, adapter->req_tx_queues);
2420         return ret;
2421
2422 }
2423
2424 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2425 {
2426         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2427         int i;
2428
2429         switch (stringset) {
2430         case ETH_SS_STATS:
2431                 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2432                                 i++, data += ETH_GSTRING_LEN)
2433                         memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2434
2435                 for (i = 0; i < adapter->req_tx_queues; i++) {
2436                         snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2437                         data += ETH_GSTRING_LEN;
2438
2439                         snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2440                         data += ETH_GSTRING_LEN;
2441
2442                         snprintf(data, ETH_GSTRING_LEN,
2443                                  "tx%d_dropped_packets", i);
2444                         data += ETH_GSTRING_LEN;
2445                 }
2446
2447                 for (i = 0; i < adapter->req_rx_queues; i++) {
2448                         snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2449                         data += ETH_GSTRING_LEN;
2450
2451                         snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2452                         data += ETH_GSTRING_LEN;
2453
2454                         snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2455                         data += ETH_GSTRING_LEN;
2456                 }
2457                 break;
2458
2459         case ETH_SS_PRIV_FLAGS:
2460                 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2461                         strcpy(data + i * ETH_GSTRING_LEN,
2462                                ibmvnic_priv_flags[i]);
2463                 break;
2464         default:
2465                 return;
2466         }
2467 }
2468
2469 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2470 {
2471         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2472
2473         switch (sset) {
2474         case ETH_SS_STATS:
2475                 return ARRAY_SIZE(ibmvnic_stats) +
2476                        adapter->req_tx_queues * NUM_TX_STATS +
2477                        adapter->req_rx_queues * NUM_RX_STATS;
2478         case ETH_SS_PRIV_FLAGS:
2479                 return ARRAY_SIZE(ibmvnic_priv_flags);
2480         default:
2481                 return -EOPNOTSUPP;
2482         }
2483 }
2484
2485 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2486                                       struct ethtool_stats *stats, u64 *data)
2487 {
2488         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2489         union ibmvnic_crq crq;
2490         int i, j;
2491         int rc;
2492
2493         memset(&crq, 0, sizeof(crq));
2494         crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2495         crq.request_statistics.cmd = REQUEST_STATISTICS;
2496         crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2497         crq.request_statistics.len =
2498             cpu_to_be32(sizeof(struct ibmvnic_statistics));
2499
2500         /* Wait for data to be written */
2501         init_completion(&adapter->stats_done);
2502         rc = ibmvnic_send_crq(adapter, &crq);
2503         if (rc)
2504                 return;
2505         wait_for_completion(&adapter->stats_done);
2506
2507         for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2508                 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2509                                                 ibmvnic_stats[i].offset));
2510
2511         for (j = 0; j < adapter->req_tx_queues; j++) {
2512                 data[i] = adapter->tx_stats_buffers[j].packets;
2513                 i++;
2514                 data[i] = adapter->tx_stats_buffers[j].bytes;
2515                 i++;
2516                 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2517                 i++;
2518         }
2519
2520         for (j = 0; j < adapter->req_rx_queues; j++) {
2521                 data[i] = adapter->rx_stats_buffers[j].packets;
2522                 i++;
2523                 data[i] = adapter->rx_stats_buffers[j].bytes;
2524                 i++;
2525                 data[i] = adapter->rx_stats_buffers[j].interrupts;
2526                 i++;
2527         }
2528 }
2529
2530 static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2531 {
2532         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2533
2534         return adapter->priv_flags;
2535 }
2536
2537 static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2538 {
2539         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2540         bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2541
2542         if (which_maxes)
2543                 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2544         else
2545                 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2546
2547         return 0;
2548 }
2549 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2550         .get_drvinfo            = ibmvnic_get_drvinfo,
2551         .get_msglevel           = ibmvnic_get_msglevel,
2552         .set_msglevel           = ibmvnic_set_msglevel,
2553         .get_link               = ibmvnic_get_link,
2554         .get_ringparam          = ibmvnic_get_ringparam,
2555         .set_ringparam          = ibmvnic_set_ringparam,
2556         .get_channels           = ibmvnic_get_channels,
2557         .set_channels           = ibmvnic_set_channels,
2558         .get_strings            = ibmvnic_get_strings,
2559         .get_sset_count         = ibmvnic_get_sset_count,
2560         .get_ethtool_stats      = ibmvnic_get_ethtool_stats,
2561         .get_link_ksettings     = ibmvnic_get_link_ksettings,
2562         .get_priv_flags         = ibmvnic_get_priv_flags,
2563         .set_priv_flags         = ibmvnic_set_priv_flags,
2564 };
2565
2566 /* Routines for managing CRQs/sCRQs  */
2567
2568 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2569                                    struct ibmvnic_sub_crq_queue *scrq)
2570 {
2571         int rc;
2572
2573         if (scrq->irq) {
2574                 free_irq(scrq->irq, scrq);
2575                 irq_dispose_mapping(scrq->irq);
2576                 scrq->irq = 0;
2577         }
2578
2579         memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2580         atomic_set(&scrq->used, 0);
2581         scrq->cur = 0;
2582
2583         rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2584                            4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2585         return rc;
2586 }
2587
2588 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2589 {
2590         int i, rc;
2591
2592         for (i = 0; i < adapter->req_tx_queues; i++) {
2593                 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2594                 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2595                 if (rc)
2596                         return rc;
2597         }
2598
2599         for (i = 0; i < adapter->req_rx_queues; i++) {
2600                 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2601                 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2602                 if (rc)
2603                         return rc;
2604         }
2605
2606         return rc;
2607 }
2608
2609 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2610                                   struct ibmvnic_sub_crq_queue *scrq,
2611                                   bool do_h_free)
2612 {
2613         struct device *dev = &adapter->vdev->dev;
2614         long rc;
2615
2616         netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2617
2618         if (do_h_free) {
2619                 /* Close the sub-crqs */
2620                 do {
2621                         rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2622                                                 adapter->vdev->unit_address,
2623                                                 scrq->crq_num);
2624                 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2625
2626                 if (rc) {
2627                         netdev_err(adapter->netdev,
2628                                    "Failed to release sub-CRQ %16lx, rc = %ld\n",
2629                                    scrq->crq_num, rc);
2630                 }
2631         }
2632
2633         dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2634                          DMA_BIDIRECTIONAL);
2635         free_pages((unsigned long)scrq->msgs, 2);
2636         kfree(scrq);
2637 }
2638
2639 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2640                                                         *adapter)
2641 {
2642         struct device *dev = &adapter->vdev->dev;
2643         struct ibmvnic_sub_crq_queue *scrq;
2644         int rc;
2645
2646         scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2647         if (!scrq)
2648                 return NULL;
2649
2650         scrq->msgs =
2651                 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2652         if (!scrq->msgs) {
2653                 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2654                 goto zero_page_failed;
2655         }
2656
2657         scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2658                                          DMA_BIDIRECTIONAL);
2659         if (dma_mapping_error(dev, scrq->msg_token)) {
2660                 dev_warn(dev, "Couldn't map crq queue messages page\n");
2661                 goto map_failed;
2662         }
2663
2664         rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2665                            4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2666
2667         if (rc == H_RESOURCE)
2668                 rc = ibmvnic_reset_crq(adapter);
2669
2670         if (rc == H_CLOSED) {
2671                 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2672         } else if (rc) {
2673                 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2674                 goto reg_failed;
2675         }
2676
2677         scrq->adapter = adapter;
2678         scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2679         spin_lock_init(&scrq->lock);
2680
2681         netdev_dbg(adapter->netdev,
2682                    "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2683                    scrq->crq_num, scrq->hw_irq, scrq->irq);
2684
2685         return scrq;
2686
2687 reg_failed:
2688         dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2689                          DMA_BIDIRECTIONAL);
2690 map_failed:
2691         free_pages((unsigned long)scrq->msgs, 2);
2692 zero_page_failed:
2693         kfree(scrq);
2694
2695         return NULL;
2696 }
2697
2698 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2699 {
2700         int i;
2701
2702         if (adapter->tx_scrq) {
2703                 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2704                         if (!adapter->tx_scrq[i])
2705                                 continue;
2706
2707                         netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2708                                    i);
2709                         if (adapter->tx_scrq[i]->irq) {
2710                                 free_irq(adapter->tx_scrq[i]->irq,
2711                                          adapter->tx_scrq[i]);
2712                                 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2713                                 adapter->tx_scrq[i]->irq = 0;
2714                         }
2715
2716                         release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2717                                               do_h_free);
2718                 }
2719
2720                 kfree(adapter->tx_scrq);
2721                 adapter->tx_scrq = NULL;
2722                 adapter->num_active_tx_scrqs = 0;
2723         }
2724
2725         if (adapter->rx_scrq) {
2726                 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2727                         if (!adapter->rx_scrq[i])
2728                                 continue;
2729
2730                         netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2731                                    i);
2732                         if (adapter->rx_scrq[i]->irq) {
2733                                 free_irq(adapter->rx_scrq[i]->irq,
2734                                          adapter->rx_scrq[i]);
2735                                 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2736                                 adapter->rx_scrq[i]->irq = 0;
2737                         }
2738
2739                         release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2740                                               do_h_free);
2741                 }
2742
2743                 kfree(adapter->rx_scrq);
2744                 adapter->rx_scrq = NULL;
2745                 adapter->num_active_rx_scrqs = 0;
2746         }
2747 }
2748
2749 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2750                             struct ibmvnic_sub_crq_queue *scrq)
2751 {
2752         struct device *dev = &adapter->vdev->dev;
2753         unsigned long rc;
2754
2755         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2756                                 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2757         if (rc)
2758                 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2759                         scrq->hw_irq, rc);
2760         return rc;
2761 }
2762
2763 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2764                            struct ibmvnic_sub_crq_queue *scrq)
2765 {
2766         struct device *dev = &adapter->vdev->dev;
2767         unsigned long rc;
2768
2769         if (scrq->hw_irq > 0x100000000ULL) {
2770                 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2771                 return 1;
2772         }
2773
2774         if (adapter->resetting &&
2775             adapter->reset_reason == VNIC_RESET_MOBILITY) {
2776                 u64 val = (0xff000000) | scrq->hw_irq;
2777
2778                 rc = plpar_hcall_norets(H_EOI, val);
2779                 if (rc)
2780                         dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2781                                 val, rc);
2782         }
2783
2784         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2785                                 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2786         if (rc)
2787                 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2788                         scrq->hw_irq, rc);
2789         return rc;
2790 }
2791
2792 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2793                                struct ibmvnic_sub_crq_queue *scrq)
2794 {
2795         struct device *dev = &adapter->vdev->dev;
2796         struct ibmvnic_tx_pool *tx_pool;
2797         struct ibmvnic_tx_buff *txbuff;
2798         union sub_crq *next;
2799         int index;
2800         int i, j;
2801         u8 *first;
2802
2803 restart_loop:
2804         while (pending_scrq(adapter, scrq)) {
2805                 unsigned int pool = scrq->pool_index;
2806                 int num_entries = 0;
2807
2808                 next = ibmvnic_next_scrq(adapter, scrq);
2809                 for (i = 0; i < next->tx_comp.num_comps; i++) {
2810                         if (next->tx_comp.rcs[i]) {
2811                                 dev_err(dev, "tx error %x\n",
2812                                         next->tx_comp.rcs[i]);
2813                                 continue;
2814                         }
2815                         index = be32_to_cpu(next->tx_comp.correlators[i]);
2816                         if (index & IBMVNIC_TSO_POOL_MASK) {
2817                                 tx_pool = &adapter->tso_pool[pool];
2818                                 index &= ~IBMVNIC_TSO_POOL_MASK;
2819                         } else {
2820                                 tx_pool = &adapter->tx_pool[pool];
2821                         }
2822
2823                         txbuff = &tx_pool->tx_buff[index];
2824
2825                         for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2826                                 if (!txbuff->data_dma[j])
2827                                         continue;
2828
2829                                 txbuff->data_dma[j] = 0;
2830                         }
2831                         /* if sub_crq was sent indirectly */
2832                         first = &txbuff->indir_arr[0].generic.first;
2833                         if (*first == IBMVNIC_CRQ_CMD) {
2834                                 dma_unmap_single(dev, txbuff->indir_dma,
2835                                                  sizeof(txbuff->indir_arr),
2836                                                  DMA_TO_DEVICE);
2837                                 *first = 0;
2838                         }
2839
2840                         if (txbuff->last_frag) {
2841                                 dev_kfree_skb_any(txbuff->skb);
2842                                 txbuff->skb = NULL;
2843                         }
2844
2845                         num_entries += txbuff->num_entries;
2846
2847                         tx_pool->free_map[tx_pool->producer_index] = index;
2848                         tx_pool->producer_index =
2849                                 (tx_pool->producer_index + 1) %
2850                                         tx_pool->num_buffers;
2851                 }
2852                 /* remove tx_comp scrq*/
2853                 next->tx_comp.first = 0;
2854
2855                 if (atomic_sub_return(num_entries, &scrq->used) <=
2856                     (adapter->req_tx_entries_per_subcrq / 2) &&
2857                     __netif_subqueue_stopped(adapter->netdev,
2858                                              scrq->pool_index)) {
2859                         netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2860                         netdev_dbg(adapter->netdev, "Started queue %d\n",
2861                                    scrq->pool_index);
2862                 }
2863         }
2864
2865         enable_scrq_irq(adapter, scrq);
2866
2867         if (pending_scrq(adapter, scrq)) {
2868                 disable_scrq_irq(adapter, scrq);
2869                 goto restart_loop;
2870         }
2871
2872         return 0;
2873 }
2874
2875 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2876 {
2877         struct ibmvnic_sub_crq_queue *scrq = instance;
2878         struct ibmvnic_adapter *adapter = scrq->adapter;
2879
2880         disable_scrq_irq(adapter, scrq);
2881         ibmvnic_complete_tx(adapter, scrq);
2882
2883         return IRQ_HANDLED;
2884 }
2885
2886 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2887 {
2888         struct ibmvnic_sub_crq_queue *scrq = instance;
2889         struct ibmvnic_adapter *adapter = scrq->adapter;
2890
2891         /* When booting a kdump kernel we can hit pending interrupts
2892          * prior to completing driver initialization.
2893          */
2894         if (unlikely(adapter->state != VNIC_OPEN))
2895                 return IRQ_NONE;
2896
2897         adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2898
2899         if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2900                 disable_scrq_irq(adapter, scrq);
2901                 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2902         }
2903
2904         return IRQ_HANDLED;
2905 }
2906
2907 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2908 {
2909         struct device *dev = &adapter->vdev->dev;
2910         struct ibmvnic_sub_crq_queue *scrq;
2911         int i = 0, j = 0;
2912         int rc = 0;
2913
2914         for (i = 0; i < adapter->req_tx_queues; i++) {
2915                 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2916                            i);
2917                 scrq = adapter->tx_scrq[i];
2918                 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2919
2920                 if (!scrq->irq) {
2921                         rc = -EINVAL;
2922                         dev_err(dev, "Error mapping irq\n");
2923                         goto req_tx_irq_failed;
2924                 }
2925
2926                 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2927                                  0, "ibmvnic_tx", scrq);
2928
2929                 if (rc) {
2930                         dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2931                                 scrq->irq, rc);
2932                         irq_dispose_mapping(scrq->irq);
2933                         goto req_tx_irq_failed;
2934                 }
2935         }
2936
2937         for (i = 0; i < adapter->req_rx_queues; i++) {
2938                 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2939                            i);
2940                 scrq = adapter->rx_scrq[i];
2941                 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2942                 if (!scrq->irq) {
2943                         rc = -EINVAL;
2944                         dev_err(dev, "Error mapping irq\n");
2945                         goto req_rx_irq_failed;
2946                 }
2947                 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2948                                  0, "ibmvnic_rx", scrq);
2949                 if (rc) {
2950                         dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2951                                 scrq->irq, rc);
2952                         irq_dispose_mapping(scrq->irq);
2953                         goto req_rx_irq_failed;
2954                 }
2955         }
2956         return rc;
2957
2958 req_rx_irq_failed:
2959         for (j = 0; j < i; j++) {
2960                 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2961                 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2962         }
2963         i = adapter->req_tx_queues;
2964 req_tx_irq_failed:
2965         for (j = 0; j < i; j++) {
2966                 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2967                 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2968         }
2969         release_sub_crqs(adapter, 1);
2970         return rc;
2971 }
2972
2973 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2974 {
2975         struct device *dev = &adapter->vdev->dev;
2976         struct ibmvnic_sub_crq_queue **allqueues;
2977         int registered_queues = 0;
2978         int total_queues;
2979         int more = 0;
2980         int i;
2981
2982         total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2983
2984         allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2985         if (!allqueues)
2986                 return -1;
2987
2988         for (i = 0; i < total_queues; i++) {
2989                 allqueues[i] = init_sub_crq_queue(adapter);
2990                 if (!allqueues[i]) {
2991                         dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2992                         break;
2993                 }
2994                 registered_queues++;
2995         }
2996
2997         /* Make sure we were able to register the minimum number of queues */
2998         if (registered_queues <
2999             adapter->min_tx_queues + adapter->min_rx_queues) {
3000                 dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
3001                 goto tx_failed;
3002         }
3003
3004         /* Distribute the failed allocated queues*/
3005         for (i = 0; i < total_queues - registered_queues + more ; i++) {
3006                 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3007                 switch (i % 3) {
3008                 case 0:
3009                         if (adapter->req_rx_queues > adapter->min_rx_queues)
3010                                 adapter->req_rx_queues--;
3011                         else
3012                                 more++;
3013                         break;
3014                 case 1:
3015                         if (adapter->req_tx_queues > adapter->min_tx_queues)
3016                                 adapter->req_tx_queues--;
3017                         else
3018                                 more++;
3019                         break;
3020                 }
3021         }
3022
3023         adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3024                                    sizeof(*adapter->tx_scrq), GFP_KERNEL);
3025         if (!adapter->tx_scrq)
3026                 goto tx_failed;
3027
3028         for (i = 0; i < adapter->req_tx_queues; i++) {
3029                 adapter->tx_scrq[i] = allqueues[i];
3030                 adapter->tx_scrq[i]->pool_index = i;
3031                 adapter->num_active_tx_scrqs++;
3032         }
3033
3034         adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3035                                    sizeof(*adapter->rx_scrq), GFP_KERNEL);
3036         if (!adapter->rx_scrq)
3037                 goto rx_failed;
3038
3039         for (i = 0; i < adapter->req_rx_queues; i++) {
3040                 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3041                 adapter->rx_scrq[i]->scrq_num = i;
3042                 adapter->num_active_rx_scrqs++;
3043         }
3044
3045         kfree(allqueues);
3046         return 0;
3047
3048 rx_failed:
3049         kfree(adapter->tx_scrq);
3050         adapter->tx_scrq = NULL;
3051 tx_failed:
3052         for (i = 0; i < registered_queues; i++)
3053                 release_sub_crq_queue(adapter, allqueues[i], 1);
3054         kfree(allqueues);
3055         return -1;
3056 }
3057
3058 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3059 {
3060         struct device *dev = &adapter->vdev->dev;
3061         union ibmvnic_crq crq;
3062         int max_entries;
3063
3064         if (!retry) {