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