ibmvnic: queue reset when CRQ gets closed during reset
[muen/linux.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_resources(struct ibmvnic_adapter *adapter)
795 {
796         int i;
797
798         release_vpd_data(adapter);
799
800         release_tx_pools(adapter);
801         release_rx_pools(adapter);
802
803         release_stats_token(adapter);
804         release_stats_buffers(adapter);
805         release_error_buffers(adapter);
806
807         if (adapter->napi) {
808                 for (i = 0; i < adapter->req_rx_queues; i++) {
809                         if (&adapter->napi[i]) {
810                                 netdev_dbg(adapter->netdev,
811                                            "Releasing napi[%d]\n", i);
812                                 netif_napi_del(&adapter->napi[i]);
813                         }
814                 }
815         }
816 }
817
818 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
819 {
820         struct net_device *netdev = adapter->netdev;
821         unsigned long timeout = msecs_to_jiffies(30000);
822         union ibmvnic_crq crq;
823         bool resend;
824         int rc;
825
826         netdev_dbg(netdev, "setting link state %d\n", link_state);
827
828         memset(&crq, 0, sizeof(crq));
829         crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
830         crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
831         crq.logical_link_state.link_state = link_state;
832
833         do {
834                 resend = false;
835
836                 reinit_completion(&adapter->init_done);
837                 rc = ibmvnic_send_crq(adapter, &crq);
838                 if (rc) {
839                         netdev_err(netdev, "Failed to set link state\n");
840                         return rc;
841                 }
842
843                 if (!wait_for_completion_timeout(&adapter->init_done,
844                                                  timeout)) {
845                         netdev_err(netdev, "timeout setting link state\n");
846                         return -1;
847                 }
848
849                 if (adapter->init_done_rc == 1) {
850                         /* Partuial success, delay and re-send */
851                         mdelay(1000);
852                         resend = true;
853                 }
854         } while (resend);
855
856         return 0;
857 }
858
859 static int set_real_num_queues(struct net_device *netdev)
860 {
861         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
862         int rc;
863
864         netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
865                    adapter->req_tx_queues, adapter->req_rx_queues);
866
867         rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
868         if (rc) {
869                 netdev_err(netdev, "failed to set the number of tx queues\n");
870                 return rc;
871         }
872
873         rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
874         if (rc)
875                 netdev_err(netdev, "failed to set the number of rx queues\n");
876
877         return rc;
878 }
879
880 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
881 {
882         struct device *dev = &adapter->vdev->dev;
883         union ibmvnic_crq crq;
884         int len = 0;
885
886         if (adapter->vpd->buff)
887                 len = adapter->vpd->len;
888
889         init_completion(&adapter->fw_done);
890         crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
891         crq.get_vpd_size.cmd = GET_VPD_SIZE;
892         ibmvnic_send_crq(adapter, &crq);
893         wait_for_completion(&adapter->fw_done);
894
895         if (!adapter->vpd->len)
896                 return -ENODATA;
897
898         if (!adapter->vpd->buff)
899                 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
900         else if (adapter->vpd->len != len)
901                 adapter->vpd->buff =
902                         krealloc(adapter->vpd->buff,
903                                  adapter->vpd->len, GFP_KERNEL);
904
905         if (!adapter->vpd->buff) {
906                 dev_err(dev, "Could allocate VPD buffer\n");
907                 return -ENOMEM;
908         }
909
910         adapter->vpd->dma_addr =
911                 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
912                                DMA_FROM_DEVICE);
913         if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
914                 dev_err(dev, "Could not map VPD buffer\n");
915                 kfree(adapter->vpd->buff);
916                 adapter->vpd->buff = NULL;
917                 return -ENOMEM;
918         }
919
920         reinit_completion(&adapter->fw_done);
921         crq.get_vpd.first = IBMVNIC_CRQ_CMD;
922         crq.get_vpd.cmd = GET_VPD;
923         crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
924         crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
925         ibmvnic_send_crq(adapter, &crq);
926         wait_for_completion(&adapter->fw_done);
927
928         return 0;
929 }
930
931 static int init_resources(struct ibmvnic_adapter *adapter)
932 {
933         struct net_device *netdev = adapter->netdev;
934         int i, rc;
935
936         rc = set_real_num_queues(netdev);
937         if (rc)
938                 return rc;
939
940         rc = init_stats_buffers(adapter);
941         if (rc)
942                 return rc;
943
944         rc = init_stats_token(adapter);
945         if (rc)
946                 return rc;
947
948         adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
949         if (!adapter->vpd)
950                 return -ENOMEM;
951
952         /* Vital Product Data (VPD) */
953         rc = ibmvnic_get_vpd(adapter);
954         if (rc) {
955                 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
956                 return rc;
957         }
958
959         adapter->map_id = 1;
960         adapter->napi = kcalloc(adapter->req_rx_queues,
961                                 sizeof(struct napi_struct), GFP_KERNEL);
962         if (!adapter->napi)
963                 return -ENOMEM;
964
965         for (i = 0; i < adapter->req_rx_queues; i++) {
966                 netdev_dbg(netdev, "Adding napi[%d]\n", i);
967                 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
968                                NAPI_POLL_WEIGHT);
969         }
970
971         send_map_query(adapter);
972
973         rc = init_rx_pools(netdev);
974         if (rc)
975                 return rc;
976
977         rc = init_tx_pools(netdev);
978         return rc;
979 }
980
981 static int __ibmvnic_open(struct net_device *netdev)
982 {
983         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
984         enum vnic_state prev_state = adapter->state;
985         int i, rc;
986
987         adapter->state = VNIC_OPENING;
988         replenish_pools(adapter);
989         ibmvnic_napi_enable(adapter);
990
991         /* We're ready to receive frames, enable the sub-crq interrupts and
992          * set the logical link state to up
993          */
994         for (i = 0; i < adapter->req_rx_queues; i++) {
995                 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
996                 if (prev_state == VNIC_CLOSED)
997                         enable_irq(adapter->rx_scrq[i]->irq);
998                 else
999                         enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1000         }
1001
1002         for (i = 0; i < adapter->req_tx_queues; i++) {
1003                 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1004                 if (prev_state == VNIC_CLOSED)
1005                         enable_irq(adapter->tx_scrq[i]->irq);
1006                 else
1007                         enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1008         }
1009
1010         rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1011         if (rc) {
1012                 for (i = 0; i < adapter->req_rx_queues; i++)
1013                         napi_disable(&adapter->napi[i]);
1014                 release_resources(adapter);
1015                 return rc;
1016         }
1017
1018         netif_tx_start_all_queues(netdev);
1019
1020         if (prev_state == VNIC_CLOSED) {
1021                 for (i = 0; i < adapter->req_rx_queues; i++)
1022                         napi_schedule(&adapter->napi[i]);
1023         }
1024
1025         adapter->state = VNIC_OPEN;
1026         return rc;
1027 }
1028
1029 static int ibmvnic_open(struct net_device *netdev)
1030 {
1031         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1032         int rc;
1033
1034         mutex_lock(&adapter->reset_lock);
1035
1036         if (adapter->state != VNIC_CLOSED) {
1037                 rc = ibmvnic_login(netdev);
1038                 if (rc) {
1039                         mutex_unlock(&adapter->reset_lock);
1040                         return rc;
1041                 }
1042
1043                 rc = init_resources(adapter);
1044                 if (rc) {
1045                         netdev_err(netdev, "failed to initialize resources\n");
1046                         release_resources(adapter);
1047                         mutex_unlock(&adapter->reset_lock);
1048                         return rc;
1049                 }
1050         }
1051
1052         rc = __ibmvnic_open(netdev);
1053         netif_carrier_on(netdev);
1054
1055         mutex_unlock(&adapter->reset_lock);
1056
1057         return rc;
1058 }
1059
1060 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1061 {
1062         struct ibmvnic_tx_pool *tx_pool;
1063         u64 tx_entries;
1064         int tx_scrqs;
1065         int i, j;
1066
1067         if (!adapter->tx_pool)
1068                 return;
1069
1070         tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1071         tx_entries = adapter->req_tx_entries_per_subcrq;
1072
1073         /* Free any remaining skbs in the tx buffer pools */
1074         for (i = 0; i < tx_scrqs; i++) {
1075                 tx_pool = &adapter->tx_pool[i];
1076                 if (!tx_pool)
1077                         continue;
1078
1079                 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1080                 for (j = 0; j < tx_entries; j++) {
1081                         if (tx_pool->tx_buff[j].skb) {
1082                                 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1083                                 tx_pool->tx_buff[j].skb = NULL;
1084                         }
1085                 }
1086         }
1087 }
1088
1089 static int __ibmvnic_close(struct net_device *netdev)
1090 {
1091         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1092         int rc = 0;
1093         int i;
1094
1095         adapter->state = VNIC_CLOSING;
1096
1097         /* ensure that transmissions are stopped if called by do_reset */
1098         if (adapter->resetting)
1099                 netif_tx_disable(netdev);
1100         else
1101                 netif_tx_stop_all_queues(netdev);
1102
1103         ibmvnic_napi_disable(adapter);
1104
1105         if (adapter->tx_scrq) {
1106                 for (i = 0; i < adapter->req_tx_queues; i++)
1107                         if (adapter->tx_scrq[i]->irq) {
1108                                 netdev_dbg(adapter->netdev,
1109                                            "Disabling tx_scrq[%d] irq\n", i);
1110                                 disable_irq(adapter->tx_scrq[i]->irq);
1111                         }
1112         }
1113
1114         rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1115         if (rc)
1116                 return rc;
1117
1118         if (adapter->rx_scrq) {
1119                 for (i = 0; i < adapter->req_rx_queues; i++) {
1120                         int retries = 10;
1121
1122                         while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1123                                 retries--;
1124                                 mdelay(100);
1125
1126                                 if (retries == 0)
1127                                         break;
1128                         }
1129
1130                         if (adapter->rx_scrq[i]->irq) {
1131                                 netdev_dbg(adapter->netdev,
1132                                            "Disabling rx_scrq[%d] irq\n", i);
1133                                 disable_irq(adapter->rx_scrq[i]->irq);
1134                         }
1135                 }
1136         }
1137
1138         clean_tx_pools(adapter);
1139         adapter->state = VNIC_CLOSED;
1140         return rc;
1141 }
1142
1143 static int ibmvnic_close(struct net_device *netdev)
1144 {
1145         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1146         int rc;
1147
1148         mutex_lock(&adapter->reset_lock);
1149         rc = __ibmvnic_close(netdev);
1150         mutex_unlock(&adapter->reset_lock);
1151
1152         return rc;
1153 }
1154
1155 /**
1156  * build_hdr_data - creates L2/L3/L4 header data buffer
1157  * @hdr_field - bitfield determining needed headers
1158  * @skb - socket buffer
1159  * @hdr_len - array of header lengths
1160  * @tot_len - total length of data
1161  *
1162  * Reads hdr_field to determine which headers are needed by firmware.
1163  * Builds a buffer containing these headers.  Saves individual header
1164  * lengths and total buffer length to be used to build descriptors.
1165  */
1166 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1167                           int *hdr_len, u8 *hdr_data)
1168 {
1169         int len = 0;
1170         u8 *hdr;
1171
1172         hdr_len[0] = sizeof(struct ethhdr);
1173
1174         if (skb->protocol == htons(ETH_P_IP)) {
1175                 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1176                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1177                         hdr_len[2] = tcp_hdrlen(skb);
1178                 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1179                         hdr_len[2] = sizeof(struct udphdr);
1180         } else if (skb->protocol == htons(ETH_P_IPV6)) {
1181                 hdr_len[1] = sizeof(struct ipv6hdr);
1182                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1183                         hdr_len[2] = tcp_hdrlen(skb);
1184                 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1185                         hdr_len[2] = sizeof(struct udphdr);
1186         } else if (skb->protocol == htons(ETH_P_ARP)) {
1187                 hdr_len[1] = arp_hdr_len(skb->dev);
1188                 hdr_len[2] = 0;
1189         }
1190
1191         memset(hdr_data, 0, 120);
1192         if ((hdr_field >> 6) & 1) {
1193                 hdr = skb_mac_header(skb);
1194                 memcpy(hdr_data, hdr, hdr_len[0]);
1195                 len += hdr_len[0];
1196         }
1197
1198         if ((hdr_field >> 5) & 1) {
1199                 hdr = skb_network_header(skb);
1200                 memcpy(hdr_data + len, hdr, hdr_len[1]);
1201                 len += hdr_len[1];
1202         }
1203
1204         if ((hdr_field >> 4) & 1) {
1205                 hdr = skb_transport_header(skb);
1206                 memcpy(hdr_data + len, hdr, hdr_len[2]);
1207                 len += hdr_len[2];
1208         }
1209         return len;
1210 }
1211
1212 /**
1213  * create_hdr_descs - create header and header extension descriptors
1214  * @hdr_field - bitfield determining needed headers
1215  * @data - buffer containing header data
1216  * @len - length of data buffer
1217  * @hdr_len - array of individual header lengths
1218  * @scrq_arr - descriptor array
1219  *
1220  * Creates header and, if needed, header extension descriptors and
1221  * places them in a descriptor array, scrq_arr
1222  */
1223
1224 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1225                             union sub_crq *scrq_arr)
1226 {
1227         union sub_crq hdr_desc;
1228         int tmp_len = len;
1229         int num_descs = 0;
1230         u8 *data, *cur;
1231         int tmp;
1232
1233         while (tmp_len > 0) {
1234                 cur = hdr_data + len - tmp_len;
1235
1236                 memset(&hdr_desc, 0, sizeof(hdr_desc));
1237                 if (cur != hdr_data) {
1238                         data = hdr_desc.hdr_ext.data;
1239                         tmp = tmp_len > 29 ? 29 : tmp_len;
1240                         hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1241                         hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1242                         hdr_desc.hdr_ext.len = tmp;
1243                 } else {
1244                         data = hdr_desc.hdr.data;
1245                         tmp = tmp_len > 24 ? 24 : tmp_len;
1246                         hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1247                         hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1248                         hdr_desc.hdr.len = tmp;
1249                         hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1250                         hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1251                         hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1252                         hdr_desc.hdr.flag = hdr_field << 1;
1253                 }
1254                 memcpy(data, cur, tmp);
1255                 tmp_len -= tmp;
1256                 *scrq_arr = hdr_desc;
1257                 scrq_arr++;
1258                 num_descs++;
1259         }
1260
1261         return num_descs;
1262 }
1263
1264 /**
1265  * build_hdr_descs_arr - build a header descriptor array
1266  * @skb - socket buffer
1267  * @num_entries - number of descriptors to be sent
1268  * @subcrq - first TX descriptor
1269  * @hdr_field - bit field determining which headers will be sent
1270  *
1271  * This function will build a TX descriptor array with applicable
1272  * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1273  */
1274
1275 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1276                                 int *num_entries, u8 hdr_field)
1277 {
1278         int hdr_len[3] = {0, 0, 0};
1279         int tot_len;
1280         u8 *hdr_data = txbuff->hdr_data;
1281
1282         tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1283                                  txbuff->hdr_data);
1284         *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1285                          txbuff->indir_arr + 1);
1286 }
1287
1288 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1289 {
1290         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1291         int queue_num = skb_get_queue_mapping(skb);
1292         u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1293         struct device *dev = &adapter->vdev->dev;
1294         struct ibmvnic_tx_buff *tx_buff = NULL;
1295         struct ibmvnic_sub_crq_queue *tx_scrq;
1296         struct ibmvnic_tx_pool *tx_pool;
1297         unsigned int tx_send_failed = 0;
1298         unsigned int tx_map_failed = 0;
1299         unsigned int tx_dropped = 0;
1300         unsigned int tx_packets = 0;
1301         unsigned int tx_bytes = 0;
1302         dma_addr_t data_dma_addr;
1303         struct netdev_queue *txq;
1304         unsigned long lpar_rc;
1305         union sub_crq tx_crq;
1306         unsigned int offset;
1307         int num_entries = 1;
1308         unsigned char *dst;
1309         u64 *handle_array;
1310         int index = 0;
1311         u8 proto = 0;
1312         int ret = 0;
1313
1314         if (adapter->resetting) {
1315                 if (!netif_subqueue_stopped(netdev, skb))
1316                         netif_stop_subqueue(netdev, queue_num);
1317                 dev_kfree_skb_any(skb);
1318
1319                 tx_send_failed++;
1320                 tx_dropped++;
1321                 ret = NETDEV_TX_OK;
1322                 goto out;
1323         }
1324
1325         tx_pool = &adapter->tx_pool[queue_num];
1326         tx_scrq = adapter->tx_scrq[queue_num];
1327         txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1328         handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1329                 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1330
1331         index = tx_pool->free_map[tx_pool->consumer_index];
1332
1333         if (skb_is_gso(skb)) {
1334                 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1335                 dst = tx_pool->tso_ltb.buff + offset;
1336                 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1337                 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1338                 tx_pool->tso_index++;
1339                 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1340                         tx_pool->tso_index = 0;
1341         } else {
1342                 offset = index * adapter->req_mtu;
1343                 dst = tx_pool->long_term_buff.buff + offset;
1344                 memset(dst, 0, adapter->req_mtu);
1345                 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1346         }
1347
1348         if (skb_shinfo(skb)->nr_frags) {
1349                 int cur, i;
1350
1351                 /* Copy the head */
1352                 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1353                 cur = skb_headlen(skb);
1354
1355                 /* Copy the frags */
1356                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1357                         const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1358
1359                         memcpy(dst + cur,
1360                                page_address(skb_frag_page(frag)) +
1361                                frag->page_offset, skb_frag_size(frag));
1362                         cur += skb_frag_size(frag);
1363                 }
1364         } else {
1365                 skb_copy_from_linear_data(skb, dst, skb->len);
1366         }
1367
1368         tx_pool->consumer_index =
1369             (tx_pool->consumer_index + 1) %
1370                 adapter->req_tx_entries_per_subcrq;
1371
1372         tx_buff = &tx_pool->tx_buff[index];
1373         tx_buff->skb = skb;
1374         tx_buff->data_dma[0] = data_dma_addr;
1375         tx_buff->data_len[0] = skb->len;
1376         tx_buff->index = index;
1377         tx_buff->pool_index = queue_num;
1378         tx_buff->last_frag = true;
1379
1380         memset(&tx_crq, 0, sizeof(tx_crq));
1381         tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1382         tx_crq.v1.type = IBMVNIC_TX_DESC;
1383         tx_crq.v1.n_crq_elem = 1;
1384         tx_crq.v1.n_sge = 1;
1385         tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1386         tx_crq.v1.correlator = cpu_to_be32(index);
1387         if (skb_is_gso(skb))
1388                 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1389         else
1390                 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1391         tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1392         tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1393
1394         if (adapter->vlan_header_insertion) {
1395                 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1396                 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1397         }
1398
1399         if (skb->protocol == htons(ETH_P_IP)) {
1400                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1401                 proto = ip_hdr(skb)->protocol;
1402         } else if (skb->protocol == htons(ETH_P_IPV6)) {
1403                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1404                 proto = ipv6_hdr(skb)->nexthdr;
1405         }
1406
1407         if (proto == IPPROTO_TCP)
1408                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1409         else if (proto == IPPROTO_UDP)
1410                 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1411
1412         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1413                 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1414                 hdrs += 2;
1415         }
1416         if (skb_is_gso(skb)) {
1417                 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1418                 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1419                 hdrs += 2;
1420         }
1421         /* determine if l2/3/4 headers are sent to firmware */
1422         if ((*hdrs >> 7) & 1 &&
1423             (skb->protocol == htons(ETH_P_IP) ||
1424              skb->protocol == htons(ETH_P_IPV6) ||
1425              skb->protocol == htons(ETH_P_ARP))) {
1426                 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1427                 tx_crq.v1.n_crq_elem = num_entries;
1428                 tx_buff->indir_arr[0] = tx_crq;
1429                 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1430                                                     sizeof(tx_buff->indir_arr),
1431                                                     DMA_TO_DEVICE);
1432                 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1433                         dev_kfree_skb_any(skb);
1434                         tx_buff->skb = NULL;
1435                         if (!firmware_has_feature(FW_FEATURE_CMO))
1436                                 dev_err(dev, "tx: unable to map descriptor array\n");
1437                         tx_map_failed++;
1438                         tx_dropped++;
1439                         ret = NETDEV_TX_OK;
1440                         goto out;
1441                 }
1442                 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1443                                                (u64)tx_buff->indir_dma,
1444                                                (u64)num_entries);
1445         } else {
1446                 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1447                                       &tx_crq);
1448         }
1449         if (lpar_rc != H_SUCCESS) {
1450                 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1451
1452                 if (tx_pool->consumer_index == 0)
1453                         tx_pool->consumer_index =
1454                                 adapter->req_tx_entries_per_subcrq - 1;
1455                 else
1456                         tx_pool->consumer_index--;
1457
1458                 dev_kfree_skb_any(skb);
1459                 tx_buff->skb = NULL;
1460
1461                 if (lpar_rc == H_CLOSED) {
1462                         /* Disable TX and report carrier off if queue is closed.
1463                          * Firmware guarantees that a signal will be sent to the
1464                          * driver, triggering a reset or some other action.
1465                          */
1466                         netif_tx_stop_all_queues(netdev);
1467                         netif_carrier_off(netdev);
1468                 }
1469
1470                 tx_send_failed++;
1471                 tx_dropped++;
1472                 ret = NETDEV_TX_OK;
1473                 goto out;
1474         }
1475
1476         if (atomic_inc_return(&tx_scrq->used)
1477                                         >= adapter->req_tx_entries_per_subcrq) {
1478                 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1479                 netif_stop_subqueue(netdev, queue_num);
1480         }
1481
1482         tx_packets++;
1483         tx_bytes += skb->len;
1484         txq->trans_start = jiffies;
1485         ret = NETDEV_TX_OK;
1486
1487 out:
1488         netdev->stats.tx_dropped += tx_dropped;
1489         netdev->stats.tx_bytes += tx_bytes;
1490         netdev->stats.tx_packets += tx_packets;
1491         adapter->tx_send_failed += tx_send_failed;
1492         adapter->tx_map_failed += tx_map_failed;
1493         adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1494         adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1495         adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1496
1497         return ret;
1498 }
1499
1500 static void ibmvnic_set_multi(struct net_device *netdev)
1501 {
1502         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1503         struct netdev_hw_addr *ha;
1504         union ibmvnic_crq crq;
1505
1506         memset(&crq, 0, sizeof(crq));
1507         crq.request_capability.first = IBMVNIC_CRQ_CMD;
1508         crq.request_capability.cmd = REQUEST_CAPABILITY;
1509
1510         if (netdev->flags & IFF_PROMISC) {
1511                 if (!adapter->promisc_supported)
1512                         return;
1513         } else {
1514                 if (netdev->flags & IFF_ALLMULTI) {
1515                         /* Accept all multicast */
1516                         memset(&crq, 0, sizeof(crq));
1517                         crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1518                         crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1519                         crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1520                         ibmvnic_send_crq(adapter, &crq);
1521                 } else if (netdev_mc_empty(netdev)) {
1522                         /* Reject all multicast */
1523                         memset(&crq, 0, sizeof(crq));
1524                         crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1525                         crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1526                         crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1527                         ibmvnic_send_crq(adapter, &crq);
1528                 } else {
1529                         /* Accept one or more multicast(s) */
1530                         netdev_for_each_mc_addr(ha, netdev) {
1531                                 memset(&crq, 0, sizeof(crq));
1532                                 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1533                                 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1534                                 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1535                                 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1536                                                 ha->addr);
1537                                 ibmvnic_send_crq(adapter, &crq);
1538                         }
1539                 }
1540         }
1541 }
1542
1543 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1544 {
1545         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1546         struct sockaddr *addr = p;
1547         union ibmvnic_crq crq;
1548
1549         if (!is_valid_ether_addr(addr->sa_data))
1550                 return -EADDRNOTAVAIL;
1551
1552         memset(&crq, 0, sizeof(crq));
1553         crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1554         crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1555         ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1556
1557         init_completion(&adapter->fw_done);
1558         ibmvnic_send_crq(adapter, &crq);
1559         wait_for_completion(&adapter->fw_done);
1560         /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1561         return adapter->fw_done_rc ? -EIO : 0;
1562 }
1563
1564 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1565 {
1566         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1567         struct sockaddr *addr = p;
1568         int rc;
1569
1570         if (adapter->state == VNIC_PROBED) {
1571                 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1572                 adapter->mac_change_pending = true;
1573                 return 0;
1574         }
1575
1576         rc = __ibmvnic_set_mac(netdev, addr);
1577
1578         return rc;
1579 }
1580
1581 /**
1582  * do_reset returns zero if we are able to keep processing reset events, or
1583  * non-zero if we hit a fatal error and must halt.
1584  */
1585 static int do_reset(struct ibmvnic_adapter *adapter,
1586                     struct ibmvnic_rwi *rwi, u32 reset_state)
1587 {
1588         u64 old_num_rx_queues, old_num_tx_queues;
1589         struct net_device *netdev = adapter->netdev;
1590         int i, rc;
1591
1592         netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1593                    rwi->reset_reason);
1594
1595         netif_carrier_off(netdev);
1596         adapter->reset_reason = rwi->reset_reason;
1597
1598         old_num_rx_queues = adapter->req_rx_queues;
1599         old_num_tx_queues = adapter->req_tx_queues;
1600
1601         if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1602                 rc = ibmvnic_reenable_crq_queue(adapter);
1603                 if (rc)
1604                         return 0;
1605         }
1606
1607         rc = __ibmvnic_close(netdev);
1608         if (rc)
1609                 return rc;
1610
1611         if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1612             adapter->wait_for_reset) {
1613                 release_resources(adapter);
1614                 release_sub_crqs(adapter);
1615                 release_crq_queue(adapter);
1616         }
1617
1618         if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1619                 /* remove the closed state so when we call open it appears
1620                  * we are coming from the probed state.
1621                  */
1622                 adapter->state = VNIC_PROBED;
1623
1624                 rc = ibmvnic_init(adapter);
1625                 if (rc)
1626                         return IBMVNIC_INIT_FAILED;
1627
1628                 /* If the adapter was in PROBE state prior to the reset,
1629                  * exit here.
1630                  */
1631                 if (reset_state == VNIC_PROBED)
1632                         return 0;
1633
1634                 rc = ibmvnic_login(netdev);
1635                 if (rc) {
1636                         adapter->state = VNIC_PROBED;
1637                         return 0;
1638                 }
1639
1640                 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1641                     adapter->wait_for_reset) {
1642                         rc = init_resources(adapter);
1643                         if (rc)
1644                                 return rc;
1645                 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1646                            adapter->req_tx_queues != old_num_tx_queues) {
1647                         release_rx_pools(adapter);
1648                         release_tx_pools(adapter);
1649                         init_rx_pools(netdev);
1650                         init_tx_pools(netdev);
1651                 } else {
1652                         rc = reset_tx_pools(adapter);
1653                         if (rc)
1654                                 return rc;
1655
1656                         rc = reset_rx_pools(adapter);
1657                         if (rc)
1658                                 return rc;
1659
1660                         if (reset_state == VNIC_CLOSED)
1661                                 return 0;
1662                 }
1663         }
1664
1665         rc = __ibmvnic_open(netdev);
1666         if (rc) {
1667                 if (list_empty(&adapter->rwi_list))
1668                         adapter->state = VNIC_CLOSED;
1669                 else
1670                         adapter->state = reset_state;
1671
1672                 return 0;
1673         }
1674
1675         netif_carrier_on(netdev);
1676
1677         /* kick napi */
1678         for (i = 0; i < adapter->req_rx_queues; i++)
1679                 napi_schedule(&adapter->napi[i]);
1680
1681         if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1682                 netdev_notify_peers(netdev);
1683
1684         return 0;
1685 }
1686
1687 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1688 {
1689         struct ibmvnic_rwi *rwi;
1690
1691         mutex_lock(&adapter->rwi_lock);
1692
1693         if (!list_empty(&adapter->rwi_list)) {
1694                 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1695                                        list);
1696                 list_del(&rwi->list);
1697         } else {
1698                 rwi = NULL;
1699         }
1700
1701         mutex_unlock(&adapter->rwi_lock);
1702         return rwi;
1703 }
1704
1705 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1706 {
1707         struct ibmvnic_rwi *rwi;
1708
1709         rwi = get_next_rwi(adapter);
1710         while (rwi) {
1711                 kfree(rwi);
1712                 rwi = get_next_rwi(adapter);
1713         }
1714 }
1715
1716 static void __ibmvnic_reset(struct work_struct *work)
1717 {
1718         struct ibmvnic_rwi *rwi;
1719         struct ibmvnic_adapter *adapter;
1720         struct net_device *netdev;
1721         u32 reset_state;
1722         int rc = 0;
1723
1724         adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1725         netdev = adapter->netdev;
1726
1727         mutex_lock(&adapter->reset_lock);
1728         adapter->resetting = true;
1729         reset_state = adapter->state;
1730
1731         rwi = get_next_rwi(adapter);
1732         while (rwi) {
1733                 rc = do_reset(adapter, rwi, reset_state);
1734                 kfree(rwi);
1735                 if (rc && rc != IBMVNIC_INIT_FAILED)
1736                         break;
1737
1738                 rwi = get_next_rwi(adapter);
1739         }
1740
1741         if (adapter->wait_for_reset) {
1742                 adapter->wait_for_reset = false;
1743                 adapter->reset_done_rc = rc;
1744                 complete(&adapter->reset_done);
1745         }
1746
1747         if (rc) {
1748                 netdev_dbg(adapter->netdev, "Reset failed\n");
1749                 free_all_rwi(adapter);
1750                 mutex_unlock(&adapter->reset_lock);
1751                 return;
1752         }
1753
1754         adapter->resetting = false;
1755         mutex_unlock(&adapter->reset_lock);
1756 }
1757
1758 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1759                           enum ibmvnic_reset_reason reason)
1760 {
1761         struct ibmvnic_rwi *rwi, *tmp;
1762         struct net_device *netdev = adapter->netdev;
1763         struct list_head *entry;
1764
1765         if (adapter->state == VNIC_REMOVING ||
1766             adapter->state == VNIC_REMOVED) {
1767                 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1768                 return;
1769         }
1770
1771         if (adapter->state == VNIC_PROBING) {
1772                 netdev_warn(netdev, "Adapter reset during probe\n");
1773                 adapter->init_done_rc = EAGAIN;
1774                 return;
1775         }
1776
1777         mutex_lock(&adapter->rwi_lock);
1778
1779         list_for_each(entry, &adapter->rwi_list) {
1780                 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1781                 if (tmp->reset_reason == reason) {
1782                         netdev_dbg(netdev, "Skipping matching reset\n");
1783                         mutex_unlock(&adapter->rwi_lock);
1784                         return;
1785                 }
1786         }
1787
1788         rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1789         if (!rwi) {
1790                 mutex_unlock(&adapter->rwi_lock);
1791                 ibmvnic_close(netdev);
1792                 return;
1793         }
1794
1795         rwi->reset_reason = reason;
1796         list_add_tail(&rwi->list, &adapter->rwi_list);
1797         mutex_unlock(&adapter->rwi_lock);
1798
1799         netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1800         schedule_work(&adapter->ibmvnic_reset);
1801 }
1802
1803 static void ibmvnic_tx_timeout(struct net_device *dev)
1804 {
1805         struct ibmvnic_adapter *adapter = netdev_priv(dev);
1806
1807         ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1808 }
1809
1810 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1811                                   struct ibmvnic_rx_buff *rx_buff)
1812 {
1813         struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1814
1815         rx_buff->skb = NULL;
1816
1817         pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1818         pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1819
1820         atomic_dec(&pool->available);
1821 }
1822
1823 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1824 {
1825         struct net_device *netdev = napi->dev;
1826         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1827         int scrq_num = (int)(napi - adapter->napi);
1828         int frames_processed = 0;
1829
1830 restart_poll:
1831         while (frames_processed < budget) {
1832                 struct sk_buff *skb;
1833                 struct ibmvnic_rx_buff *rx_buff;
1834                 union sub_crq *next;
1835                 u32 length;
1836                 u16 offset;
1837                 u8 flags = 0;
1838
1839                 if (unlikely(adapter->resetting &&
1840                              adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1841                         enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1842                         napi_complete_done(napi, frames_processed);
1843                         return frames_processed;
1844                 }
1845
1846                 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1847                         break;
1848                 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1849                 rx_buff =
1850                     (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1851                                                           rx_comp.correlator);
1852                 /* do error checking */
1853                 if (next->rx_comp.rc) {
1854                         netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1855                                    be16_to_cpu(next->rx_comp.rc));
1856                         /* free the entry */
1857                         next->rx_comp.first = 0;
1858                         remove_buff_from_pool(adapter, rx_buff);
1859                         continue;
1860                 }
1861
1862                 length = be32_to_cpu(next->rx_comp.len);
1863                 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1864                 flags = next->rx_comp.flags;
1865                 skb = rx_buff->skb;
1866                 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1867                                         length);
1868
1869                 /* VLAN Header has been stripped by the system firmware and
1870                  * needs to be inserted by the driver
1871                  */
1872                 if (adapter->rx_vlan_header_insertion &&
1873                     (flags & IBMVNIC_VLAN_STRIPPED))
1874                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1875                                                ntohs(next->rx_comp.vlan_tci));
1876
1877                 /* free the entry */
1878                 next->rx_comp.first = 0;
1879                 remove_buff_from_pool(adapter, rx_buff);
1880
1881                 skb_put(skb, length);
1882                 skb->protocol = eth_type_trans(skb, netdev);
1883                 skb_record_rx_queue(skb, scrq_num);
1884
1885                 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1886                     flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1887                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1888                 }
1889
1890                 length = skb->len;
1891                 napi_gro_receive(napi, skb); /* send it up */
1892                 netdev->stats.rx_packets++;
1893                 netdev->stats.rx_bytes += length;
1894                 adapter->rx_stats_buffers[scrq_num].packets++;
1895                 adapter->rx_stats_buffers[scrq_num].bytes += length;
1896                 frames_processed++;
1897         }
1898
1899         if (adapter->state != VNIC_CLOSING)
1900                 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1901
1902         if (frames_processed < budget) {
1903                 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1904                 napi_complete_done(napi, frames_processed);
1905                 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1906                     napi_reschedule(napi)) {
1907                         disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1908                         goto restart_poll;
1909                 }
1910         }
1911         return frames_processed;
1912 }
1913
1914 #ifdef CONFIG_NET_POLL_CONTROLLER
1915 static void ibmvnic_netpoll_controller(struct net_device *dev)
1916 {
1917         struct ibmvnic_adapter *adapter = netdev_priv(dev);
1918         int i;
1919
1920         replenish_pools(netdev_priv(dev));
1921         for (i = 0; i < adapter->req_rx_queues; i++)
1922                 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1923                                      adapter->rx_scrq[i]);
1924 }
1925 #endif
1926
1927 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1928 {
1929         adapter->fallback.mtu = adapter->req_mtu;
1930         adapter->fallback.rx_queues = adapter->req_rx_queues;
1931         adapter->fallback.tx_queues = adapter->req_tx_queues;
1932         adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1933         adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1934
1935         init_completion(&adapter->reset_done);
1936         ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1937         adapter->wait_for_reset = true;
1938         wait_for_completion(&adapter->reset_done);
1939
1940         if (adapter->reset_done_rc) {
1941                 adapter->desired.mtu = adapter->fallback.mtu;
1942                 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1943                 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1944                 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1945                 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1946
1947                 init_completion(&adapter->reset_done);
1948                 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1949                 wait_for_completion(&adapter->reset_done);
1950         }
1951         adapter->wait_for_reset = false;
1952
1953         return adapter->reset_done_rc;
1954 }
1955
1956 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1957 {
1958         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1959
1960         adapter->desired.mtu = new_mtu + ETH_HLEN;
1961
1962         return wait_for_reset(adapter);
1963 }
1964
1965 static const struct net_device_ops ibmvnic_netdev_ops = {
1966         .ndo_open               = ibmvnic_open,
1967         .ndo_stop               = ibmvnic_close,
1968         .ndo_start_xmit         = ibmvnic_xmit,
1969         .ndo_set_rx_mode        = ibmvnic_set_multi,
1970         .ndo_set_mac_address    = ibmvnic_set_mac,
1971         .ndo_validate_addr      = eth_validate_addr,
1972         .ndo_tx_timeout         = ibmvnic_tx_timeout,
1973 #ifdef CONFIG_NET_POLL_CONTROLLER
1974         .ndo_poll_controller    = ibmvnic_netpoll_controller,
1975 #endif
1976         .ndo_change_mtu         = ibmvnic_change_mtu,
1977 };
1978
1979 /* ethtool functions */
1980
1981 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1982                                       struct ethtool_link_ksettings *cmd)
1983 {
1984         u32 supported, advertising;
1985
1986         supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1987                           SUPPORTED_FIBRE);
1988         advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1989                             ADVERTISED_FIBRE);
1990         cmd->base.speed = SPEED_1000;
1991         cmd->base.duplex = DUPLEX_FULL;
1992         cmd->base.port = PORT_FIBRE;
1993         cmd->base.phy_address = 0;
1994         cmd->base.autoneg = AUTONEG_ENABLE;
1995
1996         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1997                                                 supported);
1998         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1999                                                 advertising);
2000
2001         return 0;
2002 }
2003
2004 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2005                                 struct ethtool_drvinfo *info)
2006 {
2007         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2008
2009         strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2010         strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2011         strlcpy(info->fw_version, adapter->fw_version,
2012                 sizeof(info->fw_version));
2013 }
2014
2015 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2016 {
2017         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2018
2019         return adapter->msg_enable;
2020 }
2021
2022 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2023 {
2024         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2025
2026         adapter->msg_enable = data;
2027 }
2028
2029 static u32 ibmvnic_get_link(struct net_device *netdev)
2030 {
2031         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2032
2033         /* Don't need to send a query because we request a logical link up at
2034          * init and then we wait for link state indications
2035          */
2036         return adapter->logical_link_state;
2037 }
2038
2039 static void ibmvnic_get_ringparam(struct net_device *netdev,
2040                                   struct ethtool_ringparam *ring)
2041 {
2042         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2043
2044         ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2045         ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2046         ring->rx_mini_max_pending = 0;
2047         ring->rx_jumbo_max_pending = 0;
2048         ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2049         ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2050         ring->rx_mini_pending = 0;
2051         ring->rx_jumbo_pending = 0;
2052 }
2053
2054 static int ibmvnic_set_ringparam(struct net_device *netdev,
2055                                  struct ethtool_ringparam *ring)
2056 {
2057         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2058
2059         if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq  ||
2060             ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2061                 netdev_err(netdev, "Invalid request.\n");
2062                 netdev_err(netdev, "Max tx buffers = %llu\n",
2063                            adapter->max_rx_add_entries_per_subcrq);
2064                 netdev_err(netdev, "Max rx buffers = %llu\n",
2065                            adapter->max_tx_entries_per_subcrq);
2066                 return -EINVAL;
2067         }
2068
2069         adapter->desired.rx_entries = ring->rx_pending;
2070         adapter->desired.tx_entries = ring->tx_pending;
2071
2072         return wait_for_reset(adapter);
2073 }
2074
2075 static void ibmvnic_get_channels(struct net_device *netdev,
2076                                  struct ethtool_channels *channels)
2077 {
2078         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2079
2080         channels->max_rx = adapter->max_rx_queues;
2081         channels->max_tx = adapter->max_tx_queues;
2082         channels->max_other = 0;
2083         channels->max_combined = 0;
2084         channels->rx_count = adapter->req_rx_queues;
2085         channels->tx_count = adapter->req_tx_queues;
2086         channels->other_count = 0;
2087         channels->combined_count = 0;
2088 }
2089
2090 static int ibmvnic_set_channels(struct net_device *netdev,
2091                                 struct ethtool_channels *channels)
2092 {
2093         struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2094
2095         adapter->desired.rx_queues = channels->rx_count;
2096         adapter->desired.tx_queues = channels->tx_count;
2097
2098         return wait_for_reset(adapter);
2099 }
2100
2101 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2102 {
2103         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2104         int i;
2105
2106         if (stringset != ETH_SS_STATS)
2107                 return;
2108
2109         for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2110                 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2111
2112         for (i = 0; i < adapter->req_tx_queues; i++) {
2113                 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2114                 data += ETH_GSTRING_LEN;
2115
2116                 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2117                 data += ETH_GSTRING_LEN;
2118
2119                 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2120                 data += ETH_GSTRING_LEN;
2121         }
2122
2123         for (i = 0; i < adapter->req_rx_queues; i++) {
2124                 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2125                 data += ETH_GSTRING_LEN;
2126
2127                 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2128                 data += ETH_GSTRING_LEN;
2129
2130                 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2131                 data += ETH_GSTRING_LEN;
2132         }
2133 }
2134
2135 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2136 {
2137         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2138
2139         switch (sset) {
2140         case ETH_SS_STATS:
2141                 return ARRAY_SIZE(ibmvnic_stats) +
2142                        adapter->req_tx_queues * NUM_TX_STATS +
2143                        adapter->req_rx_queues * NUM_RX_STATS;
2144         default:
2145                 return -EOPNOTSUPP;
2146         }
2147 }
2148
2149 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2150                                       struct ethtool_stats *stats, u64 *data)
2151 {
2152         struct ibmvnic_adapter *adapter = netdev_priv(dev);
2153         union ibmvnic_crq crq;
2154         int i, j;
2155
2156         memset(&crq, 0, sizeof(crq));
2157         crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2158         crq.request_statistics.cmd = REQUEST_STATISTICS;
2159         crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2160         crq.request_statistics.len =
2161             cpu_to_be32(sizeof(struct ibmvnic_statistics));
2162
2163         /* Wait for data to be written */
2164         init_completion(&adapter->stats_done);
2165         ibmvnic_send_crq(adapter, &crq);
2166         wait_for_completion(&adapter->stats_done);
2167
2168         for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2169                 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2170                                                 ibmvnic_stats[i].offset));
2171
2172         for (j = 0; j < adapter->req_tx_queues; j++) {
2173                 data[i] = adapter->tx_stats_buffers[j].packets;
2174                 i++;
2175                 data[i] = adapter->tx_stats_buffers[j].bytes;
2176                 i++;
2177                 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2178                 i++;
2179         }
2180
2181         for (j = 0; j < adapter->req_rx_queues; j++) {
2182                 data[i] = adapter->rx_stats_buffers[j].packets;
2183                 i++;
2184                 data[i] = adapter->rx_stats_buffers[j].bytes;
2185                 i++;
2186                 data[i] = adapter->rx_stats_buffers[j].interrupts;
2187                 i++;
2188         }
2189 }
2190
2191 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2192         .get_drvinfo            = ibmvnic_get_drvinfo,
2193         .get_msglevel           = ibmvnic_get_msglevel,
2194         .set_msglevel           = ibmvnic_set_msglevel,
2195         .get_link               = ibmvnic_get_link,
2196         .get_ringparam          = ibmvnic_get_ringparam,
2197         .set_ringparam          = ibmvnic_set_ringparam,
2198         .get_channels           = ibmvnic_get_channels,
2199         .set_channels           = ibmvnic_set_channels,
2200         .get_strings            = ibmvnic_get_strings,
2201         .get_sset_count         = ibmvnic_get_sset_count,
2202         .get_ethtool_stats      = ibmvnic_get_ethtool_stats,
2203         .get_link_ksettings     = ibmvnic_get_link_ksettings,
2204 };
2205
2206 /* Routines for managing CRQs/sCRQs  */
2207
2208 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2209                                    struct ibmvnic_sub_crq_queue *scrq)
2210 {
2211         int rc;
2212
2213         if (scrq->irq) {
2214                 free_irq(scrq->irq, scrq);
2215                 irq_dispose_mapping(scrq->irq);
2216                 scrq->irq = 0;
2217         }
2218
2219         memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2220         scrq->cur = 0;
2221
2222         rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2223                            4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2224         return rc;
2225 }
2226
2227 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2228 {
2229         int i, rc;
2230
2231         for (i = 0; i < adapter->req_tx_queues; i++) {
2232                 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2233                 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2234                 if (rc)
2235                         return rc;
2236         }
2237
2238         for (i = 0; i < adapter->req_rx_queues; i++) {
2239                 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2240                 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2241                 if (rc)
2242                         return rc;
2243         }
2244
2245         return rc;
2246 }
2247
2248 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2249                                   struct ibmvnic_sub_crq_queue *scrq)
2250 {
2251         struct device *dev = &adapter->vdev->dev;
2252         long rc;
2253
2254         netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2255
2256         /* Close the sub-crqs */
2257         do {
2258                 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2259                                         adapter->vdev->unit_address,
2260                                         scrq->crq_num);
2261         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2262
2263         if (rc) {
2264                 netdev_err(adapter->netdev,
2265                            "Failed to release sub-CRQ %16lx, rc = %ld\n",
2266                            scrq->crq_num, rc);
2267         }
2268
2269         dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2270                          DMA_BIDIRECTIONAL);
2271         free_pages((unsigned long)scrq->msgs, 2);
2272         kfree(scrq);
2273 }
2274
2275 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2276                                                         *adapter)
2277 {
2278         struct device *dev = &adapter->vdev->dev;
2279         struct ibmvnic_sub_crq_queue *scrq;
2280         int rc;
2281
2282         scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2283         if (!scrq)
2284                 return NULL;
2285
2286         scrq->msgs =
2287                 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2288         if (!scrq->msgs) {
2289                 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2290                 goto zero_page_failed;
2291         }
2292
2293         scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2294                                          DMA_BIDIRECTIONAL);
2295         if (dma_mapping_error(dev, scrq->msg_token)) {
2296                 dev_warn(dev, "Couldn't map crq queue messages page\n");
2297                 goto map_failed;
2298         }
2299
2300         rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2301                            4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2302
2303         if (rc == H_RESOURCE)
2304                 rc = ibmvnic_reset_crq(adapter);
2305
2306         if (rc == H_CLOSED) {
2307                 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2308         } else if (rc) {
2309                 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2310                 goto reg_failed;
2311         }
2312
2313         scrq->adapter = adapter;
2314         scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2315         spin_lock_init(&scrq->lock);
2316
2317         netdev_dbg(adapter->netdev,
2318                    "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2319                    scrq->crq_num, scrq->hw_irq, scrq->irq);
2320
2321         return scrq;
2322
2323 reg_failed:
2324         dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2325                          DMA_BIDIRECTIONAL);
2326 map_failed:
2327         free_pages((unsigned long)scrq->msgs, 2);
2328 zero_page_failed:
2329         kfree(scrq);
2330
2331         return NULL;
2332 }
2333
2334 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2335 {
2336         int i;
2337
2338         if (adapter->tx_scrq) {
2339                 for (i = 0; i < adapter->req_tx_queues; i++) {
2340                         if (!adapter->tx_scrq[i])
2341                                 continue;
2342
2343                         netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2344                                    i);
2345                         if (adapter->tx_scrq[i]->irq) {
2346                                 free_irq(adapter->tx_scrq[i]->irq,
2347                                          adapter->tx_scrq[i]);
2348                                 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2349                                 adapter->tx_scrq[i]->irq = 0;
2350                         }
2351
2352                         release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2353                 }
2354
2355                 kfree(adapter->tx_scrq);
2356                 adapter->tx_scrq = NULL;
2357         }
2358
2359         if (adapter->rx_scrq) {
2360                 for (i = 0; i < adapter->req_rx_queues; i++) {
2361                         if (!adapter->rx_scrq[i])
2362                                 continue;
2363
2364                         netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2365                                    i);
2366                         if (adapter->rx_scrq[i]->irq) {
2367                                 free_irq(adapter->rx_scrq[i]->irq,
2368                                          adapter->rx_scrq[i]);
2369                                 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2370                                 adapter->rx_scrq[i]->irq = 0;
2371                         }
2372
2373                         release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2374                 }
2375
2376                 kfree(adapter->rx_scrq);
2377                 adapter->rx_scrq = NULL;
2378         }
2379 }
2380
2381 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2382                             struct ibmvnic_sub_crq_queue *scrq)
2383 {
2384         struct device *dev = &adapter->vdev->dev;
2385         unsigned long rc;
2386
2387         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2388                                 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2389         if (rc)
2390                 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2391                         scrq->hw_irq, rc);
2392         return rc;
2393 }
2394
2395 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2396                            struct ibmvnic_sub_crq_queue *scrq)
2397 {
2398         struct device *dev = &adapter->vdev->dev;
2399         unsigned long rc;
2400
2401         if (scrq->hw_irq > 0x100000000ULL) {
2402                 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2403                 return 1;
2404         }
2405
2406         rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2407                                 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2408         if (rc)
2409                 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2410                         scrq->hw_irq, rc);
2411         return rc;
2412 }
2413
2414 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2415                                struct ibmvnic_sub_crq_queue *scrq)
2416 {
2417         struct device *dev = &adapter->vdev->dev;
2418         struct ibmvnic_tx_buff *txbuff;
2419         union sub_crq *next;
2420         int index;
2421         int i, j;
2422         u8 first;
2423
2424 restart_loop:
2425         while (pending_scrq(adapter, scrq)) {
2426                 unsigned int pool = scrq->pool_index;
2427
2428                 next = ibmvnic_next_scrq(adapter, scrq);
2429                 for (i = 0; i < next->tx_comp.num_comps; i++) {
2430                         if (next->tx_comp.rcs[i]) {
2431                                 dev_err(dev, "tx error %x\n",
2432                                         next->tx_comp.rcs[i]);
2433                                 continue;
2434                         }
2435                         index = be32_to_cpu(next->tx_comp.correlators[i]);
2436                         txbuff = &adapter->tx_pool[pool].tx_buff[index];
2437
2438                         for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2439                                 if (!txbuff->data_dma[j])
2440                                         continue;
2441
2442                                 txbuff->data_dma[j] = 0;
2443                         }
2444                         /* if sub_crq was sent indirectly */
2445                         first = txbuff->indir_arr[0].generic.first;
2446                         if (first == IBMVNIC_CRQ_CMD) {
2447                                 dma_unmap_single(dev, txbuff->indir_dma,
2448                                                  sizeof(txbuff->indir_arr),
2449                                                  DMA_TO_DEVICE);
2450                         }
2451
2452                         if (txbuff->last_frag) {
2453                                 dev_kfree_skb_any(txbuff->skb);
2454                                 txbuff->skb = NULL;
2455                         }
2456
2457                         adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2458                                                      producer_index] = index;
2459                         adapter->tx_pool[pool].producer_index =
2460                             (adapter->tx_pool[pool].producer_index + 1) %
2461                             adapter->req_tx_entries_per_subcrq;
2462                 }
2463                 /* remove tx_comp scrq*/
2464                 next->tx_comp.first = 0;
2465
2466                 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2467                     (adapter->req_tx_entries_per_subcrq / 2) &&
2468                     __netif_subqueue_stopped(adapter->netdev,
2469                                              scrq->pool_index)) {
2470                         netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2471                         netdev_info(adapter->netdev, "Started queue %d\n",
2472                                     scrq->pool_index);
2473                 }
2474         }
2475
2476         enable_scrq_irq(adapter, scrq);
2477
2478         if (pending_scrq(adapter, scrq)) {
2479                 disable_scrq_irq(adapter, scrq);
2480                 goto restart_loop;
2481         }
2482
2483         return 0;
2484 }
2485
2486 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2487 {
2488         struct ibmvnic_sub_crq_queue *scrq = instance;
2489         struct ibmvnic_adapter *adapter = scrq->adapter;
2490
2491         disable_scrq_irq(adapter, scrq);
2492         ibmvnic_complete_tx(adapter, scrq);
2493
2494         return IRQ_HANDLED;
2495 }
2496
2497 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2498 {
2499         struct ibmvnic_sub_crq_queue *scrq = instance;
2500         struct ibmvnic_adapter *adapter = scrq->adapter;
2501
2502         /* When booting a kdump kernel we can hit pending interrupts
2503          * prior to completing driver initialization.
2504          */
2505         if (unlikely(adapter->state != VNIC_OPEN))
2506                 return IRQ_NONE;
2507
2508         adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2509
2510         if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2511                 disable_scrq_irq(adapter, scrq);
2512                 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2513         }
2514
2515         return IRQ_HANDLED;
2516 }
2517
2518 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2519 {
2520         struct device *dev = &adapter->vdev->dev;
2521         struct ibmvnic_sub_crq_queue *scrq;
2522         int i = 0, j = 0;
2523         int rc = 0;
2524
2525         for (i = 0; i < adapter->req_tx_queues; i++) {
2526                 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2527                            i);
2528                 scrq = adapter->tx_scrq[i];
2529                 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2530
2531                 if (!scrq->irq) {
2532                         rc = -EINVAL;
2533                         dev_err(dev, "Error mapping irq\n");
2534                         goto req_tx_irq_failed;
2535                 }
2536
2537                 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2538                                  0, "ibmvnic_tx", scrq);
2539
2540                 if (rc) {
2541                         dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2542                                 scrq->irq, rc);
2543                         irq_dispose_mapping(scrq->irq);
2544                         goto req_rx_irq_failed;
2545                 }
2546         }
2547
2548         for (i = 0; i < adapter->req_rx_queues; i++) {
2549                 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2550                            i);
2551                 scrq = adapter->rx_scrq[i];
2552                 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2553                 if (!scrq->irq) {
2554                         rc = -EINVAL;
2555                         dev_err(dev, "Error mapping irq\n");
2556                         goto req_rx_irq_failed;
2557                 }
2558                 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2559                                  0, "ibmvnic_rx", scrq);
2560                 if (rc) {
2561                         dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2562                                 scrq->irq, rc);
2563                         irq_dispose_mapping(scrq->irq);
2564                         goto req_rx_irq_failed;
2565                 }
2566         }
2567         return rc;
2568
2569 req_rx_irq_failed:
2570         for (j = 0; j < i; j++) {
2571                 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2572                 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2573         }
2574         i = adapter->req_tx_queues;
2575 req_tx_irq_failed:
2576         for (j = 0; j < i; j++) {
2577                 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2578                 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2579         }
2580         release_sub_crqs(adapter);
2581         return rc;
2582 }
2583
2584 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2585 {
2586         struct device *dev = &adapter->vdev->dev;
2587         struct ibmvnic_sub_crq_queue **allqueues;
2588         int registered_queues = 0;
2589         int total_queues;
2590         int more = 0;
2591         int i;
2592
2593         total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2594
2595         allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2596         if (!allqueues)
2597                 return -1;
2598
2599         for (i = 0; i < total_queues; i++) {
2600                 allqueues[i] = init_sub_crq_queue(adapter);
2601                 if (!allqueues[i]) {
2602                         dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2603                         break;
2604                 }
2605                 registered_queues++;
2606         }
2607
2608         /* Make sure we were able to register the minimum number of queues */
2609         if (registered_queues <
2610             adapter->min_tx_queues + adapter->min_rx_queues) {
2611                 dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
2612                 goto tx_failed;
2613         }
2614
2615         /* Distribute the failed allocated queues*/
2616         for (i = 0; i < total_queues - registered_queues + more ; i++) {
2617                 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2618                 switch (i % 3) {
2619                 case 0:
2620                         if (adapter->req_rx_queues > adapter->min_rx_queues)
2621                                 adapter->req_rx_queues--;
2622                         else
2623                                 more++;
2624                         break;
2625                 case 1:
2626                         if (adapter->req_tx_queues > adapter->min_tx_queues)
2627                                 adapter->req_tx_queues--;
2628                         else
2629                                 more++;
2630                         break;
2631                 }
2632         }
2633
2634         adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2635                                    sizeof(*adapter->tx_scrq), GFP_KERNEL);
2636         if (!adapter->tx_scrq)
2637                 goto tx_failed;
2638
2639         for (i = 0; i < adapter->req_tx_queues; i++) {
2640                 adapter->tx_scrq[i] = allqueues[i];
2641                 adapter->tx_scrq[i]->pool_index = i;
2642         }
2643
2644         adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2645                                    sizeof(*adapter->rx_scrq), GFP_KERNEL);
2646         if (!adapter->rx_scrq)
2647                 goto rx_failed;
2648
2649         for (i = 0; i < adapter->req_rx_queues; i++) {
2650                 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2651                 adapter->rx_scrq[i]->scrq_num = i;
2652         }
2653
2654         kfree(allqueues);
2655         return 0;
2656
2657 rx_failed:
2658         kfree(adapter->tx_scrq);
2659         adapter->tx_scrq = NULL;
2660 tx_failed:
2661         for (i = 0; i < registered_queues; i++)
2662                 release_sub_crq_queue(adapter, allqueues[i]);
2663         kfree(allqueues);
2664         return -1;
2665 }
2666
2667 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2668 {
2669         struct device *dev = &adapter->vdev->dev;
2670         union ibmvnic_crq crq;
2671         int max_entries;
2672
2673         if (!retry) {
2674                 /* Sub-CRQ entries are 32 byte long */
2675                 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2676
2677                 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2678                     adapter->min_rx_add_entries_per_subcrq > entries_page) {
2679                         dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2680                         return;
2681                 }
2682
2683                 if (adapter->desired.mtu)
2684                         adapter->req_mtu = adapter->desired.mtu;
2685                 else
2686                         adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2687
2688                 if (!adapter->desired.tx_entries)
2689                         adapter->desired.tx_entries =
2690                                         adapter->max_tx_entries_per_subcrq;
2691                 if (!adapter->desired.rx_entries)
2692                         adapter->desired.rx_entries =
2693                                         adapter->max_rx_add_entries_per_subcrq;
2694
2695                 max_entries = IBMVNIC_MAX_LTB_SIZE /
2696                               (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2697
2698                 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2699                         adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2700                         adapter->desired.tx_entries = max_entries;
2701                 }
2702
2703                 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2704                         adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2705                         adapter->desired.rx_entries = max_entries;
2706                 }
2707
2708                 if (adapter->desired.tx_entries)
2709                         adapter->req_tx_entries_per_subcrq =
2710                                         adapter->desired.tx_entries;
2711                 else
2712                         adapter->req_tx_entries_per_subcrq =
2713                                         adapter->max_tx_entries_per_subcrq;
2714
2715                 if (adapter->desired.rx_entries)
2716                         adapter->req_rx_add_entries_per_subcrq =
2717                                         adapter->desired.rx_entries;
2718                 else
2719                         adapter->req_rx_add_entries_per_subcrq =
2720                                         adapter->max_rx_add_entries_per_subcrq;
2721
2722                 if (adapter->desired.tx_queues)
2723                         adapter->req_tx_queues =
2724                                         adapter->desired.tx_queues;
2725                 else
2726                         adapter->req_tx_queues =
2727                                         adapter->opt_tx_comp_sub_queues;
2728
2729                 if (adapter->desired.rx_queues)
2730                         adapter->req_rx_queues =
2731                                         adapter->desired.rx_queues;
2732                 else
2733                         adapter->req_rx_queues =
2734                                         adapter->opt_rx_comp_queues;
2735
2736                 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2737         }
2738
2739         memset(&crq, 0, sizeof(crq));
2740         crq.request_capability.first = IBMVNIC_CRQ_CMD;
2741         crq.request_capability.cmd = REQUEST_CAPABILITY;
2742
2743         crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2744         crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2745         atomic_inc(&adapter->running_cap_crqs);
2746         ibmvnic_send_crq(adapter, &crq);
2747
2748         crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2749         crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2750         atomic_inc(&adapter->running_cap_crqs);
2751         ibmvnic_send_crq(adapter, &crq);
2752
2753         crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2754         crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2755         atomic_inc(&adapter->running_cap_crqs);
2756         ibmvnic_send_crq(adapter, &crq);
2757
2758         crq.request_capability.capability =
2759             cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2760         crq.request_capability.number =
2761             cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2762         atomic_inc(&adapter->running_cap_crqs);
2763         ibmvnic_send_crq(adapter, &crq);
2764
2765         crq.request_capability.capability =
2766             cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2767         crq.request_capability.number =
2768             cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2769         atomic_inc(&adapter->running_cap_crqs);
2770         ibmvnic_send_crq(adapter, &crq);
2771
2772         crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2773         crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2774         atomic_inc(&adapter->running_cap_crqs);
2775         ibmvnic_send_crq(adapter, &crq);
2776
2777         if (adapter->netdev->flags & IFF_PROMISC) {
2778                 if (adapter->promisc_supported) {
2779                         crq.request_capability.capability =
2780                             cpu_to_be16(PROMISC_REQUESTED);
2781                         crq.request_capability.number = cpu_to_be64(1);
2782                         atomic_inc(&adapter->running_cap_crqs);
2783                         ibmvnic_send_crq(adapter, &crq);
2784                 }
2785         } else {
2786                 crq.request_capability.capability =
2787                     cpu_to_be16(PROMISC_REQUESTED);
2788                 crq.request_capability.number = cpu_to_be64(0);
2789                 atomic_inc(&adapter->running_cap_crqs);
2790                 ibmvnic_send_crq(adapter, &crq);
2791         }
2792 }
2793
2794 static int pending_scrq(struct ibmvnic_adapter *adapter,
2795                         struct ibmvnic_sub_crq_queue *scrq)
2796 {
2797         union sub_crq *entry = &scrq->msgs[scrq->cur];
2798
2799         if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2800                 return 1;
2801         else
2802                 return 0;
2803 }
2804
2805 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2806                                         struct ibmvnic_sub_crq_queue *scrq)
2807 {
2808         union sub_crq *entry;
2809         unsigned long flags;
2810
2811         spin_lock_irqsave(&scrq->lock, flags);
2812         entry = &scrq->msgs[scrq->cur];
2813         if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2814                 if (++scrq->cur == scrq->size)
2815                         scrq->cur = 0;
2816         } else {
2817                 entry = NULL;
2818         }
2819         spin_unlock_irqrestore(&scrq->lock, flags);
2820
2821         return entry;
2822 }
2823
2824 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2825 {
2826         struct ibmvnic_crq_queue *queue = &adapter->crq;
2827         union ibmvnic_crq *crq;
2828
2829         crq = &queue->msgs[queue->cur];
2830         if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2831                 if (++queue->cur == queue->size)
2832                         queue->cur = 0;
2833         } else {
2834                 crq = NULL;
2835         }
2836
2837         return crq;
2838 }
2839
2840 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2841                        union sub_crq *sub_crq)
2842 {
2843         unsigned int ua = adapter->vdev->unit_address;
2844         struct device *dev = &adapter->vdev->dev;
2845         u64 *u64_crq = (u64 *)sub_crq;
2846         int rc;
2847
2848         netdev_dbg(adapter->netdev,
2849                    "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2850                    (unsigned long int)cpu_to_be64(remote_handle),
2851                    (unsigned long int)cpu_to_be64(u64_crq[0]),
2852                    (unsigned long int)cpu_to_be64(u64_crq[1]),
2853                    (unsigned long int)cpu_to_be64(u64_crq[2]),
2854                    (unsigned long int)cpu_to_be64(u64_crq[3]));
2855
2856         /* Make sure the hypervisor sees the complete request */
2857         mb();
2858
2859         rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2860                                 cpu_to_be64(remote_handle),
2861                                 cpu_to_be64(u64_crq[0]),
2862                                 cpu_to_be64(u64_crq[1]),
2863                                 cpu_to_be64(u64_crq[2]),
2864                                 cpu_to_be64(u64_crq[3]));
2865
2866         if (rc) {
2867                 if (rc == H_CLOSED)
2868                         dev_warn(dev, "CRQ Queue closed\n");
2869                 dev_err(dev, "Send error (rc=%d)\n", rc);
2870         }
2871
2872         return rc;
2873 }
2874
2875 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2876                                 u64 remote_handle, u64 ioba, u64 num_entries)
2877 {
2878         unsigned int ua = adapter->vdev->unit_address;
2879         struct device *dev = &adapter->vdev->dev;
2880         int rc;
2881
2882         /* Make sure the hypervisor sees the complete request */
2883         mb();
2884         rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2885                                 cpu_to_be64(remote_handle),
2886                                 ioba, num_entries);
2887
2888         if (rc) {
2889                 if (rc == H_CLOSED)
2890                         dev_warn(dev, "CRQ Queue closed\n");
2891                 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2892         }
2893
2894         return rc;
2895 }
2896
2897 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2898                             union ibmvnic_crq *crq)
2899 {
2900         unsigned int ua = adapter->vdev->unit_address;
2901         struct device *dev = &adapter->vdev->dev;
2902         u64 *u64_crq = (u64 *)crq;
2903         int rc;
2904
2905         netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2906                    (unsigned long int)cpu_to_be64(u64_crq[0]),
2907                    (unsigned long int)cpu_to_be64(u64_crq[1]));
2908
2909         /* Make sure the hypervisor sees the complete request */
2910         mb();
2911
2912         rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2913                                 cpu_to_be64(u64_crq[0]),
2914                                 cpu_to_be64(u64_crq[1]));
2915
2916         if (rc) {
2917                 if (rc == H_CLOSED) {
2918                         dev_warn(dev, "CRQ Queue closed\n");
2919                         if (adapter->resetting)
2920                                 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2921                 }
2922
2923                 dev_warn(dev, "Send error (rc=%d)\n", rc);
2924         }
2925
2926         return rc;
2927 }
2928
2929 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2930 {
2931         union ibmvnic_crq crq;
2932
2933         memset(&crq, 0, sizeof(crq));
2934         crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2935         crq.generic.cmd = IBMVNIC_CRQ_INIT;
2936         netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2937
2938         return ibmvnic_send_crq(adapter, &crq);
2939 }
2940
2941 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2942 {
2943         union ibmvnic_crq crq;
2944
2945         memset(&crq, 0, sizeof(crq));
2946         crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2947         crq.version_exchange.cmd = VERSION_EXCHANGE;
2948         crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2949
2950         return ibmvnic_send_crq(adapter, &crq);
2951 }
2952
2953 struct vnic_login_client_data {
2954         u8      type;
2955         __be16  len;
2956         char    name;
2957 } __packed;
2958
2959 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2960 {
2961         int len;
2962
2963         /* Calculate the amount of buffer space needed for the
2964          * vnic client data in the login buffer. There are four entries,
2965          * OS name, LPAR name, device name, and a null last entry.
2966          */
2967         len = 4 * sizeof(struct vnic_login_client_data);
2968         len += 6; /* "Linux" plus NULL */
2969         len += strlen(utsname()->nodename) + 1;
2970         len += strlen(adapter->netdev->name) + 1;
2971
2972         return len;
2973 }
2974
2975 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2976                                  struct vnic_login_client_data *vlcd)
2977 {
2978         const char *os_name = "Linux";
2979         int len;
2980
2981         /* Type 1 - LPAR OS */
2982         vlcd->type = 1;
2983         len = strlen(os_name) + 1;
2984         vlcd->len = cpu_to_be16(len);
2985         strncpy(&vlcd->name, os_name, len);
2986         vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2987
2988         /* Type 2 - LPAR name */
2989         vlcd->type = 2;
2990         len = strlen(utsname()->nodename) + 1;
2991         vlcd->len = cpu_to_be16(len);
2992         strncpy(&vlcd->name, utsname()->nodename, len);
2993         vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2994
2995         /* Type 3 - device name */
2996         vlcd->type = 3;
2997         len = strlen(adapter->netdev->name) + 1;
2998         vlcd->len = cpu_to_be16(len);
2999         strncpy(&vlcd->name, adapter->netdev->name, len);
3000 }
3001
3002 static void send_login(struct ibmvnic_adapter *adapter)
3003 {
3004         struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3005         struct ibmvnic_login_buffer *login_buffer;
3006         struct device *dev = &adapter->vdev->dev;
3007         dma_addr_t rsp_buffer_token;
3008         dma_addr_t buffer_token;
3009         size_t rsp_buffer_size;
3010         union ibmvnic_crq crq;
3011         size_t buffer_size;
3012         __be64 *tx_list_p;
3013         __be64 *rx_list_p;
3014         int client_data_len;
3015         struct vnic_login_client_data *vlcd;
3016         int i;
3017
3018         client_data_len = vnic_client_data_len(adapter);
3019
3020         buffer_size =
3021             sizeof(struct ibmvnic_login_buffer) +
3022             sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3023             client_data_len;
3024
3025         login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3026         if (!login_buffer)
3027                 goto buf_alloc_failed;
3028
3029         buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3030                                       DMA_TO_DEVICE);
3031         if (dma_mapping_error(dev, buffer_token)) {
3032                 dev_err(dev, "Couldn't map login buffer\n");
3033                 goto buf_map_failed;
3034         }
3035
3036         rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3037                           sizeof(u64) * adapter->req_tx_queues +
3038                           sizeof(u64) * adapter->req_rx_queues +
3039                           sizeof(u64) * adapter->req_rx_queues +
3040                           sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3041
3042         login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3043         if (!login_rsp_buffer)
3044                 goto buf_rsp_alloc_failed;
3045
3046         rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3047                                           rsp_buffer_size, DMA_FROM_DEVICE);
3048         if (dma_mapping_error(dev, rsp_buffer_token)) {
3049                 dev_err(dev, "Couldn't map login rsp buffer\n");
3050                 goto buf_rsp_map_failed;
3051         }
3052
3053         adapter->login_buf = login_buffer;
3054         adapter->login_buf_token = buffer_token;
3055         adapter->login_buf_sz = buffer_size;
3056         adapter->login_rsp_buf = login_rsp_buffer;
3057         adapter->login_rsp_buf_token = rsp_buffer_token;
3058         adapter->login_rsp_buf_sz = rsp_buffer_size;
3059
3060         login_buffer->len = cpu_to_be32(buffer_size);
3061         login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3062         login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3063         login_buffer->off_txcomp_subcrqs =
3064             cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3065         login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3066         login_buffer->off_rxcomp_subcrqs =
3067             cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3068                         sizeof(u64) * adapter->req_tx_queues);
3069         login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3070         login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3071
3072         tx_list_p = (__be64 *)((char *)login_buffer +
3073                                       sizeof(struct ibmvnic_login_buffer));
3074         rx_list_p = (__be64 *)((char *)login_buffer +
3075                                       sizeof(struct ibmvnic_login_buffer) +
3076                                       sizeof(u64) * adapter->req_tx_queues);
3077
3078         for (i = 0; i < adapter->req_tx_queues; i++) {
3079                 if (adapter->tx_scrq[i]) {
3080                         tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3081                                                    crq_num);
3082                 }
3083         }
3084
3085         for (i = 0; i < adapter->req_rx_queues; i++) {
3086                 if (adapter->rx_scrq[i]) {
3087                         rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3088                                                    crq_num);
3089                 }
3090         }
3091
3092         /* Insert vNIC login client data */
3093         vlcd = (struct vnic_login_client_data *)
3094                 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3095         login_buffer->client_data_offset =
3096                         cpu_to_be32((char *)vlcd - (char *)login_buffer);
3097         login_buffer->client_data_len = cpu_to_be32(client_data_len);
3098
3099         vnic_add_client_data(adapter, vlcd);
3100
3101         netdev_dbg(adapter->netdev, "Login Buffer:\n");
3102         for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3103                 netdev_dbg(adapter->netdev, "%016lx\n",
3104                            ((unsigned long int *)(adapter->login_buf))[i]);
3105         }
3106
3107         memset(&crq, 0, sizeof(crq));
3108         crq.login.first = IBMVNIC_CRQ_CMD;
3109         crq.login.cmd = LOGIN;
3110         crq.login.ioba = cpu_to_be32(buffer_token);
3111         crq.login.len = cpu_to_be32(buffer_size);
3112         ibmvnic_send_crq(adapter, &crq);
3113
3114         return;
3115
3116 buf_rsp_map_failed:
3117         kfree(login_rsp_buffer);
3118 buf_rsp_alloc_failed:
3119         dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3120 buf_map_failed:
3121         kfree(login_buffer);
3122 buf_alloc_failed:
3123         return;
3124 }
3125
3126 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3127                              u32 len, u8 map_id)
3128 {
3129         union ibmvnic_crq crq;
3130
3131         memset(&crq, 0, sizeof(crq));
3132         crq.request_map.first = IBMVNIC_CRQ_CMD;
3133         crq.request_map.cmd = REQUEST_MAP;
3134         crq.request_map.map_id = map_id;
3135         crq.request_map.ioba = cpu_to_be32(addr);
3136         crq.request_map.len = cpu_to_be32(len);
3137         ibmvnic_send_crq(adapter, &crq);
3138 }
3139
3140 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3141 {
3142         union ibmvnic_crq crq;
3143
3144         memset(&crq, 0, sizeof(crq));
3145         crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3146         crq.request_unmap.cmd = REQUEST_UNMAP;
3147         crq.request_unmap.map_id = map_id;
3148         ibmvnic_send_crq(adapter, &crq);
3149 }
3150
3151 static void send_map_query(struct ibmvnic_adapter *adapter)
3152 {
3153         union ibmvnic_crq crq;
3154
3155         memset(&crq, 0, sizeof(crq));
3156         crq.query_map.first = IBMVNIC_CRQ_CMD;
3157         crq.query_map.cmd = QUERY_MAP;
3158         ibmvnic_send_crq(adapter, &crq);
3159 }
3160
3161 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3162 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3163 {
3164         union ibmvnic_crq crq;
3165
3166         atomic_set(&adapter->running_cap_crqs, 0);
3167         memset(&crq, 0, sizeof(crq));
3168         crq.query_capability.first = IBMVNIC_CRQ_CMD;
3169         crq.query_capability.cmd = QUERY_CAPABILITY;
3170
3171         crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3172         atomic_inc(&adapter->running_cap_crqs);
3173         ibmvnic_send_crq(adapter, &crq);
3174
3175         crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3176         atomic_inc(&adapter->running_cap_crqs);
3177         ibmvnic_send_crq(adapter, &crq);
3178
3179         crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3180         atomic_inc(&adapter->running_cap_crqs);
3181         ibmvnic_send_crq(adapter, &crq);
3182
3183         crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3184         atomic_inc(&adapter->running_cap_crqs);
3185         ibmvnic_send_crq(adapter, &crq);
3186
3187         crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3188         atomic_inc(&adapter->running_cap_crqs);
3189         ibmvnic_send_crq(adapter, &crq);
3190
3191         crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3192         atomic_inc(&adapter->running_cap_crqs);
3193         ibmvnic_send_crq(adapter, &crq);
3194
3195         crq.query_capability.capability =
3196             cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3197         atomic_inc(&adapter->running_cap_crqs);
3198         ibmvnic_send_crq(adapter, &crq);
3199
3200         crq.query_capability.capability =
3201             cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3202         atomic_inc(&adapter->running_cap_crqs);
3203         ibmvnic_send_crq(adapter, &crq);
3204
3205         crq.query_capability.capability =
3206             cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3207         atomic_inc(&adapter->running_cap_crqs);
3208         ibmvnic_send_crq(adapter, &crq);
3209
3210         crq.query_capability.capability =
3211             cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3212         atomic_inc(&adapter->running_cap_crqs);
3213         ibmvnic_send_crq(adapter, &crq);
3214
3215         crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3216         atomic_inc(&adapter->running_cap_crqs);
3217         ibmvnic_send_crq(adapter, &crq);
3218
3219         crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3220         atomic_inc(&adapter->running_cap_crqs);
3221         ibmvnic_send_crq(adapter, &crq);
3222