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