Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / drivers / net / ehea / ehea_main.c
1 /*
2  *  linux/drivers/net/ehea/ehea_main.c
3  *
4  *  eHEA ethernet device driver for IBM eServer System p
5  *
6  *  (C) Copyright IBM Corp. 2006
7  *
8  *  Authors:
9  *       Christoph Raisch <raisch@de.ibm.com>
10  *       Jan-Bernd Themann <themann@de.ibm.com>
11  *       Thomas Klein <tklein@de.ibm.com>
12  *
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/tcp.h>
32 #include <linux/udp.h>
33 #include <linux/if.h>
34 #include <linux/list.h>
35 #include <linux/if_ether.h>
36 #include <linux/notifier.h>
37 #include <linux/reboot.h>
38
39 #include <net/ip.h>
40
41 #include "ehea.h"
42 #include "ehea_qmr.h"
43 #include "ehea_phyp.h"
44
45
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
48 MODULE_DESCRIPTION("IBM eServer HEA Driver");
49 MODULE_VERSION(DRV_VERSION);
50
51
52 static int msg_level = -1;
53 static int rq1_entries = EHEA_DEF_ENTRIES_RQ1;
54 static int rq2_entries = EHEA_DEF_ENTRIES_RQ2;
55 static int rq3_entries = EHEA_DEF_ENTRIES_RQ3;
56 static int sq_entries = EHEA_DEF_ENTRIES_SQ;
57 static int use_mcs;
58 static int use_lro;
59 static int lro_max_aggr = EHEA_LRO_MAX_AGGR;
60 static int num_tx_qps = EHEA_NUM_TX_QP;
61 static int prop_carrier_state;
62
63 module_param(msg_level, int, 0);
64 module_param(rq1_entries, int, 0);
65 module_param(rq2_entries, int, 0);
66 module_param(rq3_entries, int, 0);
67 module_param(sq_entries, int, 0);
68 module_param(prop_carrier_state, int, 0);
69 module_param(use_mcs, int, 0);
70 module_param(use_lro, int, 0);
71 module_param(lro_max_aggr, int, 0);
72 module_param(num_tx_qps, int, 0);
73
74 MODULE_PARM_DESC(num_tx_qps, "Number of TX-QPS");
75 MODULE_PARM_DESC(msg_level, "msg_level");
76 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
77                  "port to stack. 1:yes, 0:no.  Default = 0 ");
78 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
79                  "[2^x - 1], x = [6..14]. Default = "
80                  __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
81 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
82                  "[2^x - 1], x = [6..14]. Default = "
83                  __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
84 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
85                  "[2^x - 1], x = [6..14]. Default = "
86                  __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
87 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue  "
88                  "[2^x - 1], x = [6..14]. Default = "
89                  __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
90 MODULE_PARM_DESC(use_mcs, " 0:NAPI, 1:Multiple receive queues, Default = 0 ");
91
92 MODULE_PARM_DESC(lro_max_aggr, " LRO: Max packets to be aggregated. Default = "
93                  __MODULE_STRING(EHEA_LRO_MAX_AGGR));
94 MODULE_PARM_DESC(use_lro, " Large Receive Offload, 1: enable, 0: disable, "
95                  "Default = 0");
96
97 static int port_name_cnt;
98 static LIST_HEAD(adapter_list);
99 u64 ehea_driver_flags;
100 struct work_struct ehea_rereg_mr_task;
101
102 struct semaphore dlpar_mem_lock;
103
104 static int __devinit ehea_probe_adapter(struct of_device *dev,
105                                         const struct of_device_id *id);
106
107 static int __devexit ehea_remove(struct of_device *dev);
108
109 static struct of_device_id ehea_device_table[] = {
110         {
111                 .name = "lhea",
112                 .compatible = "IBM,lhea",
113         },
114         {},
115 };
116
117 static struct of_platform_driver ehea_driver = {
118         .name = "ehea",
119         .match_table = ehea_device_table,
120         .probe = ehea_probe_adapter,
121         .remove = ehea_remove,
122 };
123
124 void ehea_dump(void *adr, int len, char *msg)
125 {
126         int x;
127         unsigned char *deb = adr;
128         for (x = 0; x < len; x += 16) {
129                 printk(DRV_NAME " %s adr=%p ofs=%04x %016lx %016lx\n", msg,
130                           deb, x, *((u64 *)&deb[0]), *((u64 *)&deb[8]));
131                 deb += 16;
132         }
133 }
134
135 static struct net_device_stats *ehea_get_stats(struct net_device *dev)
136 {
137         struct ehea_port *port = netdev_priv(dev);
138         struct net_device_stats *stats = &port->stats;
139         struct hcp_ehea_port_cb2 *cb2;
140         u64 hret, rx_packets, tx_packets;
141         int i;
142
143         memset(stats, 0, sizeof(*stats));
144
145         cb2 = kzalloc(PAGE_SIZE, GFP_KERNEL);
146         if (!cb2) {
147                 ehea_error("no mem for cb2");
148                 goto out;
149         }
150
151         hret = ehea_h_query_ehea_port(port->adapter->handle,
152                                       port->logical_port_id,
153                                       H_PORT_CB2, H_PORT_CB2_ALL, cb2);
154         if (hret != H_SUCCESS) {
155                 ehea_error("query_ehea_port failed");
156                 goto out_herr;
157         }
158
159         if (netif_msg_hw(port))
160                 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
161
162         rx_packets = 0;
163         for (i = 0; i < port->num_def_qps; i++)
164                 rx_packets += port->port_res[i].rx_packets;
165
166         tx_packets = 0;
167         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
168                 tx_packets += port->port_res[i].tx_packets;
169
170         stats->tx_packets = tx_packets;
171         stats->multicast = cb2->rxmcp;
172         stats->rx_errors = cb2->rxuerr;
173         stats->rx_bytes = cb2->rxo;
174         stats->tx_bytes = cb2->txo;
175         stats->rx_packets = rx_packets;
176
177 out_herr:
178         kfree(cb2);
179 out:
180         return stats;
181 }
182
183 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
184 {
185         struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
186         struct net_device *dev = pr->port->netdev;
187         int max_index_mask = pr->rq1_skba.len - 1;
188         int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
189         int adder = 0;
190         int i;
191
192         pr->rq1_skba.os_skbs = 0;
193
194         if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
195                 pr->rq1_skba.index = index;
196                 pr->rq1_skba.os_skbs = fill_wqes;
197                 return;
198         }
199
200         for (i = 0; i < fill_wqes; i++) {
201                 if (!skb_arr_rq1[index]) {
202                         skb_arr_rq1[index] = netdev_alloc_skb(dev,
203                                                               EHEA_L_PKT_SIZE);
204                         if (!skb_arr_rq1[index]) {
205                                 pr->rq1_skba.os_skbs = fill_wqes - i;
206                                 ehea_error("%s: no mem for skb/%d wqes filled",
207                                            dev->name, i);
208                                 break;
209                         }
210                 }
211                 index--;
212                 index &= max_index_mask;
213                 adder++;
214         }
215
216         if (adder == 0)
217                 return;
218
219         /* Ring doorbell */
220         ehea_update_rq1a(pr->qp, adder);
221 }
222
223 static int ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
224 {
225         int ret = 0;
226         struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
227         struct net_device *dev = pr->port->netdev;
228         int i;
229
230         for (i = 0; i < pr->rq1_skba.len; i++) {
231                 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
232                 if (!skb_arr_rq1[i]) {
233                         ehea_error("%s: no mem for skb/%d wqes filled",
234                                    dev->name, i);
235                         ret = -ENOMEM;
236                         goto out;
237                 }
238         }
239         /* Ring doorbell */
240         ehea_update_rq1a(pr->qp, nr_rq1a);
241 out:
242         return ret;
243 }
244
245 static int ehea_refill_rq_def(struct ehea_port_res *pr,
246                               struct ehea_q_skb_arr *q_skba, int rq_nr,
247                               int num_wqes, int wqe_type, int packet_size)
248 {
249         struct net_device *dev = pr->port->netdev;
250         struct ehea_qp *qp = pr->qp;
251         struct sk_buff **skb_arr = q_skba->arr;
252         struct ehea_rwqe *rwqe;
253         int i, index, max_index_mask, fill_wqes;
254         int adder = 0;
255         int ret = 0;
256
257         fill_wqes = q_skba->os_skbs + num_wqes;
258         q_skba->os_skbs = 0;
259
260         if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
261                 q_skba->os_skbs = fill_wqes;
262                 return ret;
263         }
264
265         index = q_skba->index;
266         max_index_mask = q_skba->len - 1;
267         for (i = 0; i < fill_wqes; i++) {
268                 u64 tmp_addr;
269                 struct sk_buff *skb = netdev_alloc_skb(dev, packet_size);
270                 if (!skb) {
271                         ehea_error("%s: no mem for skb/%d wqes filled",
272                                    pr->port->netdev->name, i);
273                         q_skba->os_skbs = fill_wqes - i;
274                         ret = -ENOMEM;
275                         break;
276                 }
277                 skb_reserve(skb, NET_IP_ALIGN);
278
279                 skb_arr[index] = skb;
280                 tmp_addr = ehea_map_vaddr(skb->data);
281                 if (tmp_addr == -1) {
282                         dev_kfree_skb(skb);
283                         q_skba->os_skbs = fill_wqes - i;
284                         ret = 0;
285                         break;
286                 }
287
288                 rwqe = ehea_get_next_rwqe(qp, rq_nr);
289                 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
290                             | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
291                 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
292                 rwqe->sg_list[0].vaddr = tmp_addr;
293                 rwqe->sg_list[0].len = packet_size;
294                 rwqe->data_segments = 1;
295
296                 index++;
297                 index &= max_index_mask;
298                 adder++;
299         }
300
301         q_skba->index = index;
302         if (adder == 0)
303                 goto out;
304
305         /* Ring doorbell */
306         iosync();
307         if (rq_nr == 2)
308                 ehea_update_rq2a(pr->qp, adder);
309         else
310                 ehea_update_rq3a(pr->qp, adder);
311 out:
312         return ret;
313 }
314
315
316 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
317 {
318         return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
319                                   nr_of_wqes, EHEA_RWQE2_TYPE,
320                                   EHEA_RQ2_PKT_SIZE + NET_IP_ALIGN);
321 }
322
323
324 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
325 {
326         return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
327                                   nr_of_wqes, EHEA_RWQE3_TYPE,
328                                   EHEA_MAX_PACKET_SIZE + NET_IP_ALIGN);
329 }
330
331 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
332 {
333         *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
334         if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
335                 return 0;
336         if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
337             (cqe->header_length == 0))
338                 return 0;
339         return -EINVAL;
340 }
341
342 static inline void ehea_fill_skb(struct net_device *dev,
343                                  struct sk_buff *skb, struct ehea_cqe *cqe)
344 {
345         int length = cqe->num_bytes_transfered - 4;     /*remove CRC */
346
347         skb_put(skb, length);
348         skb->ip_summed = CHECKSUM_UNNECESSARY;
349         skb->protocol = eth_type_trans(skb, dev);
350 }
351
352 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
353                                                int arr_len,
354                                                struct ehea_cqe *cqe)
355 {
356         int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
357         struct sk_buff *skb;
358         void *pref;
359         int x;
360
361         x = skb_index + 1;
362         x &= (arr_len - 1);
363
364         pref = skb_array[x];
365         prefetchw(pref);
366         prefetchw(pref + EHEA_CACHE_LINE);
367
368         pref = (skb_array[x]->data);
369         prefetch(pref);
370         prefetch(pref + EHEA_CACHE_LINE);
371         prefetch(pref + EHEA_CACHE_LINE * 2);
372         prefetch(pref + EHEA_CACHE_LINE * 3);
373         skb = skb_array[skb_index];
374         skb_array[skb_index] = NULL;
375         return skb;
376 }
377
378 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
379                                                   int arr_len, int wqe_index)
380 {
381         struct sk_buff *skb;
382         void *pref;
383         int x;
384
385         x = wqe_index + 1;
386         x &= (arr_len - 1);
387
388         pref = skb_array[x];
389         prefetchw(pref);
390         prefetchw(pref + EHEA_CACHE_LINE);
391
392         pref = (skb_array[x]->data);
393         prefetchw(pref);
394         prefetchw(pref + EHEA_CACHE_LINE);
395
396         skb = skb_array[wqe_index];
397         skb_array[wqe_index] = NULL;
398         return skb;
399 }
400
401 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
402                                  struct ehea_cqe *cqe, int *processed_rq2,
403                                  int *processed_rq3)
404 {
405         struct sk_buff *skb;
406
407         if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
408                 pr->p_stats.err_tcp_cksum++;
409         if (cqe->status & EHEA_CQE_STAT_ERR_IP)
410                 pr->p_stats.err_ip_cksum++;
411         if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
412                 pr->p_stats.err_frame_crc++;
413
414         if (rq == 2) {
415                 *processed_rq2 += 1;
416                 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
417                 dev_kfree_skb(skb);
418         } else if (rq == 3) {
419                 *processed_rq3 += 1;
420                 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
421                 dev_kfree_skb(skb);
422         }
423
424         if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
425                 if (netif_msg_rx_err(pr->port)) {
426                         ehea_error("Critical receive error for QP %d. "
427                                    "Resetting port.", pr->qp->init_attr.qp_nr);
428                         ehea_dump(cqe, sizeof(*cqe), "CQE");
429                 }
430                 schedule_work(&pr->port->reset_task);
431                 return 1;
432         }
433
434         return 0;
435 }
436
437 static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
438                        void **tcph, u64 *hdr_flags, void *priv)
439 {
440         struct ehea_cqe *cqe = priv;
441         unsigned int ip_len;
442         struct iphdr *iph;
443
444         /* non tcp/udp packets */
445         if (!cqe->header_length)
446                 return -1;
447
448         /* non tcp packet */
449         skb_reset_network_header(skb);
450         iph = ip_hdr(skb);
451         if (iph->protocol != IPPROTO_TCP)
452                 return -1;
453
454         ip_len = ip_hdrlen(skb);
455         skb_set_transport_header(skb, ip_len);
456         *tcph = tcp_hdr(skb);
457
458         /* check if ip header and tcp header are complete */
459         if (iph->tot_len < ip_len + tcp_hdrlen(skb))
460                 return -1;
461
462         *hdr_flags = LRO_IPV4 | LRO_TCP;
463         *iphdr = iph;
464
465         return 0;
466 }
467
468 static void ehea_proc_skb(struct ehea_port_res *pr, struct ehea_cqe *cqe,
469                           struct sk_buff *skb)
470 {
471         int vlan_extracted = (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
472                 && pr->port->vgrp;
473
474         if (use_lro) {
475                 if (vlan_extracted)
476                         lro_vlan_hwaccel_receive_skb(&pr->lro_mgr, skb,
477                                                      pr->port->vgrp,
478                                                      cqe->vlan_tag,
479                                                      cqe);
480                 else
481                         lro_receive_skb(&pr->lro_mgr, skb, cqe);
482         } else {
483                 if (vlan_extracted)
484                         vlan_hwaccel_receive_skb(skb, pr->port->vgrp,
485                                                  cqe->vlan_tag);
486                 else
487                         netif_receive_skb(skb);
488         }
489 }
490
491 static int ehea_proc_rwqes(struct net_device *dev,
492                            struct ehea_port_res *pr,
493                            int budget)
494 {
495         struct ehea_port *port = pr->port;
496         struct ehea_qp *qp = pr->qp;
497         struct ehea_cqe *cqe;
498         struct sk_buff *skb;
499         struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
500         struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
501         struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
502         int skb_arr_rq1_len = pr->rq1_skba.len;
503         int skb_arr_rq2_len = pr->rq2_skba.len;
504         int skb_arr_rq3_len = pr->rq3_skba.len;
505         int processed, processed_rq1, processed_rq2, processed_rq3;
506         int wqe_index, last_wqe_index, rq, port_reset;
507
508         processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
509         last_wqe_index = 0;
510
511         cqe = ehea_poll_rq1(qp, &wqe_index);
512         while ((processed < budget) && cqe) {
513                 ehea_inc_rq1(qp);
514                 processed_rq1++;
515                 processed++;
516                 if (netif_msg_rx_status(port))
517                         ehea_dump(cqe, sizeof(*cqe), "CQE");
518
519                 last_wqe_index = wqe_index;
520                 rmb();
521                 if (!ehea_check_cqe(cqe, &rq)) {
522                         if (rq == 1) {
523                                 /* LL RQ1 */
524                                 skb = get_skb_by_index_ll(skb_arr_rq1,
525                                                           skb_arr_rq1_len,
526                                                           wqe_index);
527                                 if (unlikely(!skb)) {
528                                         if (netif_msg_rx_err(port))
529                                                 ehea_error("LL rq1: skb=NULL");
530
531                                         skb = netdev_alloc_skb(dev,
532                                                                EHEA_L_PKT_SIZE);
533                                         if (!skb)
534                                                 break;
535                                 }
536                                 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
537                                                  cqe->num_bytes_transfered - 4);
538                                 ehea_fill_skb(dev, skb, cqe);
539                         } else if (rq == 2) {
540                                 /* RQ2 */
541                                 skb = get_skb_by_index(skb_arr_rq2,
542                                                        skb_arr_rq2_len, cqe);
543                                 if (unlikely(!skb)) {
544                                         if (netif_msg_rx_err(port))
545                                                 ehea_error("rq2: skb=NULL");
546                                         break;
547                                 }
548                                 ehea_fill_skb(dev, skb, cqe);
549                                 processed_rq2++;
550                         } else {
551                                 /* RQ3 */
552                                 skb = get_skb_by_index(skb_arr_rq3,
553                                                        skb_arr_rq3_len, cqe);
554                                 if (unlikely(!skb)) {
555                                         if (netif_msg_rx_err(port))
556                                                 ehea_error("rq3: skb=NULL");
557                                         break;
558                                 }
559                                 ehea_fill_skb(dev, skb, cqe);
560                                 processed_rq3++;
561                         }
562
563                         ehea_proc_skb(pr, cqe, skb);
564                         dev->last_rx = jiffies;
565                 } else {
566                         pr->p_stats.poll_receive_errors++;
567                         port_reset = ehea_treat_poll_error(pr, rq, cqe,
568                                                            &processed_rq2,
569                                                            &processed_rq3);
570                         if (port_reset)
571                                 break;
572                 }
573                 cqe = ehea_poll_rq1(qp, &wqe_index);
574         }
575         if (use_lro)
576                 lro_flush_all(&pr->lro_mgr);
577
578         pr->rx_packets += processed;
579
580         ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
581         ehea_refill_rq2(pr, processed_rq2);
582         ehea_refill_rq3(pr, processed_rq3);
583
584         return processed;
585 }
586
587 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
588 {
589         struct sk_buff *skb;
590         struct ehea_cq *send_cq = pr->send_cq;
591         struct ehea_cqe *cqe;
592         int quota = my_quota;
593         int cqe_counter = 0;
594         int swqe_av = 0;
595         int index;
596         unsigned long flags;
597
598         cqe = ehea_poll_cq(send_cq);
599         while (cqe && (quota > 0)) {
600                 ehea_inc_cq(send_cq);
601
602                 cqe_counter++;
603                 rmb();
604                 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
605                         ehea_error("Send Completion Error: Resetting port");
606                         if (netif_msg_tx_err(pr->port))
607                                 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
608                         schedule_work(&pr->port->reset_task);
609                         break;
610                 }
611
612                 if (netif_msg_tx_done(pr->port))
613                         ehea_dump(cqe, sizeof(*cqe), "CQE");
614
615                 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
616                            == EHEA_SWQE2_TYPE)) {
617
618                         index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
619                         skb = pr->sq_skba.arr[index];
620                         dev_kfree_skb(skb);
621                         pr->sq_skba.arr[index] = NULL;
622                 }
623
624                 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
625                 quota--;
626
627                 cqe = ehea_poll_cq(send_cq);
628         };
629
630         ehea_update_feca(send_cq, cqe_counter);
631         atomic_add(swqe_av, &pr->swqe_avail);
632
633         spin_lock_irqsave(&pr->netif_queue, flags);
634
635         if (pr->queue_stopped && (atomic_read(&pr->swqe_avail)
636                                   >= pr->swqe_refill_th)) {
637                 netif_wake_queue(pr->port->netdev);
638                 pr->queue_stopped = 0;
639         }
640         spin_unlock_irqrestore(&pr->netif_queue, flags);
641
642         return cqe;
643 }
644
645 #define EHEA_NAPI_POLL_NUM_BEFORE_IRQ 16
646 #define EHEA_POLL_MAX_CQES 65535
647
648 static int ehea_poll(struct napi_struct *napi, int budget)
649 {
650         struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
651                                                 napi);
652         struct net_device *dev = pr->port->netdev;
653         struct ehea_cqe *cqe;
654         struct ehea_cqe *cqe_skb = NULL;
655         int force_irq, wqe_index;
656         int rx = 0;
657
658         force_irq = (pr->poll_counter > EHEA_NAPI_POLL_NUM_BEFORE_IRQ);
659         cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
660
661         if (!force_irq)
662                 rx += ehea_proc_rwqes(dev, pr, budget - rx);
663
664         while ((rx != budget) || force_irq) {
665                 pr->poll_counter = 0;
666                 force_irq = 0;
667                 netif_rx_complete(dev, napi);
668                 ehea_reset_cq_ep(pr->recv_cq);
669                 ehea_reset_cq_ep(pr->send_cq);
670                 ehea_reset_cq_n1(pr->recv_cq);
671                 ehea_reset_cq_n1(pr->send_cq);
672                 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
673                 cqe_skb = ehea_poll_cq(pr->send_cq);
674
675                 if (!cqe && !cqe_skb)
676                         return rx;
677
678                 if (!netif_rx_reschedule(dev, napi))
679                         return rx;
680
681                 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
682                 rx += ehea_proc_rwqes(dev, pr, budget - rx);
683         }
684
685         pr->poll_counter++;
686         return rx;
687 }
688
689 #ifdef CONFIG_NET_POLL_CONTROLLER
690 static void ehea_netpoll(struct net_device *dev)
691 {
692         struct ehea_port *port = netdev_priv(dev);
693         int i;
694
695         for (i = 0; i < port->num_def_qps; i++)
696                 netif_rx_schedule(dev, &port->port_res[i].napi);
697 }
698 #endif
699
700 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
701 {
702         struct ehea_port_res *pr = param;
703
704         netif_rx_schedule(pr->port->netdev, &pr->napi);
705
706         return IRQ_HANDLED;
707 }
708
709 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
710 {
711         struct ehea_port *port = param;
712         struct ehea_eqe *eqe;
713         struct ehea_qp *qp;
714         u32 qp_token;
715
716         eqe = ehea_poll_eq(port->qp_eq);
717
718         while (eqe) {
719                 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
720                 ehea_error("QP aff_err: entry=0x%lx, token=0x%x",
721                            eqe->entry, qp_token);
722
723                 qp = port->port_res[qp_token].qp;
724                 ehea_error_data(port->adapter, qp->fw_handle);
725                 eqe = ehea_poll_eq(port->qp_eq);
726         }
727
728         schedule_work(&port->reset_task);
729
730         return IRQ_HANDLED;
731 }
732
733 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
734                                        int logical_port)
735 {
736         int i;
737
738         for (i = 0; i < EHEA_MAX_PORTS; i++)
739                 if (adapter->port[i])
740                         if (adapter->port[i]->logical_port_id == logical_port)
741                                 return adapter->port[i];
742         return NULL;
743 }
744
745 int ehea_sense_port_attr(struct ehea_port *port)
746 {
747         int ret;
748         u64 hret;
749         struct hcp_ehea_port_cb0 *cb0;
750
751         /* may be called via ehea_neq_tasklet() */
752         cb0 = kzalloc(PAGE_SIZE, GFP_ATOMIC);
753         if (!cb0) {
754                 ehea_error("no mem for cb0");
755                 ret = -ENOMEM;
756                 goto out;
757         }
758
759         hret = ehea_h_query_ehea_port(port->adapter->handle,
760                                       port->logical_port_id, H_PORT_CB0,
761                                       EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
762                                       cb0);
763         if (hret != H_SUCCESS) {
764                 ret = -EIO;
765                 goto out_free;
766         }
767
768         /* MAC address */
769         port->mac_addr = cb0->port_mac_addr << 16;
770
771         if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
772                 ret = -EADDRNOTAVAIL;
773                 goto out_free;
774         }
775
776         /* Port speed */
777         switch (cb0->port_speed) {
778         case H_SPEED_10M_H:
779                 port->port_speed = EHEA_SPEED_10M;
780                 port->full_duplex = 0;
781                 break;
782         case H_SPEED_10M_F:
783                 port->port_speed = EHEA_SPEED_10M;
784                 port->full_duplex = 1;
785                 break;
786         case H_SPEED_100M_H:
787                 port->port_speed = EHEA_SPEED_100M;
788                 port->full_duplex = 0;
789                 break;
790         case H_SPEED_100M_F:
791                 port->port_speed = EHEA_SPEED_100M;
792                 port->full_duplex = 1;
793                 break;
794         case H_SPEED_1G_F:
795                 port->port_speed = EHEA_SPEED_1G;
796                 port->full_duplex = 1;
797                 break;
798         case H_SPEED_10G_F:
799                 port->port_speed = EHEA_SPEED_10G;
800                 port->full_duplex = 1;
801                 break;
802         default:
803                 port->port_speed = 0;
804                 port->full_duplex = 0;
805                 break;
806         }
807
808         port->autoneg = 1;
809         port->num_mcs = cb0->num_default_qps;
810
811         /* Number of default QPs */
812         if (use_mcs)
813                 port->num_def_qps = cb0->num_default_qps;
814         else
815                 port->num_def_qps = 1;
816
817         if (!port->num_def_qps) {
818                 ret = -EINVAL;
819                 goto out_free;
820         }
821
822         port->num_tx_qps = num_tx_qps;
823
824         if (port->num_def_qps >= port->num_tx_qps)
825                 port->num_add_tx_qps = 0;
826         else
827                 port->num_add_tx_qps = port->num_tx_qps - port->num_def_qps;
828
829         ret = 0;
830 out_free:
831         if (ret || netif_msg_probe(port))
832                 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
833         kfree(cb0);
834 out:
835         return ret;
836 }
837
838 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
839 {
840         struct hcp_ehea_port_cb4 *cb4;
841         u64 hret;
842         int ret = 0;
843
844         cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
845         if (!cb4) {
846                 ehea_error("no mem for cb4");
847                 ret = -ENOMEM;
848                 goto out;
849         }
850
851         cb4->port_speed = port_speed;
852
853         netif_carrier_off(port->netdev);
854
855         hret = ehea_h_modify_ehea_port(port->adapter->handle,
856                                        port->logical_port_id,
857                                        H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
858         if (hret == H_SUCCESS) {
859                 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
860
861                 hret = ehea_h_query_ehea_port(port->adapter->handle,
862                                               port->logical_port_id,
863                                               H_PORT_CB4, H_PORT_CB4_SPEED,
864                                               cb4);
865                 if (hret == H_SUCCESS) {
866                         switch (cb4->port_speed) {
867                         case H_SPEED_10M_H:
868                                 port->port_speed = EHEA_SPEED_10M;
869                                 port->full_duplex = 0;
870                                 break;
871                         case H_SPEED_10M_F:
872                                 port->port_speed = EHEA_SPEED_10M;
873                                 port->full_duplex = 1;
874                                 break;
875                         case H_SPEED_100M_H:
876                                 port->port_speed = EHEA_SPEED_100M;
877                                 port->full_duplex = 0;
878                                 break;
879                         case H_SPEED_100M_F:
880                                 port->port_speed = EHEA_SPEED_100M;
881                                 port->full_duplex = 1;
882                                 break;
883                         case H_SPEED_1G_F:
884                                 port->port_speed = EHEA_SPEED_1G;
885                                 port->full_duplex = 1;
886                                 break;
887                         case H_SPEED_10G_F:
888                                 port->port_speed = EHEA_SPEED_10G;
889                                 port->full_duplex = 1;
890                                 break;
891                         default:
892                                 port->port_speed = 0;
893                                 port->full_duplex = 0;
894                                 break;
895                         }
896                 } else {
897                         ehea_error("Failed sensing port speed");
898                         ret = -EIO;
899                 }
900         } else {
901                 if (hret == H_AUTHORITY) {
902                         ehea_info("Hypervisor denied setting port speed");
903                         ret = -EPERM;
904                 } else {
905                         ret = -EIO;
906                         ehea_error("Failed setting port speed");
907                 }
908         }
909         if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
910                 netif_carrier_on(port->netdev);
911
912         kfree(cb4);
913 out:
914         return ret;
915 }
916
917 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
918 {
919         int ret;
920         u8 ec;
921         u8 portnum;
922         struct ehea_port *port;
923
924         ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
925         portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
926         port = ehea_get_port(adapter, portnum);
927
928         switch (ec) {
929         case EHEA_EC_PORTSTATE_CHG:     /* port state change */
930
931                 if (!port) {
932                         ehea_error("unknown portnum %x", portnum);
933                         break;
934                 }
935
936                 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
937                         if (!netif_carrier_ok(port->netdev)) {
938                                 ret = ehea_sense_port_attr(port);
939                                 if (ret) {
940                                         ehea_error("failed resensing port "
941                                                    "attributes");
942                                         break;
943                                 }
944
945                                 if (netif_msg_link(port))
946                                         ehea_info("%s: Logical port up: %dMbps "
947                                                   "%s Duplex",
948                                                   port->netdev->name,
949                                                   port->port_speed,
950                                                   port->full_duplex ==
951                                                   1 ? "Full" : "Half");
952
953                                 netif_carrier_on(port->netdev);
954                                 netif_wake_queue(port->netdev);
955                         }
956                 } else
957                         if (netif_carrier_ok(port->netdev)) {
958                                 if (netif_msg_link(port))
959                                         ehea_info("%s: Logical port down",
960                                                   port->netdev->name);
961                                 netif_carrier_off(port->netdev);
962                                 netif_stop_queue(port->netdev);
963                         }
964
965                 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
966                         port->phy_link = EHEA_PHY_LINK_UP;
967                         if (netif_msg_link(port))
968                                 ehea_info("%s: Physical port up",
969                                           port->netdev->name);
970                         if (prop_carrier_state)
971                                 netif_carrier_on(port->netdev);
972                 } else {
973                         port->phy_link = EHEA_PHY_LINK_DOWN;
974                         if (netif_msg_link(port))
975                                 ehea_info("%s: Physical port down",
976                                           port->netdev->name);
977                         if (prop_carrier_state)
978                                 netif_carrier_off(port->netdev);
979                 }
980
981                 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
982                         ehea_info("External switch port is primary port");
983                 else
984                         ehea_info("External switch port is backup port");
985
986                 break;
987         case EHEA_EC_ADAPTER_MALFUNC:
988                 ehea_error("Adapter malfunction");
989                 break;
990         case EHEA_EC_PORT_MALFUNC:
991                 ehea_info("Port malfunction: Device: %s", port->netdev->name);
992                 netif_carrier_off(port->netdev);
993                 netif_stop_queue(port->netdev);
994                 break;
995         default:
996                 ehea_error("unknown event code %x, eqe=0x%lX", ec, eqe);
997                 break;
998         }
999 }
1000
1001 static void ehea_neq_tasklet(unsigned long data)
1002 {
1003         struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1004         struct ehea_eqe *eqe;
1005         u64 event_mask;
1006
1007         eqe = ehea_poll_eq(adapter->neq);
1008         ehea_debug("eqe=%p", eqe);
1009
1010         while (eqe) {
1011                 ehea_debug("*eqe=%lx", eqe->entry);
1012                 ehea_parse_eqe(adapter, eqe->entry);
1013                 eqe = ehea_poll_eq(adapter->neq);
1014                 ehea_debug("next eqe=%p", eqe);
1015         }
1016
1017         event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1018                    | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1019                    | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1020
1021         ehea_h_reset_events(adapter->handle,
1022                             adapter->neq->fw_handle, event_mask);
1023 }
1024
1025 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1026 {
1027         struct ehea_adapter *adapter = param;
1028         tasklet_hi_schedule(&adapter->neq_tasklet);
1029         return IRQ_HANDLED;
1030 }
1031
1032
1033 static int ehea_fill_port_res(struct ehea_port_res *pr)
1034 {
1035         int ret;
1036         struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1037
1038         ret = ehea_init_fill_rq1(pr, init_attr->act_nr_rwqes_rq1
1039                                      - init_attr->act_nr_rwqes_rq2
1040                                      - init_attr->act_nr_rwqes_rq3 - 1);
1041
1042         ret |= ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1043
1044         ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1045
1046         return ret;
1047 }
1048
1049 static int ehea_reg_interrupts(struct net_device *dev)
1050 {
1051         struct ehea_port *port = netdev_priv(dev);
1052         struct ehea_port_res *pr;
1053         int i, ret;
1054
1055
1056         snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1057                  dev->name);
1058
1059         ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1060                                   ehea_qp_aff_irq_handler,
1061                                   IRQF_DISABLED, port->int_aff_name, port);
1062         if (ret) {
1063                 ehea_error("failed registering irq for qp_aff_irq_handler:"
1064                            "ist=%X", port->qp_eq->attr.ist1);
1065                 goto out_free_qpeq;
1066         }
1067
1068         if (netif_msg_ifup(port))
1069                 ehea_info("irq_handle 0x%X for function qp_aff_irq_handler "
1070                           "registered", port->qp_eq->attr.ist1);
1071
1072
1073         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1074                 pr = &port->port_res[i];
1075                 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1076                          "%s-queue%d", dev->name, i);
1077                 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1078                                           ehea_recv_irq_handler,
1079                                           IRQF_DISABLED, pr->int_send_name,
1080                                           pr);
1081                 if (ret) {
1082                         ehea_error("failed registering irq for ehea_queue "
1083                                    "port_res_nr:%d, ist=%X", i,
1084                                    pr->eq->attr.ist1);
1085                         goto out_free_req;
1086                 }
1087                 if (netif_msg_ifup(port))
1088                         ehea_info("irq_handle 0x%X for function ehea_queue_int "
1089                                   "%d registered", pr->eq->attr.ist1, i);
1090         }
1091 out:
1092         return ret;
1093
1094
1095 out_free_req:
1096         while (--i >= 0) {
1097                 u32 ist = port->port_res[i].eq->attr.ist1;
1098                 ibmebus_free_irq(ist, &port->port_res[i]);
1099         }
1100
1101 out_free_qpeq:
1102         ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1103         i = port->num_def_qps;
1104
1105         goto out;
1106
1107 }
1108
1109 static void ehea_free_interrupts(struct net_device *dev)
1110 {
1111         struct ehea_port *port = netdev_priv(dev);
1112         struct ehea_port_res *pr;
1113         int i;
1114
1115         /* send */
1116
1117         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1118                 pr = &port->port_res[i];
1119                 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1120                 if (netif_msg_intr(port))
1121                         ehea_info("free send irq for res %d with handle 0x%X",
1122                                   i, pr->eq->attr.ist1);
1123         }
1124
1125         /* associated events */
1126         ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1127         if (netif_msg_intr(port))
1128                 ehea_info("associated event interrupt for handle 0x%X freed",
1129                           port->qp_eq->attr.ist1);
1130 }
1131
1132 static int ehea_configure_port(struct ehea_port *port)
1133 {
1134         int ret, i;
1135         u64 hret, mask;
1136         struct hcp_ehea_port_cb0 *cb0;
1137
1138         ret = -ENOMEM;
1139         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1140         if (!cb0)
1141                 goto out;
1142
1143         cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1144                      | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1145                      | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1146                      | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1147                      | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1148                                       PXLY_RC_VLAN_FILTER)
1149                      | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1150
1151         for (i = 0; i < port->num_mcs; i++)
1152                 if (use_mcs)
1153                         cb0->default_qpn_arr[i] =
1154                                 port->port_res[i].qp->init_attr.qp_nr;
1155                 else
1156                         cb0->default_qpn_arr[i] =
1157                                 port->port_res[0].qp->init_attr.qp_nr;
1158
1159         if (netif_msg_ifup(port))
1160                 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1161
1162         mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1163              | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1164
1165         hret = ehea_h_modify_ehea_port(port->adapter->handle,
1166                                        port->logical_port_id,
1167                                        H_PORT_CB0, mask, cb0);
1168         ret = -EIO;
1169         if (hret != H_SUCCESS)
1170                 goto out_free;
1171
1172         ret = 0;
1173
1174 out_free:
1175         kfree(cb0);
1176 out:
1177         return ret;
1178 }
1179
1180 int ehea_gen_smrs(struct ehea_port_res *pr)
1181 {
1182         int ret;
1183         struct ehea_adapter *adapter = pr->port->adapter;
1184
1185         ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1186         if (ret)
1187                 goto out;
1188
1189         ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1190         if (ret)
1191                 goto out_free;
1192
1193         return 0;
1194
1195 out_free:
1196         ehea_rem_mr(&pr->send_mr);
1197 out:
1198         ehea_error("Generating SMRS failed\n");
1199         return -EIO;
1200 }
1201
1202 int ehea_rem_smrs(struct ehea_port_res *pr)
1203 {
1204         if ((ehea_rem_mr(&pr->send_mr))
1205             || (ehea_rem_mr(&pr->recv_mr)))
1206                 return -EIO;
1207         else
1208                 return 0;
1209 }
1210
1211 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1212 {
1213         int arr_size = sizeof(void *) * max_q_entries;
1214
1215         q_skba->arr = vmalloc(arr_size);
1216         if (!q_skba->arr)
1217                 return -ENOMEM;
1218
1219         memset(q_skba->arr, 0, arr_size);
1220
1221         q_skba->len = max_q_entries;
1222         q_skba->index = 0;
1223         q_skba->os_skbs = 0;
1224
1225         return 0;
1226 }
1227
1228 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1229                               struct port_res_cfg *pr_cfg, int queue_token)
1230 {
1231         struct ehea_adapter *adapter = port->adapter;
1232         enum ehea_eq_type eq_type = EHEA_EQ;
1233         struct ehea_qp_init_attr *init_attr = NULL;
1234         int ret = -EIO;
1235
1236         memset(pr, 0, sizeof(struct ehea_port_res));
1237
1238         pr->port = port;
1239         spin_lock_init(&pr->xmit_lock);
1240         spin_lock_init(&pr->netif_queue);
1241
1242         pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1243         if (!pr->eq) {
1244                 ehea_error("create_eq failed (eq)");
1245                 goto out_free;
1246         }
1247
1248         pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1249                                      pr->eq->fw_handle,
1250                                      port->logical_port_id);
1251         if (!pr->recv_cq) {
1252                 ehea_error("create_cq failed (cq_recv)");
1253                 goto out_free;
1254         }
1255
1256         pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1257                                      pr->eq->fw_handle,
1258                                      port->logical_port_id);
1259         if (!pr->send_cq) {
1260                 ehea_error("create_cq failed (cq_send)");
1261                 goto out_free;
1262         }
1263
1264         if (netif_msg_ifup(port))
1265                 ehea_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d",
1266                           pr->send_cq->attr.act_nr_of_cqes,
1267                           pr->recv_cq->attr.act_nr_of_cqes);
1268
1269         init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1270         if (!init_attr) {
1271                 ret = -ENOMEM;
1272                 ehea_error("no mem for ehea_qp_init_attr");
1273                 goto out_free;
1274         }
1275
1276         init_attr->low_lat_rq1 = 1;
1277         init_attr->signalingtype = 1;   /* generate CQE if specified in WQE */
1278         init_attr->rq_count = 3;
1279         init_attr->qp_token = queue_token;
1280         init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1281         init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1282         init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1283         init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1284         init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1285         init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1286         init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1287         init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1288         init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1289         init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1290         init_attr->port_nr = port->logical_port_id;
1291         init_attr->send_cq_handle = pr->send_cq->fw_handle;
1292         init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1293         init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1294
1295         pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1296         if (!pr->qp) {
1297                 ehea_error("create_qp failed");
1298                 ret = -EIO;
1299                 goto out_free;
1300         }
1301
1302         if (netif_msg_ifup(port))
1303                 ehea_info("QP: qp_nr=%d\n act_nr_snd_wqe=%d\n nr_rwqe_rq1=%d\n "
1304                           "nr_rwqe_rq2=%d\n nr_rwqe_rq3=%d", init_attr->qp_nr,
1305                           init_attr->act_nr_send_wqes,
1306                           init_attr->act_nr_rwqes_rq1,
1307                           init_attr->act_nr_rwqes_rq2,
1308                           init_attr->act_nr_rwqes_rq3);
1309
1310         ret = ehea_init_q_skba(&pr->sq_skba, init_attr->act_nr_send_wqes + 1);
1311         ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1312         ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1313         ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1314         if (ret)
1315                 goto out_free;
1316
1317         pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1318         if (ehea_gen_smrs(pr) != 0) {
1319                 ret = -EIO;
1320                 goto out_free;
1321         }
1322
1323         atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1324
1325         kfree(init_attr);
1326
1327         netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1328
1329         pr->lro_mgr.max_aggr = pr->port->lro_max_aggr;
1330         pr->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1331         pr->lro_mgr.lro_arr = pr->lro_desc;
1332         pr->lro_mgr.get_skb_header = get_skb_hdr;
1333         pr->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1334         pr->lro_mgr.dev = port->netdev;
1335         pr->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1336         pr->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1337
1338         ret = 0;
1339         goto out;
1340
1341 out_free:
1342         kfree(init_attr);
1343         vfree(pr->sq_skba.arr);
1344         vfree(pr->rq1_skba.arr);
1345         vfree(pr->rq2_skba.arr);
1346         vfree(pr->rq3_skba.arr);
1347         ehea_destroy_qp(pr->qp);
1348         ehea_destroy_cq(pr->send_cq);
1349         ehea_destroy_cq(pr->recv_cq);
1350         ehea_destroy_eq(pr->eq);
1351 out:
1352         return ret;
1353 }
1354
1355 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1356 {
1357         int ret, i;
1358
1359         ret = ehea_destroy_qp(pr->qp);
1360
1361         if (!ret) {
1362                 ehea_destroy_cq(pr->send_cq);
1363                 ehea_destroy_cq(pr->recv_cq);
1364                 ehea_destroy_eq(pr->eq);
1365
1366                 for (i = 0; i < pr->rq1_skba.len; i++)
1367                         if (pr->rq1_skba.arr[i])
1368                                 dev_kfree_skb(pr->rq1_skba.arr[i]);
1369
1370                 for (i = 0; i < pr->rq2_skba.len; i++)
1371                         if (pr->rq2_skba.arr[i])
1372                                 dev_kfree_skb(pr->rq2_skba.arr[i]);
1373
1374                 for (i = 0; i < pr->rq3_skba.len; i++)
1375                         if (pr->rq3_skba.arr[i])
1376                                 dev_kfree_skb(pr->rq3_skba.arr[i]);
1377
1378                 for (i = 0; i < pr->sq_skba.len; i++)
1379                         if (pr->sq_skba.arr[i])
1380                                 dev_kfree_skb(pr->sq_skba.arr[i]);
1381
1382                 vfree(pr->rq1_skba.arr);
1383                 vfree(pr->rq2_skba.arr);
1384                 vfree(pr->rq3_skba.arr);
1385                 vfree(pr->sq_skba.arr);
1386                 ret = ehea_rem_smrs(pr);
1387         }
1388         return ret;
1389 }
1390
1391 /*
1392  * The write_* functions store information in swqe which is used by
1393  * the hardware to calculate the ip/tcp/udp checksum
1394  */
1395
1396 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1397                                       const struct sk_buff *skb)
1398 {
1399         swqe->ip_start = skb_network_offset(skb);
1400         swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1401 }
1402
1403 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1404                                         const struct sk_buff *skb)
1405 {
1406         swqe->tcp_offset =
1407                 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1408
1409         swqe->tcp_end = (u16)skb->len - 1;
1410 }
1411
1412 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1413                                         const struct sk_buff *skb)
1414 {
1415         swqe->tcp_offset =
1416                 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1417
1418         swqe->tcp_end = (u16)skb->len - 1;
1419 }
1420
1421
1422 static void write_swqe2_TSO(struct sk_buff *skb,
1423                             struct ehea_swqe *swqe, u32 lkey)
1424 {
1425         struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1426         u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1427         int skb_data_size = skb->len - skb->data_len;
1428         int headersize;
1429
1430         /* Packet is TCP with TSO enabled */
1431         swqe->tx_control |= EHEA_SWQE_TSO;
1432         swqe->mss = skb_shinfo(skb)->gso_size;
1433         /* copy only eth/ip/tcp headers to immediate data and
1434          * the rest of skb->data to sg1entry
1435          */
1436         headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1437
1438         skb_data_size = skb->len - skb->data_len;
1439
1440         if (skb_data_size >= headersize) {
1441                 /* copy immediate data */
1442                 skb_copy_from_linear_data(skb, imm_data, headersize);
1443                 swqe->immediate_data_length = headersize;
1444
1445                 if (skb_data_size > headersize) {
1446                         /* set sg1entry data */
1447                         sg1entry->l_key = lkey;
1448                         sg1entry->len = skb_data_size - headersize;
1449                         sg1entry->vaddr =
1450                                 ehea_map_vaddr(skb->data + headersize);
1451                         swqe->descriptors++;
1452                 }
1453         } else
1454                 ehea_error("cannot handle fragmented headers");
1455 }
1456
1457 static void write_swqe2_nonTSO(struct sk_buff *skb,
1458                                struct ehea_swqe *swqe, u32 lkey)
1459 {
1460         int skb_data_size = skb->len - skb->data_len;
1461         u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1462         struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1463
1464         /* Packet is any nonTSO type
1465          *
1466          * Copy as much as possible skb->data to immediate data and
1467          * the rest to sg1entry
1468          */
1469         if (skb_data_size >= SWQE2_MAX_IMM) {
1470                 /* copy immediate data */
1471                 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1472
1473                 swqe->immediate_data_length = SWQE2_MAX_IMM;
1474
1475                 if (skb_data_size > SWQE2_MAX_IMM) {
1476                         /* copy sg1entry data */
1477                         sg1entry->l_key = lkey;
1478                         sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1479                         sg1entry->vaddr =
1480                                 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1481                         swqe->descriptors++;
1482                 }
1483         } else {
1484                 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1485                 swqe->immediate_data_length = skb_data_size;
1486         }
1487 }
1488
1489 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1490                                     struct ehea_swqe *swqe, u32 lkey)
1491 {
1492         struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1493         skb_frag_t *frag;
1494         int nfrags, sg1entry_contains_frag_data, i;
1495
1496         nfrags = skb_shinfo(skb)->nr_frags;
1497         sg1entry = &swqe->u.immdata_desc.sg_entry;
1498         sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1499         swqe->descriptors = 0;
1500         sg1entry_contains_frag_data = 0;
1501
1502         if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1503                 write_swqe2_TSO(skb, swqe, lkey);
1504         else
1505                 write_swqe2_nonTSO(skb, swqe, lkey);
1506
1507         /* write descriptors */
1508         if (nfrags > 0) {
1509                 if (swqe->descriptors == 0) {
1510                         /* sg1entry not yet used */
1511                         frag = &skb_shinfo(skb)->frags[0];
1512
1513                         /* copy sg1entry data */
1514                         sg1entry->l_key = lkey;
1515                         sg1entry->len = frag->size;
1516                         sg1entry->vaddr =
1517                                 ehea_map_vaddr(page_address(frag->page)
1518                                                + frag->page_offset);
1519                         swqe->descriptors++;
1520                         sg1entry_contains_frag_data = 1;
1521                 }
1522
1523                 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1524
1525                         frag = &skb_shinfo(skb)->frags[i];
1526                         sgentry = &sg_list[i - sg1entry_contains_frag_data];
1527
1528                         sgentry->l_key = lkey;
1529                         sgentry->len = frag->size;
1530                         sgentry->vaddr =
1531                                 ehea_map_vaddr(page_address(frag->page)
1532                                                + frag->page_offset);
1533                         swqe->descriptors++;
1534                 }
1535         }
1536 }
1537
1538 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1539 {
1540         int ret = 0;
1541         u64 hret;
1542         u8 reg_type;
1543
1544         /* De/Register untagged packets */
1545         reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1546         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1547                                      port->logical_port_id,
1548                                      reg_type, port->mac_addr, 0, hcallid);
1549         if (hret != H_SUCCESS) {
1550                 ehea_error("%sregistering bc address failed (tagged)",
1551                            hcallid == H_REG_BCMC ? "" : "de");
1552                 ret = -EIO;
1553                 goto out_herr;
1554         }
1555
1556         /* De/Register VLAN packets */
1557         reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1558         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1559                                      port->logical_port_id,
1560                                      reg_type, port->mac_addr, 0, hcallid);
1561         if (hret != H_SUCCESS) {
1562                 ehea_error("%sregistering bc address failed (vlan)",
1563                            hcallid == H_REG_BCMC ? "" : "de");
1564                 ret = -EIO;
1565         }
1566 out_herr:
1567         return ret;
1568 }
1569
1570 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1571 {
1572         struct ehea_port *port = netdev_priv(dev);
1573         struct sockaddr *mac_addr = sa;
1574         struct hcp_ehea_port_cb0 *cb0;
1575         int ret;
1576         u64 hret;
1577
1578         if (!is_valid_ether_addr(mac_addr->sa_data)) {
1579                 ret = -EADDRNOTAVAIL;
1580                 goto out;
1581         }
1582
1583         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1584         if (!cb0) {
1585                 ehea_error("no mem for cb0");
1586                 ret = -ENOMEM;
1587                 goto out;
1588         }
1589
1590         memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1591
1592         cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1593
1594         hret = ehea_h_modify_ehea_port(port->adapter->handle,
1595                                        port->logical_port_id, H_PORT_CB0,
1596                                        EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1597         if (hret != H_SUCCESS) {
1598                 ret = -EIO;
1599                 goto out_free;
1600         }
1601
1602         memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1603
1604         /* Deregister old MAC in pHYP */
1605         ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1606         if (ret)
1607                 goto out_free;
1608
1609         port->mac_addr = cb0->port_mac_addr << 16;
1610
1611         /* Register new MAC in pHYP */
1612         ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1613         if (ret)
1614                 goto out_free;
1615
1616         ret = 0;
1617 out_free:
1618         kfree(cb0);
1619 out:
1620         return ret;
1621 }
1622
1623 static void ehea_promiscuous_error(u64 hret, int enable)
1624 {
1625         if (hret == H_AUTHORITY)
1626                 ehea_info("Hypervisor denied %sabling promiscuous mode",
1627                           enable == 1 ? "en" : "dis");
1628         else
1629                 ehea_error("failed %sabling promiscuous mode",
1630                            enable == 1 ? "en" : "dis");
1631 }
1632
1633 static void ehea_promiscuous(struct net_device *dev, int enable)
1634 {
1635         struct ehea_port *port = netdev_priv(dev);
1636         struct hcp_ehea_port_cb7 *cb7;
1637         u64 hret;
1638
1639         if ((enable && port->promisc) || (!enable && !port->promisc))
1640                 return;
1641
1642         cb7 = kzalloc(PAGE_SIZE, GFP_ATOMIC);
1643         if (!cb7) {
1644                 ehea_error("no mem for cb7");
1645                 goto out;
1646         }
1647
1648         /* Modify Pxs_DUCQPN in CB7 */
1649         cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1650
1651         hret = ehea_h_modify_ehea_port(port->adapter->handle,
1652                                        port->logical_port_id,
1653                                        H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1654         if (hret) {
1655                 ehea_promiscuous_error(hret, enable);
1656                 goto out;
1657         }
1658
1659         port->promisc = enable;
1660 out:
1661         kfree(cb7);
1662         return;
1663 }
1664
1665 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1666                                      u32 hcallid)
1667 {
1668         u64 hret;
1669         u8 reg_type;
1670
1671         reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1672                  | EHEA_BCMC_UNTAGGED;
1673
1674         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1675                                      port->logical_port_id,
1676                                      reg_type, mc_mac_addr, 0, hcallid);
1677         if (hret)
1678                 goto out;
1679
1680         reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1681                  | EHEA_BCMC_VLANID_ALL;
1682
1683         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1684                                      port->logical_port_id,
1685                                      reg_type, mc_mac_addr, 0, hcallid);
1686 out:
1687         return hret;
1688 }
1689
1690 static int ehea_drop_multicast_list(struct net_device *dev)
1691 {
1692         struct ehea_port *port = netdev_priv(dev);
1693         struct ehea_mc_list *mc_entry = port->mc_list;
1694         struct list_head *pos;
1695         struct list_head *temp;
1696         int ret = 0;
1697         u64 hret;
1698
1699         list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1700                 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1701
1702                 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1703                                                  H_DEREG_BCMC);
1704                 if (hret) {
1705                         ehea_error("failed deregistering mcast MAC");
1706                         ret = -EIO;
1707                 }
1708
1709                 list_del(pos);
1710                 kfree(mc_entry);
1711         }
1712         return ret;
1713 }
1714
1715 static void ehea_allmulti(struct net_device *dev, int enable)
1716 {
1717         struct ehea_port *port = netdev_priv(dev);
1718         u64 hret;
1719
1720         if (!port->allmulti) {
1721                 if (enable) {
1722                         /* Enable ALLMULTI */
1723                         ehea_drop_multicast_list(dev);
1724                         hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1725                         if (!hret)
1726                                 port->allmulti = 1;
1727                         else
1728                                 ehea_error("failed enabling IFF_ALLMULTI");
1729                 }
1730         } else
1731                 if (!enable) {
1732                         /* Disable ALLMULTI */
1733                         hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1734                         if (!hret)
1735                                 port->allmulti = 0;
1736                         else
1737                                 ehea_error("failed disabling IFF_ALLMULTI");
1738                 }
1739 }
1740
1741 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
1742 {
1743         struct ehea_mc_list *ehea_mcl_entry;
1744         u64 hret;
1745
1746         ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1747         if (!ehea_mcl_entry) {
1748                 ehea_error("no mem for mcl_entry");
1749                 return;
1750         }
1751
1752         INIT_LIST_HEAD(&ehea_mcl_entry->list);
1753
1754         memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1755
1756         hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1757                                          H_REG_BCMC);
1758         if (!hret)
1759                 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1760         else {
1761                 ehea_error("failed registering mcast MAC");
1762                 kfree(ehea_mcl_entry);
1763         }
1764 }
1765
1766 static void ehea_set_multicast_list(struct net_device *dev)
1767 {
1768         struct ehea_port *port = netdev_priv(dev);
1769         struct dev_mc_list *k_mcl_entry;
1770         int ret, i;
1771
1772         if (dev->flags & IFF_PROMISC) {
1773                 ehea_promiscuous(dev, 1);
1774                 return;
1775         }
1776         ehea_promiscuous(dev, 0);
1777
1778         if (dev->flags & IFF_ALLMULTI) {
1779                 ehea_allmulti(dev, 1);
1780                 return;
1781         }
1782         ehea_allmulti(dev, 0);
1783
1784         if (dev->mc_count) {
1785                 ret = ehea_drop_multicast_list(dev);
1786                 if (ret) {
1787                         /* Dropping the current multicast list failed.
1788                          * Enabling ALL_MULTI is the best we can do.
1789                          */
1790                         ehea_allmulti(dev, 1);
1791                 }
1792
1793                 if (dev->mc_count > port->adapter->max_mc_mac) {
1794                         ehea_info("Mcast registration limit reached (0x%lx). "
1795                                   "Use ALLMULTI!",
1796                                   port->adapter->max_mc_mac);
1797                         goto out;
1798                 }
1799
1800                 for (i = 0, k_mcl_entry = dev->mc_list; i < dev->mc_count; i++,
1801                              k_mcl_entry = k_mcl_entry->next)
1802                         ehea_add_multicast_entry(port, k_mcl_entry->dmi_addr);
1803
1804         }
1805 out:
1806         return;
1807 }
1808
1809 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1810 {
1811         if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1812                 return -EINVAL;
1813         dev->mtu = new_mtu;
1814         return 0;
1815 }
1816
1817 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
1818                        struct ehea_swqe *swqe, u32 lkey)
1819 {
1820         if (skb->protocol == htons(ETH_P_IP)) {
1821                 const struct iphdr *iph = ip_hdr(skb);
1822
1823                 /* IPv4 */
1824                 swqe->tx_control |= EHEA_SWQE_CRC
1825                                  | EHEA_SWQE_IP_CHECKSUM
1826                                  | EHEA_SWQE_TCP_CHECKSUM
1827                                  | EHEA_SWQE_IMM_DATA_PRESENT
1828                                  | EHEA_SWQE_DESCRIPTORS_PRESENT;
1829
1830                 write_ip_start_end(swqe, skb);
1831
1832                 if (iph->protocol == IPPROTO_UDP) {
1833                         if ((iph->frag_off & IP_MF)
1834                             || (iph->frag_off & IP_OFFSET))
1835                                 /* IP fragment, so don't change cs */
1836                                 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
1837                         else
1838                                 write_udp_offset_end(swqe, skb);
1839                 } else if (iph->protocol == IPPROTO_TCP) {
1840                         write_tcp_offset_end(swqe, skb);
1841                 }
1842
1843                 /* icmp (big data) and ip segmentation packets (all other ip
1844                    packets) do not require any special handling */
1845
1846         } else {
1847                 /* Other Ethernet Protocol */
1848                 swqe->tx_control |= EHEA_SWQE_CRC
1849                                  | EHEA_SWQE_IMM_DATA_PRESENT
1850                                  | EHEA_SWQE_DESCRIPTORS_PRESENT;
1851         }
1852
1853         write_swqe2_data(skb, dev, swqe, lkey);
1854 }
1855
1856 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
1857                        struct ehea_swqe *swqe)
1858 {
1859         int nfrags = skb_shinfo(skb)->nr_frags;
1860         u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
1861         skb_frag_t *frag;
1862         int i;
1863
1864         if (skb->protocol == htons(ETH_P_IP)) {
1865                 const struct iphdr *iph = ip_hdr(skb);
1866
1867                 /* IPv4 */
1868                 write_ip_start_end(swqe, skb);
1869
1870                 if (iph->protocol == IPPROTO_TCP) {
1871                         swqe->tx_control |= EHEA_SWQE_CRC
1872                                          | EHEA_SWQE_IP_CHECKSUM
1873                                          | EHEA_SWQE_TCP_CHECKSUM
1874                                          | EHEA_SWQE_IMM_DATA_PRESENT;
1875
1876                         write_tcp_offset_end(swqe, skb);
1877
1878                 } else if (iph->protocol == IPPROTO_UDP) {
1879                         if ((iph->frag_off & IP_MF)
1880                             || (iph->frag_off & IP_OFFSET))
1881                                 /* IP fragment, so don't change cs */
1882                                 swqe->tx_control |= EHEA_SWQE_CRC
1883                                                  | EHEA_SWQE_IMM_DATA_PRESENT;
1884                         else {
1885                                 swqe->tx_control |= EHEA_SWQE_CRC
1886                                                  | EHEA_SWQE_IP_CHECKSUM
1887                                                  | EHEA_SWQE_TCP_CHECKSUM
1888                                                  | EHEA_SWQE_IMM_DATA_PRESENT;
1889
1890                                 write_udp_offset_end(swqe, skb);
1891                         }
1892                 } else {
1893                         /* icmp (big data) and
1894                            ip segmentation packets (all other ip packets) */
1895                         swqe->tx_control |= EHEA_SWQE_CRC
1896                                          | EHEA_SWQE_IP_CHECKSUM
1897                                          | EHEA_SWQE_IMM_DATA_PRESENT;
1898                 }
1899         } else {
1900                 /* Other Ethernet Protocol */
1901                 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
1902         }
1903         /* copy (immediate) data */
1904         if (nfrags == 0) {
1905                 /* data is in a single piece */
1906                 skb_copy_from_linear_data(skb, imm_data, skb->len);
1907         } else {
1908                 /* first copy data from the skb->data buffer ... */
1909                 skb_copy_from_linear_data(skb, imm_data,
1910                                           skb->len - skb->data_len);
1911                 imm_data += skb->len - skb->data_len;
1912
1913                 /* ... then copy data from the fragments */
1914                 for (i = 0; i < nfrags; i++) {
1915                         frag = &skb_shinfo(skb)->frags[i];
1916                         memcpy(imm_data,
1917                                page_address(frag->page) + frag->page_offset,
1918                                frag->size);
1919                         imm_data += frag->size;
1920                 }
1921         }
1922         swqe->immediate_data_length = skb->len;
1923         dev_kfree_skb(skb);
1924 }
1925
1926 static inline int ehea_hash_skb(struct sk_buff *skb, int num_qps)
1927 {
1928         struct tcphdr *tcp;
1929         u32 tmp;
1930
1931         if ((skb->protocol == htons(ETH_P_IP)) &&
1932             (ip_hdr(skb)->protocol == IPPROTO_TCP)) {
1933                 tcp = (struct tcphdr *)(skb_network_header(skb) +
1934                                         (ip_hdr(skb)->ihl * 4));
1935                 tmp = (tcp->source + (tcp->dest << 16)) % 31;
1936                 tmp += ip_hdr(skb)->daddr % 31;
1937                 return tmp % num_qps;
1938         } else
1939                 return 0;
1940 }
1941
1942 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
1943 {
1944         struct ehea_port *port = netdev_priv(dev);
1945         struct ehea_swqe *swqe;
1946         unsigned long flags;
1947         u32 lkey;
1948         int swqe_index;
1949         struct ehea_port_res *pr;
1950
1951         pr = &port->port_res[ehea_hash_skb(skb, port->num_tx_qps)];
1952
1953         if (!spin_trylock(&pr->xmit_lock))
1954                 return NETDEV_TX_BUSY;
1955
1956         if (pr->queue_stopped) {
1957                 spin_unlock(&pr->xmit_lock);
1958                 return NETDEV_TX_BUSY;
1959         }
1960
1961         swqe = ehea_get_swqe(pr->qp, &swqe_index);
1962         memset(swqe, 0, SWQE_HEADER_SIZE);
1963         atomic_dec(&pr->swqe_avail);
1964
1965         if (skb->len <= SWQE3_MAX_IMM) {
1966                 u32 sig_iv = port->sig_comp_iv;
1967                 u32 swqe_num = pr->swqe_id_counter;
1968                 ehea_xmit3(skb, dev, swqe);
1969                 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
1970                         | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
1971                 if (pr->swqe_ll_count >= (sig_iv - 1)) {
1972                         swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
1973                                                       sig_iv);
1974                         swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1975                         pr->swqe_ll_count = 0;
1976                 } else
1977                         pr->swqe_ll_count += 1;
1978         } else {
1979                 swqe->wr_id =
1980                         EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
1981                       | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
1982                       | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
1983                       | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
1984                 pr->sq_skba.arr[pr->sq_skba.index] = skb;
1985
1986                 pr->sq_skba.index++;
1987                 pr->sq_skba.index &= (pr->sq_skba.len - 1);
1988
1989                 lkey = pr->send_mr.lkey;
1990                 ehea_xmit2(skb, dev, swqe, lkey);
1991                 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1992         }
1993         pr->swqe_id_counter += 1;
1994
1995         if (port->vgrp && vlan_tx_tag_present(skb)) {
1996                 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
1997                 swqe->vlan_tag = vlan_tx_tag_get(skb);
1998         }
1999
2000         if (netif_msg_tx_queued(port)) {
2001                 ehea_info("post swqe on QP %d", pr->qp->init_attr.qp_nr);
2002                 ehea_dump(swqe, 512, "swqe");
2003         }
2004
2005         if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2006                 netif_stop_queue(dev);
2007                 swqe->tx_control |= EHEA_SWQE_PURGE;
2008         }
2009
2010         ehea_post_swqe(pr->qp, swqe);
2011         pr->tx_packets++;
2012
2013         if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2014                 spin_lock_irqsave(&pr->netif_queue, flags);
2015                 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2016                         pr->p_stats.queue_stopped++;
2017                         netif_stop_queue(dev);
2018                         pr->queue_stopped = 1;
2019                 }
2020                 spin_unlock_irqrestore(&pr->netif_queue, flags);
2021         }
2022         dev->trans_start = jiffies;
2023         spin_unlock(&pr->xmit_lock);
2024
2025         return NETDEV_TX_OK;
2026 }
2027
2028 static void ehea_vlan_rx_register(struct net_device *dev,
2029                                   struct vlan_group *grp)
2030 {
2031         struct ehea_port *port = netdev_priv(dev);
2032         struct ehea_adapter *adapter = port->adapter;
2033         struct hcp_ehea_port_cb1 *cb1;
2034         u64 hret;
2035
2036         port->vgrp = grp;
2037
2038         cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2039         if (!cb1) {
2040                 ehea_error("no mem for cb1");
2041                 goto out;
2042         }
2043
2044         memset(cb1->vlan_filter, 0, sizeof(cb1->vlan_filter));
2045
2046         hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2047                                        H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2048         if (hret != H_SUCCESS)
2049                 ehea_error("modify_ehea_port failed");
2050
2051         kfree(cb1);
2052 out:
2053         return;
2054 }
2055
2056 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
2057 {
2058         struct ehea_port *port = netdev_priv(dev);
2059         struct ehea_adapter *adapter = port->adapter;
2060         struct hcp_ehea_port_cb1 *cb1;
2061         int index;
2062         u64 hret;
2063
2064         cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2065         if (!cb1) {
2066                 ehea_error("no mem for cb1");
2067                 goto out;
2068         }
2069
2070         hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2071                                       H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2072         if (hret != H_SUCCESS) {
2073                 ehea_error("query_ehea_port failed");
2074                 goto out;
2075         }
2076
2077         index = (vid / 64);
2078         cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2079
2080         hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2081                                        H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2082         if (hret != H_SUCCESS)
2083                 ehea_error("modify_ehea_port failed");
2084 out:
2085         kfree(cb1);
2086         return;
2087 }
2088
2089 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2090 {
2091         struct ehea_port *port = netdev_priv(dev);
2092         struct ehea_adapter *adapter = port->adapter;
2093         struct hcp_ehea_port_cb1 *cb1;
2094         int index;
2095         u64 hret;
2096
2097         vlan_group_set_device(port->vgrp, vid, NULL);
2098
2099         cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2100         if (!cb1) {
2101                 ehea_error("no mem for cb1");
2102                 goto out;
2103         }
2104
2105         hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2106                                       H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2107         if (hret != H_SUCCESS) {
2108                 ehea_error("query_ehea_port failed");
2109                 goto out;
2110         }
2111
2112         index = (vid / 64);
2113         cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2114
2115         hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2116                                        H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2117         if (hret != H_SUCCESS)
2118                 ehea_error("modify_ehea_port failed");
2119 out:
2120         kfree(cb1);
2121         return;
2122 }
2123
2124 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2125 {
2126         int ret = -EIO;
2127         u64 hret;
2128         u16 dummy16 = 0;
2129         u64 dummy64 = 0;
2130         struct hcp_modify_qp_cb0 *cb0;
2131
2132         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2133         if (!cb0) {
2134                 ret = -ENOMEM;
2135                 goto out;
2136         }
2137
2138         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2139                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2140         if (hret != H_SUCCESS) {
2141                 ehea_error("query_ehea_qp failed (1)");
2142                 goto out;
2143         }
2144
2145         cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2146         hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2147                                      EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2148                                      &dummy64, &dummy64, &dummy16, &dummy16);
2149         if (hret != H_SUCCESS) {
2150                 ehea_error("modify_ehea_qp failed (1)");
2151                 goto out;
2152         }
2153
2154         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2155                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2156         if (hret != H_SUCCESS) {
2157                 ehea_error("query_ehea_qp failed (2)");
2158                 goto out;
2159         }
2160
2161         cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2162         hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2163                                      EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2164                                      &dummy64, &dummy64, &dummy16, &dummy16);
2165         if (hret != H_SUCCESS) {
2166                 ehea_error("modify_ehea_qp failed (2)");
2167                 goto out;
2168         }
2169
2170         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2171                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2172         if (hret != H_SUCCESS) {
2173                 ehea_error("query_ehea_qp failed (3)");
2174                 goto out;
2175         }
2176
2177         cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2178         hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2179                                      EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2180                                      &dummy64, &dummy64, &dummy16, &dummy16);
2181         if (hret != H_SUCCESS) {
2182                 ehea_error("modify_ehea_qp failed (3)");
2183                 goto out;
2184         }
2185
2186         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2187                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2188         if (hret != H_SUCCESS) {
2189                 ehea_error("query_ehea_qp failed (4)");
2190                 goto out;
2191         }
2192
2193         ret = 0;
2194 out:
2195         kfree(cb0);
2196         return ret;
2197 }
2198
2199 static int ehea_port_res_setup(struct ehea_port *port, int def_qps,
2200                                int add_tx_qps)
2201 {
2202         int ret, i;
2203         struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2204         enum ehea_eq_type eq_type = EHEA_EQ;
2205
2206         port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2207                                    EHEA_MAX_ENTRIES_EQ, 1);
2208         if (!port->qp_eq) {
2209                 ret = -EINVAL;
2210                 ehea_error("ehea_create_eq failed (qp_eq)");
2211                 goto out_kill_eq;
2212         }
2213
2214         pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2215         pr_cfg.max_entries_scq = sq_entries * 2;
2216         pr_cfg.max_entries_sq = sq_entries;
2217         pr_cfg.max_entries_rq1 = rq1_entries;
2218         pr_cfg.max_entries_rq2 = rq2_entries;
2219         pr_cfg.max_entries_rq3 = rq3_entries;
2220
2221         pr_cfg_small_rx.max_entries_rcq = 1;
2222         pr_cfg_small_rx.max_entries_scq = sq_entries;
2223         pr_cfg_small_rx.max_entries_sq = sq_entries;
2224         pr_cfg_small_rx.max_entries_rq1 = 1;
2225         pr_cfg_small_rx.max_entries_rq2 = 1;
2226         pr_cfg_small_rx.max_entries_rq3 = 1;
2227
2228         for (i = 0; i < def_qps; i++) {
2229                 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2230                 if (ret)
2231                         goto out_clean_pr;
2232         }
2233         for (i = def_qps; i < def_qps + add_tx_qps; i++) {
2234                 ret = ehea_init_port_res(port, &port->port_res[i],
2235                                          &pr_cfg_small_rx, i);
2236                 if (ret)
2237                         goto out_clean_pr;
2238         }
2239
2240         return 0;
2241
2242 out_clean_pr:
2243         while (--i >= 0)
2244                 ehea_clean_portres(port, &port->port_res[i]);
2245
2246 out_kill_eq:
2247         ehea_destroy_eq(port->qp_eq);
2248         return ret;
2249 }
2250
2251 static int ehea_clean_all_portres(struct ehea_port *port)
2252 {
2253         int ret = 0;
2254         int i;
2255
2256         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2257                 ret |= ehea_clean_portres(port, &port->port_res[i]);
2258
2259         ret |= ehea_destroy_eq(port->qp_eq);
2260
2261         return ret;
2262 }
2263
2264 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2265 {
2266         if (adapter->active_ports)
2267                 return;
2268
2269         ehea_rem_mr(&adapter->mr);
2270 }
2271
2272 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2273 {
2274         if (adapter->active_ports)
2275                 return 0;
2276
2277         return ehea_reg_kernel_mr(adapter, &adapter->mr);
2278 }
2279
2280 static int ehea_up(struct net_device *dev)
2281 {
2282         int ret, i;
2283         struct ehea_port *port = netdev_priv(dev);
2284
2285         if (port->state == EHEA_PORT_UP)
2286                 return 0;
2287
2288         ret = ehea_port_res_setup(port, port->num_def_qps,
2289                                   port->num_add_tx_qps);
2290         if (ret) {
2291                 ehea_error("port_res_failed");
2292                 goto out;
2293         }
2294
2295         /* Set default QP for this port */
2296         ret = ehea_configure_port(port);
2297         if (ret) {
2298                 ehea_error("ehea_configure_port failed. ret:%d", ret);
2299                 goto out_clean_pr;
2300         }
2301
2302         ret = ehea_reg_interrupts(dev);
2303         if (ret) {
2304                 ehea_error("reg_interrupts failed. ret:%d", ret);
2305                 goto out_clean_pr;
2306         }
2307
2308         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2309                 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2310                 if (ret) {
2311                         ehea_error("activate_qp failed");
2312                         goto out_free_irqs;
2313                 }
2314         }
2315
2316         for (i = 0; i < port->num_def_qps; i++) {
2317                 ret = ehea_fill_port_res(&port->port_res[i]);
2318                 if (ret) {
2319                         ehea_error("out_free_irqs");
2320                         goto out_free_irqs;
2321                 }
2322         }
2323
2324         ret = 0;
2325         port->state = EHEA_PORT_UP;
2326         goto out;
2327
2328 out_free_irqs:
2329         ehea_free_interrupts(dev);
2330
2331 out_clean_pr:
2332         ehea_clean_all_portres(port);
2333 out:
2334         if (ret)
2335                 ehea_info("Failed starting %s. ret=%i", dev->name, ret);
2336
2337         return ret;
2338 }
2339
2340 static void port_napi_disable(struct ehea_port *port)
2341 {
2342         int i;
2343
2344         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2345                 napi_disable(&port->port_res[i].napi);
2346 }
2347
2348 static void port_napi_enable(struct ehea_port *port)
2349 {
2350         int i;
2351
2352         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2353                 napi_enable(&port->port_res[i].napi);
2354 }
2355
2356 static int ehea_open(struct net_device *dev)
2357 {
2358         int ret;
2359         struct ehea_port *port = netdev_priv(dev);
2360
2361         down(&port->port_lock);
2362
2363         if (netif_msg_ifup(port))
2364                 ehea_info("enabling port %s", dev->name);
2365
2366         ret = ehea_up(dev);
2367         if (!ret) {
2368                 port_napi_enable(port);
2369                 netif_start_queue(dev);
2370         }
2371
2372         up(&port->port_lock);
2373
2374         return ret;
2375 }
2376
2377 static int ehea_down(struct net_device *dev)
2378 {
2379         int ret;
2380         struct ehea_port *port = netdev_priv(dev);
2381
2382         if (port->state == EHEA_PORT_DOWN)
2383                 return 0;
2384
2385         ehea_drop_multicast_list(dev);
2386         ehea_free_interrupts(dev);
2387
2388         port->state = EHEA_PORT_DOWN;
2389
2390         ret = ehea_clean_all_portres(port);
2391         if (ret)
2392                 ehea_info("Failed freeing resources for %s. ret=%i",
2393                           dev->name, ret);
2394
2395         return ret;
2396 }
2397
2398 static int ehea_stop(struct net_device *dev)
2399 {
2400         int ret;
2401         struct ehea_port *port = netdev_priv(dev);
2402
2403         if (netif_msg_ifdown(port))
2404                 ehea_info("disabling port %s", dev->name);
2405
2406         flush_scheduled_work();
2407         down(&port->port_lock);
2408         netif_stop_queue(dev);
2409         port_napi_disable(port);
2410         ret = ehea_down(dev);
2411         up(&port->port_lock);
2412         return ret;
2413 }
2414
2415 void ehea_purge_sq(struct ehea_qp *orig_qp)
2416 {
2417         struct ehea_qp qp = *orig_qp;
2418         struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2419         struct ehea_swqe *swqe;
2420         int wqe_index;
2421         int i;
2422
2423         for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2424                 swqe = ehea_get_swqe(&qp, &wqe_index);
2425                 swqe->tx_control |= EHEA_SWQE_PURGE;
2426         }
2427 }
2428
2429 int ehea_stop_qps(struct net_device *dev)
2430 {
2431         struct ehea_port *port = netdev_priv(dev);
2432         struct ehea_adapter *adapter = port->adapter;
2433         struct hcp_modify_qp_cb0 *cb0;
2434         int ret = -EIO;
2435         int dret;
2436         int i;
2437         u64 hret;
2438         u64 dummy64 = 0;
2439         u16 dummy16 = 0;
2440
2441         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2442         if (!cb0) {
2443                 ret = -ENOMEM;
2444                 goto out;
2445         }
2446
2447         for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2448                 struct ehea_port_res *pr =  &port->port_res[i];
2449                 struct ehea_qp *qp = pr->qp;
2450
2451                 /* Purge send queue */
2452                 ehea_purge_sq(qp);
2453
2454                 /* Disable queue pair */
2455                 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2456                                             EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2457                                             cb0);
2458                 if (hret != H_SUCCESS) {
2459                         ehea_error("query_ehea_qp failed (1)");
2460                         goto out;
2461                 }
2462
2463                 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2464                 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2465
2466                 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2467                                              EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2468                                                             1), cb0, &dummy64,
2469                                              &dummy64, &dummy16, &dummy16);
2470                 if (hret != H_SUCCESS) {
2471                         ehea_error("modify_ehea_qp failed (1)");
2472                         goto out;
2473                 }
2474
2475                 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2476                                             EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2477                                             cb0);
2478                 if (hret != H_SUCCESS) {
2479                         ehea_error("query_ehea_qp failed (2)");
2480                         goto out;
2481                 }
2482
2483                 /* deregister shared memory regions */
2484                 dret = ehea_rem_smrs(pr);
2485                 if (dret) {
2486                         ehea_error("unreg shared memory region failed");
2487                         goto out;
2488                 }
2489         }
2490
2491         ret = 0;
2492 out:
2493         kfree(cb0);
2494
2495         return ret;
2496 }
2497
2498 void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2499 {
2500         struct ehea_qp qp = *orig_qp;
2501         struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2502         struct ehea_rwqe *rwqe;
2503         struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2504         struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2505         struct sk_buff *skb;
2506         u32 lkey = pr->recv_mr.lkey;
2507
2508
2509         int i;
2510         int index;
2511
2512         for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2513                 rwqe = ehea_get_next_rwqe(&qp, 2);
2514                 rwqe->sg_list[0].l_key = lkey;
2515                 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2516                 skb = skba_rq2[index];
2517                 if (skb)
2518                         rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2519         }
2520
2521         for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2522                 rwqe = ehea_get_next_rwqe(&qp, 3);
2523                 rwqe->sg_list[0].l_key = lkey;
2524                 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2525                 skb = skba_rq3[index];
2526                 if (skb)
2527                         rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2528         }
2529 }
2530
2531 int ehea_restart_qps(struct net_device *dev)
2532 {
2533         struct ehea_port *port = netdev_priv(dev);
2534         struct ehea_adapter *adapter = port->adapter;
2535         int ret = 0;
2536         int i;
2537
2538         struct hcp_modify_qp_cb0 *cb0;
2539         u64 hret;
2540         u64 dummy64 = 0;
2541         u16 dummy16 = 0;
2542
2543         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2544         if (!cb0) {
2545                 ret = -ENOMEM;
2546                 goto out;
2547         }
2548
2549         for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2550                 struct ehea_port_res *pr =  &port->port_res[i];
2551                 struct ehea_qp *qp = pr->qp;
2552
2553                 ret = ehea_gen_smrs(pr);
2554                 if (ret) {
2555                         ehea_error("creation of shared memory regions failed");
2556                         goto out;
2557                 }
2558
2559                 ehea_update_rqs(qp, pr);
2560
2561                 /* Enable queue pair */
2562                 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2563                                             EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2564                                             cb0);
2565                 if (hret != H_SUCCESS) {
2566                         ehea_error("query_ehea_qp failed (1)");
2567                         goto out;
2568                 }
2569
2570                 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2571                 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2572
2573                 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2574                                              EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2575                                                             1), cb0, &dummy64,
2576                                              &dummy64, &dummy16, &dummy16);
2577                 if (hret != H_SUCCESS) {
2578                         ehea_error("modify_ehea_qp failed (1)");
2579                         goto out;
2580                 }
2581
2582                 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2583                                             EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2584                                             cb0);
2585                 if (hret != H_SUCCESS) {
2586                         ehea_error("query_ehea_qp failed (2)");
2587                         goto out;
2588                 }
2589
2590                 /* refill entire queue */
2591                 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2592                 ehea_refill_rq2(pr, 0);
2593                 ehea_refill_rq3(pr, 0);
2594         }
2595 out:
2596         kfree(cb0);
2597
2598         return ret;
2599 }
2600
2601 static void ehea_reset_port(struct work_struct *work)
2602 {
2603         int ret;
2604         struct ehea_port *port =
2605                 container_of(work, struct ehea_port, reset_task);
2606         struct net_device *dev = port->netdev;
2607
2608         port->resets++;
2609         down(&port->port_lock);
2610         netif_stop_queue(dev);
2611
2612         port_napi_disable(port);
2613
2614         ehea_down(dev);
2615
2616         ret = ehea_up(dev);
2617         if (ret)
2618                 goto out;
2619
2620         ehea_set_multicast_list(dev);
2621
2622         if (netif_msg_timer(port))
2623                 ehea_info("Device %s resetted successfully", dev->name);
2624
2625         port_napi_enable(port);
2626
2627         netif_wake_queue(dev);
2628 out:
2629         up(&port->port_lock);
2630         return;
2631 }
2632
2633 static void ehea_rereg_mrs(struct work_struct *work)
2634 {
2635         int ret, i;
2636         struct ehea_adapter *adapter;
2637
2638         down(&dlpar_mem_lock);
2639         ehea_info("LPAR memory enlarged - re-initializing driver");
2640
2641         list_for_each_entry(adapter, &adapter_list, list)
2642                 if (adapter->active_ports) {
2643                         /* Shutdown all ports */
2644                         for (i = 0; i < EHEA_MAX_PORTS; i++) {
2645                                 struct ehea_port *port = adapter->port[i];
2646
2647                                 if (port) {
2648                                         struct net_device *dev = port->netdev;
2649
2650                                         if (dev->flags & IFF_UP) {
2651                                                 down(&port->port_lock);
2652                                                 netif_stop_queue(dev);
2653                                                 ret = ehea_stop_qps(dev);
2654                                                 if (ret) {
2655                                                         up(&port->port_lock);
2656                                                         goto out;
2657                                                 }
2658                                                 port_napi_disable(port);
2659                                                 up(&port->port_lock);
2660                                         }
2661                                 }
2662                         }
2663
2664                         /* Unregister old memory region */
2665                         ret = ehea_rem_mr(&adapter->mr);
2666                         if (ret) {
2667                                 ehea_error("unregister MR failed - driver"
2668                                            " inoperable!");
2669                                 goto out;
2670                         }
2671                 }
2672
2673         ehea_destroy_busmap();
2674         ret = ehea_create_busmap();
2675         if (ret) {
2676                 ehea_error("creating ehea busmap failed");
2677                 goto out;
2678         }
2679
2680         clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2681
2682         list_for_each_entry(adapter, &adapter_list, list)
2683                 if (adapter->active_ports) {
2684                         /* Register new memory region */
2685                         ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2686                         if (ret) {
2687                                 ehea_error("register MR failed - driver"
2688                                            " inoperable!");
2689                                 goto out;
2690                         }
2691
2692                         /* Restart all ports */
2693                         for (i = 0; i < EHEA_MAX_PORTS; i++) {
2694                                 struct ehea_port *port = adapter->port[i];
2695
2696                                 if (port) {
2697                                         struct net_device *dev = port->netdev;
2698
2699                                         if (dev->flags & IFF_UP) {
2700                                                 down(&port->port_lock);
2701                                                 port_napi_enable(port);
2702                                                 ret = ehea_restart_qps(dev);
2703                                                 if (!ret)
2704                                                         netif_wake_queue(dev);
2705                                                 up(&port->port_lock);
2706                                         }
2707                                 }
2708                         }
2709                 }
2710        up(&dlpar_mem_lock);
2711        ehea_info("re-initializing driver complete");
2712 out:
2713         return;
2714 }
2715
2716 static void ehea_tx_watchdog(struct net_device *dev)
2717 {
2718         struct ehea_port *port = netdev_priv(dev);
2719
2720         if (netif_carrier_ok(dev) &&
2721             !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2722                 schedule_work(&port->reset_task);
2723 }
2724
2725 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2726 {
2727         struct hcp_query_ehea *cb;
2728         u64 hret;
2729         int ret;
2730
2731         cb = kzalloc(PAGE_SIZE, GFP_KERNEL);
2732         if (!cb) {
2733                 ret = -ENOMEM;
2734                 goto out;
2735         }
2736
2737         hret = ehea_h_query_ehea(adapter->handle, cb);
2738
2739         if (hret != H_SUCCESS) {
2740                 ret = -EIO;
2741                 goto out_herr;
2742         }
2743
2744         adapter->max_mc_mac = cb->max_mc_mac - 1;
2745         ret = 0;
2746
2747 out_herr:
2748         kfree(cb);
2749 out:
2750         return ret;
2751 }
2752
2753 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2754 {
2755         struct hcp_ehea_port_cb4 *cb4;
2756         u64 hret;
2757         int ret = 0;
2758
2759         *jumbo = 0;
2760
2761         /* (Try to) enable *jumbo frames */
2762         cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2763         if (!cb4) {
2764                 ehea_error("no mem for cb4");
2765                 ret = -ENOMEM;
2766                 goto out;
2767         } else {
2768                 hret = ehea_h_query_ehea_port(port->adapter->handle,
2769                                               port->logical_port_id,
2770                                               H_PORT_CB4,
2771                                               H_PORT_CB4_JUMBO, cb4);
2772                 if (hret == H_SUCCESS) {
2773                         if (cb4->jumbo_frame)
2774                                 *jumbo = 1;
2775                         else {
2776                                 cb4->jumbo_frame = 1;
2777                                 hret = ehea_h_modify_ehea_port(port->adapter->
2778                                                                handle,
2779                                                                port->
2780                                                                logical_port_id,
2781                                                                H_PORT_CB4,
2782                                                                H_PORT_CB4_JUMBO,
2783                                                                cb4);
2784                                 if (hret == H_SUCCESS)
2785                                         *jumbo = 1;
2786                         }
2787                 } else
2788                         ret = -EINVAL;
2789
2790                 kfree(cb4);
2791         }
2792 out:
2793         return ret;
2794 }
2795
2796 static ssize_t ehea_show_port_id(struct device *dev,
2797                                  struct device_attribute *attr, char *buf)
2798 {
2799         struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2800         return sprintf(buf, "%d", port->logical_port_id);
2801 }
2802
2803 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2804                    NULL);
2805
2806 static void __devinit logical_port_release(struct device *dev)
2807 {
2808         struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2809         of_node_put(port->ofdev.node);
2810 }
2811
2812 static struct device *ehea_register_port(struct ehea_port *port,
2813                                          struct device_node *dn)
2814 {
2815         int ret;
2816
2817         port->ofdev.node = of_node_get(dn);
2818         port->ofdev.dev.parent = &port->adapter->ofdev->dev;
2819         port->ofdev.dev.bus = &ibmebus_bus_type;
2820
2821         sprintf(port->ofdev.dev.bus_id, "port%d", port_name_cnt++);
2822         port->ofdev.dev.release = logical_port_release;
2823
2824         ret = of_device_register(&port->ofdev);
2825         if (ret) {
2826                 ehea_error("failed to register device. ret=%d", ret);
2827                 goto out;
2828         }
2829
2830         ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2831         if (ret) {
2832                 ehea_error("failed to register attributes, ret=%d", ret);
2833                 goto out_unreg_of_dev;
2834         }
2835
2836         return &port->ofdev.dev;
2837
2838 out_unreg_of_dev:
2839         of_device_unregister(&port->ofdev);
2840 out:
2841         return NULL;
2842 }
2843
2844 static void ehea_unregister_port(struct ehea_port *port)
2845 {
2846         device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2847         of_device_unregister(&port->ofdev);
2848 }
2849
2850 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2851                                          u32 logical_port_id,
2852                                          struct device_node *dn)
2853 {
2854         int ret;
2855         struct net_device *dev;
2856         struct ehea_port *port;
2857         struct device *port_dev;
2858         int jumbo;
2859
2860         /* allocate memory for the port structures */
2861         dev = alloc_etherdev(sizeof(struct ehea_port));
2862
2863         if (!dev) {
2864                 ehea_error("no mem for net_device");
2865                 ret = -ENOMEM;
2866                 goto out_err;
2867         }
2868
2869         port = netdev_priv(dev);
2870
2871         sema_init(&port->port_lock, 1);
2872         port->state = EHEA_PORT_DOWN;
2873         port->sig_comp_iv = sq_entries / 10;
2874
2875         port->adapter = adapter;
2876         port->netdev = dev;
2877         port->logical_port_id = logical_port_id;
2878
2879         port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2880
2881         port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2882         if (!port->mc_list) {
2883                 ret = -ENOMEM;
2884                 goto out_free_ethdev;
2885         }
2886
2887         INIT_LIST_HEAD(&port->mc_list->list);
2888
2889         ret = ehea_sense_port_attr(port);
2890         if (ret)
2891                 goto out_free_mc_list;
2892
2893         port_dev = ehea_register_port(port, dn);
2894         if (!port_dev)
2895                 goto out_free_mc_list;
2896
2897         SET_NETDEV_DEV(dev, port_dev);
2898
2899         /* initialize net_device structure */
2900         memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
2901
2902         dev->open = ehea_open;
2903 #ifdef CONFIG_NET_POLL_CONTROLLER
2904         dev->poll_controller = ehea_netpoll;
2905 #endif
2906         dev->stop = ehea_stop;
2907         dev->hard_start_xmit = ehea_start_xmit;
2908         dev->get_stats = ehea_get_stats;
2909         dev->set_multicast_list = ehea_set_multicast_list;
2910         dev->set_mac_address = ehea_set_mac_addr;
2911         dev->change_mtu = ehea_change_mtu;
2912         dev->vlan_rx_register = ehea_vlan_rx_register;
2913         dev->vlan_rx_add_vid = ehea_vlan_rx_add_vid;
2914         dev->vlan_rx_kill_vid = ehea_vlan_rx_kill_vid;
2915         dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
2916                       | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX
2917                       | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
2918                       | NETIF_F_LLTX;
2919         dev->tx_timeout = &ehea_tx_watchdog;
2920         dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
2921
2922         INIT_WORK(&port->reset_task, ehea_reset_port);
2923
2924         ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2925         if (ret) {
2926                 ret = -EIO;
2927                 goto out_unreg_port;
2928         }
2929
2930         ehea_set_ethtool_ops(dev);
2931
2932         ret = register_netdev(dev);
2933         if (ret) {
2934                 ehea_error("register_netdev failed. ret=%d", ret);
2935                 goto out_dereg_bc;
2936         }
2937
2938         port->lro_max_aggr = lro_max_aggr;
2939
2940         ret = ehea_get_jumboframe_status(port, &jumbo);
2941         if (ret)
2942                 ehea_error("failed determining jumbo frame status for %s",
2943                            port->netdev->name);
2944
2945         ehea_info("%s: Jumbo frames are %sabled", dev->name,
2946                   jumbo == 1 ? "en" : "dis");
2947
2948         adapter->active_ports++;
2949
2950         return port;
2951
2952 out_dereg_bc:
2953         ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2954
2955 out_unreg_port:
2956         ehea_unregister_port(port);
2957
2958 out_free_mc_list:
2959         kfree(port->mc_list);
2960
2961 out_free_ethdev:
2962         free_netdev(dev);
2963
2964 out_err:
2965         ehea_error("setting up logical port with id=%d failed, ret=%d",
2966                    logical_port_id, ret);
2967         return NULL;
2968 }
2969
2970 static void ehea_shutdown_single_port(struct ehea_port *port)
2971 {
2972         unregister_netdev(port->netdev);
2973         ehea_unregister_port(port);
2974         ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2975         kfree(port->mc_list);
2976         free_netdev(port->netdev);
2977         port->adapter->active_ports--;
2978 }
2979
2980 static int ehea_setup_ports(struct ehea_adapter *adapter)
2981 {
2982         struct device_node *lhea_dn;
2983         struct device_node *eth_dn = NULL;
2984
2985         const u32 *dn_log_port_id;
2986         int i = 0;
2987
2988         lhea_dn = adapter->ofdev->node;
2989         while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
2990
2991                 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
2992                                                  NULL);
2993                 if (!dn_log_port_id) {
2994                         ehea_error("bad device node: eth_dn name=%s",
2995                                    eth_dn->full_name);
2996                         continue;
2997                 }
2998
2999                 if (ehea_add_adapter_mr(adapter)) {
3000                         ehea_error("creating MR failed");
3001                         of_node_put(eth_dn);
3002                         return -EIO;
3003                 }
3004
3005                 adapter->port[i] = ehea_setup_single_port(adapter,
3006                                                           *dn_log_port_id,
3007                                                           eth_dn);
3008                 if (adapter->port[i])
3009                         ehea_info("%s -> logical port id #%d",
3010                                   adapter->port[i]->netdev->name,
3011                                   *dn_log_port_id);
3012                 else
3013                         ehea_remove_adapter_mr(adapter);
3014
3015                 i++;
3016         };
3017
3018         return 0;
3019 }
3020
3021 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3022                                            u32 logical_port_id)
3023 {
3024         struct device_node *lhea_dn;
3025         struct device_node *eth_dn = NULL;
3026         const u32 *dn_log_port_id;
3027
3028         lhea_dn = adapter->ofdev->node;
3029         while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3030
3031                 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3032                                                  NULL);
3033                 if (dn_log_port_id)
3034                         if (*dn_log_port_id == logical_port_id)
3035                                 return eth_dn;
3036         };
3037
3038         return NULL;
3039 }
3040
3041 static ssize_t ehea_probe_port(struct device *dev,
3042                                struct device_attribute *attr,
3043                                const char *buf, size_t count)
3044 {
3045         struct ehea_adapter *adapter = dev->driver_data;
3046         struct ehea_port *port;
3047         struct device_node *eth_dn = NULL;
3048         int i;
3049
3050         u32 logical_port_id;
3051
3052         sscanf(buf, "%d", &logical_port_id);
3053
3054         port = ehea_get_port(adapter, logical_port_id);
3055
3056         if (port) {
3057                 ehea_info("adding port with logical port id=%d failed. port "
3058                           "already configured as %s.", logical_port_id,
3059                           port->netdev->name);
3060                 return -EINVAL;
3061         }
3062
3063         eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3064
3065         if (!eth_dn) {
3066                 ehea_info("no logical port with id %d found", logical_port_id);
3067                 return -EINVAL;
3068         }
3069
3070         if (ehea_add_adapter_mr(adapter)) {
3071                 ehea_error("creating MR failed");
3072                 return -EIO;
3073         }
3074
3075         port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3076
3077         of_node_put(eth_dn);
3078
3079         if (port) {
3080                 for (i = 0; i < EHEA_MAX_PORTS; i++)
3081                         if (!adapter->port[i]) {
3082                                 adapter->port[i] = port;
3083                                 break;
3084                         }
3085
3086                 ehea_info("added %s (logical port id=%d)", port->netdev->name,
3087                           logical_port_id);
3088         } else {
3089                 ehea_remove_adapter_mr(adapter);
3090                 return -EIO;
3091         }
3092
3093         return (ssize_t) count;
3094 }
3095
3096 static ssize_t ehea_remove_port(struct device *dev,
3097                                 struct device_attribute *attr,
3098                                 const char *buf, size_t count)
3099 {
3100         struct ehea_adapter *adapter = dev->driver_data;
3101         struct ehea_port *port;
3102         int i;
3103         u32 logical_port_id;
3104
3105         sscanf(buf, "%d", &logical_port_id);
3106
3107         port = ehea_get_port(adapter, logical_port_id);
3108
3109         if (port) {
3110                 ehea_info("removed %s (logical port id=%d)", port->netdev->name,
3111                           logical_port_id);
3112
3113                 ehea_shutdown_single_port(port);
3114
3115                 for (i = 0; i < EHEA_MAX_PORTS; i++)
3116                         if (adapter->port[i] == port) {
3117                                 adapter->port[i] = NULL;
3118                                 break;
3119                         }
3120         } else {
3121                 ehea_error("removing port with logical port id=%d failed. port "
3122                            "not configured.", logical_port_id);
3123                 return -EINVAL;
3124         }
3125
3126         ehea_remove_adapter_mr(adapter);
3127
3128         return (ssize_t) count;
3129 }
3130
3131 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3132 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3133
3134 int ehea_create_device_sysfs(struct of_device *dev)
3135 {
3136         int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3137         if (ret)
3138                 goto out;
3139
3140         ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3141 out:
3142         return ret;
3143 }
3144
3145 void ehea_remove_device_sysfs(struct of_device *dev)
3146 {
3147         device_remove_file(&dev->dev, &dev_attr_probe_port);
3148         device_remove_file(&dev->dev, &dev_attr_remove_port);
3149 }
3150
3151 static int __devinit ehea_probe_adapter(struct of_device *dev,
3152                                         const struct of_device_id *id)
3153 {
3154         struct ehea_adapter *adapter;
3155         const u64 *adapter_handle;
3156         int ret;
3157
3158         if (!dev || !dev->node) {
3159                 ehea_error("Invalid ibmebus device probed");
3160                 return -EINVAL;
3161         }
3162
3163         adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3164         if (!adapter) {
3165                 ret = -ENOMEM;
3166                 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3167                 goto out;
3168         }
3169
3170         list_add(&adapter->list, &adapter_list);
3171
3172         adapter->ofdev = dev;
3173
3174         adapter_handle = of_get_property(dev->node, "ibm,hea-handle",
3175                                          NULL);
3176         if (adapter_handle)
3177                 adapter->handle = *adapter_handle;
3178
3179         if (!adapter->handle) {
3180                 dev_err(&dev->dev, "failed getting handle for adapter"
3181                         " '%s'\n", dev->node->full_name);
3182                 ret = -ENODEV;
3183                 goto out_free_ad;
3184         }
3185
3186         adapter->pd = EHEA_PD_ID;
3187
3188         dev->dev.driver_data = adapter;
3189
3190
3191         /* initialize adapter and ports */
3192         /* get adapter properties */
3193         ret = ehea_sense_adapter_attr(adapter);
3194         if (ret) {
3195                 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3196                 goto out_free_ad;
3197         }
3198
3199         adapter->neq = ehea_create_eq(adapter,
3200                                       EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3201         if (!adapter->neq) {
3202                 ret = -EIO;
3203                 dev_err(&dev->dev, "NEQ creation failed\n");
3204                 goto out_free_ad;
3205         }
3206
3207         tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3208                      (unsigned long)adapter);
3209
3210         ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3211                                   ehea_interrupt_neq, IRQF_DISABLED,
3212                                   "ehea_neq", adapter);
3213         if (ret) {
3214                 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3215                 goto out_kill_eq;
3216         }
3217
3218         ret = ehea_create_device_sysfs(dev);
3219         if (ret)
3220                 goto out_free_irq;
3221
3222         ret = ehea_setup_ports(adapter);
3223         if (ret) {
3224                 dev_err(&dev->dev, "setup_ports failed\n");
3225                 goto out_rem_dev_sysfs;
3226         }
3227
3228         ret = 0;
3229         goto out;
3230
3231 out_rem_dev_sysfs:
3232         ehea_remove_device_sysfs(dev);
3233
3234 out_free_irq:
3235         ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3236
3237 out_kill_eq:
3238         ehea_destroy_eq(adapter->neq);
3239
3240 out_free_ad:
3241         kfree(adapter);
3242 out:
3243         return ret;
3244 }
3245
3246 static int __devexit ehea_remove(struct of_device *dev)
3247 {
3248         struct ehea_adapter *adapter = dev->dev.driver_data;
3249         int i;
3250
3251         for (i = 0; i < EHEA_MAX_PORTS; i++)
3252                 if (adapter->port[i]) {
3253                         ehea_shutdown_single_port(adapter->port[i]);
3254                         adapter->port[i] = NULL;
3255                 }
3256
3257         ehea_remove_device_sysfs(dev);
3258
3259         flush_scheduled_work();
3260
3261         ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3262         tasklet_kill(&adapter->neq_tasklet);
3263
3264         ehea_destroy_eq(adapter->neq);
3265         ehea_remove_adapter_mr(adapter);
3266         list_del(&adapter->list);
3267
3268         kfree(adapter);
3269
3270         return 0;
3271 }
3272
3273 static int ehea_reboot_notifier(struct notifier_block *nb,
3274                                 unsigned long action, void *unused)
3275 {
3276         if (action == SYS_RESTART) {
3277                 ehea_info("Reboot: freeing all eHEA resources");
3278                 ibmebus_unregister_driver(&ehea_driver);
3279         }
3280         return NOTIFY_DONE;
3281 }
3282
3283 static struct notifier_block ehea_reboot_nb = {
3284         .notifier_call = ehea_reboot_notifier,
3285 };
3286
3287 static int check_module_parm(void)
3288 {
3289         int ret = 0;
3290
3291         if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3292             (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3293                 ehea_info("Bad parameter: rq1_entries");
3294                 ret = -EINVAL;
3295         }
3296         if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3297             (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3298                 ehea_info("Bad parameter: rq2_entries");
3299                 ret = -EINVAL;
3300         }
3301         if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3302             (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3303                 ehea_info("Bad parameter: rq3_entries");
3304                 ret = -EINVAL;
3305         }
3306         if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3307             (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3308                 ehea_info("Bad parameter: sq_entries");
3309                 ret = -EINVAL;
3310         }
3311
3312         return ret;
3313 }
3314
3315 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3316                                       char *buf)
3317 {
3318         return sprintf(buf, "%d", EHEA_CAPABILITIES);
3319 }
3320
3321 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3322                    ehea_show_capabilities, NULL);
3323
3324 int __init ehea_module_init(void)
3325 {
3326         int ret;
3327
3328         printk(KERN_INFO "IBM eHEA ethernet device driver (Release %s)\n",
3329                DRV_VERSION);
3330
3331
3332         INIT_WORK(&ehea_rereg_mr_task, ehea_rereg_mrs);
3333         sema_init(&dlpar_mem_lock, 1);
3334
3335         ret = check_module_parm();
3336         if (ret)
3337                 goto out;
3338
3339         ret = ehea_create_busmap();
3340         if (ret)
3341                 goto out;
3342
3343         register_reboot_notifier(&ehea_reboot_nb);
3344
3345         ret = ibmebus_register_driver(&ehea_driver);
3346         if (ret) {
3347                 ehea_error("failed registering eHEA device driver on ebus");
3348                 goto out;
3349         }
3350
3351         ret = driver_create_file(&ehea_driver.driver,
3352                                  &driver_attr_capabilities);
3353         if (ret) {
3354                 ehea_error("failed to register capabilities attribute, ret=%d",
3355                            ret);
3356                 unregister_reboot_notifier(&ehea_reboot_nb);
3357                 ibmebus_unregister_driver(&ehea_driver);
3358                 goto out;
3359         }
3360
3361 out:
3362         return ret;
3363 }
3364
3365 static void __exit ehea_module_exit(void)
3366 {
3367         flush_scheduled_work();
3368         driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3369         ibmebus_unregister_driver(&ehea_driver);
3370         unregister_reboot_notifier(&ehea_reboot_nb);
3371         ehea_destroy_busmap();
3372 }
3373
3374 module_init(ehea_module_init);
3375 module_exit(ehea_module_exit);