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