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