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