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