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