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