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