fca0533d517f4c993747c53822de648bfa76d8fa
[platform/kernel/linux-starfive.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 void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static void 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 void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
121
122 struct ibmvnic_stat {
123         char name[ETH_GSTRING_LEN];
124         int offset;
125 };
126
127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128                              offsetof(struct ibmvnic_statistics, stat))
129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
130
131 static const struct ibmvnic_stat ibmvnic_stats[] = {
132         {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133         {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134         {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135         {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136         {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137         {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138         {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139         {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140         {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141         {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142         {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143         {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144         {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145         {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146         {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147         {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148         {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149         {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150         {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151         {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152         {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153         {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
154 };
155
156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157                           unsigned long length, unsigned long *number,
158                           unsigned long *irq)
159 {
160         unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
161         long rc;
162
163         rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
164         *number = retbuf[0];
165         *irq = retbuf[1];
166
167         return rc;
168 }
169
170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171                                 struct ibmvnic_long_term_buff *ltb, int size)
172 {
173         struct device *dev = &adapter->vdev->dev;
174
175         ltb->size = size;
176         ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
177                                        GFP_KERNEL);
178
179         if (!ltb->buff) {
180                 dev_err(dev, "Couldn't alloc long term buffer\n");
181                 return -ENOMEM;
182         }
183         ltb->map_id = adapter->map_id;
184         adapter->map_id++;
185
186         init_completion(&adapter->fw_done);
187         send_request_map(adapter, ltb->addr,
188                          ltb->size, ltb->map_id);
189         wait_for_completion(&adapter->fw_done);
190
191         if (adapter->fw_done_rc) {
192                 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193                         adapter->fw_done_rc);
194                 return -1;
195         }
196         return 0;
197 }
198
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200                                 struct ibmvnic_long_term_buff *ltb)
201 {
202         struct device *dev = &adapter->vdev->dev;
203
204         if (!ltb->buff)
205                 return;
206
207         if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208             adapter->reset_reason != VNIC_RESET_MOBILITY)
209                 send_request_unmap(adapter, ltb->map_id);
210         dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
211 }
212
213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214                                 struct ibmvnic_long_term_buff *ltb)
215 {
216         memset(ltb->buff, 0, ltb->size);
217
218         init_completion(&adapter->fw_done);
219         send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220         wait_for_completion(&adapter->fw_done);
221
222         if (adapter->fw_done_rc) {
223                 dev_info(&adapter->vdev->dev,
224                          "Reset failed, attempting to free and reallocate buffer\n");
225                 free_long_term_buff(adapter, ltb);
226                 return alloc_long_term_buff(adapter, ltb, ltb->size);
227         }
228         return 0;
229 }
230
231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
232 {
233         int i;
234
235         for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
236              i++)
237                 adapter->rx_pool[i].active = 0;
238 }
239
240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241                               struct ibmvnic_rx_pool *pool)
242 {
243         int count = pool->size - atomic_read(&pool->available);
244         struct device *dev = &adapter->vdev->dev;
245         int buffers_added = 0;
246         unsigned long lpar_rc;
247         union sub_crq sub_crq;
248         struct sk_buff *skb;
249         unsigned int offset;
250         dma_addr_t dma_addr;
251         unsigned char *dst;
252         u64 *handle_array;
253         int shift = 0;
254         int index;
255         int i;
256
257         if (!pool->active)
258                 return;
259
260         handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261                                       be32_to_cpu(adapter->login_rsp_buf->
262                                       off_rxadd_subcrqs));
263
264         for (i = 0; i < count; ++i) {
265                 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
266                 if (!skb) {
267                         dev_err(dev, "Couldn't replenish rx buff\n");
268                         adapter->replenish_no_mem++;
269                         break;
270                 }
271
272                 index = pool->free_map[pool->next_free];
273
274                 if (pool->rx_buff[index].skb)
275                         dev_err(dev, "Inconsistent free_map!\n");
276
277                 /* Copy the skb to the long term mapped DMA buffer */
278                 offset = index * pool->buff_size;
279                 dst = pool->long_term_buff.buff + offset;
280                 memset(dst, 0, pool->buff_size);
281                 dma_addr = pool->long_term_buff.addr + offset;
282                 pool->rx_buff[index].data = dst;
283
284                 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285                 pool->rx_buff[index].dma = dma_addr;
286                 pool->rx_buff[index].skb = skb;
287                 pool->rx_buff[index].pool_index = pool->index;
288                 pool->rx_buff[index].size = pool->buff_size;
289
290                 memset(&sub_crq, 0, sizeof(sub_crq));
291                 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292                 sub_crq.rx_add.correlator =
293                     cpu_to_be64((u64)&pool->rx_buff[index]);
294                 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295                 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
296
297                 /* The length field of the sCRQ is defined to be 24 bits so the
298                  * buffer size needs to be left shifted by a byte before it is
299                  * converted to big endian to prevent the last byte from being
300                  * truncated.
301                  */
302 #ifdef __LITTLE_ENDIAN__
303                 shift = 8;
304 #endif
305                 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
306
307                 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
308                                       &sub_crq);
309                 if (lpar_rc != H_SUCCESS)
310                         goto failure;
311
312                 buffers_added++;
313                 adapter->replenish_add_buff_success++;
314                 pool->next_free = (pool->next_free + 1) % pool->size;
315         }
316         atomic_add(buffers_added, &pool->available);
317         return;
318
319 failure:
320         dev_info(dev, "replenish pools failure\n");
321         pool->free_map[pool->next_free] = index;
322         pool->rx_buff[index].skb = NULL;
323         if (!dma_mapping_error(dev, dma_addr))
324                 dma_unmap_single(dev, dma_addr, pool->buff_size,
325                                  DMA_FROM_DEVICE);
326
327         dev_kfree_skb_any(skb);
328         adapter->replenish_add_buff_failure++;
329         atomic_add(buffers_added, &pool->available);
330
331         if (lpar_rc == H_CLOSED) {
332                 /* Disable buffer pool replenishment and report carrier off if
333                  * queue is closed. Firmware guarantees that a signal will
334                  * be sent to the driver, triggering a reset.
335                  */
336                 deactivate_rx_pools(adapter);
337                 netif_carrier_off(adapter->netdev);
338         }
339 }
340
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
342 {
343         int i;
344
345         adapter->replenish_task_cycles++;
346         for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
347              i++) {
348                 if (adapter->rx_pool[i].active)
349                         replenish_rx_pool(adapter, &adapter->rx_pool[i]);
350         }
351 }
352
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
354 {
355         kfree(adapter->tx_stats_buffers);
356         kfree(adapter->rx_stats_buffers);
357         adapter->tx_stats_buffers = NULL;
358         adapter->rx_stats_buffers = NULL;
359 }
360
361 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
362 {
363         adapter->tx_stats_buffers =
364                                 kcalloc(IBMVNIC_MAX_QUEUES,
365                                         sizeof(struct ibmvnic_tx_queue_stats),
366                                         GFP_KERNEL);
367         if (!adapter->tx_stats_buffers)
368                 return -ENOMEM;
369
370         adapter->rx_stats_buffers =
371                                 kcalloc(IBMVNIC_MAX_QUEUES,
372                                         sizeof(struct ibmvnic_rx_queue_stats),
373                                         GFP_KERNEL);
374         if (!adapter->rx_stats_buffers)
375                 return -ENOMEM;
376
377         return 0;
378 }
379
380 static void release_stats_token(struct ibmvnic_adapter *adapter)
381 {
382         struct device *dev = &adapter->vdev->dev;
383
384         if (!adapter->stats_token)
385                 return;
386
387         dma_unmap_single(dev, adapter->stats_token,
388                          sizeof(struct ibmvnic_statistics),
389                          DMA_FROM_DEVICE);
390         adapter->stats_token = 0;
391 }
392
393 static int init_stats_token(struct ibmvnic_adapter *adapter)
394 {
395         struct device *dev = &adapter->vdev->dev;
396         dma_addr_t stok;
397
398         stok = dma_map_single(dev, &adapter->stats,
399                               sizeof(struct ibmvnic_statistics),
400                               DMA_FROM_DEVICE);
401         if (dma_mapping_error(dev, stok)) {
402                 dev_err(dev, "Couldn't map stats buffer\n");
403                 return -1;
404         }
405
406         adapter->stats_token = stok;
407         netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
408         return 0;
409 }
410
411 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
412 {
413         struct ibmvnic_rx_pool *rx_pool;
414         int rx_scrqs;
415         int i, j, rc;
416         u64 *size_array;
417
418         size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419                 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
420
421         rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422         for (i = 0; i < rx_scrqs; i++) {
423                 rx_pool = &adapter->rx_pool[i];
424
425                 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
426
427                 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428                         free_long_term_buff(adapter, &rx_pool->long_term_buff);
429                         rx_pool->buff_size = be64_to_cpu(size_array[i]);
430                         alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
431                                              rx_pool->size *
432                                              rx_pool->buff_size);
433                 } else {
434                         rc = reset_long_term_buff(adapter,
435                                                   &rx_pool->long_term_buff);
436                 }
437
438                 if (rc)
439                         return rc;
440
441                 for (j = 0; j < rx_pool->size; j++)
442                         rx_pool->free_map[j] = j;
443
444                 memset(rx_pool->rx_buff, 0,
445                        rx_pool->size * sizeof(struct ibmvnic_rx_buff));
446
447                 atomic_set(&rx_pool->available, 0);
448                 rx_pool->next_alloc = 0;
449                 rx_pool->next_free = 0;
450                 rx_pool->active = 1;
451         }
452
453         return 0;
454 }
455
456 static void release_rx_pools(struct ibmvnic_adapter *adapter)
457 {
458         struct ibmvnic_rx_pool *rx_pool;
459         int i, j;
460
461         if (!adapter->rx_pool)
462                 return;
463
464         for (i = 0; i < adapter->num_active_rx_pools; i++) {
465                 rx_pool = &adapter->rx_pool[i];
466
467                 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
468
469                 kfree(rx_pool->free_map);
470                 free_long_term_buff(adapter, &rx_pool->long_term_buff);
471
472                 if (!rx_pool->rx_buff)
473                         continue;
474
475                 for (j = 0; j < rx_pool->size; j++) {
476                         if (rx_pool->rx_buff[j].skb) {
477                                 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478                                 rx_pool->rx_buff[i].skb = NULL;
479                         }
480                 }
481
482                 kfree(rx_pool->rx_buff);
483         }
484
485         kfree(adapter->rx_pool);
486         adapter->rx_pool = NULL;
487         adapter->num_active_rx_pools = 0;
488 }
489
490 static int init_rx_pools(struct net_device *netdev)
491 {
492         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493         struct device *dev = &adapter->vdev->dev;
494         struct ibmvnic_rx_pool *rx_pool;
495         int rxadd_subcrqs;
496         u64 *size_array;
497         int i, j;
498
499         rxadd_subcrqs =
500                 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501         size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502                 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
503
504         adapter->rx_pool = kcalloc(rxadd_subcrqs,
505                                    sizeof(struct ibmvnic_rx_pool),
506                                    GFP_KERNEL);
507         if (!adapter->rx_pool) {
508                 dev_err(dev, "Failed to allocate rx pools\n");
509                 return -1;
510         }
511
512         adapter->num_active_rx_pools = rxadd_subcrqs;
513
514         for (i = 0; i < rxadd_subcrqs; i++) {
515                 rx_pool = &adapter->rx_pool[i];
516
517                 netdev_dbg(adapter->netdev,
518                            "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
519                            i, adapter->req_rx_add_entries_per_subcrq,
520                            be64_to_cpu(size_array[i]));
521
522                 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
523                 rx_pool->index = i;
524                 rx_pool->buff_size = be64_to_cpu(size_array[i]);
525                 rx_pool->active = 1;
526
527                 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
528                                             GFP_KERNEL);
529                 if (!rx_pool->free_map) {
530                         release_rx_pools(adapter);
531                         return -1;
532                 }
533
534                 rx_pool->rx_buff = kcalloc(rx_pool->size,
535                                            sizeof(struct ibmvnic_rx_buff),
536                                            GFP_KERNEL);
537                 if (!rx_pool->rx_buff) {
538                         dev_err(dev, "Couldn't alloc rx buffers\n");
539                         release_rx_pools(adapter);
540                         return -1;
541                 }
542
543                 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544                                          rx_pool->size * rx_pool->buff_size)) {
545                         release_rx_pools(adapter);
546                         return -1;
547                 }
548
549                 for (j = 0; j < rx_pool->size; ++j)
550                         rx_pool->free_map[j] = j;
551
552                 atomic_set(&rx_pool->available, 0);
553                 rx_pool->next_alloc = 0;
554                 rx_pool->next_free = 0;
555         }
556
557         return 0;
558 }
559
560 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
561 {
562         struct ibmvnic_tx_pool *tx_pool;
563         int tx_scrqs;
564         int i, j, rc;
565
566         tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
567         for (i = 0; i < tx_scrqs; i++) {
568                 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
569
570                 tx_pool = &adapter->tx_pool[i];
571
572                 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
573                 if (rc)
574                         return rc;
575
576                 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
577                 if (rc)
578                         return rc;
579
580                 memset(tx_pool->tx_buff, 0,
581                        adapter->req_tx_entries_per_subcrq *
582                        sizeof(struct ibmvnic_tx_buff));
583
584                 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
585                         tx_pool->free_map[j] = j;
586
587                 tx_pool->consumer_index = 0;
588                 tx_pool->producer_index = 0;
589                 tx_pool->tso_index = 0;
590         }
591
592         return 0;
593 }
594
595 static void release_vpd_data(struct ibmvnic_adapter *adapter)
596 {
597         if (!adapter->vpd)
598                 return;
599
600         kfree(adapter->vpd->buff);
601         kfree(adapter->vpd);
602
603         adapter->vpd = NULL;
604 }
605
606 static void release_tx_pools(struct ibmvnic_adapter *adapter)
607 {
608         struct ibmvnic_tx_pool *tx_pool;
609         int i;
610
611         if (!adapter->tx_pool)
612                 return;
613
614         for (i = 0; i < adapter->num_active_tx_pools; i++) {
615                 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
616                 tx_pool = &adapter->tx_pool[i];
617                 kfree(tx_pool->tx_buff);
618                 free_long_term_buff(adapter, &tx_pool->long_term_buff);
619                 free_long_term_buff(adapter, &tx_pool->tso_ltb);
620                 kfree(tx_pool->free_map);
621         }
622
623         kfree(adapter->tx_pool);
624         adapter->tx_pool = NULL;
625         adapter->num_active_tx_pools = 0;
626 }
627
628 static int init_tx_pools(struct net_device *netdev)
629 {
630         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
631         struct device *dev = &adapter->vdev->dev;
632         struct ibmvnic_tx_pool *tx_pool;
633         int tx_subcrqs;
634         int i, j;
635
636         tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
637         adapter->tx_pool = kcalloc(tx_subcrqs,
638                                    sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
639         if (!adapter->tx_pool)
640                 return -1;
641
642         adapter->num_active_tx_pools = tx_subcrqs;
643
644         for (i = 0; i < tx_subcrqs; i++) {
645                 tx_pool = &adapter->tx_pool[i];
646
647                 netdev_dbg(adapter->netdev,
648                            "Initializing tx_pool[%d], %lld buffs\n",
649                            i, adapter->req_tx_entries_per_subcrq);
650
651                 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
652                                            sizeof(struct ibmvnic_tx_buff),
653                                            GFP_KERNEL);
654                 if (!tx_pool->tx_buff) {
655                         dev_err(dev, "tx pool buffer allocation failed\n");
656                         release_tx_pools(adapter);
657                         return -1;
658                 }
659
660                 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
661                                          adapter->req_tx_entries_per_subcrq *
662                                          adapter->req_mtu)) {
663                         release_tx_pools(adapter);
664                         return -1;
665                 }
666
667                 /* alloc TSO ltb */
668                 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
669                                          IBMVNIC_TSO_BUFS *
670                                          IBMVNIC_TSO_BUF_SZ)) {
671                         release_tx_pools(adapter);
672                         return -1;
673                 }
674
675                 tx_pool->tso_index = 0;
676
677                 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
678                                             sizeof(int), GFP_KERNEL);
679                 if (!tx_pool->free_map) {
680                         release_tx_pools(adapter);
681                         return -1;
682                 }
683
684                 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
685                         tx_pool->free_map[j] = j;
686
687                 tx_pool->consumer_index = 0;
688                 tx_pool->producer_index = 0;
689         }
690
691         return 0;
692 }
693
694 static void release_error_buffers(struct ibmvnic_adapter *adapter)
695 {
696         struct device *dev = &adapter->vdev->dev;
697         struct ibmvnic_error_buff *error_buff, *tmp;
698         unsigned long flags;
699
700         spin_lock_irqsave(&adapter->error_list_lock, flags);
701         list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
702                 list_del(&error_buff->list);
703                 dma_unmap_single(dev, error_buff->dma, error_buff->len,
704                                  DMA_FROM_DEVICE);
705                 kfree(error_buff->buff);
706                 kfree(error_buff);
707         }
708         spin_unlock_irqrestore(&adapter->error_list_lock, flags);
709 }
710
711 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
712 {
713         int i;
714
715         if (adapter->napi_enabled)
716                 return;
717
718         for (i = 0; i < adapter->req_rx_queues; i++)
719                 napi_enable(&adapter->napi[i]);
720
721         adapter->napi_enabled = true;
722 }
723
724 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
725 {
726         int i;
727
728         if (!adapter->napi_enabled)
729                 return;
730
731         for (i = 0; i < adapter->req_rx_queues; i++) {
732                 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
733                 napi_disable(&adapter->napi[i]);
734         }
735
736         adapter->napi_enabled = false;
737 }
738
739 static int init_napi(struct ibmvnic_adapter *adapter)
740 {
741         int i;
742
743         adapter->napi = kcalloc(adapter->req_rx_queues,
744                                 sizeof(struct napi_struct), GFP_KERNEL);
745         if (!adapter->napi)
746                 return -ENOMEM;
747
748         for (i = 0; i < adapter->req_rx_queues; i++) {
749                 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
750                 netif_napi_add(adapter->netdev, &adapter->napi[i],
751                                ibmvnic_poll, NAPI_POLL_WEIGHT);
752         }
753
754         adapter->num_active_rx_napi = adapter->req_rx_queues;
755         return 0;
756 }
757
758 static void release_napi(struct ibmvnic_adapter *adapter)
759 {
760         int i;
761
762         if (!adapter->napi)
763                 return;
764
765         for (i = 0; i < adapter->num_active_rx_napi; i++) {
766                 if (&adapter->napi[i]) {
767                         netdev_dbg(adapter->netdev,
768                                    "Releasing napi[%d]\n", i);
769                         netif_napi_del(&adapter->napi[i]);
770                 }
771         }
772
773         kfree(adapter->napi);
774         adapter->napi = NULL;
775         adapter->num_active_rx_napi = 0;
776 }
777
778 static int ibmvnic_login(struct net_device *netdev)
779 {
780         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
781         unsigned long timeout = msecs_to_jiffies(30000);
782         struct device *dev = &adapter->vdev->dev;
783         int rc;
784
785         do {
786                 if (adapter->renegotiate) {
787                         adapter->renegotiate = false;
788                         release_sub_crqs(adapter, 1);
789
790                         reinit_completion(&adapter->init_done);
791                         send_cap_queries(adapter);
792                         if (!wait_for_completion_timeout(&adapter->init_done,
793                                                          timeout)) {
794                                 dev_err(dev, "Capabilities query timeout\n");
795                                 return -1;
796                         }
797                         rc = init_sub_crqs(adapter);
798                         if (rc) {
799                                 dev_err(dev,
800                                         "Initialization of SCRQ's failed\n");
801                                 return -1;
802                         }
803                         rc = init_sub_crq_irqs(adapter);
804                         if (rc) {
805                                 dev_err(dev,
806                                         "Initialization of SCRQ's irqs failed\n");
807                                 return -1;
808                         }
809                 }
810
811                 reinit_completion(&adapter->init_done);
812                 rc = send_login(adapter);
813                 if (rc) {
814                         dev_err(dev, "Unable to attempt device login\n");
815                         return rc;
816                 } else if (!wait_for_completion_timeout(&adapter->init_done,
817                                                  timeout)) {
818                         dev_err(dev, "Login timeout\n");
819                         return -1;
820                 }
821         } while (adapter->renegotiate);
822
823         /* handle pending MAC address changes after successful login */
824         if (adapter->mac_change_pending) {
825                 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
826                 adapter->mac_change_pending = false;
827         }
828
829         return 0;
830 }
831
832 static void release_login_buffer(struct ibmvnic_adapter *adapter)
833 {
834         kfree(adapter->login_buf);
835         adapter->login_buf = NULL;
836 }
837
838 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
839 {
840         kfree(adapter->login_rsp_buf);
841         adapter->login_rsp_buf = NULL;
842 }
843
844 static void release_resources(struct ibmvnic_adapter *adapter)
845 {
846         release_vpd_data(adapter);
847
848         release_tx_pools(adapter);
849         release_rx_pools(adapter);
850
851         release_error_buffers(adapter);
852         release_napi(adapter);
853         release_login_rsp_buffer(adapter);
854 }
855
856 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
857 {
858         struct net_device *netdev = adapter->netdev;
859         unsigned long timeout = msecs_to_jiffies(30000);
860         union ibmvnic_crq crq;
861         bool resend;
862         int rc;
863
864         netdev_dbg(netdev, "setting link state %d\n", link_state);
865
866         memset(&crq, 0, sizeof(crq));
867         crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
868         crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
869         crq.logical_link_state.link_state = link_state;
870
871         do {
872                 resend = false;
873
874                 reinit_completion(&adapter->init_done);
875                 rc = ibmvnic_send_crq(adapter, &crq);
876                 if (rc) {
877                         netdev_err(netdev, "Failed to set link state\n");
878                         return rc;
879                 }
880
881                 if (!wait_for_completion_timeout(&adapter->init_done,
882                                                  timeout)) {
883                         netdev_err(netdev, "timeout setting link state\n");
884                         return -1;
885                 }
886
887                 if (adapter->init_done_rc == 1) {
888                         /* Partuial success, delay and re-send */
889                         mdelay(1000);
890                         resend = true;
891                 }
892         } while (resend);
893
894         return 0;
895 }
896
897 static int set_real_num_queues(struct net_device *netdev)
898 {
899         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
900         int rc;
901
902         netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
903                    adapter->req_tx_queues, adapter->req_rx_queues);
904
905         rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
906         if (rc) {
907                 netdev_err(netdev, "failed to set the number of tx queues\n");
908                 return rc;
909         }
910
911         rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
912         if (rc)
913                 netdev_err(netdev, "failed to set the number of rx queues\n");
914
915         return rc;
916 }
917
918 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
919 {
920         struct device *dev = &adapter->vdev->dev;
921         union ibmvnic_crq crq;
922         int len = 0;
923
924         if (adapter->vpd->buff)
925                 len = adapter->vpd->len;
926
927         init_completion(&adapter->fw_done);
928         crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
929         crq.get_vpd_size.cmd = GET_VPD_SIZE;
930         ibmvnic_send_crq(adapter, &crq);
931         wait_for_completion(&adapter->fw_done);
932
933         if (!adapter->vpd->len)
934                 return -ENODATA;
935
936         if (!adapter->vpd->buff)
937                 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
938         else if (adapter->vpd->len != len)
939                 adapter->vpd->buff =
940                         krealloc(adapter->vpd->buff,
941                                  adapter->vpd->len, GFP_KERNEL);
942
943         if (!adapter->vpd->buff) {
944                 dev_err(dev, "Could allocate VPD buffer\n");
945                 return -ENOMEM;
946         }
947
948         adapter->vpd->dma_addr =
949                 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
950                                DMA_FROM_DEVICE);
951         if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
952                 dev_err(dev, "Could not map VPD buffer\n");
953                 kfree(adapter->vpd->buff);
954                 adapter->vpd->buff = NULL;
955                 return -ENOMEM;
956         }
957
958         reinit_completion(&adapter->fw_done);
959         crq.get_vpd.first = IBMVNIC_CRQ_CMD;
960         crq.get_vpd.cmd = GET_VPD;
961         crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
962         crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
963         ibmvnic_send_crq(adapter, &crq);
964         wait_for_completion(&adapter->fw_done);
965
966         return 0;
967 }
968
969 static int init_resources(struct ibmvnic_adapter *adapter)
970 {
971         struct net_device *netdev = adapter->netdev;
972         int rc;
973
974         rc = set_real_num_queues(netdev);
975         if (rc)
976                 return rc;
977
978         adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
979         if (!adapter->vpd)
980                 return -ENOMEM;
981
982         /* Vital Product Data (VPD) */
983         rc = ibmvnic_get_vpd(adapter);
984         if (rc) {
985                 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
986                 return rc;
987         }
988
989         adapter->map_id = 1;
990
991         rc = init_napi(adapter);
992         if (rc)
993                 return rc;
994
995         send_map_query(adapter);
996
997         rc = init_rx_pools(netdev);
998         if (rc)
999                 return rc;
1000
1001         rc = init_tx_pools(netdev);
1002         return rc;
1003 }
1004
1005 static int __ibmvnic_open(struct net_device *netdev)
1006 {
1007         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1008         enum vnic_state prev_state = adapter->state;
1009         int i, rc;
1010
1011         adapter->state = VNIC_OPENING;
1012         replenish_pools(adapter);
1013         ibmvnic_napi_enable(adapter);
1014
1015         /* We're ready to receive frames, enable the sub-crq interrupts and
1016          * set the logical link state to up
1017          */
1018         for (i = 0; i < adapter->req_rx_queues; i++) {
1019                 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1020                 if (prev_state == VNIC_CLOSED)
1021                         enable_irq(adapter->rx_scrq[i]->irq);
1022                 else
1023                         enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1024         }
1025
1026         for (i = 0; i < adapter->req_tx_queues; i++) {
1027                 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1028                 if (prev_state == VNIC_CLOSED)
1029                         enable_irq(adapter->tx_scrq[i]->irq);
1030                 else
1031                         enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1032         }
1033
1034         rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1035         if (rc) {
1036                 for (i = 0; i < adapter->req_rx_queues; i++)
1037                         napi_disable(&adapter->napi[i]);
1038                 release_resources(adapter);
1039                 return rc;
1040         }
1041
1042         netif_tx_start_all_queues(netdev);
1043
1044         if (prev_state == VNIC_CLOSED) {
1045                 for (i = 0; i < adapter->req_rx_queues; i++)
1046                         napi_schedule(&adapter->napi[i]);
1047         }
1048
1049         adapter->state = VNIC_OPEN;
1050         return rc;
1051 }
1052
1053 static int ibmvnic_open(struct net_device *netdev)
1054 {
1055         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1056         int rc;
1057
1058         mutex_lock(&adapter->reset_lock);
1059
1060         if (adapter->state != VNIC_CLOSED) {
1061                 rc = ibmvnic_login(netdev);
1062                 if (rc) {
1063                         mutex_unlock(&adapter->reset_lock);
1064                         return rc;
1065                 }
1066
1067                 rc = init_resources(adapter);
1068                 if (rc) {
1069                         netdev_err(netdev, "failed to initialize resources\n");
1070                         release_resources(adapter);
1071                         mutex_unlock(&adapter->reset_lock);
1072                         return rc;
1073                 }
1074         }
1075
1076         rc = __ibmvnic_open(netdev);
1077         netif_carrier_on(netdev);
1078
1079         mutex_unlock(&adapter->reset_lock);
1080
1081         return rc;
1082 }
1083
1084 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1085 {
1086         struct ibmvnic_rx_pool *rx_pool;
1087         struct ibmvnic_rx_buff *rx_buff;
1088         u64 rx_entries;
1089         int rx_scrqs;
1090         int i, j;
1091
1092         if (!adapter->rx_pool)
1093                 return;
1094
1095         rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1096         rx_entries = adapter->req_rx_add_entries_per_subcrq;
1097
1098         /* Free any remaining skbs in the rx buffer pools */
1099         for (i = 0; i < rx_scrqs; i++) {
1100                 rx_pool = &adapter->rx_pool[i];
1101                 if (!rx_pool || !rx_pool->rx_buff)
1102                         continue;
1103
1104                 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1105                 for (j = 0; j < rx_entries; j++) {
1106                         rx_buff = &rx_pool->rx_buff[j];
1107                         if (rx_buff && rx_buff->skb) {
1108                                 dev_kfree_skb_any(rx_buff->skb);
1109                                 rx_buff->skb = NULL;
1110                         }
1111                 }
1112         }
1113 }
1114
1115 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1116 {
1117         struct ibmvnic_tx_pool *tx_pool;
1118         struct ibmvnic_tx_buff *tx_buff;
1119         u64 tx_entries;
1120         int tx_scrqs;
1121         int i, j;
1122
1123         if (!adapter->tx_pool)
1124                 return;
1125
1126         tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1127         tx_entries = adapter->req_tx_entries_per_subcrq;
1128
1129         /* Free any remaining skbs in the tx buffer pools */
1130         for (i = 0; i < tx_scrqs; i++) {
1131                 tx_pool = &adapter->tx_pool[i];
1132                 if (!tx_pool && !tx_pool->tx_buff)
1133                         continue;
1134
1135                 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1136                 for (j = 0; j < tx_entries; j++) {
1137                         tx_buff = &tx_pool->tx_buff[j];
1138                         if (tx_buff && tx_buff->skb) {
1139                                 dev_kfree_skb_any(tx_buff->skb);
1140                                 tx_buff->skb = NULL;
1141                         }
1142                 }
1143         }
1144 }
1145
1146 static int __ibmvnic_close(struct net_device *netdev)
1147 {
1148         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1149         int rc = 0;
1150         int i;
1151
1152         adapter->state = VNIC_CLOSING;
1153
1154         /* ensure that transmissions are stopped if called by do_reset */
1155         if (adapter->resetting)
1156                 netif_tx_disable(netdev);
1157         else
1158                 netif_tx_stop_all_queues(netdev);
1159
1160         ibmvnic_napi_disable(adapter);
1161
1162         if (adapter->tx_scrq) {
1163                 for (i = 0; i < adapter->req_tx_queues; i++)
1164                         if (adapter->tx_scrq[i]->irq) {
1165                                 netdev_dbg(netdev,
1166                                            "Disabling tx_scrq[%d] irq\n", i);
1167                                 disable_irq(adapter->tx_scrq[i]->irq);
1168                         }
1169         }
1170
1171         rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1172         if (rc)
1173                 return rc;
1174
1175         if (adapter->rx_scrq) {
1176                 for (i = 0; i < adapter->req_rx_queues; i++) {
1177                         if (adapter->rx_scrq[i]->irq) {
1178                                 netdev_dbg(netdev,
1179                                            "Disabling rx_scrq[%d] irq\n", i);
1180                                 disable_irq(adapter->rx_scrq[i]->irq);
1181                         }
1182                 }
1183         }
1184         clean_rx_pools(adapter);
1185         clean_tx_pools(adapter);
1186         adapter->state = VNIC_CLOSED;
1187         return rc;
1188 }
1189
1190 static int ibmvnic_close(struct net_device *netdev)
1191 {
1192         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1193         int rc;
1194
1195         mutex_lock(&adapter->reset_lock);
1196         rc = __ibmvnic_close(netdev);
1197         mutex_unlock(&adapter->reset_lock);
1198
1199         return rc;
1200 }
1201
1202 /**
1203  * build_hdr_data - creates L2/L3/L4 header data buffer
1204  * @hdr_field - bitfield determining needed headers
1205  * @skb - socket buffer
1206  * @hdr_len - array of header lengths
1207  * @tot_len - total length of data
1208  *
1209  * Reads hdr_field to determine which headers are needed by firmware.
1210  * Builds a buffer containing these headers.  Saves individual header
1211  * lengths and total buffer length to be used to build descriptors.
1212  */
1213 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1214                           int *hdr_len, u8 *hdr_data)
1215 {
1216         int len = 0;
1217         u8 *hdr;
1218
1219         hdr_len[0] = sizeof(struct ethhdr);
1220
1221         if (skb->protocol == htons(ETH_P_IP)) {
1222                 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1223                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1224                         hdr_len[2] = tcp_hdrlen(skb);
1225                 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1226                         hdr_len[2] = sizeof(struct udphdr);
1227         } else if (skb->protocol == htons(ETH_P_IPV6)) {
1228                 hdr_len[1] = sizeof(struct ipv6hdr);
1229                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1230                         hdr_len[2] = tcp_hdrlen(skb);
1231                 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1232                         hdr_len[2] = sizeof(struct udphdr);
1233         } else if (skb->protocol == htons(ETH_P_ARP)) {
1234                 hdr_len[1] = arp_hdr_len(skb->dev);
1235                 hdr_len[2] = 0;
1236         }
1237
1238         memset(hdr_data, 0, 120);
1239         if ((hdr_field >> 6) & 1) {
1240                 hdr = skb_mac_header(skb);
1241                 memcpy(hdr_data, hdr, hdr_len[0]);
1242                 len += hdr_len[0];
1243         }
1244
1245         if ((hdr_field >> 5) & 1) {
1246                 hdr = skb_network_header(skb);
1247                 memcpy(hdr_data + len, hdr, hdr_len[1]);
1248                 len += hdr_len[1];
1249         }
1250
1251         if ((hdr_field >> 4) & 1) {
1252                 hdr = skb_transport_header(skb);
1253                 memcpy(hdr_data + len, hdr, hdr_len[2]);
1254                 len += hdr_len[2];
1255         }
1256         return len;
1257 }
1258
1259 /**
1260  * create_hdr_descs - create header and header extension descriptors
1261  * @hdr_field - bitfield determining needed headers
1262  * @data - buffer containing header data
1263  * @len - length of data buffer
1264  * @hdr_len - array of individual header lengths
1265  * @scrq_arr - descriptor array
1266  *
1267  * Creates header and, if needed, header extension descriptors and
1268  * places them in a descriptor array, scrq_arr
1269  */
1270
1271 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1272                             union sub_crq *scrq_arr)
1273 {
1274         union sub_crq hdr_desc;
1275         int tmp_len = len;
1276         int num_descs = 0;
1277         u8 *data, *cur;
1278         int tmp;
1279
1280         while (tmp_len > 0) {
1281                 cur = hdr_data + len - tmp_len;
1282
1283                 memset(&hdr_desc, 0, sizeof(hdr_desc));
1284                 if (cur != hdr_data) {
1285                         data = hdr_desc.hdr_ext.data;
1286                         tmp = tmp_len > 29 ? 29 : tmp_len;
1287                         hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1288                         hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1289                         hdr_desc.hdr_ext.len = tmp;
1290                 } else {
1291                         data = hdr_desc.hdr.data;
1292                         tmp = tmp_len > 24 ? 24 : tmp_len;
1293                         hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1294                         hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1295                         hdr_desc.hdr.len = tmp;
1296                         hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1297                         hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1298                         hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1299                         hdr_desc.hdr.flag = hdr_field << 1;
1300                 }
1301                 memcpy(data, cur, tmp);
1302                 tmp_len -= tmp;
1303                 *scrq_arr = hdr_desc;
1304                 scrq_arr++;
1305                 num_descs++;
1306         }
1307
1308         return num_descs;
1309 }
1310
1311 /**
1312  * build_hdr_descs_arr - build a header descriptor array
1313  * @skb - socket buffer
1314  * @num_entries - number of descriptors to be sent
1315  * @subcrq - first TX descriptor
1316  * @hdr_field - bit field determining which headers will be sent
1317  *
1318  * This function will build a TX descriptor array with applicable
1319  * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1320  */
1321
1322 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1323                                 int *num_entries, u8 hdr_field)
1324 {
1325         int hdr_len[3] = {0, 0, 0};
1326         int tot_len;
1327         u8 *hdr_data = txbuff->hdr_data;
1328
1329         tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1330                                  txbuff->hdr_data);
1331         *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1332                          txbuff->indir_arr + 1);
1333 }
1334
1335 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1336 {
1337         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1338         int queue_num = skb_get_queue_mapping(skb);
1339         u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1340         struct device *dev = &adapter->vdev->dev;
1341         struct ibmvnic_tx_buff *tx_buff = NULL;
1342         struct ibmvnic_sub_crq_queue *tx_scrq;
1343         struct ibmvnic_tx_pool *tx_pool;
1344         unsigned int tx_send_failed = 0;
1345         unsigned int tx_map_failed = 0;
1346         unsigned int tx_dropped = 0;
1347         unsigned int tx_packets = 0;
1348         unsigned int tx_bytes = 0;
1349         dma_addr_t data_dma_addr;
1350         struct netdev_queue *txq;
1351         unsigned long lpar_rc;
1352         union sub_crq tx_crq;
1353         unsigned int offset;
1354         int num_entries = 1;
1355         unsigned char *dst;
1356         u64 *handle_array;
1357         int index = 0;
1358         u8 proto = 0;
1359         int ret = 0;
1360
1361         if (adapter->resetting) {
1362                 if (!netif_subqueue_stopped(netdev, skb))
1363                         netif_stop_subqueue(netdev, queue_num);
1364                 dev_kfree_skb_any(skb);
1365
1366                 tx_send_failed++;
1367                 tx_dropped++;
1368                 ret = NETDEV_TX_OK;
1369                 goto out;
1370         }
1371
1372         tx_pool = &adapter->tx_pool[queue_num];
1373         tx_scrq = adapter->tx_scrq[queue_num];
1374         txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1375         handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1376                 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1377
1378         index = tx_pool->free_map[tx_pool->consumer_index];
1379
1380         if (skb_is_gso(skb)) {
1381                 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1382                 dst = tx_pool->tso_ltb.buff + offset;
1383                 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1384                 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1385                 tx_pool->tso_index++;
1386                 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1387                         tx_pool->tso_index = 0;
1388         } else {
1389                 offset = index * adapter->req_mtu;
1390                 dst = tx_pool->long_term_buff.buff + offset;
1391                 memset(dst, 0, adapter->req_mtu);
1392                 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1393         }
1394
1395         if (skb_shinfo(skb)->nr_frags) {
1396                 int cur, i;
1397
1398                 /* Copy the head */
1399                 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1400                 cur = skb_headlen(skb);
1401
1402                 /* Copy the frags */
1403                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1404                         const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1405
1406                         memcpy(dst + cur,
1407                                page_address(skb_frag_page(frag)) +
1408                                frag->page_offset, skb_frag_size(frag));
1409                         cur += skb_frag_size(frag);
1410                 }
1411         } else {
1412                 skb_copy_from_linear_data(skb, dst, skb->len);
1413         }
1414
1415         tx_pool->consumer_index =
1416             (tx_pool->consumer_index + 1) %
1417                 adapter->req_tx_entries_per_subcrq;
1418
1419         tx_buff = &tx_pool->tx_buff[index];
1420         tx_buff->skb = skb;
1421         tx_buff->data_dma[0] = data_dma_addr;
1422         tx_buff->data_len[0] = skb->len;
1423         tx_buff->index = index;
1424         tx_buff->pool_index = queue_num;
1425         tx_buff->last_frag = true;
1426
1427         memset(&tx_crq, 0, sizeof(tx_crq));
1428         tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1429         tx_crq.v1.type = IBMVNIC_TX_DESC;
1430         tx_crq.v1.n_crq_elem = 1;
1431         tx_crq.v1.n_sge = 1;
1432         tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1433         tx_crq.v1.correlator = cpu_to_be32(index);
1434         if (skb_is_gso(skb))
1435                 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1436         else
1437                 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1438         tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1439         tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1440
1441         if (adapter->vlan_header_insertion) {
1442                 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1443                 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1444         }
1445
1446         if (skb->protocol == htons(ETH_P_IP)) {
1447                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1448                 proto = ip_hdr(skb)->protocol;
1449         } else if (skb->protocol == htons(ETH_P_IPV6)) {
1450                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1451                 proto = ipv6_hdr(skb)->nexthdr;
1452         }
1453
1454         if (proto == IPPROTO_TCP)
1455                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1456         else if (proto == IPPROTO_UDP)
1457                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1458
1459         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1460                 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1461                 hdrs += 2;
1462         }
1463         if (skb_is_gso(skb)) {
1464                 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1465                 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1466                 hdrs += 2;
1467         }
1468         /* determine if l2/3/4 headers are sent to firmware */
1469         if ((*hdrs >> 7) & 1) {
1470                 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1471                 tx_crq.v1.n_crq_elem = num_entries;
1472                 tx_buff->num_entries = num_entries;
1473                 tx_buff->indir_arr[0] = tx_crq;
1474                 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1475                                                     sizeof(tx_buff->indir_arr),
1476                                                     DMA_TO_DEVICE);
1477                 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1478                         dev_kfree_skb_any(skb);
1479                         tx_buff->skb = NULL;
1480                         if (!firmware_has_feature(FW_FEATURE_CMO))
1481                                 dev_err(dev, "tx: unable to map descriptor array\n");
1482                         tx_map_failed++;
1483                         tx_dropped++;
1484                         ret = NETDEV_TX_OK;
1485                         goto out;
1486                 }
1487                 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1488                                                (u64)tx_buff->indir_dma,
1489                                                (u64)num_entries);
1490         } else {
1491                 tx_buff->num_entries = num_entries;
1492                 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1493                                       &tx_crq);
1494         }
1495         if (lpar_rc != H_SUCCESS) {
1496                 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1497
1498                 if (tx_pool->consumer_index == 0)
1499                         tx_pool->consumer_index =
1500                                 adapter->req_tx_entries_per_subcrq - 1;
1501                 else
1502                         tx_pool->consumer_index--;
1503
1504                 dev_kfree_skb_any(skb);
1505                 tx_buff->skb = NULL;
1506
1507                 if (lpar_rc == H_CLOSED) {
1508                         /* Disable TX and report carrier off if queue is closed.
1509                          * Firmware guarantees that a signal will be sent to the
1510                          * driver, triggering a reset or some other action.
1511                          */
1512                         netif_tx_stop_all_queues(netdev);
1513                         netif_carrier_off(netdev);
1514                 }
1515
1516                 tx_send_failed++;
1517                 tx_dropped++;
1518                 ret = NETDEV_TX_OK;
1519                 goto out;
1520         }
1521
1522         if (atomic_add_return(num_entries, &tx_scrq->used)
1523                                         >= adapter->req_tx_entries_per_subcrq) {
1524                 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1525                 netif_stop_subqueue(netdev, queue_num);
1526         }
1527
1528         tx_packets++;
1529         tx_bytes += skb->len;
1530         txq->trans_start = jiffies;
1531         ret = NETDEV_TX_OK;
1532
1533 out:
1534         netdev->stats.tx_dropped += tx_dropped;
1535         netdev->stats.tx_bytes += tx_bytes;
1536         netdev->stats.tx_packets += tx_packets;
1537         adapter->tx_send_failed += tx_send_failed;
1538         adapter->tx_map_failed += tx_map_failed;
1539         adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1540         adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1541         adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1542
1543         return ret;
1544 }
1545
1546 static void ibmvnic_set_multi(struct net_device *netdev)
1547 {
1548         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1549         struct netdev_hw_addr *ha;
1550         union ibmvnic_crq crq;
1551
1552         memset(&crq, 0, sizeof(crq));
1553         crq.request_capability.first = IBMVNIC_CRQ_CMD;
1554         crq.request_capability.cmd = REQUEST_CAPABILITY;
1555
1556         if (netdev->flags & IFF_PROMISC) {
1557                 if (!adapter->promisc_supported)
1558                         return;
1559         } else {
1560                 if (netdev->flags & IFF_ALLMULTI) {
1561                         /* Accept all multicast */
1562                         memset(&crq, 0, sizeof(crq));
1563                         crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1564                         crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1565                         crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1566                         ibmvnic_send_crq(adapter, &crq);
1567                 } else if (netdev_mc_empty(netdev)) {
1568                         /* Reject all multicast */
1569                         memset(&crq, 0, sizeof(crq));
1570                         crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1571                         crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1572                         crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1573                         ibmvnic_send_crq(adapter, &crq);
1574                 } else {
1575                         /* Accept one or more multicast(s) */
1576                         netdev_for_each_mc_addr(ha, netdev) {
1577                                 memset(&crq, 0, sizeof(crq));
1578                                 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1579                                 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1580                                 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1581                                 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1582                                                 ha->addr);
1583                                 ibmvnic_send_crq(adapter, &crq);
1584                         }
1585                 }
1586         }
1587 }
1588
1589 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1590 {
1591         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1592         struct sockaddr *addr = p;
1593         union ibmvnic_crq crq;
1594
1595         if (!is_valid_ether_addr(addr->sa_data))
1596                 return -EADDRNOTAVAIL;
1597
1598         memset(&crq, 0, sizeof(crq));
1599         crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1600         crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1601         ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1602
1603         init_completion(&adapter->fw_done);
1604         ibmvnic_send_crq(adapter, &crq);
1605         wait_for_completion(&adapter->fw_done);
1606         /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1607         return adapter->fw_done_rc ? -EIO : 0;
1608 }
1609
1610 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1611 {
1612         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1613         struct sockaddr *addr = p;
1614         int rc;
1615
1616         if (adapter->state == VNIC_PROBED) {
1617                 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1618                 adapter->mac_change_pending = true;
1619                 return 0;
1620         }
1621
1622         rc = __ibmvnic_set_mac(netdev, addr);
1623
1624         return rc;
1625 }
1626
1627 /**
1628  * do_reset returns zero if we are able to keep processing reset events, or
1629  * non-zero if we hit a fatal error and must halt.
1630  */
1631 static int do_reset(struct ibmvnic_adapter *adapter,
1632                     struct ibmvnic_rwi *rwi, u32 reset_state)
1633 {
1634         u64 old_num_rx_queues, old_num_tx_queues;
1635         struct net_device *netdev = adapter->netdev;
1636         int i, rc;
1637
1638         netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1639                    rwi->reset_reason);
1640
1641         netif_carrier_off(netdev);
1642         adapter->reset_reason = rwi->reset_reason;
1643
1644         old_num_rx_queues = adapter->req_rx_queues;
1645         old_num_tx_queues = adapter->req_tx_queues;
1646
1647         if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1648                 rc = ibmvnic_reenable_crq_queue(adapter);
1649                 if (rc)
1650                         return 0;
1651         }
1652
1653         rc = __ibmvnic_close(netdev);
1654         if (rc)
1655                 return rc;
1656
1657         if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1658             adapter->wait_for_reset) {
1659                 release_resources(adapter);
1660                 release_sub_crqs(adapter, 1);
1661                 release_crq_queue(adapter);
1662         }
1663
1664         if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1665                 /* remove the closed state so when we call open it appears
1666                  * we are coming from the probed state.
1667                  */
1668                 adapter->state = VNIC_PROBED;
1669
1670                 rc = ibmvnic_init(adapter);
1671                 if (rc)
1672                         return IBMVNIC_INIT_FAILED;
1673
1674                 /* If the adapter was in PROBE state prior to the reset,
1675                  * exit here.
1676                  */
1677                 if (reset_state == VNIC_PROBED)
1678                         return 0;
1679
1680                 rc = ibmvnic_login(netdev);
1681                 if (rc) {
1682                         adapter->state = VNIC_PROBED;
1683                         return 0;
1684                 }
1685
1686                 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1687                     adapter->wait_for_reset) {
1688                         rc = init_resources(adapter);
1689                         if (rc)
1690                                 return rc;
1691                 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1692                            adapter->req_tx_queues != old_num_tx_queues) {
1693                         adapter->map_id = 1;
1694                         release_rx_pools(adapter);
1695                         release_tx_pools(adapter);
1696                         init_rx_pools(netdev);
1697                         init_tx_pools(netdev);
1698
1699                         release_napi(adapter);
1700                         init_napi(adapter);
1701                 } else {
1702                         rc = reset_tx_pools(adapter);
1703                         if (rc)
1704                                 return rc;
1705
1706                         rc = reset_rx_pools(adapter);
1707                         if (rc)
1708                                 return rc;
1709
1710                         if (reset_state == VNIC_CLOSED)
1711                                 return 0;
1712                 }
1713         }
1714
1715         rc = __ibmvnic_open(netdev);
1716         if (rc) {
1717                 if (list_empty(&adapter->rwi_list))
1718                         adapter->state = VNIC_CLOSED;
1719                 else
1720                         adapter->state = reset_state;
1721
1722                 return 0;
1723         }
1724
1725         /* kick napi */
1726         for (i = 0; i < adapter->req_rx_queues; i++)
1727                 napi_schedule(&adapter->napi[i]);
1728
1729         if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1730                 netdev_notify_peers(netdev);
1731
1732         netif_carrier_on(netdev);
1733
1734         return 0;
1735 }
1736
1737 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1738 {
1739         struct ibmvnic_rwi *rwi;
1740
1741         mutex_lock(&adapter->rwi_lock);
1742
1743         if (!list_empty(&adapter->rwi_list)) {
1744                 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1745                                        list);
1746                 list_del(&rwi->list);
1747         } else {
1748                 rwi = NULL;
1749         }
1750
1751         mutex_unlock(&adapter->rwi_lock);
1752         return rwi;
1753 }
1754
1755 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1756 {
1757         struct ibmvnic_rwi *rwi;
1758
1759         rwi = get_next_rwi(adapter);
1760         while (rwi) {
1761                 kfree(rwi);
1762                 rwi = get_next_rwi(adapter);
1763         }
1764 }
1765
1766 static void __ibmvnic_reset(struct work_struct *work)
1767 {
1768         struct ibmvnic_rwi *rwi;
1769         struct ibmvnic_adapter *adapter;
1770         struct net_device *netdev;
1771         u32 reset_state;
1772         int rc = 0;
1773
1774         adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1775         netdev = adapter->netdev;
1776
1777         mutex_lock(&adapter->reset_lock);
1778         adapter->resetting = true;
1779         reset_state = adapter->state;
1780
1781         rwi = get_next_rwi(adapter);
1782         while (rwi) {
1783                 rc = do_reset(adapter, rwi, reset_state);
1784                 kfree(rwi);
1785                 if (rc && rc != IBMVNIC_INIT_FAILED)
1786                         break;
1787
1788                 rwi = get_next_rwi(adapter);
1789         }
1790
1791         if (adapter->wait_for_reset) {
1792                 adapter->wait_for_reset = false;
1793                 adapter->reset_done_rc = rc;
1794                 complete(&adapter->reset_done);
1795         }
1796
1797         if (rc) {
1798                 netdev_dbg(adapter->netdev, "Reset failed\n");
1799                 free_all_rwi(adapter);
1800                 mutex_unlock(&adapter->reset_lock);
1801                 return;
1802         }
1803
1804         adapter->resetting = false;
1805         mutex_unlock(&adapter->reset_lock);
1806 }
1807
1808 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1809                           enum ibmvnic_reset_reason reason)
1810 {
1811         struct ibmvnic_rwi *rwi, *tmp;
1812         struct net_device *netdev = adapter->netdev;
1813         struct list_head *entry;
1814
1815         if (adapter->state == VNIC_REMOVING ||
1816             adapter->state == VNIC_REMOVED) {
1817                 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1818                 return;
1819         }
1820
1821         if (adapter->state == VNIC_PROBING) {
1822                 netdev_warn(netdev, "Adapter reset during probe\n");
1823                 adapter->init_done_rc = EAGAIN;
1824                 return;
1825         }
1826
1827         mutex_lock(&adapter->rwi_lock);
1828
1829         list_for_each(entry, &adapter->rwi_list) {
1830                 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1831                 if (tmp->reset_reason == reason) {
1832                         netdev_dbg(netdev, "Skipping matching reset\n");
1833                         mutex_unlock(&adapter->rwi_lock);
1834                         return;
1835                 }
1836         }
1837
1838         rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1839         if (!rwi) {
1840                 mutex_unlock(&adapter->rwi_lock);
1841                 ibmvnic_close(netdev);
1842                 return;
1843         }
1844
1845         rwi->reset_reason = reason;
1846         list_add_tail(&rwi->list, &adapter->rwi_list);
1847         mutex_unlock(&adapter->rwi_lock);
1848
1849         netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1850         schedule_work(&adapter->ibmvnic_reset);
1851 }
1852
1853 static void ibmvnic_tx_timeout(struct net_device *dev)
1854 {
1855         struct ibmvnic_adapter *adapter = netdev_priv(dev);
1856
1857         ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1858 }
1859
1860 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1861                                   struct ibmvnic_rx_buff *rx_buff)
1862 {
1863         struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1864
1865         rx_buff->skb = NULL;
1866
1867         pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1868         pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1869
1870         atomic_dec(&pool->available);
1871 }
1872
1873 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1874 {
1875         struct net_device *netdev = napi->dev;
1876         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1877         int scrq_num = (int)(napi - adapter->napi);
1878         int frames_processed = 0;
1879
1880 restart_poll:
1881         while (frames_processed < budget) {
1882                 struct sk_buff *skb;
1883                 struct ibmvnic_rx_buff *rx_buff;
1884                 union sub_crq *next;
1885                 u32 length;
1886                 u16 offset;
1887                 u8 flags = 0;
1888
1889                 if (unlikely(adapter->resetting &&
1890                              adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1891                         enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1892                         napi_complete_done(napi, frames_processed);
1893                         return frames_processed;
1894                 }
1895
1896                 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1897                         break;
1898                 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1899                 rx_buff =
1900                     (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1901                                                           rx_comp.correlator);
1902                 /* do error checking */
1903                 if (next->rx_comp.rc) {
1904                         netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1905                                    be16_to_cpu(next->rx_comp.rc));
1906                         /* free the entry */
1907                         next->rx_comp.first = 0;
1908                         dev_kfree_skb_any(rx_buff->skb);
1909                         remove_buff_from_pool(adapter, rx_buff);
1910                         continue;
1911                 } else if (!rx_buff->skb) {
1912                         /* free the entry */
1913                         next->rx_comp.first = 0;
1914                         remove_buff_from_pool(adapter, rx_buff);
1915                         continue;
1916                 }
1917
1918                 length = be32_to_cpu(next->rx_comp.len);
1919                 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1920                 flags = next->rx_comp.flags;
1921                 skb = rx_buff->skb;
1922                 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1923                                         length);
1924
1925                 /* VLAN Header has been stripped by the system firmware and
1926                  * needs to be inserted by the driver
1927                  */
1928                 if (adapter->rx_vlan_header_insertion &&
1929                     (flags & IBMVNIC_VLAN_STRIPPED))
1930                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1931                                                ntohs(next->rx_comp.vlan_tci));
1932
1933                 /* free the entry */
1934                 next->rx_comp.first = 0;
1935                 remove_buff_from_pool(adapter, rx_buff);
1936
1937                 skb_put(skb, length);
1938                 skb->protocol = eth_type_trans(skb, netdev);
1939                 skb_record_rx_queue(skb, scrq_num);
1940
1941                 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1942                     flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1943                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1944                 }
1945
1946                 length = skb->len;
1947                 napi_gro_receive(napi, skb); /* send it up */
1948                 netdev->stats.rx_packets++;
1949                 netdev->stats.rx_bytes += length;
1950                 adapter->rx_stats_buffers[scrq_num].packets++;
1951                 adapter->rx_stats_buffers[scrq_num].bytes += length;
1952                 frames_processed++;
1953         }
1954
1955         if (adapter->state != VNIC_CLOSING)
1956                 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1957
1958         if (frames_processed < budget) {
1959                 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1960                 napi_complete_done(napi, frames_processed);
1961                 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1962                     napi_reschedule(napi)) {
1963                         disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1964                         goto restart_poll;
1965                 }
1966         }
1967         return frames_processed;
1968 }
1969
1970 #ifdef CONFIG_NET_POLL_CONTROLLER
1971 static void ibmvnic_netpoll_controller(struct net_device *dev)
1972 {
1973         struct ibmvnic_adapter *adapter = netdev_priv(dev);
1974         int i;
1975
1976         replenish_pools(netdev_priv(dev));
1977         for (i = 0; i < adapter->req_rx_queues; i++)
1978                 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1979                                      adapter->rx_scrq[i]);
1980 }
1981 #endif
1982
1983 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1984 {
1985         adapter->fallback.mtu = adapter->req_mtu;
1986         adapter->fallback.rx_queues = adapter->req_rx_queues;
1987         adapter->fallback.tx_queues = adapter->req_tx_queues;
1988         adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1989         adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1990
1991         init_completion(&adapter->reset_done);
1992         ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1993         adapter->wait_for_reset = true;
1994         wait_for_completion(&adapter->reset_done);
1995
1996         if (adapter->reset_done_rc) {
1997                 adapter->desired.mtu = adapter->fallback.mtu;
1998                 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1999                 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2000                 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2001                 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2002
2003                 init_completion(&adapter->reset_done);
2004                 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2005                 wait_for_completion(&adapter->reset_done);
2006         }
2007         adapter->wait_for_reset = false;
2008
2009         return adapter->reset_done_rc;
2010 }
2011
2012 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2013 {
2014         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2015
2016         adapter->desired.mtu = new_mtu + ETH_HLEN;
2017
2018         return wait_for_reset(adapter);
2019 }
2020
2021 static const struct net_device_ops ibmvnic_netdev_ops = {
2022         .ndo_open               = ibmvnic_open,
2023         .ndo_stop               = ibmvnic_close,
2024         .ndo_start_xmit         = ibmvnic_xmit,
2025         .ndo_set_rx_mode        = ibmvnic_set_multi,
2026         .ndo_set_mac_address    = ibmvnic_set_mac,
2027         .ndo_validate_addr      = eth_validate_addr,
2028         .ndo_tx_timeout         = ibmvnic_tx_timeout,
2029 #ifdef CONFIG_NET_POLL_CONTROLLER
2030         .ndo_poll_controller    = ibmvnic_netpoll_controller,
2031 #endif
2032         .ndo_change_mtu         = ibmvnic_change_mtu,
2033 };
2034
2035 /* ethtool functions */
2036
2037 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2038                                       struct ethtool_link_ksettings *cmd)
2039 {
2040         u32 supported, advertising;
2041
2042         supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2043                           SUPPORTED_FIBRE);
2044         advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2045                             ADVERTISED_FIBRE);
2046         cmd->base.speed = SPEED_1000;
2047         cmd->base.duplex = DUPLEX_FULL;
2048         cmd->base.port = PORT_FIBRE;
2049         cmd->base.phy_address = 0;
2050         cmd->base.autoneg = AUTONEG_ENABLE;
2051
2052         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2053                                                 supported);
2054         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2055                                                 advertising);
2056
2057         return 0;
2058 }
2059
2060 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2061                                 struct ethtool_drvinfo *info)
2062 {
2063         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2064
2065         strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2066         strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2067         strlcpy(info->fw_version, adapter->fw_version,
2068                 sizeof(info->fw_version));
2069 }
2070
2071 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2072 {
2073         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2074
2075         return adapter->msg_enable;
2076 }
2077
2078 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2079 {
2080         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2081
2082         adapter->msg_enable = data;
2083 }
2084
2085 static u32 ibmvnic_get_link(struct net_device *netdev)
2086 {
2087         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2088
2089         /* Don't need to send a query because we request a logical link up at
2090          * init and then we wait for link state indications
2091          */
2092         return adapter->logical_link_state;
2093 }
2094
2095 static void ibmvnic_get_ringparam(struct net_device *netdev,
2096                                   struct ethtool_ringparam *ring)
2097 {
2098         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2099
2100         ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2101         ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2102         ring->rx_mini_max_pending = 0;
2103         ring->rx_jumbo_max_pending = 0;
2104         ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2105         ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2106         ring->rx_mini_pending = 0;
2107         ring->rx_jumbo_pending = 0;
2108 }
2109
2110 static int ibmvnic_set_ringparam(struct net_device *netdev,
2111                                  struct ethtool_ringparam *ring)
2112 {
2113         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2114
2115         if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq  ||
2116             ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2117                 netdev_err(netdev, "Invalid request.\n");
2118                 netdev_err(netdev, "Max tx buffers = %llu\n",
2119                            adapter->max_rx_add_entries_per_subcrq);
2120                 netdev_err(netdev, "Max rx buffers = %llu\n",
2121                            adapter->max_tx_entries_per_subcrq);
2122                 return -EINVAL;
2123         }
2124
2125         adapter->desired.rx_entries = ring->rx_pending;
2126         adapter->desired.tx_entries = ring->tx_pending;
2127
2128         return wait_for_reset(adapter);
2129 }
2130
2131 static void ibmvnic_get_channels(struct net_device *netdev,
2132                                  struct ethtool_channels *channels)
2133 {
2134         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2135
2136         channels->max_rx = adapter->max_rx_queues;
2137         channels->max_tx = adapter->max_tx_queues;
2138         channels->max_other = 0;
2139         channels->max_combined = 0;
2140         channels->rx_count = adapter->req_rx_queues;
2141         channels->tx_count = adapter->req_tx_queues;
2142         channels->other_count = 0;
2143         channels->combined_count = 0;
2144 }
2145
2146 static int ibmvnic_set_channels(struct net_device *netdev,
2147                                 struct ethtool_channels *channels)
2148 {
2149         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2150
2151         adapter->desired.rx_queues = channels->rx_count;
2152         adapter->desired.tx_queues = channels->tx_count;
2153
2154         return wait_for_reset(adapter);
2155 }
2156
2157 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2158 {
2159         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2160         int i;
2161
2162         if (stringset != ETH_SS_STATS)
2163                 return;
2164
2165         for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2166                 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2167
2168         for (i = 0; i < adapter->req_tx_queues; i++) {
2169                 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2170                 data += ETH_GSTRING_LEN;
2171
2172                 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2173                 data += ETH_GSTRING_LEN;
2174
2175                 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2176                 data += ETH_GSTRING_LEN;
2177         }
2178
2179         for (i = 0; i < adapter->req_rx_queues; i++) {
2180                 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2181                 data += ETH_GSTRING_LEN;
2182
2183                 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2184                 data += ETH_GSTRING_LEN;
2185
2186                 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2187                 data += ETH_GSTRING_LEN;
2188         }
2189 }
2190
2191 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2192 {
2193         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2194
2195         switch (sset) {
2196         case ETH_SS_STATS:
2197                 return ARRAY_SIZE(ibmvnic_stats) +
2198                        adapter->req_tx_queues * NUM_TX_STATS +
2199                        adapter->req_rx_queues * NUM_RX_STATS;
2200         default:
2201                 return -EOPNOTSUPP;
2202         }
2203 }
2204
2205 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2206                                       struct ethtool_stats *stats, u64 *data)
2207 {
2208         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2209         union ibmvnic_crq crq;
2210         int i, j;
2211
2212         memset(&crq, 0, sizeof(crq));
2213         crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2214         crq.request_statistics.cmd = REQUEST_STATISTICS;
2215         crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2216         crq.request_statistics.len =
2217             cpu_to_be32(sizeof(struct ibmvnic_statistics));
2218
2219         /* Wait for data to be written */
2220         init_completion(&adapter->stats_done);
2221         ibmvnic_send_crq(adapter, &crq);
2222         wait_for_completion(&adapter->stats_done);
2223
2224         for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2225                 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2226                                                 ibmvnic_stats[i].offset));
2227
2228         for (j = 0; j < adapter->req_tx_queues; j++) {
2229                 data[i] = adapter->tx_stats_buffers[j].packets;
2230                 i++;
2231                 data[i] = adapter->tx_stats_buffers[j].bytes;
2232                 i++;
2233                 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2234                 i++;
2235         }
2236
2237         for (j = 0; j < adapter->req_rx_queues; j++) {
2238                 data[i] = adapter->rx_stats_buffers[j].packets;
2239                 i++;
2240                 data[i] = adapter->rx_stats_buffers[j].bytes;
2241                 i++;
2242                 data[i] = adapter->rx_stats_buffers[j].interrupts;
2243                 i++;
2244         }
2245 }
2246
2247 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2248         .get_drvinfo            = ibmvnic_get_drvinfo,
2249         .get_msglevel           = ibmvnic_get_msglevel,
2250         .set_msglevel           = ibmvnic_set_msglevel,
2251         .get_link               = ibmvnic_get_link,
2252         .get_ringparam          = ibmvnic_get_ringparam,
2253         .set_ringparam          = ibmvnic_set_ringparam,
2254         .get_channels           = ibmvnic_get_channels,
2255         .set_channels           = ibmvnic_set_channels,
2256         .get_strings            = ibmvnic_get_strings,
2257         .get_sset_count         = ibmvnic_get_sset_count,
2258         .get_ethtool_stats      = ibmvnic_get_ethtool_stats,
2259         .get_link_ksettings     = ibmvnic_get_link_ksettings,
2260 };
2261
2262 /* Routines for managing CRQs/sCRQs  */
2263
2264 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2265                                    struct ibmvnic_sub_crq_queue *scrq)
2266 {
2267         int rc;
2268
2269         if (scrq->irq) {
2270                 free_irq(scrq->irq, scrq);
2271                 irq_dispose_mapping(scrq->irq);
2272                 scrq->irq = 0;
2273         }
2274
2275         memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2276         scrq->cur = 0;
2277
2278         rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2279                            4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2280         return rc;
2281 }
2282
2283 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2284 {
2285         int i, rc;
2286
2287         for (i = 0; i < adapter->req_tx_queues; i++) {
2288                 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2289                 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2290                 if (rc)
2291                         return rc;
2292         }
2293
2294         for (i = 0; i < adapter->req_rx_queues; i++) {
2295                 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2296                 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2297                 if (rc)
2298                         return rc;
2299         }
2300
2301         return rc;
2302 }
2303
2304 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2305                                   struct ibmvnic_sub_crq_queue *scrq,
2306                                   bool do_h_free)
2307 {
2308         struct device *dev = &adapter->vdev->dev;
2309         long rc;
2310
2311         netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2312
2313         if (do_h_free) {
2314                 /* Close the sub-crqs */
2315                 do {
2316                         rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2317                                                 adapter->vdev->unit_address,
2318                                                 scrq->crq_num);
2319                 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2320
2321                 if (rc) {
2322                         netdev_err(adapter->netdev,
2323                                    "Failed to release sub-CRQ %16lx, rc = %ld\n",
2324                                    scrq->crq_num, rc);
2325                 }
2326         }
2327
2328         dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2329                          DMA_BIDIRECTIONAL);
2330         free_pages((unsigned long)scrq->msgs, 2);
2331         kfree(scrq);
2332 }
2333
2334 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2335                                                         *adapter)
2336 {
2337         struct device *dev = &adapter->vdev->dev;
2338         struct ibmvnic_sub_crq_queue *scrq;
2339         int rc;
2340
2341         scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2342         if (!scrq)
2343                 return NULL;
2344
2345         scrq->msgs =
2346                 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2347         if (!scrq->msgs) {
2348                 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2349                 goto zero_page_failed;
2350         }
2351
2352         scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2353                                          DMA_BIDIRECTIONAL);
2354         if (dma_mapping_error(dev, scrq->msg_token)) {
2355                 dev_warn(dev, "Couldn't map crq queue messages page\n");
2356                 goto map_failed;
2357         }
2358
2359         rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2360                            4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2361
2362         if (rc == H_RESOURCE)
2363                 rc = ibmvnic_reset_crq(adapter);
2364
2365         if (rc == H_CLOSED) {
2366                 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2367         } else if (rc) {
2368                 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2369                 goto reg_failed;
2370         }
2371
2372         scrq->adapter = adapter;
2373         scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2374         spin_lock_init(&scrq->lock);
2375
2376         netdev_dbg(adapter->netdev,
2377                    "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2378                    scrq->crq_num, scrq->hw_irq, scrq->irq);
2379
2380         return scrq;
2381
2382 reg_failed:
2383         dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2384                          DMA_BIDIRECTIONAL);
2385 map_failed:
2386         free_pages((unsigned long)scrq->msgs, 2);
2387 zero_page_failed:
2388         kfree(scrq);
2389
2390         return NULL;
2391 }
2392
2393 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2394 {
2395         int i;
2396
2397         if (adapter->tx_scrq) {
2398                 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2399                         if (!adapter->tx_scrq[i])
2400                                 continue;
2401
2402                         netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2403                                    i);
2404                         if (adapter->tx_scrq[i]->irq) {
2405                                 free_irq(adapter->tx_scrq[i]->irq,
2406                                          adapter->tx_scrq[i]);
2407                                 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2408                                 adapter->tx_scrq[i]->irq = 0;
2409                         }
2410
2411                         release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2412                                               do_h_free);
2413                 }
2414
2415                 kfree(adapter->tx_scrq);
2416                 adapter->tx_scrq = NULL;
2417                 adapter->num_active_tx_scrqs = 0;
2418         }
2419
2420         if (adapter->rx_scrq) {
2421                 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2422                         if (!adapter->rx_scrq[i])
2423                                 continue;
2424
2425                         netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2426                                    i);
2427                         if (adapter->rx_scrq[i]->irq) {
2428                                 free_irq(adapter->rx_scrq[i]->irq,
2429                                          adapter->rx_scrq[i]);
2430                                 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2431                                 adapter->rx_scrq[i]->irq = 0;
2432                         }
2433
2434                         release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2435                                               do_h_free);
2436                 }
2437
2438                 kfree(adapter->rx_scrq);
2439                 adapter->rx_scrq = NULL;
2440                 adapter->num_active_rx_scrqs = 0;
2441         }
2442 }
2443
2444 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2445                             struct ibmvnic_sub_crq_queue *scrq)
2446 {
2447         struct device *dev = &adapter->vdev->dev;
2448         unsigned long rc;
2449
2450         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2451                                 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2452         if (rc)
2453                 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2454                         scrq->hw_irq, rc);
2455         return rc;
2456 }
2457
2458 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2459                            struct ibmvnic_sub_crq_queue *scrq)
2460 {
2461         struct device *dev = &adapter->vdev->dev;
2462         unsigned long rc;
2463
2464         if (scrq->hw_irq > 0x100000000ULL) {
2465                 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2466                 return 1;
2467         }
2468
2469         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2470                                 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2471         if (rc)
2472                 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2473                         scrq->hw_irq, rc);
2474         return rc;
2475 }
2476
2477 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2478                                struct ibmvnic_sub_crq_queue *scrq)
2479 {
2480         struct device *dev = &adapter->vdev->dev;
2481         struct ibmvnic_tx_buff *txbuff;
2482         union sub_crq *next;
2483         int index;
2484         int i, j;
2485         u8 first;
2486
2487 restart_loop:
2488         while (pending_scrq(adapter, scrq)) {
2489                 unsigned int pool = scrq->pool_index;
2490                 int num_entries = 0;
2491
2492                 next = ibmvnic_next_scrq(adapter, scrq);
2493                 for (i = 0; i < next->tx_comp.num_comps; i++) {
2494                         if (next->tx_comp.rcs[i]) {
2495                                 dev_err(dev, "tx error %x\n",
2496                                         next->tx_comp.rcs[i]);
2497                                 continue;
2498                         }
2499                         index = be32_to_cpu(next->tx_comp.correlators[i]);
2500                         txbuff = &adapter->tx_pool[pool].tx_buff[index];
2501
2502                         for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2503                                 if (!txbuff->data_dma[j])
2504                                         continue;
2505
2506                                 txbuff->data_dma[j] = 0;
2507                         }
2508                         /* if sub_crq was sent indirectly */
2509                         first = txbuff->indir_arr[0].generic.first;
2510                         if (first == IBMVNIC_CRQ_CMD) {
2511                                 dma_unmap_single(dev, txbuff->indir_dma,
2512                                                  sizeof(txbuff->indir_arr),
2513                                                  DMA_TO_DEVICE);
2514                         }
2515
2516                         if (txbuff->last_frag) {
2517                                 dev_kfree_skb_any(txbuff->skb);
2518                                 txbuff->skb = NULL;
2519                         }
2520
2521                         num_entries += txbuff->num_entries;
2522
2523                         adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2524                                                      producer_index] = index;
2525                         adapter->tx_pool[pool].producer_index =
2526                             (adapter->tx_pool[pool].producer_index + 1) %
2527                             adapter->req_tx_entries_per_subcrq;
2528                 }
2529                 /* remove tx_comp scrq*/
2530                 next->tx_comp.first = 0;
2531
2532                 if (atomic_sub_return(num_entries, &scrq->used) <=
2533                     (adapter->req_tx_entries_per_subcrq / 2) &&
2534                     __netif_subqueue_stopped(adapter->netdev,
2535                                              scrq->pool_index)) {
2536                         netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2537                         netdev_dbg(adapter->netdev, "Started queue %d\n",
2538                                    scrq->pool_index);
2539                 }
2540         }
2541
2542         enable_scrq_irq(adapter, scrq);
2543
2544         if (pending_scrq(adapter, scrq)) {
2545                 disable_scrq_irq(adapter, scrq);
2546                 goto restart_loop;
2547         }
2548
2549         return 0;
2550 }
2551
2552 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2553 {
2554         struct ibmvnic_sub_crq_queue *scrq = instance;
2555         struct ibmvnic_adapter *adapter = scrq->adapter;
2556
2557         disable_scrq_irq(adapter, scrq);
2558         ibmvnic_complete_tx(adapter, scrq);
2559
2560         return IRQ_HANDLED;
2561 }
2562
2563 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2564 {
2565         struct ibmvnic_sub_crq_queue *scrq = instance;
2566         struct ibmvnic_adapter *adapter = scrq->adapter;
2567
2568         /* When booting a kdump kernel we can hit pending interrupts
2569          * prior to completing driver initialization.
2570          */
2571         if (unlikely(adapter->state != VNIC_OPEN))
2572                 return IRQ_NONE;
2573
2574         adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2575
2576         if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2577                 disable_scrq_irq(adapter, scrq);
2578                 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2579         }
2580
2581         return IRQ_HANDLED;
2582 }
2583
2584 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2585 {
2586         struct device *dev = &adapter->vdev->dev;
2587         struct ibmvnic_sub_crq_queue *scrq;
2588         int i = 0, j = 0;
2589         int rc = 0;
2590
2591         for (i = 0; i < adapter->req_tx_queues; i++) {
2592                 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2593                            i);
2594                 scrq = adapter->tx_scrq[i];
2595                 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2596
2597                 if (!scrq->irq) {
2598                         rc = -EINVAL;
2599                         dev_err(dev, "Error mapping irq\n");
2600                         goto req_tx_irq_failed;
2601                 }
2602
2603                 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2604                                  0, "ibmvnic_tx", scrq);
2605
2606                 if (rc) {
2607                         dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2608                                 scrq->irq, rc);
2609                         irq_dispose_mapping(scrq->irq);
2610                         goto req_tx_irq_failed;
2611                 }
2612         }
2613
2614         for (i = 0; i < adapter->req_rx_queues; i++) {
2615                 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2616                            i);
2617                 scrq = adapter->rx_scrq[i];
2618                 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2619                 if (!scrq->irq) {
2620                         rc = -EINVAL;
2621                         dev_err(dev, "Error mapping irq\n");
2622                         goto req_rx_irq_failed;
2623                 }
2624                 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2625                                  0, "ibmvnic_rx", scrq);
2626                 if (rc) {
2627                         dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2628                                 scrq->irq, rc);
2629                         irq_dispose_mapping(scrq->irq);
2630                         goto req_rx_irq_failed;
2631                 }
2632         }
2633         return rc;
2634
2635 req_rx_irq_failed:
2636         for (j = 0; j < i; j++) {
2637                 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2638                 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2639         }
2640         i = adapter->req_tx_queues;
2641 req_tx_irq_failed:
2642         for (j = 0; j < i; j++) {
2643                 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2644                 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2645         }
2646         release_sub_crqs(adapter, 1);
2647         return rc;
2648 }
2649
2650 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2651 {
2652         struct device *dev = &adapter->vdev->dev;
2653         struct ibmvnic_sub_crq_queue **allqueues;
2654         int registered_queues = 0;
2655         int total_queues;
2656         int more = 0;
2657         int i;
2658
2659         total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2660
2661         allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2662         if (!allqueues)
2663                 return -1;
2664
2665         for (i = 0; i < total_queues; i++) {
2666                 allqueues[i] = init_sub_crq_queue(adapter);
2667                 if (!allqueues[i]) {
2668                         dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2669                         break;
2670                 }
2671                 registered_queues++;
2672         }
2673
2674         /* Make sure we were able to register the minimum number of queues */
2675         if (registered_queues <
2676             adapter->min_tx_queues + adapter->min_rx_queues) {
2677                 dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
2678                 goto tx_failed;
2679         }
2680
2681         /* Distribute the failed allocated queues*/
2682         for (i = 0; i < total_queues - registered_queues + more ; i++) {
2683                 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2684                 switch (i % 3) {
2685                 case 0:
2686                         if (adapter->req_rx_queues > adapter->min_rx_queues)
2687                                 adapter->req_rx_queues--;
2688                         else
2689                                 more++;
2690                         break;
2691                 case 1:
2692                         if (adapter->req_tx_queues > adapter->min_tx_queues)
2693                                 adapter->req_tx_queues--;
2694                         else
2695                                 more++;
2696                         break;
2697                 }
2698         }
2699
2700         adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2701                                    sizeof(*adapter->tx_scrq), GFP_KERNEL);
2702         if (!adapter->tx_scrq)
2703                 goto tx_failed;
2704
2705         for (i = 0; i < adapter->req_tx_queues; i++) {
2706                 adapter->tx_scrq[i] = allqueues[i];
2707                 adapter->tx_scrq[i]->pool_index = i;
2708                 adapter->num_active_tx_scrqs++;
2709         }
2710
2711         adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2712                                    sizeof(*adapter->rx_scrq), GFP_KERNEL);
2713         if (!adapter->rx_scrq)
2714                 goto rx_failed;
2715
2716         for (i = 0; i < adapter->req_rx_queues; i++) {
2717                 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2718                 adapter->rx_scrq[i]->scrq_num = i;
2719                 adapter->num_active_rx_scrqs++;
2720         }
2721
2722         kfree(allqueues);
2723         return 0;
2724
2725 rx_failed:
2726         kfree(adapter->tx_scrq);
2727         adapter->tx_scrq = NULL;
2728 tx_failed:
2729         for (i = 0; i < registered_queues; i++)
2730                 release_sub_crq_queue(adapter, allqueues[i], 1);
2731         kfree(allqueues);
2732         return -1;
2733 }
2734
2735 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2736 {
2737         struct device *dev = &adapter->vdev->dev;
2738         union ibmvnic_crq crq;
2739         int max_entries;
2740
2741         if (!retry) {
2742                 /* Sub-CRQ entries are 32 byte long */
2743                 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2744
2745                 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2746                     adapter->min_rx_add_entries_per_subcrq > entries_page) {
2747                         dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2748                         return;
2749                 }
2750
2751                 if (adapter->desired.mtu)
2752                         adapter->req_mtu = adapter->desired.mtu;
2753                 else
2754                         adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2755
2756                 if (!adapter->desired.tx_entries)
2757                         adapter->desired.tx_entries =
2758                                         adapter->max_tx_entries_per_subcrq;
2759                 if (!adapter->desired.rx_entries)
2760                         adapter->desired.rx_entries =
2761                                         adapter->max_rx_add_entries_per_subcrq;
2762
2763                 max_entries = IBMVNIC_MAX_LTB_SIZE /
2764                               (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2765
2766                 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2767                         adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2768                         adapter->desired.tx_entries = max_entries;
2769                 }
2770
2771                 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2772                         adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2773                         adapter->desired.rx_entries = max_entries;
2774                 }
2775
2776                 if (adapter->desired.tx_entries)
2777                         adapter->req_tx_entries_per_subcrq =
2778                                         adapter->desired.tx_entries;
2779                 else
2780                         adapter->req_tx_entries_per_subcrq =
2781                                         adapter->max_tx_entries_per_subcrq;
2782
2783                 if (adapter->desired.rx_entries)
2784                         adapter->req_rx_add_entries_per_subcrq =
2785                                         adapter->desired.rx_entries;
2786                 else
2787                         adapter->req_rx_add_entries_per_subcrq =
2788                                         adapter->max_rx_add_entries_per_subcrq;
2789
2790                 if (adapter->desired.tx_queues)
2791                         adapter->req_tx_queues =
2792                                         adapter->desired.tx_queues;
2793                 else
2794                         adapter->req_tx_queues =
2795                                         adapter->opt_tx_comp_sub_queues;
2796
2797                 if (adapter->desired.rx_queues)
2798                         adapter->req_rx_queues =
2799                                         adapter->desired.rx_queues;
2800                 else
2801                         adapter->req_rx_queues =
2802                                         adapter->opt_rx_comp_queues;
2803
2804                 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2805         }
2806
2807         memset(&crq, 0, sizeof(crq));
2808         crq.request_capability.first = IBMVNIC_CRQ_CMD;
2809         crq.request_capability.cmd = REQUEST_CAPABILITY;
2810
2811         crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2812         crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2813         atomic_inc(&adapter->running_cap_crqs);
2814         ibmvnic_send_crq(adapter, &crq);
2815
2816         crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2817         crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2818         atomic_inc(&adapter->running_cap_crqs);
2819         ibmvnic_send_crq(adapter, &crq);
2820
2821         crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2822         crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2823         atomic_inc(&adapter->running_cap_crqs);
2824         ibmvnic_send_crq(adapter, &crq);
2825
2826         crq.request_capability.capability =
2827             cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2828         crq.request_capability.number =
2829             cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2830         atomic_inc(&adapter->running_cap_crqs);
2831         ibmvnic_send_crq(adapter, &crq);
2832
2833         crq.request_capability.capability =
2834             cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2835         crq.request_capability.number =
2836             cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2837         atomic_inc(&adapter->running_cap_crqs);
2838         ibmvnic_send_crq(adapter, &crq);
2839
2840         crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2841         crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2842         atomic_inc(&adapter->running_cap_crqs);
2843         ibmvnic_send_crq(adapter, &crq);
2844
2845         if (adapter->netdev->flags & IFF_PROMISC) {
2846                 if (adapter->promisc_supported) {
2847                         crq.request_capability.capability =
2848                             cpu_to_be16(PROMISC_REQUESTED);
2849                         crq.request_capability.number = cpu_to_be64(1);
2850                         atomic_inc(&adapter->running_cap_crqs);
2851                         ibmvnic_send_crq(adapter, &crq);
2852                 }
2853         } else {
2854                 crq.request_capability.capability =
2855                     cpu_to_be16(PROMISC_REQUESTED);
2856                 crq.request_capability.number = cpu_to_be64(0);
2857                 atomic_inc(&adapter->running_cap_crqs);
2858                 ibmvnic_send_crq(adapter, &crq);
2859         }
2860 }
2861
2862 static int pending_scrq(struct ibmvnic_adapter *adapter,
2863                         struct ibmvnic_sub_crq_queue *scrq)
2864 {
2865         union sub_crq *entry = &scrq->msgs[scrq->cur];
2866
2867         if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2868                 return 1;
2869         else
2870                 return 0;
2871 }
2872
2873 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2874                                         struct ibmvnic_sub_crq_queue *scrq)
2875 {
2876         union sub_crq *entry;
2877         unsigned long flags;
2878
2879         spin_lock_irqsave(&scrq->lock, flags);
2880         entry = &scrq->msgs[scrq->cur];
2881         if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2882                 if (++scrq->cur == scrq->size)
2883                         scrq->cur = 0;
2884         } else {
2885                 entry = NULL;
2886         }
2887         spin_unlock_irqrestore(&scrq->lock, flags);
2888
2889         return entry;
2890 }
2891
2892 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2893 {
2894         struct ibmvnic_crq_queue *queue = &adapter->crq;
2895         union ibmvnic_crq *crq;
2896
2897         crq = &queue->msgs[queue->cur];
2898         if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2899                 if (++queue->cur == queue->size)
2900                         queue->cur = 0;
2901         } else {
2902                 crq = NULL;
2903         }
2904
2905         return crq;
2906 }
2907
2908 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2909                        union sub_crq *sub_crq)
2910 {
2911         unsigned int ua = adapter->vdev->unit_address;
2912         struct device *dev = &adapter->vdev->dev;
2913         u64 *u64_crq = (u64 *)sub_crq;
2914         int rc;
2915
2916         netdev_dbg(adapter->netdev,
2917                    "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2918                    (unsigned long int)cpu_to_be64(remote_handle),
2919                    (unsigned long int)cpu_to_be64(u64_crq[0]),
2920                    (unsigned long int)cpu_to_be64(u64_crq[1]),
2921                    (unsigned long int)cpu_to_be64(u64_crq[2]),
2922                    (unsigned long int)cpu_to_be64(u64_crq[3]));
2923
2924         /* Make sure the hypervisor sees the complete request */
2925         mb();
2926
2927         rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2928                                 cpu_to_be64(remote_handle),
2929                                 cpu_to_be64(u64_crq[0]),
2930                                 cpu_to_be64(u64_crq[1]),
2931                                 cpu_to_be64(u64_crq[2]),
2932                                 cpu_to_be64(u64_crq[3]));
2933
2934         if (rc) {
2935                 if (rc == H_CLOSED)
2936                         dev_warn(dev, "CRQ Queue closed\n");
2937                 dev_err(dev, "Send error (rc=%d)\n", rc);
2938         }
2939
2940         return rc;
2941 }
2942
2943 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2944                                 u64 remote_handle, u64 ioba, u64 num_entries)
2945 {
2946         unsigned int ua = adapter->vdev->unit_address;
2947         struct device *dev = &adapter->vdev->dev;
2948         int rc;
2949
2950         /* Make sure the hypervisor sees the complete request */
2951         mb();
2952         rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2953                                 cpu_to_be64(remote_handle),
2954                                 ioba, num_entries);
2955
2956         if (rc) {
2957                 if (rc == H_CLOSED)
2958                         dev_warn(dev, "CRQ Queue closed\n");
2959                 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2960         }
2961
2962         return rc;
2963 }
2964
2965 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2966                             union ibmvnic_crq *crq)
2967 {
2968         unsigned int ua = adapter->vdev->unit_address;
2969         struct device *dev = &adapter->vdev->dev;
2970         u64 *u64_crq = (u64 *)crq;
2971         int rc;
2972
2973         netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2974                    (unsigned long int)cpu_to_be64(u64_crq[0]),
2975                    (unsigned long int)cpu_to_be64(u64_crq[1]));
2976
2977         /* Make sure the hypervisor sees the complete request */
2978         mb();
2979
2980         rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2981                                 cpu_to_be64(u64_crq[0]),
2982                                 cpu_to_be64(u64_crq[1]));
2983
2984         if (rc) {
2985                 if (rc == H_CLOSED) {
2986                         dev_warn(dev, "CRQ Queue closed\n");
2987                         if (adapter->resetting)
2988                                 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2989                 }
2990
2991                 dev_warn(dev, "Send error (rc=%d)\n", rc);
2992         }
2993
2994         return rc;
2995 }
2996
2997 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2998 {
2999         union ibmvnic_crq crq;
3000
3001         memset(&crq, 0, sizeof(crq));
3002         crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3003         crq.generic.cmd = IBMVNIC_CRQ_INIT;
3004         netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3005
3006         return ibmvnic_send_crq(adapter, &crq);
3007 }
3008
3009 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3010 {
3011         union ibmvnic_crq crq;
3012
3013         memset(&crq, 0, sizeof(crq));
3014         crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3015         crq.version_exchange.cmd = VERSION_EXCHANGE;
3016         crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3017
3018         return ibmvnic_send_crq(adapter, &crq);
3019 }
3020
3021 struct vnic_login_client_data {
3022         u8      type;
3023         __be16  len;
3024         char    name;
3025 } __packed;
3026
3027 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3028 {
3029         int len;
3030
3031         /* Calculate the amount of buffer space needed for the
3032          * vnic client data in the login buffer. There are four entries,
3033          * OS name, LPAR name, device name, and a null last entry.
3034          */
3035         len = 4 * sizeof(struct vnic_login_client_data);
3036         len += 6; /* "Linux" plus NULL */
3037         len += strlen(utsname()->nodename) + 1;
3038         len += strlen(adapter->netdev->name) + 1;
3039
3040         return len;
3041 }
3042
3043 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3044                                  struct vnic_login_client_data *vlcd)
3045 {
3046         const char *os_name = "Linux";
3047         int len;
3048
3049         /* Type 1 - LPAR OS */
3050         vlcd->type = 1;
3051         len = strlen(os_name) + 1;
3052         vlcd->len = cpu_to_be16(len);
3053         strncpy(&vlcd->name, os_name, len);
3054         vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3055
3056         /* Type 2 - LPAR name */
3057         vlcd->type = 2;
3058         len = strlen(utsname()->nodename) + 1;
3059         vlcd->len = cpu_to_be16(len);
3060         strncpy(&vlcd->name, utsname()->nodename, len);
3061         vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3062
3063         /* Type 3 - device name */
3064         vlcd->type = 3;
3065         len = strlen(adapter->netdev->name) + 1;
3066         vlcd->len = cpu_to_be16(len);
3067         strncpy(&vlcd->name, adapter->netdev->name, len);
3068 }
3069
3070 static int send_login(struct ibmvnic_adapter *adapter)
3071 {
3072         struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3073         struct ibmvnic_login_buffer *login_buffer;
3074         struct device *dev = &adapter->vdev->dev;
3075         dma_addr_t rsp_buffer_token;
3076         dma_addr_t buffer_token;
3077         size_t rsp_buffer_size;
3078         union ibmvnic_crq crq;
3079         size_t buffer_size;
3080         __be64 *tx_list_p;
3081         __be64 *rx_list_p;
3082         int client_data_len;
3083         struct vnic_login_client_data *vlcd;
3084         int i;
3085
3086         if (!adapter->tx_scrq || !adapter->rx_scrq) {
3087                 netdev_err(adapter->netdev,
3088                            "RX or TX queues are not allocated, device login failed\n");
3089                 return -1;
3090         }
3091
3092         release_login_rsp_buffer(adapter);
3093         client_data_len = vnic_client_data_len(adapter);
3094
3095         buffer_size =
3096             sizeof(struct ibmvnic_login_buffer) +
3097             sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3098             client_data_len;
3099
3100         login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3101         if (!login_buffer)
3102                 goto buf_alloc_failed;
3103
3104         buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3105                                       DMA_TO_DEVICE);
3106         if (dma_mapping_error(dev, buffer_token)) {
3107                 dev_err(dev, "Couldn't map login buffer\n");
3108                 goto buf_map_failed;
3109         }
3110
3111         rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3112                           sizeof(u64) * adapter->req_tx_queues +
3113                           sizeof(u64) * adapter->req_rx_queues +
3114                           sizeof(u64) * adapter->req_rx_queues +
3115                           sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3116
3117         login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3118         if (!login_rsp_buffer)
3119                 goto buf_rsp_alloc_failed;
3120
3121         rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3122                                           rsp_buffer_size, DMA_FROM_DEVICE);
3123         if (dma_mapping_error(dev, rsp_buffer_token)) {
3124                 dev_err(dev, "Couldn't map login rsp buffer\n");
3125                 goto buf_rsp_map_failed;
3126         }
3127
3128         adapter->login_buf = login_buffer;
3129         adapter->login_buf_token = buffer_token;
3130         adapter->login_buf_sz = buffer_size;
3131         adapter->login_rsp_buf = login_rsp_buffer;
3132         adapter->login_rsp_buf_token = rsp_buffer_token;
3133         adapter->login_rsp_buf_sz = rsp_buffer_size;
3134
3135         login_buffer->len = cpu_to_be32(buffer_size);
3136         login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3137         login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3138         login_buffer->off_txcomp_subcrqs =
3139             cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3140         login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3141         login_buffer->off_rxcomp_subcrqs =
3142             cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3143                         sizeof(u64) * adapter->req_tx_queues);
3144         login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3145         login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3146
3147         tx_list_p = (__be64 *)((char *)login_buffer +
3148                                       sizeof(struct ibmvnic_login_buffer));
3149         rx_list_p = (__be64 *)((char *)login_buffer +
3150                                       sizeof(struct ibmvnic_login_buffer) +
3151                                       sizeof(u64) * adapter->req_tx_queues);
3152
3153         for (i = 0; i < adapter->req_tx_queues; i++) {
3154                 if (adapter->tx_scrq[i]) {
3155                         tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3156                                                    crq_num);
3157                 }
3158         }
3159
3160         for (i = 0; i < adapter->req_rx_queues; i++) {
3161                 if (adapter->rx_scrq[i]) {
3162                         rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3163                                                    crq_num);
3164                 }
3165         }
3166
3167         /* Insert vNIC login client data */
3168         vlcd = (struct vnic_login_client_data *)
3169                 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3170         login_buffer->client_data_offset =
3171                         cpu_to_be32((char *)vlcd - (char *)login_buffer);
3172         login_buffer->client_data_len = cpu_to_be32(client_data_len);
3173
3174         vnic_add_client_data(adapter, vlcd);
3175
3176         netdev_dbg(adapter->netdev, "Login Buffer:\n");
3177         for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3178                 netdev_dbg(adapter->netdev, "%016lx\n",
3179                            ((unsigned long int *)(adapter->login_buf))[i]);
3180         }
3181
3182         memset(&crq, 0, sizeof(crq));
3183         crq.login.first = IBMVNIC_CRQ_CMD;
3184         crq.login.cmd = LOGIN;
3185         crq.login.ioba = cpu_to_be32(buffer_token);
3186         crq.login.len = cpu_to_be32(buffer_size);
3187         ibmvnic_send_crq(adapter, &crq);
3188
3189         return 0;
3190
3191 buf_rsp_map_failed:
3192         kfree(login_rsp_buffer);
3193 buf_rsp_alloc_failed:
3194         dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3195 buf_map_failed:
3196         kfree(login_buffer);
3197 buf_alloc_failed:
3198         return -1;
3199 }
3200
3201 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3202                              u32 len, u8 map_id)
3203 {
3204         union ibmvnic_crq crq;
3205
3206         memset(&crq, 0, sizeof(crq));
3207         crq.request_map.first = IBMVNIC_CRQ_CMD;
3208         crq.request_map.cmd = REQUEST_MAP;
3209         crq.request_map.map_id = map_id;
3210         crq.request_map.ioba = cpu_to_be32(addr);
3211         crq.request_map.len = cpu_to_be32(len);
3212         ibmvnic_send_crq(adapter, &crq);
3213 }
3214
3215 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3216 {
3217         union ibmvnic_crq crq;
3218
3219         memset(&crq, 0, sizeof(crq));
3220         crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3221         crq.request_unmap.cmd = REQUEST_UNMAP;
3222         crq.request_unmap.map_id = map_id;
3223         ibmvnic_send_crq(adapter, &crq);
3224 }
3225
3226 static void send_map_query(struct ibmvnic_adapter *adapter)
3227 {
3228         union ibmvnic_crq crq;
3229
3230         memset(&crq, 0, sizeof(crq));
3231         crq.query_map.first = IBMVNIC_CRQ_CMD;
3232         crq.query_map.cmd = QUERY_MAP;
3233         ibmvnic_send_crq(adapter, &crq);
3234 }
3235
3236 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3237 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3238 {
3239         union ibmvnic_crq crq;
3240
3241         atomic_set(&adapter->running_cap_crqs, 0);
3242         memset(&crq, 0, sizeof(crq));
3243         crq.query_capability.first = IBMVNIC_CRQ_CMD;
3244         crq.query_capability.cmd = QUERY_CAPABILITY;
3245
3246         crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3247         atomic_inc(&adapter->running_cap_crqs);
3248         ibmvnic_send_crq(adapter, &crq);
3249
3250         crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3251         atomic_inc(&adapter->running_cap_crqs);
3252         ibmvnic_send_crq(adapter, &crq);
3253
3254         crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3255         atomic_inc(&adapter->running_cap_crqs);
3256         ibmvnic_send_crq(adapter, &crq);
3257
3258         crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3259         atomic_inc(&adapter->running_cap_crqs);
3260         ibmvnic_send_crq(adapter, &crq);
3261
3262         crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3263         atomic_inc(&adapter->running_cap_crqs);
3264         ibmvnic_send_crq(adapter, &crq);
3265
3266         crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3267         atomic_inc(&adapter->running_cap_crqs);
3268         ibmvnic_send_crq(adapter, &crq);
3269
3270         crq.query_capability.capability =
3271             cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3272         atomic_inc(&adapter->running_cap_crqs);
3273         ibmvnic_send_crq(adapter, &crq);
3274
3275         crq.query_capability.capability =
3276             cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3277         atomic_inc(&adapter->running_cap_crqs);
3278         ibmvnic_send_crq(adapter, &crq);
3279
3280         crq.query_capability.capability =
3281             cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3282         atomic_inc(&adapter->running_cap_crqs);
3283         ibmvnic_send_crq(adapter, &crq);
3284
3285         crq.query_capability.capability =
3286             cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3287         atomic_inc(&adapter->running_cap_crqs);
3288         ibmvnic_send_crq(adapter, &crq);
3289
3290         crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3291         atomic_inc(&adapter->running_cap_crqs);
3292         ibmvnic_send_crq(adapter, &crq);
3293
3294         crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3295         atomic_inc(&adapter->running_cap_crqs);
3296         ibmvnic_send_crq(adapter, &crq);
3297
3298         crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3299         atomic_inc(&adapter->running_cap_crqs);
3300         ibmvnic_send_crq(adapter, &crq);
3301
3302         crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3303         atomic_inc(&adapter->running_cap_crqs);
3304         ibmvnic_send_crq(adapter, &crq);
3305
3306         crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3307         atomic_inc(&adapter->running_cap_crqs);
3308         ibmvnic_send_crq(adapter, &crq);
3309
3310         crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3311         atomic_inc(&adapter->running_cap_crqs);
3312         ibmvnic_send_crq(adapter, &crq);
3313
3314         crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3315         atomic_inc(&adapter->running_cap_crqs);
3316         ibmvnic_send_crq(adapter, &crq);
3317
3318         crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3319         atomic_inc(&adapter->running_cap_crqs);
3320         ibmvnic_send_crq(adapter, &crq);
3321
3322         crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3323         atomic_inc(&adapter->running_cap_crqs);
3324         ibmvnic_send_crq(adapter, &crq);
3325
3326         crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3327         atomic_inc(&adapter->running_cap_crqs);
3328         ibmvnic_send_crq(adapter, &crq);
3329
3330         crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3331         atomic_inc(&adapter->running_cap_crqs);
3332         ibmvnic_send_crq(adapter, &crq);
3333
3334         crq.query_capability.capability =
3335                         cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3336         atomic_inc(&adapter->running_cap_crqs);
3337         ibmvnic_send_crq(adapter, &crq);
3338
3339         crq.query_capability.capability =
3340                         cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3341         atomic_inc(&adapter->running_cap_crqs);
3342         ibmvnic_send_crq(adapter, &crq);
3343
3344         crq.query_capability.capability =
3345                         cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3346         atomic_inc(&adapter->running_cap_crqs);
3347         ibmvnic_send_crq(adapter, &crq);
3348
3349         crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3350         atomic_inc(&adapter->running_cap_crqs);
3351         ibmvnic_send_crq(adapter, &crq);
3352 }
3353
3354 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3355                                 struct ibmvnic_adapter *adapter)
3356 {
3357         struct device *dev = &adapter->vdev->dev;
3358
3359         if (crq->get_vpd_size_rsp.rc.code) {
3360                 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3361                         crq->get_vpd_size_rsp.rc.code);
3362                 complete(&adapter->fw_done);
3363                 return;
3364         }
3365
3366         adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3367         complete(&adapter->fw_done);
3368 }
3369
3370 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3371                            struct ibmvnic_adapter *adapter)
3372 {
3373         struct device *dev = &adapter->vdev->dev;
3374         unsigned char *substr = NULL;
3375         u8 fw_level_len = 0;
3376
3377         memset(adapter->fw_version, 0, 32);
3378
3379         dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3380                          DMA_FROM_DEVICE);
3381
3382         if (crq->get_vpd_rsp.rc.code) {
3383                 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3384                         crq->get_vpd_rsp.rc.code);
3385                 goto complete;
3386         }
3387
3388         /* get the position of the firmware version info
3389          * located after the ASCII 'RM' substring in the buffer
3390          */
3391         substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3392         if (!substr) {
3393                 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3394                 goto complete;
3395         }
3396
3397         /* get length of firmware level ASCII substring */
3398         if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3399                 fw_level_len = *(substr + 2);
3400         } else {
3401                 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3402                 goto complete;
3403         }
3404
3405         /* copy firmware version string from vpd into adapter */
3406         if ((substr + 3 + fw_level_len) <
3407             (adapter->vpd->buff + adapter->vpd->len)) {
3408                 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3409         } else {
3410                 dev_info(dev, "FW substr extrapolated VPD buff\n");
3411         }
3412
3413 complete:
3414         if (adapter->fw_version[0] == '\0')
3415                 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3416         complete(&adapter->fw_done);
3417 }
3418
3419 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3420 {
3421         struct device *dev = &adapter->vdev->dev;
3422         struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3423         union ibmvnic_crq crq;
3424         int i;
3425
3426         dma_unmap_single(dev, adapter->ip_offload_tok,
3427                          sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3428
3429         netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3430         for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3431                 netdev_dbg(adapter->netdev, "%016lx\n",
3432                            ((unsigned long int *)(buf))[i]);
3433
3434         netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3435         netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3436         netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3437                    buf->tcp_ipv4_chksum);
3438         netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3439                    buf->tcp_ipv6_chksum);
3440         netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3441                    buf->udp_ipv4_chksum);
3442         netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3443                    buf->udp_ipv6_chksum);
3444         netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3445                    buf->large_tx_ipv4);
3446         netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3447                    buf->large_tx_ipv6);
3448         netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3449                    buf->large_rx_ipv4);
3450         netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3451                    buf->large_rx_ipv6);
3452         netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3453                    buf->max_ipv4_header_size);
3454         netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3455                    buf->max_ipv6_header_size);
3456         netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3457                    buf->max_tcp_header_size);
3458         netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3459                    buf->max_udp_header_size);
3460         netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3461                    buf->max_large_tx_size);
3462         netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3463                    buf->max_large_rx_size);
3464         netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3465                    buf->ipv6_extension_header);
3466         netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3467                    buf->tcp_pseudosum_req);
3468         netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3469                    buf->num_ipv6_ext_headers);
3470         netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3471                    buf->off_ipv6_ext_headers);
3472
3473         adapter->ip_offload_ctrl_tok =
3474             dma_map_single(dev, &adapter->ip_offload_ctrl,
3475                            sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3476
3477         if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3478                 dev_err(dev, "Couldn't map ip offload control buffer\n");
3479                 return;
3480         }
3481
3482         adapter->ip_offload_ctrl.len =
3483             cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3484         adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3485         adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3486         adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3487         adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3488         adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3489         adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3490         adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3491         adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3492         adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3493
3494         /* large_rx disabled for now, additional features needed */
3495         adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3496         adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3497
3498         adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3499
3500         if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3501                 adapter->netdev->features |= NETIF_F_IP_CSUM;
3502
3503         if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3504                 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3505
3506         if ((adapter->netdev->features &
3507             (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3508                 adapter->netdev->features |= NETIF_F_RXCSUM;
3509
3510         if (buf->large_tx_ipv4)
3511                 adapter->netdev->features |= NETIF_F_TSO;
3512         if (buf->large_tx_ipv6)
3513                 adapter->netdev->features |= NETIF_F_TSO6;
3514
3515         adapter->netdev->hw_features |= adapter->netdev->features;
3516
3517         memset(&crq, 0, sizeof(crq));
3518         crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3519         crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3520         crq.control_ip_offload.len =
3521             cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3522         crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3523         ibmvnic_send_crq(adapter, &crq);
3524 }
3525
3526 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3527                                   struct ibmvnic_adapter *adapter)
3528 {
3529         struct device *dev = &adapter->vdev->dev;
3530         struct ibmvnic_error_buff *error_buff, *tmp;
3531         unsigned long flags;
3532         bool found = false;
3533         int i;
3534
3535         if (!crq->request_error_rsp.rc.code) {
3536                 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3537                          crq->request_error_rsp.rc.code);
3538                 return;
3539         }
3540
3541         spin_lock_irqsave(&adapter->error_list_lock, flags);
3542         list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3543                 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3544                         found = true;
3545                         list_del(&error_buff->list);
3546                         break;
3547                 }
3548         spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3549
3550         if (!found) {
3551                 dev_err(dev, "Couldn't find error id %x\n",
3552                         be32_to_cpu(crq->request_error_rsp.error_id));
3553                 return;
3554         }
3555
3556         dev_err(dev, "Detailed info for error id %x:",
3557                 be32_to_cpu(crq->request_error_rsp.error_id));
3558
3559         for (i = 0; i < error_buff->len; i++) {
3560                 pr_cont("%02x", (int)error_buff->buff[i]);
3561                 if (i % 8 == 7)
3562                         pr_cont(" ");
3563         }
3564         pr_cont("\n");
3565
3566         dma_unmap_single(dev, error_buff->dma, error_buff->len,
3567                          DMA_FROM_DEVICE);
3568         kfree(error_buff->buff);
3569         kfree(error_buff);
3570 }
3571
3572 static void request_error_information(struct ibmvnic_adapter *adapter,
3573                                       union ibmvnic_crq *err_crq)
3574 {
3575         struct device *dev = &adapter->vdev->dev;
3576         struct net_device *netdev = adapter->netdev;
3577         struct ibmvnic_error_buff *error_buff;
3578         unsigned long timeout = msecs_to_jiffies(30000);
3579         union ibmvnic_crq crq;
3580         unsigned long flags;
3581         int rc, detail_len;
3582
3583         error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3584         if (!error_buff)
3585                 return;
3586
3587         detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3588         error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3589         if (!error_buff->buff) {
3590                 kfree(error_buff);
3591                 return;
3592         }
3593
3594         error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3595                                          DMA_FROM_DEVICE);
3596         if (dma_mapping_error(dev, error_buff->dma)) {
3597                 netdev_err(netdev, "Couldn't map error buffer\n");
3598                 kfree(error_buff->buff);
3599                 kfree(error_buff);
3600                 return;
3601         }
3602
3603         error_buff->len = detail_len;
3604         error_buff->error_id = err_crq->error_indication.error_id;
3605
3606         spin_lock_irqsave(&adapter->error_list_lock, flags);
3607         list_add_tail(&error_buff->list, &adapter->errors);
3608         spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3609
3610         memset(&crq, 0, sizeof(crq));
3611         crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3612         crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3613         crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3614         crq.request_error_info.len = cpu_to_be32(detail_len);
3615         crq.request_error_info.error_id = err_crq->error_indication.error_id;
3616
3617         rc = ibmvnic_send_crq(adapter, &crq);
3618         if (rc) {
3619                 netdev_err(netdev, "failed to request error information\n");
3620                 goto err_info_fail;
3621         }
3622
3623         if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3624                 netdev_err(netdev, "timeout waiting for error information\n");
3625                 goto err_info_fail;
3626         }
3627
3628         return;
3629
3630 err_info_fail:
3631         spin_lock_irqsave(&adapter->error_list_lock, flags);
3632         list_del(&error_buff->list);
3633         spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3634
3635         kfree(error_buff->buff);
3636         kfree(error_buff);
3637 }
3638
3639 static void handle_error_indication(union ibmvnic_crq *crq,
3640                                     struct ibmvnic_adapter *adapter)
3641 {
3642         struct device *dev = &adapter->vdev->dev;
3643
3644         dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3645                 crq->error_indication.flags
3646                         & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3647                 be32_to_cpu(crq->error_indication.error_id),
3648                 be16_to_cpu(crq->error_indication.error_cause));
3649
3650         if (be32_to_cpu(crq->error_indication.error_id))
3651                 request_error_information(adapter, crq);
3652
3653         if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3654                 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3655         else
3656                 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3657 }
3658
3659 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3660                                  struct ibmvnic_adapter *adapter)
3661 {
3662         struct net_device *netdev = adapter->netdev;
3663         struct device *dev = &adapter->vdev->dev;
3664         long rc;
3665
3666         rc = crq->change_mac_addr_rsp.rc.code;
3667         if (rc) {
3668                 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3669                 goto out;
3670         }
3671         memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3672                ETH_ALEN);
3673 out:
3674         complete(&adapter->fw_done);
3675         return rc;
3676 }
3677
3678 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3679                                    struct ibmvnic_adapter *adapter)
3680 {
3681         struct device *dev = &adapter->vdev->dev;
3682         u64 *req_value;
3683         char *name;
3684
3685         atomic_dec(&adapter->running_cap_crqs);
3686         switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3687         case REQ_TX_QUEUES:
3688                 req_value = &adapter->req_tx_queues;
3689                 name = "tx";
3690                 break;
3691         case REQ_RX_QUEUES:
3692                 req_value = &adapter->req_rx_queues;
3693                 name = "rx";
3694                 break;
3695         case REQ_RX_ADD_QUEUES:
3696                 req_value = &adapter->req_rx_add_queues;
3697                 name = "rx_add";
3698                 break;
3699         case REQ_TX_ENTRIES_PER_SUBCRQ:
3700                 req_value = &adapter->req_tx_entries_per_subcrq;
3701                 name = "tx_entries_per_subcrq";
3702                 break;
3703         case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3704                 req_value = &adapter->req_rx_add_entries_per_subcrq;
3705                 name = "rx_add_entries_per_subcrq";
3706                 break;
3707         case REQ_MTU:
3708                 req_value = &adapter->req_mtu;
3709                 name = "mtu";
3710                 break;
3711         case PROMISC_REQUESTED:
3712                 req_value = &adapter->promisc;
3713                 name = "promisc";
3714                 break;
3715         default:
3716                 dev_err(dev, "Got invalid cap request rsp %d\n",
3717                         crq->request_capability.capability);
3718                 return;
3719         }
3720
3721         switch (crq->request_capability_rsp.rc.code) {
3722         case SUCCESS:
3723                 break;
3724         case PARTIALSUCCESS:
3725                 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3726                          *req_value,
3727                          (long int)be64_to_cpu(crq->request_capability_rsp.
3728                                                number), name);
3729
3730                 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3731                     REQ_MTU) {
3732                         pr_err("mtu of %llu is not supported. Reverting.\n",
3733                                *req_value);
3734                         *req_value = adapter->fallback.mtu;
3735                 } else {
3736                         *req_value =
3737                                 be64_to_cpu(crq->request_capability_rsp.number);
3738                 }
3739
3740                 ibmvnic_send_req_caps(adapter, 1);
3741                 return;
3742         default:
3743                 dev_err(dev, "Error %d in request cap rsp\n",
3744                         crq->request_capability_rsp.rc.code);
3745                 return;
3746         }
3747
3748         /* Done receiving requested capabilities, query IP offload support */
3749         if (atomic_read(&adapter->running_cap_crqs) == 0) {
3750                 union ibmvnic_crq newcrq;
3751                 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3752                 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3753                     &adapter->ip_offload_buf;
3754
3755                 adapter->wait_capability = false;
3756                 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3757                                                          buf_sz,
3758                                                          DMA_FROM_DEVICE);
3759
3760                 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3761                         if (!firmware_has_feature(FW_FEATURE_CMO))
3762                                 dev_err(dev, "Couldn't map offload buffer\n");
3763                         return;
3764                 }
3765
3766                 memset(&newcrq, 0, sizeof(newcrq));
3767                 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3768                 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3769                 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3770                 newcrq.query_ip_offload.ioba =
3771                     cpu_to_be32(adapter->ip_offload_tok);
3772
3773                 ibmvnic_send_crq(adapter, &newcrq);
3774         }
3775 }
3776
3777 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3778                             struct ibmvnic_adapter *adapter)
3779 {
3780         struct device *dev = &adapter->vdev->dev;
3781         struct net_device *netdev = adapter->netdev;
3782         struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3783         struct ibmvnic_login_buffer *login = adapter->login_buf;
3784         int i;
3785
3786         dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3787                          DMA_BIDIRECTIONAL);
3788         dma_unmap_single(dev, adapter->login_rsp_buf_token,
3789                          adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3790
3791         /* If the number of queues requested can't be allocated by the
3792          * server, the login response will return with code 1. We will need
3793          * to resend the login buffer with fewer queues requested.
3794          */
3795         if (login_rsp_crq->generic.rc.code) {
3796                 adapter->renegotiate = true;
3797                 complete(&adapter->init_done);
3798                 return 0;
3799         }
3800
3801         netdev->mtu = adapter->req_mtu - ETH_HLEN;
3802
3803         netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3804         for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3805                 netdev_dbg(adapter->netdev, "%016lx\n",
3806                            ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3807         }
3808
3809         /* Sanity checks */
3810         if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3811             (be32_to_cpu(login->num_rxcomp_subcrqs) *
3812              adapter->req_rx_add_queues !=
3813              be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3814                 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3815                 ibmvnic_remove(adapter->vdev);
3816                 return -EIO;
3817         }
3818         release_login_buffer(adapter);
3819         complete(&adapter->init_done);
3820
3821         return 0;
3822 }
3823
3824 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3825                                      struct ibmvnic_adapter *adapter)
3826 {
3827         struct device *dev = &adapter->vdev->dev;
3828         long rc;
3829
3830         rc = crq->request_unmap_rsp.rc.code;
3831         if (rc)
3832                 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3833 }
3834
3835 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3836                                  struct ibmvnic_adapter *adapter)
3837 {
3838         struct net_device *netdev = adapter->netdev;
3839         struct device *dev = &adapter->vdev->dev;
3840         long rc;
3841
3842         rc = crq->query_map_rsp.rc.code;
3843         if (rc) {
3844                 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3845                 return;
3846         }
3847         netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3848                    crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3849                    crq->query_map_rsp.free_pages);
3850 }
3851
3852 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3853                                  struct ibmvnic_adapter *adapter)
3854 {
3855         struct net_device *netdev = adapter->netdev;
3856         struct device *dev = &adapter->vdev->dev;
3857         long rc;
3858
3859         atomic_dec(&adapter->running_cap_crqs);
3860         netdev_dbg(netdev, "Outstanding queries: %d\n",
3861                    atomic_read(&adapter->running_cap_crqs));
3862         rc = crq->query_capability.rc.code;
3863         if (rc) {
3864                 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3865                 goto out;
3866         }
3867
3868         switch (be16_to_cpu(crq->query_capability.capability)) {
3869         case MIN_TX_QUEUES:
3870                 adapter->min_tx_queues =
3871                     be64_to_cpu(crq->query_capability.number);
3872                 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3873                            adapter->min_tx_queues);
3874                 break;
3875         case MIN_RX_QUEUES:
3876                 adapter->min_rx_queues =
3877                     be64_to_cpu(crq->query_capability.number);
3878                 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3879                            adapter->min_rx_queues);
3880                 break;
3881         case MIN_RX_ADD_QUEUES:
3882                 adapter->min_rx_add_queues =
3883                     be64_to_cpu(crq->query_capability.number);
3884                 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3885                            adapter->min_rx_add_queues);
3886                 break;
3887         case MAX_TX_QUEUES:
3888                 adapter->max_tx_queues =
3889                     be64_to_cpu(crq->query_capability.number);
3890                 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3891                            adapter->max_tx_queues);
3892                 break;
3893         case MAX_RX_QUEUES:
3894                 adapter->max_rx_queues =
3895                     be64_to_cpu(crq->query_capability.number);
3896                 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3897                            adapter->max_rx_queues);
3898                 break;
3899         case MAX_RX_ADD_QUEUES:
3900                 adapter->max_rx_add_queues =
3901                     be64_to_cpu(crq->query_capability.number);
3902                 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3903                            adapter->max_rx_add_queues);
3904                 break;
3905         case MIN_TX_ENTRIES_PER_SUBCRQ:
3906                 adapter->min_tx_entries_per_subcrq =
3907                     be64_to_cpu(crq->query_capability.number);
3908                 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3909                            adapter->min_tx_entries_per_subcrq);
3910                 break;
3911         case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3912                 adapter->min_rx_add_entries_per_subcrq =
3913                     be64_to_cpu(crq->query_capability.number);
3914                 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3915                            adapter->min_rx_add_entries_per_subcrq);
3916                 break;
3917         case MAX_TX_ENTRIES_PER_SUBCRQ:
3918                 adapter->max_tx_entries_per_subcrq =
3919                     be64_to_cpu(crq->query_capability.number);
3920                 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3921                            adapter->max_tx_entries_per_subcrq);
3922                 break;
3923         case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3924                 adapter->max_rx_add_entries_per_subcrq =
3925                     be64_to_cpu(crq->query_capability.number);
3926                 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3927                            adapter->max_rx_add_entries_per_subcrq);
3928                 break;
3929         case TCP_IP_OFFLOAD:
3930                 adapter->tcp_ip_offload =
3931                     be64_to_cpu(crq->query_capability.number);
3932                 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3933                            adapter->tcp_ip_offload);
3934                 break;
3935         case PROMISC_SUPPORTED:
3936                 adapter->promisc_supported =
3937                     be64_to_cpu(crq->query_capability.number);
3938                 netdev_dbg(netdev, "promisc_supported = %lld\n",
3939                            adapter->promisc_supported);
3940                 break;
3941         case MIN_MTU:
3942                 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3943                 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3944                 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3945                 break;
3946         case MAX_MTU:
3947                 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3948                 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3949                 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3950                 break;
3951         case MAX_MULTICAST_FILTERS:
3952                 adapter->max_multicast_filters =
3953                     be64_to_cpu(crq->query_capability.number);
3954                 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3955                            adapter->max_multicast_filters);
3956                 break;
3957         case VLAN_HEADER_INSERTION:
3958                 adapter->vlan_header_insertion =
3959                     be64_to_cpu(crq->query_capability.number);
3960                 if (adapter->vlan_header_insertion)
3961                         netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3962                 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3963                            adapter->vlan_header_insertion);
3964                 break;
3965         case RX_VLAN_HEADER_INSERTION:
3966                 adapter->rx_vlan_header_insertion =
3967                     be64_to_cpu(crq->query_capability.number);
3968                 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3969                            adapter->rx_vlan_header_insertion);
3970                 break;
3971         case MAX_TX_SG_ENTRIES:
3972                 adapter->max_tx_sg_entries =
3973                     be64_to_cpu(crq->query_capability.number);
3974                 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3975                            adapter->max_tx_sg_entries);
3976                 break;
3977         case RX_SG_SUPPORTED:
3978                 adapter->rx_sg_supported =
3979                     be64_to_cpu(crq->query_capability.number);
3980                 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3981                            adapter->rx_sg_supported);
3982                 break;
3983         case OPT_TX_COMP_SUB_QUEUES:
3984                 adapter->opt_tx_comp_sub_queues =
3985                     be64_to_cpu(crq->query_capability.number);
3986                 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3987                            adapter->opt_tx_comp_sub_queues);
3988                 break;
3989         case OPT_RX_COMP_QUEUES:
3990                 adapter->opt_rx_comp_queues =
3991                     be64_to_cpu(crq->query_capability.number);
3992                 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3993                            adapter->opt_rx_comp_queues);
3994                 break;
3995         case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3996                 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3997                     be64_to_cpu(crq->query_capability.number);
3998                 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3999                            adapter->opt_rx_bufadd_q_per_rx_comp_q);
4000                 break;
4001         case OPT_TX_ENTRIES_PER_SUBCRQ:
4002                 adapter->opt_tx_entries_per_subcrq =
4003                     be64_to_cpu(crq->query_capability.number);
4004                 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4005                            adapter->opt_tx_entries_per_subcrq);
4006                 break;
4007         case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4008                 adapter->opt_rxba_entries_per_subcrq =
4009                     be64_to_cpu(crq->query_capability.number);
4010                 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4011                            adapter->opt_rxba_entries_per_subcrq);
4012                 break;
4013         case TX_RX_DESC_REQ:
4014                 adapter->tx_rx_desc_req = crq->query_capability.number;
4015                 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4016                            adapter->tx_rx_desc_req);
4017                 break;
4018
4019         default:
4020                 netdev_err(netdev, "Got invalid cap rsp %d\n",
4021                            crq->query_capability.capability);
4022         }
4023
4024 out:
4025         if (atomic_read(&adapter->running_cap_crqs) == 0) {
4026                 adapter->wait_capability = false;
4027                 ibmvnic_send_req_caps(adapter, 0);
4028         }
4029 }
4030
4031 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4032                                struct ibmvnic_adapter *adapter)
4033 {
4034         struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4035         struct net_device *netdev = adapter->netdev;
4036         struct device *dev = &adapter->vdev->dev;
4037         u64 *u64_crq = (u64 *)crq;
4038         long rc;
4039
4040         netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4041                    (unsigned long int)cpu_to_be64(u64_crq[0]),
4042                    (unsigned long int)cpu_to_be64(u64_crq[1]));
4043         switch (gen_crq->first) {
4044         case IBMVNIC_CRQ_INIT_RSP:
4045                 switch (gen_crq->cmd) {
4046                 case IBMVNIC_CRQ_INIT:
4047                         dev_info(dev, "Partner initialized\n");
4048                         adapter->from_passive_init = true;
4049                         complete(&adapter->init_done);
4050                         break;
4051                 case IBMVNIC_CRQ_INIT_COMPLETE:
4052                         dev_info(dev, "Partner initialization complete\n");
4053                         send_version_xchg(adapter);
4054                         break;
4055                 default:
4056                         dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4057                 }
4058                 return;
4059         case IBMVNIC_CRQ_XPORT_EVENT:
4060                 netif_carrier_off(netdev);
4061                 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4062                         dev_info(dev, "Migrated, re-enabling adapter\n");
4063                         ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4064                 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4065                         dev_info(dev, "Backing device failover detected\n");
4066                         ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4067                 } else {
4068                         /* The adapter lost the connection */
4069                         dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4070                                 gen_crq->cmd);
4071                         ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4072                 }
4073                 return;
4074         case IBMVNIC_CRQ_CMD_RSP:
4075                 break;
4076         default:
4077                 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4078                         gen_crq->first);
4079                 return;
4080         }
4081
4082         switch (gen_crq->cmd) {
4083         case VERSION_EXCHANGE_RSP:
4084                 rc = crq->version_exchange_rsp.rc.code;
4085                 if (rc) {
4086                         dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4087                         break;
4088                 }
4089                 dev_info(dev, "Partner protocol version is %d\n",
4090                          crq->version_exchange_rsp.version);
4091                 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4092                     ibmvnic_version)
4093                         ibmvnic_version =
4094                             be16_to_cpu(crq->version_exchange_rsp.version);
4095                 send_cap_queries(adapter);
4096                 break;
4097         case QUERY_CAPABILITY_RSP:
4098                 handle_query_cap_rsp(crq, adapter);
4099                 break;
4100         case QUERY_MAP_RSP:
4101                 handle_query_map_rsp(crq, adapter);
4102                 break;
4103         case REQUEST_MAP_RSP:
4104                 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4105                 complete(&adapter->fw_done);
4106                 break;
4107         case REQUEST_UNMAP_RSP:
4108                 handle_request_unmap_rsp(crq, adapter);
4109                 break;
4110         case REQUEST_CAPABILITY_RSP:
4111                 handle_request_cap_rsp(crq, adapter);
4112                 break;
4113         case LOGIN_RSP:
4114                 netdev_dbg(netdev, "Got Login Response\n");
4115                 handle_login_rsp(crq, adapter);
4116                 break;
4117         case LOGICAL_LINK_STATE_RSP:
4118                 netdev_dbg(netdev,
4119                            "Got Logical Link State Response, state: %d rc: %d\n",
4120                            crq->logical_link_state_rsp.link_state,
4121                            crq->logical_link_state_rsp.rc.code);
4122                 adapter->logical_link_state =
4123                     crq->logical_link_state_rsp.link_state;
4124                 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4125                 complete(&adapter->init_done);
4126                 break;
4127         case LINK_STATE_INDICATION:
4128                 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4129                 adapter->phys_link_state =
4130                     crq->link_state_indication.phys_link_state;
4131                 adapter->logical_link_state =
4132                     crq->link_state_indication.logical_link_state;
4133                 break;
4134         case CHANGE_MAC_ADDR_RSP:
4135                 netdev_dbg(netdev, "Got MAC address change Response\n");
4136                 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4137                 break;
4138         case ERROR_INDICATION:
4139                 netdev_dbg(netdev, "Got Error Indication\n");
4140                 handle_error_indication(crq, adapter);
4141                 break;
4142         case REQUEST_ERROR_RSP:
4143                 netdev_dbg(netdev, "Got Error Detail Response\n");
4144                 handle_error_info_rsp(crq, adapter);
4145                 break;
4146         case REQUEST_STATISTICS_RSP:
4147                 netdev_dbg(netdev, "Got Statistics Response\n");
4148                 complete(&adapter->stats_done);
4149                 break;
4150         case QUERY_IP_OFFLOAD_RSP:
4151                 netdev_dbg(netdev, "Got Query IP offload Response\n");
4152                 handle_query_ip_offload_rsp(adapter);
4153                 break;
4154         case MULTICAST_CTRL_RSP:
4155                 netdev_dbg(netdev, "Got multicast control Response\n");
4156                 break;
4157         case CONTROL_IP_OFFLOAD_RSP:
4158                 netdev_dbg(netdev, "Got Control IP offload Response\n");
4159                 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4160                                  sizeof(adapter->ip_offload_ctrl),
4161                                  DMA_TO_DEVICE);
4162                 complete(&adapter->init_done);
4163                 break;
4164         case COLLECT_FW_TRACE_RSP:
4165                 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4166                 complete(&adapter->fw_done);
4167                 break;
4168         case GET_VPD_SIZE_RSP:
4169                 handle_vpd_size_rsp(crq, adapter);
4170                 break;
4171         case GET_VPD_RSP:
4172                 handle_vpd_rsp(crq, adapter);
4173                 break;
4174         default:
4175                 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4176                            gen_crq->cmd);
4177         }
4178 }
4179
4180 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4181 {
4182         struct ibmvnic_adapter *adapter = instance;
4183
4184         tasklet_schedule(&adapter->tasklet);
4185         return IRQ_HANDLED;
4186 }
4187
4188 static void ibmvnic_tasklet(void *data)
4189 {
4190         struct ibmvnic_adapter *adapter = data;
4191         struct ibmvnic_crq_queue *queue = &adapter->crq;
4192         union ibmvnic_crq *crq;
4193         unsigned long flags;
4194         bool done = false;
4195
4196         spin_lock_irqsave(&queue->lock, flags);
4197         while (!done) {
4198                 /* Pull all the valid messages off the CRQ */
4199                 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4200                         ibmvnic_handle_crq(crq, adapter);
4201                         crq->generic.first = 0;
4202                 }
4203
4204                 /* remain in tasklet until all
4205                  * capabilities responses are received
4206                  */
4207                 if (!adapter->wait_capability)
4208                         done = true;
4209         }
4210         /* if capabilities CRQ's were sent in this tasklet, the following
4211          * tasklet must wait until all responses are received
4212          */
4213         if (atomic_read(&adapter->running_cap_crqs) != 0)
4214                 adapter->wait_capability = true;
4215         spin_unlock_irqrestore(&queue->lock, flags);
4216 }
4217
4218 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4219 {
4220         struct vio_dev *vdev = adapter->vdev;
4221         int rc;
4222
4223         do {
4224                 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4225         } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4226
4227         if (rc)
4228                 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4229
4230         return rc;
4231 }
4232
4233 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4234 {
4235         struct ibmvnic_crq_queue *crq = &adapter->crq;
4236         struct device *dev = &adapter->vdev->dev;
4237         struct vio_dev *vdev = adapter->vdev;
4238         int rc;
4239
4240         /* Close the CRQ */
4241         do {
4242                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4243         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4244
4245         /* Clean out the queue */
4246         memset(crq->msgs, 0, PAGE_SIZE);
4247         crq->cur = 0;
4248
4249         /* And re-open it again */
4250         rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4251                                 crq->msg_token, PAGE_SIZE);
4252
4253         if (rc == H_CLOSED)
4254                 /* Adapter is good, but other end is not ready */
4255                 dev_warn(dev, "Partner adapter not ready\n");
4256         else if (rc != 0)
4257                 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4258
4259         return rc;
4260 }
4261
4262 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4263 {
4264         struct ibmvnic_crq_queue *crq = &adapter->crq;
4265         struct vio_dev *vdev = adapter->vdev;
4266         long rc;
4267
4268         if (!crq->msgs)
4269                 return;
4270
4271         netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4272         free_irq(vdev->irq, adapter);
4273         tasklet_kill(&adapter->tasklet);
4274         do {
4275                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4276         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4277
4278         dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4279                          DMA_BIDIRECTIONAL);
4280         free_page((unsigned long)crq->msgs);
4281         crq->msgs = NULL;
4282 }
4283
4284 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4285 {
4286         struct ibmvnic_crq_queue *crq = &adapter->crq;
4287         struct device *dev = &adapter->vdev->dev;
4288         struct vio_dev *vdev = adapter->vdev;
4289         int rc, retrc = -ENOMEM;
4290
4291         if (crq->msgs)
4292                 return 0;
4293
4294         crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4295         /* Should we allocate more than one page? */
4296
4297         if (!crq->msgs)
4298                 return -ENOMEM;
4299
4300         crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4301         crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4302                                         DMA_BIDIRECTIONAL);
4303         if (dma_mapping_error(dev, crq->msg_token))
4304                 goto map_failed;
4305
4306         rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4307                                 crq->msg_token, PAGE_SIZE);
4308
4309         if (rc == H_RESOURCE)
4310                 /* maybe kexecing and resource is busy. try a reset */
4311                 rc = ibmvnic_reset_crq(adapter);
4312         retrc = rc;
4313
4314         if (rc == H_CLOSED) {
4315                 dev_warn(dev, "Partner adapter not ready\n");
4316         } else if (rc) {
4317                 dev_warn(dev, "Error %d opening adapter\n", rc);
4318                 goto reg_crq_failed;
4319         }
4320
4321         retrc = 0;
4322
4323         tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4324                      (unsigned long)adapter);
4325
4326         netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4327         rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4328                          adapter);
4329         if (rc) {
4330                 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4331                         vdev->irq, rc);
4332                 goto req_irq_failed;
4333         }
4334
4335         rc = vio_enable_interrupts(vdev);
4336         if (rc) {
4337                 dev_err(dev, "Error %d enabling interrupts\n", rc);
4338                 goto req_irq_failed;
4339         }
4340
4341         crq->cur = 0;
4342         spin_lock_init(&crq->lock);
4343
4344         return retrc;
4345
4346 req_irq_failed:
4347         tasklet_kill(&adapter->tasklet);
4348         do {
4349                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4350         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4351 reg_crq_failed:
4352         dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4353 map_failed:
4354         free_page((unsigned long)crq->msgs);
4355         crq->msgs = NULL;
4356         return retrc;
4357 }
4358
4359 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4360 {
4361         struct device *dev = &adapter->vdev->dev;
4362         unsigned long timeout = msecs_to_jiffies(30000);
4363         u64 old_num_rx_queues, old_num_tx_queues;
4364         int rc;
4365
4366         if (adapter->resetting && !adapter->wait_for_reset) {
4367                 rc = ibmvnic_reset_crq(adapter);
4368                 if (!rc)
4369                         rc = vio_enable_interrupts(adapter->vdev);
4370         } else {
4371                 rc = init_crq_queue(adapter);
4372         }
4373
4374         if (rc) {
4375                 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4376                 return rc;
4377         }
4378
4379         adapter->from_passive_init = false;
4380
4381         old_num_rx_queues = adapter->req_rx_queues;
4382         old_num_tx_queues = adapter->req_tx_queues;
4383
4384         init_completion(&adapter->init_done);
4385         adapter->init_done_rc = 0;
4386         ibmvnic_send_crq_init(adapter);
4387         if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4388                 dev_err(dev, "Initialization sequence timed out\n");
4389                 return -1;
4390         }
4391
4392         if (adapter->init_done_rc) {
4393                 release_crq_queue(adapter);
4394                 return adapter->init_done_rc;
4395         }
4396
4397         if (adapter->from_passive_init) {
4398                 adapter->state = VNIC_OPEN;
4399                 adapter->from_passive_init = false;
4400                 return -1;
4401         }
4402
4403         if (adapter->resetting && !adapter->wait_for_reset) {
4404                 if (adapter->req_rx_queues != old_num_rx_queues ||
4405                     adapter->req_tx_queues != old_num_tx_queues) {
4406                         release_sub_crqs(adapter, 0);
4407                         rc = init_sub_crqs(adapter);
4408                 } else {
4409                         rc = reset_sub_crq_queues(adapter);
4410                 }
4411         } else {
4412                 rc = init_sub_crqs(adapter);
4413         }
4414
4415         if (rc) {
4416                 dev_err(dev, "Initialization of sub crqs failed\n");
4417                 release_crq_queue(adapter);
4418                 return rc;
4419         }
4420
4421         rc = init_sub_crq_irqs(adapter);
4422         if (rc) {
4423                 dev_err(dev, "Failed to initialize sub crq irqs\n");
4424                 release_crq_queue(adapter);
4425         }
4426
4427         rc = init_stats_buffers(adapter);
4428         if (rc)
4429                 return rc;
4430
4431         rc = init_stats_token(adapter);
4432         if (rc)
4433                 return rc;
4434
4435         return rc;
4436 }
4437
4438 static struct device_attribute dev_attr_failover;
4439
4440 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4441 {
4442         struct ibmvnic_adapter *adapter;
4443         struct net_device *netdev;
4444         unsigned char *mac_addr_p;
4445         int rc;
4446
4447         dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4448                 dev->unit_address);
4449
4450         mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4451                                                         VETH_MAC_ADDR, NULL);
4452         if (!mac_addr_p) {
4453                 dev_err(&dev->dev,
4454                         "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4455                         __FILE__, __LINE__);
4456                 return 0;
4457         }
4458
4459         netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4460                                    IBMVNIC_MAX_QUEUES);
4461         if (!netdev)
4462                 return -ENOMEM;
4463
4464         adapter = netdev_priv(netdev);
4465         adapter->state = VNIC_PROBING;
4466         dev_set_drvdata(&dev->dev, netdev);
4467         adapter->vdev = dev;
4468         adapter->netdev = netdev;
4469
4470         ether_addr_copy(adapter->mac_addr, mac_addr_p);
4471         ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4472         netdev->irq = dev->irq;
4473         netdev->netdev_ops = &ibmvnic_netdev_ops;
4474         netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4475         SET_NETDEV_DEV(netdev, &dev->dev);
4476
4477         spin_lock_init(&adapter->stats_lock);
4478
4479         INIT_LIST_HEAD(&adapter->errors);
4480         spin_lock_init(&adapter->error_list_lock);
4481
4482         INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4483         INIT_LIST_HEAD(&adapter->rwi_list);
4484         mutex_init(&adapter->reset_lock);
4485         mutex_init(&adapter->rwi_lock);
4486         adapter->resetting = false;
4487
4488         adapter->mac_change_pending = false;
4489
4490         do {
4491                 rc = ibmvnic_init(adapter);
4492                 if (rc && rc != EAGAIN)
4493                         goto ibmvnic_init_fail;
4494         } while (rc == EAGAIN);
4495
4496         netdev->mtu = adapter->req_mtu - ETH_HLEN;
4497         netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4498         netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4499
4500         rc = device_create_file(&dev->dev, &dev_attr_failover);
4501         if (rc)
4502                 goto ibmvnic_init_fail;
4503
4504         netif_carrier_off(netdev);
4505         rc = register_netdev(netdev);
4506         if (rc) {
4507                 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4508                 goto ibmvnic_register_fail;
4509         }
4510         dev_info(&dev->dev, "ibmvnic registered\n");
4511
4512         adapter->state = VNIC_PROBED;
4513
4514         adapter->wait_for_reset = false;
4515
4516         return 0;
4517
4518 ibmvnic_register_fail:
4519         device_remove_file(&dev->dev, &dev_attr_failover);
4520
4521 ibmvnic_init_fail:
4522         release_sub_crqs(adapter, 1);
4523         release_crq_queue(adapter);
4524         free_netdev(netdev);
4525
4526         return rc;
4527 }
4528
4529 static int ibmvnic_remove(struct vio_dev *dev)
4530 {
4531         struct net_device *netdev = dev_get_drvdata(&dev->dev);
4532         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4533
4534         adapter->state = VNIC_REMOVING;
4535         unregister_netdev(netdev);
4536         mutex_lock(&adapter->reset_lock);
4537
4538         release_resources(adapter);
4539         release_sub_crqs(adapter, 1);
4540         release_crq_queue(adapter);
4541
4542         release_stats_token(adapter);
4543         release_stats_buffers(adapter);
4544
4545         adapter->state = VNIC_REMOVED;
4546
4547         mutex_unlock(&adapter->reset_lock);
4548         device_remove_file(&dev->dev, &dev_attr_failover);
4549         free_netdev(netdev);
4550         dev_set_drvdata(&dev->dev, NULL);
4551
4552         return 0;
4553 }
4554
4555 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4556                               const char *buf, size_t count)
4557 {
4558         struct net_device *netdev = dev_get_drvdata(dev);
4559         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4560         unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4561         __be64 session_token;
4562         long rc;
4563
4564         if (!sysfs_streq(buf, "1"))
4565                 return -EINVAL;
4566
4567         rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4568                          H_GET_SESSION_TOKEN, 0, 0, 0);
4569         if (rc) {
4570                 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4571                            rc);
4572                 return -EINVAL;
4573         }
4574
4575         session_token = (__be64)retbuf[0];
4576         netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4577                    be64_to_cpu(session_token));
4578         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4579                                 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4580         if (rc) {
4581                 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4582                            rc);
4583                 return -EINVAL;
4584         }
4585
4586         return count;
4587 }
4588
4589 static DEVICE_ATTR_WO(failover);
4590
4591 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4592 {
4593         struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4594         struct ibmvnic_adapter *adapter;
4595         struct iommu_table *tbl;
4596         unsigned long ret = 0;
4597         int i;
4598
4599         tbl = get_iommu_table_base(&vdev->dev);
4600
4601         /* netdev inits at probe time along with the structures we need below*/
4602         if (!netdev)
4603                 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4604
4605         adapter = netdev_priv(netdev);
4606
4607         ret += PAGE_SIZE; /* the crq message queue */
4608         ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4609
4610         for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4611                 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4612
4613         for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4614              i++)
4615                 ret += adapter->rx_pool[i].size *
4616                     IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4617
4618         return ret;
4619 }
4620
4621 static int ibmvnic_resume(struct device *dev)
4622 {
4623         struct net_device *netdev = dev_get_drvdata(dev);
4624         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4625
4626         if (adapter->state != VNIC_OPEN)
4627                 return 0;
4628
4629         tasklet_schedule(&adapter->tasklet);
4630
4631         return 0;
4632 }
4633
4634 static const struct vio_device_id ibmvnic_device_table[] = {
4635         {"network", "IBM,vnic"},
4636         {"", "" }
4637 };
4638 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4639
4640 static const struct dev_pm_ops ibmvnic_pm_ops = {
4641         .resume = ibmvnic_resume
4642 };
4643
4644 static struct vio_driver ibmvnic_driver = {
4645         .id_table       = ibmvnic_device_table,
4646         .probe          = ibmvnic_probe,
4647         .remove         = ibmvnic_remove,
4648         .get_desired_dma = ibmvnic_get_desired_dma,
4649         .name           = ibmvnic_driver_name,
4650         .pm             = &ibmvnic_pm_ops,
4651 };
4652
4653 /* module functions */
4654 static int __init ibmvnic_module_init(void)
4655 {
4656         pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4657                 IBMVNIC_DRIVER_VERSION);
4658
4659         return vio_register_driver(&ibmvnic_driver);
4660 }
4661
4662 static void __exit ibmvnic_module_exit(void)
4663 {
4664         vio_unregister_driver(&ibmvnic_driver);
4665 }
4666
4667 module_init(ibmvnic_module_init);
4668 module_exit(ibmvnic_module_exit);