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