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