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