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