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