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