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