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