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