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