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