Linux 3.11-rc3
[cris-mirror.git] / drivers / net / ethernet / ibm / ehea / ehea_main.c
blob35853b43d66e860bd24e5bad2265b6dae9c9f7cc
1 /*
2 * linux/drivers/net/ethernet/ibm/ehea/ehea_main.c
4 * eHEA ethernet device driver for IBM eServer System p
6 * (C) Copyright IBM Corp. 2006
8 * Authors:
9 * Christoph Raisch <raisch@de.ibm.com>
10 * Jan-Bernd Themann <themann@de.ibm.com>
11 * Thomas Klein <tklein@de.ibm.com>
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.
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.
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.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
46 #include <net/ip.h>
48 #include "ehea.h"
49 #include "ehea_qmr.h"
50 #include "ehea_phyp.h"
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);
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;
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);
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 = [7..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 = [7..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 = [7..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 = [7..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");
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;
101 static int ehea_probe_adapter(struct platform_device *dev);
103 static int ehea_remove(struct platform_device *dev);
105 static struct of_device_id ehea_device_table[] = {
107 .name = "lhea",
108 .compatible = "IBM,lhea",
112 MODULE_DEVICE_TABLE(of, ehea_device_table);
114 static struct platform_driver ehea_driver = {
115 .driver = {
116 .name = "ehea",
117 .owner = THIS_MODULE,
118 .of_match_table = ehea_device_table,
120 .probe = ehea_probe_adapter,
121 .remove = ehea_remove,
124 void ehea_dump(void *adr, int len, char *msg)
126 int x;
127 unsigned char *deb = adr;
128 for (x = 0; x < len; x += 16) {
129 pr_info("%s adr=%p ofs=%04x %016llx %016llx\n",
130 msg, deb, x, *((u64 *)&deb[0]), *((u64 *)&deb[8]));
131 deb += 16;
135 static void ehea_schedule_port_reset(struct ehea_port *port)
137 if (!test_bit(__EHEA_DISABLE_PORT_RESET, &port->flags))
138 schedule_work(&port->reset_task);
141 static void ehea_update_firmware_handles(void)
143 struct ehea_fw_handle_entry *arr = NULL;
144 struct ehea_adapter *adapter;
145 int num_adapters = 0;
146 int num_ports = 0;
147 int num_portres = 0;
148 int i = 0;
149 int num_fw_handles, k, l;
151 /* Determine number of handles */
152 mutex_lock(&ehea_fw_handles.lock);
154 list_for_each_entry(adapter, &adapter_list, list) {
155 num_adapters++;
157 for (k = 0; k < EHEA_MAX_PORTS; k++) {
158 struct ehea_port *port = adapter->port[k];
160 if (!port || (port->state != EHEA_PORT_UP))
161 continue;
163 num_ports++;
164 num_portres += port->num_def_qps;
168 num_fw_handles = num_adapters * EHEA_NUM_ADAPTER_FW_HANDLES +
169 num_ports * EHEA_NUM_PORT_FW_HANDLES +
170 num_portres * EHEA_NUM_PORTRES_FW_HANDLES;
172 if (num_fw_handles) {
173 arr = kcalloc(num_fw_handles, sizeof(*arr), GFP_KERNEL);
174 if (!arr)
175 goto out; /* Keep the existing array */
176 } else
177 goto out_update;
179 list_for_each_entry(adapter, &adapter_list, list) {
180 if (num_adapters == 0)
181 break;
183 for (k = 0; k < EHEA_MAX_PORTS; k++) {
184 struct ehea_port *port = adapter->port[k];
186 if (!port || (port->state != EHEA_PORT_UP) ||
187 (num_ports == 0))
188 continue;
190 for (l = 0; l < port->num_def_qps; l++) {
191 struct ehea_port_res *pr = &port->port_res[l];
193 arr[i].adh = adapter->handle;
194 arr[i++].fwh = pr->qp->fw_handle;
195 arr[i].adh = adapter->handle;
196 arr[i++].fwh = pr->send_cq->fw_handle;
197 arr[i].adh = adapter->handle;
198 arr[i++].fwh = pr->recv_cq->fw_handle;
199 arr[i].adh = adapter->handle;
200 arr[i++].fwh = pr->eq->fw_handle;
201 arr[i].adh = adapter->handle;
202 arr[i++].fwh = pr->send_mr.handle;
203 arr[i].adh = adapter->handle;
204 arr[i++].fwh = pr->recv_mr.handle;
206 arr[i].adh = adapter->handle;
207 arr[i++].fwh = port->qp_eq->fw_handle;
208 num_ports--;
211 arr[i].adh = adapter->handle;
212 arr[i++].fwh = adapter->neq->fw_handle;
214 if (adapter->mr.handle) {
215 arr[i].adh = adapter->handle;
216 arr[i++].fwh = adapter->mr.handle;
218 num_adapters--;
221 out_update:
222 kfree(ehea_fw_handles.arr);
223 ehea_fw_handles.arr = arr;
224 ehea_fw_handles.num_entries = i;
225 out:
226 mutex_unlock(&ehea_fw_handles.lock);
229 static void ehea_update_bcmc_registrations(void)
231 unsigned long flags;
232 struct ehea_bcmc_reg_entry *arr = NULL;
233 struct ehea_adapter *adapter;
234 struct ehea_mc_list *mc_entry;
235 int num_registrations = 0;
236 int i = 0;
237 int k;
239 spin_lock_irqsave(&ehea_bcmc_regs.lock, flags);
241 /* Determine number of registrations */
242 list_for_each_entry(adapter, &adapter_list, list)
243 for (k = 0; k < EHEA_MAX_PORTS; k++) {
244 struct ehea_port *port = adapter->port[k];
246 if (!port || (port->state != EHEA_PORT_UP))
247 continue;
249 num_registrations += 2; /* Broadcast registrations */
251 list_for_each_entry(mc_entry, &port->mc_list->list,list)
252 num_registrations += 2;
255 if (num_registrations) {
256 arr = kcalloc(num_registrations, sizeof(*arr), GFP_ATOMIC);
257 if (!arr)
258 goto out; /* Keep the existing array */
259 } else
260 goto out_update;
262 list_for_each_entry(adapter, &adapter_list, list) {
263 for (k = 0; k < EHEA_MAX_PORTS; k++) {
264 struct ehea_port *port = adapter->port[k];
266 if (!port || (port->state != EHEA_PORT_UP))
267 continue;
269 if (num_registrations == 0)
270 goto out_update;
272 arr[i].adh = adapter->handle;
273 arr[i].port_id = port->logical_port_id;
274 arr[i].reg_type = EHEA_BCMC_BROADCAST |
275 EHEA_BCMC_UNTAGGED;
276 arr[i++].macaddr = port->mac_addr;
278 arr[i].adh = adapter->handle;
279 arr[i].port_id = port->logical_port_id;
280 arr[i].reg_type = EHEA_BCMC_BROADCAST |
281 EHEA_BCMC_VLANID_ALL;
282 arr[i++].macaddr = port->mac_addr;
283 num_registrations -= 2;
285 list_for_each_entry(mc_entry,
286 &port->mc_list->list, list) {
287 if (num_registrations == 0)
288 goto out_update;
290 arr[i].adh = adapter->handle;
291 arr[i].port_id = port->logical_port_id;
292 arr[i].reg_type = EHEA_BCMC_MULTICAST |
293 EHEA_BCMC_UNTAGGED;
294 if (mc_entry->macaddr == 0)
295 arr[i].reg_type |= EHEA_BCMC_SCOPE_ALL;
296 arr[i++].macaddr = mc_entry->macaddr;
298 arr[i].adh = adapter->handle;
299 arr[i].port_id = port->logical_port_id;
300 arr[i].reg_type = EHEA_BCMC_MULTICAST |
301 EHEA_BCMC_VLANID_ALL;
302 if (mc_entry->macaddr == 0)
303 arr[i].reg_type |= EHEA_BCMC_SCOPE_ALL;
304 arr[i++].macaddr = mc_entry->macaddr;
305 num_registrations -= 2;
310 out_update:
311 kfree(ehea_bcmc_regs.arr);
312 ehea_bcmc_regs.arr = arr;
313 ehea_bcmc_regs.num_entries = i;
314 out:
315 spin_unlock_irqrestore(&ehea_bcmc_regs.lock, flags);
318 static struct rtnl_link_stats64 *ehea_get_stats64(struct net_device *dev,
319 struct rtnl_link_stats64 *stats)
321 struct ehea_port *port = netdev_priv(dev);
322 u64 rx_packets = 0, tx_packets = 0, rx_bytes = 0, tx_bytes = 0;
323 int i;
325 for (i = 0; i < port->num_def_qps; i++) {
326 rx_packets += port->port_res[i].rx_packets;
327 rx_bytes += port->port_res[i].rx_bytes;
330 for (i = 0; i < port->num_def_qps; i++) {
331 tx_packets += port->port_res[i].tx_packets;
332 tx_bytes += port->port_res[i].tx_bytes;
335 stats->tx_packets = tx_packets;
336 stats->rx_bytes = rx_bytes;
337 stats->tx_bytes = tx_bytes;
338 stats->rx_packets = rx_packets;
340 stats->multicast = port->stats.multicast;
341 stats->rx_errors = port->stats.rx_errors;
342 return stats;
345 static void ehea_update_stats(struct work_struct *work)
347 struct ehea_port *port =
348 container_of(work, struct ehea_port, stats_work.work);
349 struct net_device *dev = port->netdev;
350 struct rtnl_link_stats64 *stats = &port->stats;
351 struct hcp_ehea_port_cb2 *cb2;
352 u64 hret;
354 cb2 = (void *)get_zeroed_page(GFP_KERNEL);
355 if (!cb2) {
356 netdev_err(dev, "No mem for cb2. Some interface statistics were not updated\n");
357 goto resched;
360 hret = ehea_h_query_ehea_port(port->adapter->handle,
361 port->logical_port_id,
362 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
363 if (hret != H_SUCCESS) {
364 netdev_err(dev, "query_ehea_port failed\n");
365 goto out_herr;
368 if (netif_msg_hw(port))
369 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
371 stats->multicast = cb2->rxmcp;
372 stats->rx_errors = cb2->rxuerr;
374 out_herr:
375 free_page((unsigned long)cb2);
376 resched:
377 schedule_delayed_work(&port->stats_work,
378 round_jiffies_relative(msecs_to_jiffies(1000)));
381 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
383 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
384 struct net_device *dev = pr->port->netdev;
385 int max_index_mask = pr->rq1_skba.len - 1;
386 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
387 int adder = 0;
388 int i;
390 pr->rq1_skba.os_skbs = 0;
392 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
393 if (nr_of_wqes > 0)
394 pr->rq1_skba.index = index;
395 pr->rq1_skba.os_skbs = fill_wqes;
396 return;
399 for (i = 0; i < fill_wqes; i++) {
400 if (!skb_arr_rq1[index]) {
401 skb_arr_rq1[index] = netdev_alloc_skb(dev,
402 EHEA_L_PKT_SIZE);
403 if (!skb_arr_rq1[index]) {
404 pr->rq1_skba.os_skbs = fill_wqes - i;
405 break;
408 index--;
409 index &= max_index_mask;
410 adder++;
413 if (adder == 0)
414 return;
416 /* Ring doorbell */
417 ehea_update_rq1a(pr->qp, adder);
420 static void ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
422 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
423 struct net_device *dev = pr->port->netdev;
424 int i;
426 if (nr_rq1a > pr->rq1_skba.len) {
427 netdev_err(dev, "NR_RQ1A bigger than skb array len\n");
428 return;
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 break;
436 /* Ring doorbell */
437 ehea_update_rq1a(pr->qp, i - 1);
440 static int ehea_refill_rq_def(struct ehea_port_res *pr,
441 struct ehea_q_skb_arr *q_skba, int rq_nr,
442 int num_wqes, int wqe_type, int packet_size)
444 struct net_device *dev = pr->port->netdev;
445 struct ehea_qp *qp = pr->qp;
446 struct sk_buff **skb_arr = q_skba->arr;
447 struct ehea_rwqe *rwqe;
448 int i, index, max_index_mask, fill_wqes;
449 int adder = 0;
450 int ret = 0;
452 fill_wqes = q_skba->os_skbs + num_wqes;
453 q_skba->os_skbs = 0;
455 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
456 q_skba->os_skbs = fill_wqes;
457 return ret;
460 index = q_skba->index;
461 max_index_mask = q_skba->len - 1;
462 for (i = 0; i < fill_wqes; i++) {
463 u64 tmp_addr;
464 struct sk_buff *skb;
466 skb = netdev_alloc_skb_ip_align(dev, packet_size);
467 if (!skb) {
468 q_skba->os_skbs = fill_wqes - i;
469 if (q_skba->os_skbs == q_skba->len - 2) {
470 netdev_info(pr->port->netdev,
471 "rq%i ran dry - no mem for skb\n",
472 rq_nr);
473 ret = -ENOMEM;
475 break;
478 skb_arr[index] = skb;
479 tmp_addr = ehea_map_vaddr(skb->data);
480 if (tmp_addr == -1) {
481 dev_kfree_skb(skb);
482 q_skba->os_skbs = fill_wqes - i;
483 ret = 0;
484 break;
487 rwqe = ehea_get_next_rwqe(qp, rq_nr);
488 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
489 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
490 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
491 rwqe->sg_list[0].vaddr = tmp_addr;
492 rwqe->sg_list[0].len = packet_size;
493 rwqe->data_segments = 1;
495 index++;
496 index &= max_index_mask;
497 adder++;
500 q_skba->index = index;
501 if (adder == 0)
502 goto out;
504 /* Ring doorbell */
505 iosync();
506 if (rq_nr == 2)
507 ehea_update_rq2a(pr->qp, adder);
508 else
509 ehea_update_rq3a(pr->qp, adder);
510 out:
511 return ret;
515 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
517 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
518 nr_of_wqes, EHEA_RWQE2_TYPE,
519 EHEA_RQ2_PKT_SIZE);
523 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
525 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
526 nr_of_wqes, EHEA_RWQE3_TYPE,
527 EHEA_MAX_PACKET_SIZE);
530 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
532 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
533 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
534 return 0;
535 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
536 (cqe->header_length == 0))
537 return 0;
538 return -EINVAL;
541 static inline void ehea_fill_skb(struct net_device *dev,
542 struct sk_buff *skb, struct ehea_cqe *cqe,
543 struct ehea_port_res *pr)
545 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
547 skb_put(skb, length);
548 skb->protocol = eth_type_trans(skb, dev);
550 /* The packet was not an IPV4 packet so a complemented checksum was
551 calculated. The value is found in the Internet Checksum field. */
552 if (cqe->status & EHEA_CQE_BLIND_CKSUM) {
553 skb->ip_summed = CHECKSUM_COMPLETE;
554 skb->csum = csum_unfold(~cqe->inet_checksum_value);
555 } else
556 skb->ip_summed = CHECKSUM_UNNECESSARY;
558 skb_record_rx_queue(skb, pr - &pr->port->port_res[0]);
561 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
562 int arr_len,
563 struct ehea_cqe *cqe)
565 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
566 struct sk_buff *skb;
567 void *pref;
568 int x;
570 x = skb_index + 1;
571 x &= (arr_len - 1);
573 pref = skb_array[x];
574 if (pref) {
575 prefetchw(pref);
576 prefetchw(pref + EHEA_CACHE_LINE);
578 pref = (skb_array[x]->data);
579 prefetch(pref);
580 prefetch(pref + EHEA_CACHE_LINE);
581 prefetch(pref + EHEA_CACHE_LINE * 2);
582 prefetch(pref + EHEA_CACHE_LINE * 3);
585 skb = skb_array[skb_index];
586 skb_array[skb_index] = NULL;
587 return skb;
590 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
591 int arr_len, int wqe_index)
593 struct sk_buff *skb;
594 void *pref;
595 int x;
597 x = wqe_index + 1;
598 x &= (arr_len - 1);
600 pref = skb_array[x];
601 if (pref) {
602 prefetchw(pref);
603 prefetchw(pref + EHEA_CACHE_LINE);
605 pref = (skb_array[x]->data);
606 prefetchw(pref);
607 prefetchw(pref + EHEA_CACHE_LINE);
610 skb = skb_array[wqe_index];
611 skb_array[wqe_index] = NULL;
612 return skb;
615 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
616 struct ehea_cqe *cqe, int *processed_rq2,
617 int *processed_rq3)
619 struct sk_buff *skb;
621 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
622 pr->p_stats.err_tcp_cksum++;
623 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
624 pr->p_stats.err_ip_cksum++;
625 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
626 pr->p_stats.err_frame_crc++;
628 if (rq == 2) {
629 *processed_rq2 += 1;
630 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
631 dev_kfree_skb(skb);
632 } else if (rq == 3) {
633 *processed_rq3 += 1;
634 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
635 dev_kfree_skb(skb);
638 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
639 if (netif_msg_rx_err(pr->port)) {
640 pr_err("Critical receive error for QP %d. Resetting port.\n",
641 pr->qp->init_attr.qp_nr);
642 ehea_dump(cqe, sizeof(*cqe), "CQE");
644 ehea_schedule_port_reset(pr->port);
645 return 1;
648 return 0;
651 static int ehea_proc_rwqes(struct net_device *dev,
652 struct ehea_port_res *pr,
653 int budget)
655 struct ehea_port *port = pr->port;
656 struct ehea_qp *qp = pr->qp;
657 struct ehea_cqe *cqe;
658 struct sk_buff *skb;
659 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
660 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
661 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
662 int skb_arr_rq1_len = pr->rq1_skba.len;
663 int skb_arr_rq2_len = pr->rq2_skba.len;
664 int skb_arr_rq3_len = pr->rq3_skba.len;
665 int processed, processed_rq1, processed_rq2, processed_rq3;
666 u64 processed_bytes = 0;
667 int wqe_index, last_wqe_index, rq, port_reset;
669 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
670 last_wqe_index = 0;
672 cqe = ehea_poll_rq1(qp, &wqe_index);
673 while ((processed < budget) && cqe) {
674 ehea_inc_rq1(qp);
675 processed_rq1++;
676 processed++;
677 if (netif_msg_rx_status(port))
678 ehea_dump(cqe, sizeof(*cqe), "CQE");
680 last_wqe_index = wqe_index;
681 rmb();
682 if (!ehea_check_cqe(cqe, &rq)) {
683 if (rq == 1) {
684 /* LL RQ1 */
685 skb = get_skb_by_index_ll(skb_arr_rq1,
686 skb_arr_rq1_len,
687 wqe_index);
688 if (unlikely(!skb)) {
689 netif_info(port, rx_err, dev,
690 "LL rq1: skb=NULL\n");
692 skb = netdev_alloc_skb(dev,
693 EHEA_L_PKT_SIZE);
694 if (!skb)
695 break;
697 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
698 cqe->num_bytes_transfered - 4);
699 ehea_fill_skb(dev, skb, cqe, pr);
700 } else if (rq == 2) {
701 /* RQ2 */
702 skb = get_skb_by_index(skb_arr_rq2,
703 skb_arr_rq2_len, cqe);
704 if (unlikely(!skb)) {
705 netif_err(port, rx_err, dev,
706 "rq2: skb=NULL\n");
707 break;
709 ehea_fill_skb(dev, skb, cqe, pr);
710 processed_rq2++;
711 } else {
712 /* RQ3 */
713 skb = get_skb_by_index(skb_arr_rq3,
714 skb_arr_rq3_len, cqe);
715 if (unlikely(!skb)) {
716 netif_err(port, rx_err, dev,
717 "rq3: skb=NULL\n");
718 break;
720 ehea_fill_skb(dev, skb, cqe, pr);
721 processed_rq3++;
724 processed_bytes += skb->len;
726 if (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
727 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
728 cqe->vlan_tag);
730 napi_gro_receive(&pr->napi, skb);
731 } else {
732 pr->p_stats.poll_receive_errors++;
733 port_reset = ehea_treat_poll_error(pr, rq, cqe,
734 &processed_rq2,
735 &processed_rq3);
736 if (port_reset)
737 break;
739 cqe = ehea_poll_rq1(qp, &wqe_index);
742 pr->rx_packets += processed;
743 pr->rx_bytes += processed_bytes;
745 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
746 ehea_refill_rq2(pr, processed_rq2);
747 ehea_refill_rq3(pr, processed_rq3);
749 return processed;
752 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
754 static void reset_sq_restart_flag(struct ehea_port *port)
756 int i;
758 for (i = 0; i < port->num_def_qps; i++) {
759 struct ehea_port_res *pr = &port->port_res[i];
760 pr->sq_restart_flag = 0;
762 wake_up(&port->restart_wq);
765 static void check_sqs(struct ehea_port *port)
767 struct ehea_swqe *swqe;
768 int swqe_index;
769 int i, k;
771 for (i = 0; i < port->num_def_qps; i++) {
772 struct ehea_port_res *pr = &port->port_res[i];
773 int ret;
774 k = 0;
775 swqe = ehea_get_swqe(pr->qp, &swqe_index);
776 memset(swqe, 0, SWQE_HEADER_SIZE);
777 atomic_dec(&pr->swqe_avail);
779 swqe->tx_control |= EHEA_SWQE_PURGE;
780 swqe->wr_id = SWQE_RESTART_CHECK;
781 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
782 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
783 swqe->immediate_data_length = 80;
785 ehea_post_swqe(pr->qp, swqe);
787 ret = wait_event_timeout(port->restart_wq,
788 pr->sq_restart_flag == 0,
789 msecs_to_jiffies(100));
791 if (!ret) {
792 pr_err("HW/SW queues out of sync\n");
793 ehea_schedule_port_reset(pr->port);
794 return;
800 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
802 struct sk_buff *skb;
803 struct ehea_cq *send_cq = pr->send_cq;
804 struct ehea_cqe *cqe;
805 int quota = my_quota;
806 int cqe_counter = 0;
807 int swqe_av = 0;
808 int index;
809 struct netdev_queue *txq = netdev_get_tx_queue(pr->port->netdev,
810 pr - &pr->port->port_res[0]);
812 cqe = ehea_poll_cq(send_cq);
813 while (cqe && (quota > 0)) {
814 ehea_inc_cq(send_cq);
816 cqe_counter++;
817 rmb();
819 if (cqe->wr_id == SWQE_RESTART_CHECK) {
820 pr->sq_restart_flag = 1;
821 swqe_av++;
822 break;
825 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
826 pr_err("Bad send completion status=0x%04X\n",
827 cqe->status);
829 if (netif_msg_tx_err(pr->port))
830 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
832 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
833 pr_err("Resetting port\n");
834 ehea_schedule_port_reset(pr->port);
835 break;
839 if (netif_msg_tx_done(pr->port))
840 ehea_dump(cqe, sizeof(*cqe), "CQE");
842 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
843 == EHEA_SWQE2_TYPE)) {
845 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
846 skb = pr->sq_skba.arr[index];
847 dev_kfree_skb(skb);
848 pr->sq_skba.arr[index] = NULL;
851 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
852 quota--;
854 cqe = ehea_poll_cq(send_cq);
857 ehea_update_feca(send_cq, cqe_counter);
858 atomic_add(swqe_av, &pr->swqe_avail);
860 if (unlikely(netif_tx_queue_stopped(txq) &&
861 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))) {
862 __netif_tx_lock(txq, smp_processor_id());
863 if (netif_tx_queue_stopped(txq) &&
864 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))
865 netif_tx_wake_queue(txq);
866 __netif_tx_unlock(txq);
869 wake_up(&pr->port->swqe_avail_wq);
871 return cqe;
874 #define EHEA_POLL_MAX_CQES 65535
876 static int ehea_poll(struct napi_struct *napi, int budget)
878 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
879 napi);
880 struct net_device *dev = pr->port->netdev;
881 struct ehea_cqe *cqe;
882 struct ehea_cqe *cqe_skb = NULL;
883 int wqe_index;
884 int rx = 0;
886 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
887 rx += ehea_proc_rwqes(dev, pr, budget - rx);
889 while (rx != budget) {
890 napi_complete(napi);
891 ehea_reset_cq_ep(pr->recv_cq);
892 ehea_reset_cq_ep(pr->send_cq);
893 ehea_reset_cq_n1(pr->recv_cq);
894 ehea_reset_cq_n1(pr->send_cq);
895 rmb();
896 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
897 cqe_skb = ehea_poll_cq(pr->send_cq);
899 if (!cqe && !cqe_skb)
900 return rx;
902 if (!napi_reschedule(napi))
903 return rx;
905 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
906 rx += ehea_proc_rwqes(dev, pr, budget - rx);
909 return rx;
912 #ifdef CONFIG_NET_POLL_CONTROLLER
913 static void ehea_netpoll(struct net_device *dev)
915 struct ehea_port *port = netdev_priv(dev);
916 int i;
918 for (i = 0; i < port->num_def_qps; i++)
919 napi_schedule(&port->port_res[i].napi);
921 #endif
923 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
925 struct ehea_port_res *pr = param;
927 napi_schedule(&pr->napi);
929 return IRQ_HANDLED;
932 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
934 struct ehea_port *port = param;
935 struct ehea_eqe *eqe;
936 struct ehea_qp *qp;
937 u32 qp_token;
938 u64 resource_type, aer, aerr;
939 int reset_port = 0;
941 eqe = ehea_poll_eq(port->qp_eq);
943 while (eqe) {
944 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
945 pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
946 eqe->entry, qp_token);
948 qp = port->port_res[qp_token].qp;
950 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
951 &aer, &aerr);
953 if (resource_type == EHEA_AER_RESTYPE_QP) {
954 if ((aer & EHEA_AER_RESET_MASK) ||
955 (aerr & EHEA_AERR_RESET_MASK))
956 reset_port = 1;
957 } else
958 reset_port = 1; /* Reset in case of CQ or EQ error */
960 eqe = ehea_poll_eq(port->qp_eq);
963 if (reset_port) {
964 pr_err("Resetting port\n");
965 ehea_schedule_port_reset(port);
968 return IRQ_HANDLED;
971 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
972 int logical_port)
974 int i;
976 for (i = 0; i < EHEA_MAX_PORTS; i++)
977 if (adapter->port[i])
978 if (adapter->port[i]->logical_port_id == logical_port)
979 return adapter->port[i];
980 return NULL;
983 int ehea_sense_port_attr(struct ehea_port *port)
985 int ret;
986 u64 hret;
987 struct hcp_ehea_port_cb0 *cb0;
989 /* may be called via ehea_neq_tasklet() */
990 cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
991 if (!cb0) {
992 pr_err("no mem for cb0\n");
993 ret = -ENOMEM;
994 goto out;
997 hret = ehea_h_query_ehea_port(port->adapter->handle,
998 port->logical_port_id, H_PORT_CB0,
999 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
1000 cb0);
1001 if (hret != H_SUCCESS) {
1002 ret = -EIO;
1003 goto out_free;
1006 /* MAC address */
1007 port->mac_addr = cb0->port_mac_addr << 16;
1009 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1010 ret = -EADDRNOTAVAIL;
1011 goto out_free;
1014 /* Port speed */
1015 switch (cb0->port_speed) {
1016 case H_SPEED_10M_H:
1017 port->port_speed = EHEA_SPEED_10M;
1018 port->full_duplex = 0;
1019 break;
1020 case H_SPEED_10M_F:
1021 port->port_speed = EHEA_SPEED_10M;
1022 port->full_duplex = 1;
1023 break;
1024 case H_SPEED_100M_H:
1025 port->port_speed = EHEA_SPEED_100M;
1026 port->full_duplex = 0;
1027 break;
1028 case H_SPEED_100M_F:
1029 port->port_speed = EHEA_SPEED_100M;
1030 port->full_duplex = 1;
1031 break;
1032 case H_SPEED_1G_F:
1033 port->port_speed = EHEA_SPEED_1G;
1034 port->full_duplex = 1;
1035 break;
1036 case H_SPEED_10G_F:
1037 port->port_speed = EHEA_SPEED_10G;
1038 port->full_duplex = 1;
1039 break;
1040 default:
1041 port->port_speed = 0;
1042 port->full_duplex = 0;
1043 break;
1046 port->autoneg = 1;
1047 port->num_mcs = cb0->num_default_qps;
1049 /* Number of default QPs */
1050 if (use_mcs)
1051 port->num_def_qps = cb0->num_default_qps;
1052 else
1053 port->num_def_qps = 1;
1055 if (!port->num_def_qps) {
1056 ret = -EINVAL;
1057 goto out_free;
1060 ret = 0;
1061 out_free:
1062 if (ret || netif_msg_probe(port))
1063 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1064 free_page((unsigned long)cb0);
1065 out:
1066 return ret;
1069 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1071 struct hcp_ehea_port_cb4 *cb4;
1072 u64 hret;
1073 int ret = 0;
1075 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1076 if (!cb4) {
1077 pr_err("no mem for cb4\n");
1078 ret = -ENOMEM;
1079 goto out;
1082 cb4->port_speed = port_speed;
1084 netif_carrier_off(port->netdev);
1086 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1087 port->logical_port_id,
1088 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1089 if (hret == H_SUCCESS) {
1090 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1092 hret = ehea_h_query_ehea_port(port->adapter->handle,
1093 port->logical_port_id,
1094 H_PORT_CB4, H_PORT_CB4_SPEED,
1095 cb4);
1096 if (hret == H_SUCCESS) {
1097 switch (cb4->port_speed) {
1098 case H_SPEED_10M_H:
1099 port->port_speed = EHEA_SPEED_10M;
1100 port->full_duplex = 0;
1101 break;
1102 case H_SPEED_10M_F:
1103 port->port_speed = EHEA_SPEED_10M;
1104 port->full_duplex = 1;
1105 break;
1106 case H_SPEED_100M_H:
1107 port->port_speed = EHEA_SPEED_100M;
1108 port->full_duplex = 0;
1109 break;
1110 case H_SPEED_100M_F:
1111 port->port_speed = EHEA_SPEED_100M;
1112 port->full_duplex = 1;
1113 break;
1114 case H_SPEED_1G_F:
1115 port->port_speed = EHEA_SPEED_1G;
1116 port->full_duplex = 1;
1117 break;
1118 case H_SPEED_10G_F:
1119 port->port_speed = EHEA_SPEED_10G;
1120 port->full_duplex = 1;
1121 break;
1122 default:
1123 port->port_speed = 0;
1124 port->full_duplex = 0;
1125 break;
1127 } else {
1128 pr_err("Failed sensing port speed\n");
1129 ret = -EIO;
1131 } else {
1132 if (hret == H_AUTHORITY) {
1133 pr_info("Hypervisor denied setting port speed\n");
1134 ret = -EPERM;
1135 } else {
1136 ret = -EIO;
1137 pr_err("Failed setting port speed\n");
1140 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1141 netif_carrier_on(port->netdev);
1143 free_page((unsigned long)cb4);
1144 out:
1145 return ret;
1148 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1150 int ret;
1151 u8 ec;
1152 u8 portnum;
1153 struct ehea_port *port;
1154 struct net_device *dev;
1156 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1157 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1158 port = ehea_get_port(adapter, portnum);
1159 dev = port->netdev;
1161 switch (ec) {
1162 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1164 if (!port) {
1165 netdev_err(dev, "unknown portnum %x\n", portnum);
1166 break;
1169 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1170 if (!netif_carrier_ok(dev)) {
1171 ret = ehea_sense_port_attr(port);
1172 if (ret) {
1173 netdev_err(dev, "failed resensing port attributes\n");
1174 break;
1177 netif_info(port, link, dev,
1178 "Logical port up: %dMbps %s Duplex\n",
1179 port->port_speed,
1180 port->full_duplex == 1 ?
1181 "Full" : "Half");
1183 netif_carrier_on(dev);
1184 netif_wake_queue(dev);
1186 } else
1187 if (netif_carrier_ok(dev)) {
1188 netif_info(port, link, dev,
1189 "Logical port down\n");
1190 netif_carrier_off(dev);
1191 netif_tx_disable(dev);
1194 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1195 port->phy_link = EHEA_PHY_LINK_UP;
1196 netif_info(port, link, dev,
1197 "Physical port up\n");
1198 if (prop_carrier_state)
1199 netif_carrier_on(dev);
1200 } else {
1201 port->phy_link = EHEA_PHY_LINK_DOWN;
1202 netif_info(port, link, dev,
1203 "Physical port down\n");
1204 if (prop_carrier_state)
1205 netif_carrier_off(dev);
1208 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1209 netdev_info(dev,
1210 "External switch port is primary port\n");
1211 else
1212 netdev_info(dev,
1213 "External switch port is backup port\n");
1215 break;
1216 case EHEA_EC_ADAPTER_MALFUNC:
1217 netdev_err(dev, "Adapter malfunction\n");
1218 break;
1219 case EHEA_EC_PORT_MALFUNC:
1220 netdev_info(dev, "Port malfunction\n");
1221 netif_carrier_off(dev);
1222 netif_tx_disable(dev);
1223 break;
1224 default:
1225 netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1226 break;
1230 static void ehea_neq_tasklet(unsigned long data)
1232 struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1233 struct ehea_eqe *eqe;
1234 u64 event_mask;
1236 eqe = ehea_poll_eq(adapter->neq);
1237 pr_debug("eqe=%p\n", eqe);
1239 while (eqe) {
1240 pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1241 ehea_parse_eqe(adapter, eqe->entry);
1242 eqe = ehea_poll_eq(adapter->neq);
1243 pr_debug("next eqe=%p\n", eqe);
1246 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1247 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1248 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1250 ehea_h_reset_events(adapter->handle,
1251 adapter->neq->fw_handle, event_mask);
1254 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1256 struct ehea_adapter *adapter = param;
1257 tasklet_hi_schedule(&adapter->neq_tasklet);
1258 return IRQ_HANDLED;
1262 static int ehea_fill_port_res(struct ehea_port_res *pr)
1264 int ret;
1265 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1267 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1269 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1271 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1273 return ret;
1276 static int ehea_reg_interrupts(struct net_device *dev)
1278 struct ehea_port *port = netdev_priv(dev);
1279 struct ehea_port_res *pr;
1280 int i, ret;
1283 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1284 dev->name);
1286 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1287 ehea_qp_aff_irq_handler,
1288 IRQF_DISABLED, port->int_aff_name, port);
1289 if (ret) {
1290 netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1291 port->qp_eq->attr.ist1);
1292 goto out_free_qpeq;
1295 netif_info(port, ifup, dev,
1296 "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1297 port->qp_eq->attr.ist1);
1300 for (i = 0; i < port->num_def_qps; i++) {
1301 pr = &port->port_res[i];
1302 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1303 "%s-queue%d", dev->name, i);
1304 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1305 ehea_recv_irq_handler,
1306 IRQF_DISABLED, pr->int_send_name,
1307 pr);
1308 if (ret) {
1309 netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1310 i, pr->eq->attr.ist1);
1311 goto out_free_req;
1313 netif_info(port, ifup, dev,
1314 "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1315 pr->eq->attr.ist1, i);
1317 out:
1318 return ret;
1321 out_free_req:
1322 while (--i >= 0) {
1323 u32 ist = port->port_res[i].eq->attr.ist1;
1324 ibmebus_free_irq(ist, &port->port_res[i]);
1327 out_free_qpeq:
1328 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1329 i = port->num_def_qps;
1331 goto out;
1335 static void ehea_free_interrupts(struct net_device *dev)
1337 struct ehea_port *port = netdev_priv(dev);
1338 struct ehea_port_res *pr;
1339 int i;
1341 /* send */
1343 for (i = 0; i < port->num_def_qps; i++) {
1344 pr = &port->port_res[i];
1345 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1346 netif_info(port, intr, dev,
1347 "free send irq for res %d with handle 0x%X\n",
1348 i, pr->eq->attr.ist1);
1351 /* associated events */
1352 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1353 netif_info(port, intr, dev,
1354 "associated event interrupt for handle 0x%X freed\n",
1355 port->qp_eq->attr.ist1);
1358 static int ehea_configure_port(struct ehea_port *port)
1360 int ret, i;
1361 u64 hret, mask;
1362 struct hcp_ehea_port_cb0 *cb0;
1364 ret = -ENOMEM;
1365 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1366 if (!cb0)
1367 goto out;
1369 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1370 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1371 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1372 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1373 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1374 PXLY_RC_VLAN_FILTER)
1375 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1377 for (i = 0; i < port->num_mcs; i++)
1378 if (use_mcs)
1379 cb0->default_qpn_arr[i] =
1380 port->port_res[i].qp->init_attr.qp_nr;
1381 else
1382 cb0->default_qpn_arr[i] =
1383 port->port_res[0].qp->init_attr.qp_nr;
1385 if (netif_msg_ifup(port))
1386 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1388 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1389 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1391 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1392 port->logical_port_id,
1393 H_PORT_CB0, mask, cb0);
1394 ret = -EIO;
1395 if (hret != H_SUCCESS)
1396 goto out_free;
1398 ret = 0;
1400 out_free:
1401 free_page((unsigned long)cb0);
1402 out:
1403 return ret;
1406 static int ehea_gen_smrs(struct ehea_port_res *pr)
1408 int ret;
1409 struct ehea_adapter *adapter = pr->port->adapter;
1411 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1412 if (ret)
1413 goto out;
1415 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1416 if (ret)
1417 goto out_free;
1419 return 0;
1421 out_free:
1422 ehea_rem_mr(&pr->send_mr);
1423 out:
1424 pr_err("Generating SMRS failed\n");
1425 return -EIO;
1428 static int ehea_rem_smrs(struct ehea_port_res *pr)
1430 if ((ehea_rem_mr(&pr->send_mr)) ||
1431 (ehea_rem_mr(&pr->recv_mr)))
1432 return -EIO;
1433 else
1434 return 0;
1437 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1439 int arr_size = sizeof(void *) * max_q_entries;
1441 q_skba->arr = vzalloc(arr_size);
1442 if (!q_skba->arr)
1443 return -ENOMEM;
1445 q_skba->len = max_q_entries;
1446 q_skba->index = 0;
1447 q_skba->os_skbs = 0;
1449 return 0;
1452 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1453 struct port_res_cfg *pr_cfg, int queue_token)
1455 struct ehea_adapter *adapter = port->adapter;
1456 enum ehea_eq_type eq_type = EHEA_EQ;
1457 struct ehea_qp_init_attr *init_attr = NULL;
1458 int ret = -EIO;
1459 u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1461 tx_bytes = pr->tx_bytes;
1462 tx_packets = pr->tx_packets;
1463 rx_bytes = pr->rx_bytes;
1464 rx_packets = pr->rx_packets;
1466 memset(pr, 0, sizeof(struct ehea_port_res));
1468 pr->tx_bytes = rx_bytes;
1469 pr->tx_packets = tx_packets;
1470 pr->rx_bytes = rx_bytes;
1471 pr->rx_packets = rx_packets;
1473 pr->port = port;
1475 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1476 if (!pr->eq) {
1477 pr_err("create_eq failed (eq)\n");
1478 goto out_free;
1481 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1482 pr->eq->fw_handle,
1483 port->logical_port_id);
1484 if (!pr->recv_cq) {
1485 pr_err("create_cq failed (cq_recv)\n");
1486 goto out_free;
1489 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1490 pr->eq->fw_handle,
1491 port->logical_port_id);
1492 if (!pr->send_cq) {
1493 pr_err("create_cq failed (cq_send)\n");
1494 goto out_free;
1497 if (netif_msg_ifup(port))
1498 pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1499 pr->send_cq->attr.act_nr_of_cqes,
1500 pr->recv_cq->attr.act_nr_of_cqes);
1502 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1503 if (!init_attr) {
1504 ret = -ENOMEM;
1505 pr_err("no mem for ehea_qp_init_attr\n");
1506 goto out_free;
1509 init_attr->low_lat_rq1 = 1;
1510 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1511 init_attr->rq_count = 3;
1512 init_attr->qp_token = queue_token;
1513 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1514 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1515 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1516 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1517 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1518 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1519 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1520 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1521 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1522 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1523 init_attr->port_nr = port->logical_port_id;
1524 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1525 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1526 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1528 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1529 if (!pr->qp) {
1530 pr_err("create_qp failed\n");
1531 ret = -EIO;
1532 goto out_free;
1535 if (netif_msg_ifup(port))
1536 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",
1537 init_attr->qp_nr,
1538 init_attr->act_nr_send_wqes,
1539 init_attr->act_nr_rwqes_rq1,
1540 init_attr->act_nr_rwqes_rq2,
1541 init_attr->act_nr_rwqes_rq3);
1543 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1545 ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1546 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1547 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1548 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1549 if (ret)
1550 goto out_free;
1552 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1553 if (ehea_gen_smrs(pr) != 0) {
1554 ret = -EIO;
1555 goto out_free;
1558 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1560 kfree(init_attr);
1562 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1564 ret = 0;
1565 goto out;
1567 out_free:
1568 kfree(init_attr);
1569 vfree(pr->sq_skba.arr);
1570 vfree(pr->rq1_skba.arr);
1571 vfree(pr->rq2_skba.arr);
1572 vfree(pr->rq3_skba.arr);
1573 ehea_destroy_qp(pr->qp);
1574 ehea_destroy_cq(pr->send_cq);
1575 ehea_destroy_cq(pr->recv_cq);
1576 ehea_destroy_eq(pr->eq);
1577 out:
1578 return ret;
1581 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1583 int ret, i;
1585 if (pr->qp)
1586 netif_napi_del(&pr->napi);
1588 ret = ehea_destroy_qp(pr->qp);
1590 if (!ret) {
1591 ehea_destroy_cq(pr->send_cq);
1592 ehea_destroy_cq(pr->recv_cq);
1593 ehea_destroy_eq(pr->eq);
1595 for (i = 0; i < pr->rq1_skba.len; i++)
1596 if (pr->rq1_skba.arr[i])
1597 dev_kfree_skb(pr->rq1_skba.arr[i]);
1599 for (i = 0; i < pr->rq2_skba.len; i++)
1600 if (pr->rq2_skba.arr[i])
1601 dev_kfree_skb(pr->rq2_skba.arr[i]);
1603 for (i = 0; i < pr->rq3_skba.len; i++)
1604 if (pr->rq3_skba.arr[i])
1605 dev_kfree_skb(pr->rq3_skba.arr[i]);
1607 for (i = 0; i < pr->sq_skba.len; i++)
1608 if (pr->sq_skba.arr[i])
1609 dev_kfree_skb(pr->sq_skba.arr[i]);
1611 vfree(pr->rq1_skba.arr);
1612 vfree(pr->rq2_skba.arr);
1613 vfree(pr->rq3_skba.arr);
1614 vfree(pr->sq_skba.arr);
1615 ret = ehea_rem_smrs(pr);
1617 return ret;
1620 static void write_swqe2_immediate(struct sk_buff *skb, struct ehea_swqe *swqe,
1621 u32 lkey)
1623 int skb_data_size = skb_headlen(skb);
1624 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1625 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1626 unsigned int immediate_len = SWQE2_MAX_IMM;
1628 swqe->descriptors = 0;
1630 if (skb_is_gso(skb)) {
1631 swqe->tx_control |= EHEA_SWQE_TSO;
1632 swqe->mss = skb_shinfo(skb)->gso_size;
1634 * For TSO packets we only copy the headers into the
1635 * immediate area.
1637 immediate_len = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1640 if (skb_is_gso(skb) || skb_data_size >= SWQE2_MAX_IMM) {
1641 skb_copy_from_linear_data(skb, imm_data, immediate_len);
1642 swqe->immediate_data_length = immediate_len;
1644 if (skb_data_size > immediate_len) {
1645 sg1entry->l_key = lkey;
1646 sg1entry->len = skb_data_size - immediate_len;
1647 sg1entry->vaddr =
1648 ehea_map_vaddr(skb->data + immediate_len);
1649 swqe->descriptors++;
1651 } else {
1652 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1653 swqe->immediate_data_length = skb_data_size;
1657 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1658 struct ehea_swqe *swqe, u32 lkey)
1660 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1661 skb_frag_t *frag;
1662 int nfrags, sg1entry_contains_frag_data, i;
1664 nfrags = skb_shinfo(skb)->nr_frags;
1665 sg1entry = &swqe->u.immdata_desc.sg_entry;
1666 sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1667 sg1entry_contains_frag_data = 0;
1669 write_swqe2_immediate(skb, swqe, lkey);
1671 /* write descriptors */
1672 if (nfrags > 0) {
1673 if (swqe->descriptors == 0) {
1674 /* sg1entry not yet used */
1675 frag = &skb_shinfo(skb)->frags[0];
1677 /* copy sg1entry data */
1678 sg1entry->l_key = lkey;
1679 sg1entry->len = skb_frag_size(frag);
1680 sg1entry->vaddr =
1681 ehea_map_vaddr(skb_frag_address(frag));
1682 swqe->descriptors++;
1683 sg1entry_contains_frag_data = 1;
1686 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1688 frag = &skb_shinfo(skb)->frags[i];
1689 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1691 sgentry->l_key = lkey;
1692 sgentry->len = skb_frag_size(frag);
1693 sgentry->vaddr = ehea_map_vaddr(skb_frag_address(frag));
1694 swqe->descriptors++;
1699 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1701 int ret = 0;
1702 u64 hret;
1703 u8 reg_type;
1705 /* De/Register untagged packets */
1706 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1707 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1708 port->logical_port_id,
1709 reg_type, port->mac_addr, 0, hcallid);
1710 if (hret != H_SUCCESS) {
1711 pr_err("%sregistering bc address failed (tagged)\n",
1712 hcallid == H_REG_BCMC ? "" : "de");
1713 ret = -EIO;
1714 goto out_herr;
1717 /* De/Register VLAN packets */
1718 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1719 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1720 port->logical_port_id,
1721 reg_type, port->mac_addr, 0, hcallid);
1722 if (hret != H_SUCCESS) {
1723 pr_err("%sregistering bc address failed (vlan)\n",
1724 hcallid == H_REG_BCMC ? "" : "de");
1725 ret = -EIO;
1727 out_herr:
1728 return ret;
1731 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1733 struct ehea_port *port = netdev_priv(dev);
1734 struct sockaddr *mac_addr = sa;
1735 struct hcp_ehea_port_cb0 *cb0;
1736 int ret;
1737 u64 hret;
1739 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1740 ret = -EADDRNOTAVAIL;
1741 goto out;
1744 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1745 if (!cb0) {
1746 pr_err("no mem for cb0\n");
1747 ret = -ENOMEM;
1748 goto out;
1751 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1753 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1755 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1756 port->logical_port_id, H_PORT_CB0,
1757 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1758 if (hret != H_SUCCESS) {
1759 ret = -EIO;
1760 goto out_free;
1763 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1765 /* Deregister old MAC in pHYP */
1766 if (port->state == EHEA_PORT_UP) {
1767 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1768 if (ret)
1769 goto out_upregs;
1772 port->mac_addr = cb0->port_mac_addr << 16;
1774 /* Register new MAC in pHYP */
1775 if (port->state == EHEA_PORT_UP) {
1776 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1777 if (ret)
1778 goto out_upregs;
1781 ret = 0;
1783 out_upregs:
1784 ehea_update_bcmc_registrations();
1785 out_free:
1786 free_page((unsigned long)cb0);
1787 out:
1788 return ret;
1791 static void ehea_promiscuous_error(u64 hret, int enable)
1793 if (hret == H_AUTHORITY)
1794 pr_info("Hypervisor denied %sabling promiscuous mode\n",
1795 enable == 1 ? "en" : "dis");
1796 else
1797 pr_err("failed %sabling promiscuous mode\n",
1798 enable == 1 ? "en" : "dis");
1801 static void ehea_promiscuous(struct net_device *dev, int enable)
1803 struct ehea_port *port = netdev_priv(dev);
1804 struct hcp_ehea_port_cb7 *cb7;
1805 u64 hret;
1807 if (enable == port->promisc)
1808 return;
1810 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1811 if (!cb7) {
1812 pr_err("no mem for cb7\n");
1813 goto out;
1816 /* Modify Pxs_DUCQPN in CB7 */
1817 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1819 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1820 port->logical_port_id,
1821 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1822 if (hret) {
1823 ehea_promiscuous_error(hret, enable);
1824 goto out;
1827 port->promisc = enable;
1828 out:
1829 free_page((unsigned long)cb7);
1832 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1833 u32 hcallid)
1835 u64 hret;
1836 u8 reg_type;
1838 reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_UNTAGGED;
1839 if (mc_mac_addr == 0)
1840 reg_type |= EHEA_BCMC_SCOPE_ALL;
1842 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1843 port->logical_port_id,
1844 reg_type, mc_mac_addr, 0, hcallid);
1845 if (hret)
1846 goto out;
1848 reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_VLANID_ALL;
1849 if (mc_mac_addr == 0)
1850 reg_type |= EHEA_BCMC_SCOPE_ALL;
1852 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1853 port->logical_port_id,
1854 reg_type, mc_mac_addr, 0, hcallid);
1855 out:
1856 return hret;
1859 static int ehea_drop_multicast_list(struct net_device *dev)
1861 struct ehea_port *port = netdev_priv(dev);
1862 struct ehea_mc_list *mc_entry = port->mc_list;
1863 struct list_head *pos;
1864 struct list_head *temp;
1865 int ret = 0;
1866 u64 hret;
1868 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1869 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1871 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1872 H_DEREG_BCMC);
1873 if (hret) {
1874 pr_err("failed deregistering mcast MAC\n");
1875 ret = -EIO;
1878 list_del(pos);
1879 kfree(mc_entry);
1881 return ret;
1884 static void ehea_allmulti(struct net_device *dev, int enable)
1886 struct ehea_port *port = netdev_priv(dev);
1887 u64 hret;
1889 if (!port->allmulti) {
1890 if (enable) {
1891 /* Enable ALLMULTI */
1892 ehea_drop_multicast_list(dev);
1893 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1894 if (!hret)
1895 port->allmulti = 1;
1896 else
1897 netdev_err(dev,
1898 "failed enabling IFF_ALLMULTI\n");
1900 } else {
1901 if (!enable) {
1902 /* Disable ALLMULTI */
1903 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1904 if (!hret)
1905 port->allmulti = 0;
1906 else
1907 netdev_err(dev,
1908 "failed disabling IFF_ALLMULTI\n");
1913 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
1915 struct ehea_mc_list *ehea_mcl_entry;
1916 u64 hret;
1918 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1919 if (!ehea_mcl_entry)
1920 return;
1922 INIT_LIST_HEAD(&ehea_mcl_entry->list);
1924 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1926 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1927 H_REG_BCMC);
1928 if (!hret)
1929 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1930 else {
1931 pr_err("failed registering mcast MAC\n");
1932 kfree(ehea_mcl_entry);
1936 static void ehea_set_multicast_list(struct net_device *dev)
1938 struct ehea_port *port = netdev_priv(dev);
1939 struct netdev_hw_addr *ha;
1940 int ret;
1942 ehea_promiscuous(dev, !!(dev->flags & IFF_PROMISC));
1944 if (dev->flags & IFF_ALLMULTI) {
1945 ehea_allmulti(dev, 1);
1946 goto out;
1948 ehea_allmulti(dev, 0);
1950 if (!netdev_mc_empty(dev)) {
1951 ret = ehea_drop_multicast_list(dev);
1952 if (ret) {
1953 /* Dropping the current multicast list failed.
1954 * Enabling ALL_MULTI is the best we can do.
1956 ehea_allmulti(dev, 1);
1959 if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
1960 pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
1961 port->adapter->max_mc_mac);
1962 goto out;
1965 netdev_for_each_mc_addr(ha, dev)
1966 ehea_add_multicast_entry(port, ha->addr);
1969 out:
1970 ehea_update_bcmc_registrations();
1973 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1975 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1976 return -EINVAL;
1977 dev->mtu = new_mtu;
1978 return 0;
1981 static void xmit_common(struct sk_buff *skb, struct ehea_swqe *swqe)
1983 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT | EHEA_SWQE_CRC;
1985 if (skb->protocol != htons(ETH_P_IP))
1986 return;
1988 if (skb->ip_summed == CHECKSUM_PARTIAL)
1989 swqe->tx_control |= EHEA_SWQE_IP_CHECKSUM;
1991 swqe->ip_start = skb_network_offset(skb);
1992 swqe->ip_end = swqe->ip_start + ip_hdrlen(skb) - 1;
1994 switch (ip_hdr(skb)->protocol) {
1995 case IPPROTO_UDP:
1996 if (skb->ip_summed == CHECKSUM_PARTIAL)
1997 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
1999 swqe->tcp_offset = swqe->ip_end + 1 +
2000 offsetof(struct udphdr, check);
2001 break;
2003 case IPPROTO_TCP:
2004 if (skb->ip_summed == CHECKSUM_PARTIAL)
2005 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
2007 swqe->tcp_offset = swqe->ip_end + 1 +
2008 offsetof(struct tcphdr, check);
2009 break;
2013 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2014 struct ehea_swqe *swqe, u32 lkey)
2016 swqe->tx_control |= EHEA_SWQE_DESCRIPTORS_PRESENT;
2018 xmit_common(skb, swqe);
2020 write_swqe2_data(skb, dev, swqe, lkey);
2023 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2024 struct ehea_swqe *swqe)
2026 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2028 xmit_common(skb, swqe);
2030 if (!skb->data_len)
2031 skb_copy_from_linear_data(skb, imm_data, skb->len);
2032 else
2033 skb_copy_bits(skb, 0, imm_data, skb->len);
2035 swqe->immediate_data_length = skb->len;
2036 dev_kfree_skb(skb);
2039 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2041 struct ehea_port *port = netdev_priv(dev);
2042 struct ehea_swqe *swqe;
2043 u32 lkey;
2044 int swqe_index;
2045 struct ehea_port_res *pr;
2046 struct netdev_queue *txq;
2048 pr = &port->port_res[skb_get_queue_mapping(skb)];
2049 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2051 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2052 memset(swqe, 0, SWQE_HEADER_SIZE);
2053 atomic_dec(&pr->swqe_avail);
2055 if (vlan_tx_tag_present(skb)) {
2056 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2057 swqe->vlan_tag = vlan_tx_tag_get(skb);
2060 pr->tx_packets++;
2061 pr->tx_bytes += skb->len;
2063 if (skb->len <= SWQE3_MAX_IMM) {
2064 u32 sig_iv = port->sig_comp_iv;
2065 u32 swqe_num = pr->swqe_id_counter;
2066 ehea_xmit3(skb, dev, swqe);
2067 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2068 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2069 if (pr->swqe_ll_count >= (sig_iv - 1)) {
2070 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2071 sig_iv);
2072 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2073 pr->swqe_ll_count = 0;
2074 } else
2075 pr->swqe_ll_count += 1;
2076 } else {
2077 swqe->wr_id =
2078 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2079 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2080 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2081 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2082 pr->sq_skba.arr[pr->sq_skba.index] = skb;
2084 pr->sq_skba.index++;
2085 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2087 lkey = pr->send_mr.lkey;
2088 ehea_xmit2(skb, dev, swqe, lkey);
2089 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2091 pr->swqe_id_counter += 1;
2093 netif_info(port, tx_queued, dev,
2094 "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2095 if (netif_msg_tx_queued(port))
2096 ehea_dump(swqe, 512, "swqe");
2098 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2099 netif_tx_stop_queue(txq);
2100 swqe->tx_control |= EHEA_SWQE_PURGE;
2103 ehea_post_swqe(pr->qp, swqe);
2105 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2106 pr->p_stats.queue_stopped++;
2107 netif_tx_stop_queue(txq);
2110 return NETDEV_TX_OK;
2113 static int ehea_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
2115 struct ehea_port *port = netdev_priv(dev);
2116 struct ehea_adapter *adapter = port->adapter;
2117 struct hcp_ehea_port_cb1 *cb1;
2118 int index;
2119 u64 hret;
2120 int err = 0;
2122 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2123 if (!cb1) {
2124 pr_err("no mem for cb1\n");
2125 err = -ENOMEM;
2126 goto out;
2129 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2130 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2131 if (hret != H_SUCCESS) {
2132 pr_err("query_ehea_port failed\n");
2133 err = -EINVAL;
2134 goto out;
2137 index = (vid / 64);
2138 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2140 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2141 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2142 if (hret != H_SUCCESS) {
2143 pr_err("modify_ehea_port failed\n");
2144 err = -EINVAL;
2146 out:
2147 free_page((unsigned long)cb1);
2148 return err;
2151 static int ehea_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
2153 struct ehea_port *port = netdev_priv(dev);
2154 struct ehea_adapter *adapter = port->adapter;
2155 struct hcp_ehea_port_cb1 *cb1;
2156 int index;
2157 u64 hret;
2158 int err = 0;
2160 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2161 if (!cb1) {
2162 pr_err("no mem for cb1\n");
2163 err = -ENOMEM;
2164 goto out;
2167 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2168 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2169 if (hret != H_SUCCESS) {
2170 pr_err("query_ehea_port failed\n");
2171 err = -EINVAL;
2172 goto out;
2175 index = (vid / 64);
2176 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2178 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2179 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2180 if (hret != H_SUCCESS) {
2181 pr_err("modify_ehea_port failed\n");
2182 err = -EINVAL;
2184 out:
2185 free_page((unsigned long)cb1);
2186 return err;
2189 static int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2191 int ret = -EIO;
2192 u64 hret;
2193 u16 dummy16 = 0;
2194 u64 dummy64 = 0;
2195 struct hcp_modify_qp_cb0 *cb0;
2197 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2198 if (!cb0) {
2199 ret = -ENOMEM;
2200 goto out;
2203 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2204 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2205 if (hret != H_SUCCESS) {
2206 pr_err("query_ehea_qp failed (1)\n");
2207 goto out;
2210 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2211 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2212 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2213 &dummy64, &dummy64, &dummy16, &dummy16);
2214 if (hret != H_SUCCESS) {
2215 pr_err("modify_ehea_qp failed (1)\n");
2216 goto out;
2219 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2220 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2221 if (hret != H_SUCCESS) {
2222 pr_err("query_ehea_qp failed (2)\n");
2223 goto out;
2226 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2227 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2228 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2229 &dummy64, &dummy64, &dummy16, &dummy16);
2230 if (hret != H_SUCCESS) {
2231 pr_err("modify_ehea_qp failed (2)\n");
2232 goto out;
2235 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2236 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2237 if (hret != H_SUCCESS) {
2238 pr_err("query_ehea_qp failed (3)\n");
2239 goto out;
2242 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2243 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2244 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2245 &dummy64, &dummy64, &dummy16, &dummy16);
2246 if (hret != H_SUCCESS) {
2247 pr_err("modify_ehea_qp failed (3)\n");
2248 goto out;
2251 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2252 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2253 if (hret != H_SUCCESS) {
2254 pr_err("query_ehea_qp failed (4)\n");
2255 goto out;
2258 ret = 0;
2259 out:
2260 free_page((unsigned long)cb0);
2261 return ret;
2264 static int ehea_port_res_setup(struct ehea_port *port, int def_qps)
2266 int ret, i;
2267 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2268 enum ehea_eq_type eq_type = EHEA_EQ;
2270 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2271 EHEA_MAX_ENTRIES_EQ, 1);
2272 if (!port->qp_eq) {
2273 ret = -EINVAL;
2274 pr_err("ehea_create_eq failed (qp_eq)\n");
2275 goto out_kill_eq;
2278 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2279 pr_cfg.max_entries_scq = sq_entries * 2;
2280 pr_cfg.max_entries_sq = sq_entries;
2281 pr_cfg.max_entries_rq1 = rq1_entries;
2282 pr_cfg.max_entries_rq2 = rq2_entries;
2283 pr_cfg.max_entries_rq3 = rq3_entries;
2285 pr_cfg_small_rx.max_entries_rcq = 1;
2286 pr_cfg_small_rx.max_entries_scq = sq_entries;
2287 pr_cfg_small_rx.max_entries_sq = sq_entries;
2288 pr_cfg_small_rx.max_entries_rq1 = 1;
2289 pr_cfg_small_rx.max_entries_rq2 = 1;
2290 pr_cfg_small_rx.max_entries_rq3 = 1;
2292 for (i = 0; i < def_qps; i++) {
2293 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2294 if (ret)
2295 goto out_clean_pr;
2297 for (i = def_qps; i < def_qps; i++) {
2298 ret = ehea_init_port_res(port, &port->port_res[i],
2299 &pr_cfg_small_rx, i);
2300 if (ret)
2301 goto out_clean_pr;
2304 return 0;
2306 out_clean_pr:
2307 while (--i >= 0)
2308 ehea_clean_portres(port, &port->port_res[i]);
2310 out_kill_eq:
2311 ehea_destroy_eq(port->qp_eq);
2312 return ret;
2315 static int ehea_clean_all_portres(struct ehea_port *port)
2317 int ret = 0;
2318 int i;
2320 for (i = 0; i < port->num_def_qps; i++)
2321 ret |= ehea_clean_portres(port, &port->port_res[i]);
2323 ret |= ehea_destroy_eq(port->qp_eq);
2325 return ret;
2328 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2330 if (adapter->active_ports)
2331 return;
2333 ehea_rem_mr(&adapter->mr);
2336 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2338 if (adapter->active_ports)
2339 return 0;
2341 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2344 static int ehea_up(struct net_device *dev)
2346 int ret, i;
2347 struct ehea_port *port = netdev_priv(dev);
2349 if (port->state == EHEA_PORT_UP)
2350 return 0;
2352 ret = ehea_port_res_setup(port, port->num_def_qps);
2353 if (ret) {
2354 netdev_err(dev, "port_res_failed\n");
2355 goto out;
2358 /* Set default QP for this port */
2359 ret = ehea_configure_port(port);
2360 if (ret) {
2361 netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2362 goto out_clean_pr;
2365 ret = ehea_reg_interrupts(dev);
2366 if (ret) {
2367 netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2368 goto out_clean_pr;
2371 for (i = 0; i < port->num_def_qps; i++) {
2372 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2373 if (ret) {
2374 netdev_err(dev, "activate_qp failed\n");
2375 goto out_free_irqs;
2379 for (i = 0; i < port->num_def_qps; i++) {
2380 ret = ehea_fill_port_res(&port->port_res[i]);
2381 if (ret) {
2382 netdev_err(dev, "out_free_irqs\n");
2383 goto out_free_irqs;
2387 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2388 if (ret) {
2389 ret = -EIO;
2390 goto out_free_irqs;
2393 port->state = EHEA_PORT_UP;
2395 ret = 0;
2396 goto out;
2398 out_free_irqs:
2399 ehea_free_interrupts(dev);
2401 out_clean_pr:
2402 ehea_clean_all_portres(port);
2403 out:
2404 if (ret)
2405 netdev_info(dev, "Failed starting. ret=%i\n", ret);
2407 ehea_update_bcmc_registrations();
2408 ehea_update_firmware_handles();
2410 return ret;
2413 static void port_napi_disable(struct ehea_port *port)
2415 int i;
2417 for (i = 0; i < port->num_def_qps; i++)
2418 napi_disable(&port->port_res[i].napi);
2421 static void port_napi_enable(struct ehea_port *port)
2423 int i;
2425 for (i = 0; i < port->num_def_qps; i++)
2426 napi_enable(&port->port_res[i].napi);
2429 static int ehea_open(struct net_device *dev)
2431 int ret;
2432 struct ehea_port *port = netdev_priv(dev);
2434 mutex_lock(&port->port_lock);
2436 netif_info(port, ifup, dev, "enabling port\n");
2438 ret = ehea_up(dev);
2439 if (!ret) {
2440 port_napi_enable(port);
2441 netif_tx_start_all_queues(dev);
2444 mutex_unlock(&port->port_lock);
2445 schedule_delayed_work(&port->stats_work,
2446 round_jiffies_relative(msecs_to_jiffies(1000)));
2448 return ret;
2451 static int ehea_down(struct net_device *dev)
2453 int ret;
2454 struct ehea_port *port = netdev_priv(dev);
2456 if (port->state == EHEA_PORT_DOWN)
2457 return 0;
2459 ehea_drop_multicast_list(dev);
2460 ehea_allmulti(dev, 0);
2461 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2463 ehea_free_interrupts(dev);
2465 port->state = EHEA_PORT_DOWN;
2467 ehea_update_bcmc_registrations();
2469 ret = ehea_clean_all_portres(port);
2470 if (ret)
2471 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2473 ehea_update_firmware_handles();
2475 return ret;
2478 static int ehea_stop(struct net_device *dev)
2480 int ret;
2481 struct ehea_port *port = netdev_priv(dev);
2483 netif_info(port, ifdown, dev, "disabling port\n");
2485 set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2486 cancel_work_sync(&port->reset_task);
2487 cancel_delayed_work_sync(&port->stats_work);
2488 mutex_lock(&port->port_lock);
2489 netif_tx_stop_all_queues(dev);
2490 port_napi_disable(port);
2491 ret = ehea_down(dev);
2492 mutex_unlock(&port->port_lock);
2493 clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2494 return ret;
2497 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2499 struct ehea_qp qp = *orig_qp;
2500 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2501 struct ehea_swqe *swqe;
2502 int wqe_index;
2503 int i;
2505 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2506 swqe = ehea_get_swqe(&qp, &wqe_index);
2507 swqe->tx_control |= EHEA_SWQE_PURGE;
2511 static void ehea_flush_sq(struct ehea_port *port)
2513 int i;
2515 for (i = 0; i < port->num_def_qps; i++) {
2516 struct ehea_port_res *pr = &port->port_res[i];
2517 int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2518 int ret;
2520 ret = wait_event_timeout(port->swqe_avail_wq,
2521 atomic_read(&pr->swqe_avail) >= swqe_max,
2522 msecs_to_jiffies(100));
2524 if (!ret) {
2525 pr_err("WARNING: sq not flushed completely\n");
2526 break;
2531 static int ehea_stop_qps(struct net_device *dev)
2533 struct ehea_port *port = netdev_priv(dev);
2534 struct ehea_adapter *adapter = port->adapter;
2535 struct hcp_modify_qp_cb0 *cb0;
2536 int ret = -EIO;
2537 int dret;
2538 int i;
2539 u64 hret;
2540 u64 dummy64 = 0;
2541 u16 dummy16 = 0;
2543 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2544 if (!cb0) {
2545 ret = -ENOMEM;
2546 goto out;
2549 for (i = 0; i < (port->num_def_qps); i++) {
2550 struct ehea_port_res *pr = &port->port_res[i];
2551 struct ehea_qp *qp = pr->qp;
2553 /* Purge send queue */
2554 ehea_purge_sq(qp);
2556 /* Disable queue pair */
2557 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2558 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2559 cb0);
2560 if (hret != H_SUCCESS) {
2561 pr_err("query_ehea_qp failed (1)\n");
2562 goto out;
2565 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2566 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2568 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2569 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2570 1), cb0, &dummy64,
2571 &dummy64, &dummy16, &dummy16);
2572 if (hret != H_SUCCESS) {
2573 pr_err("modify_ehea_qp failed (1)\n");
2574 goto out;
2577 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2578 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2579 cb0);
2580 if (hret != H_SUCCESS) {
2581 pr_err("query_ehea_qp failed (2)\n");
2582 goto out;
2585 /* deregister shared memory regions */
2586 dret = ehea_rem_smrs(pr);
2587 if (dret) {
2588 pr_err("unreg shared memory region failed\n");
2589 goto out;
2593 ret = 0;
2594 out:
2595 free_page((unsigned long)cb0);
2597 return ret;
2600 static void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2602 struct ehea_qp qp = *orig_qp;
2603 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2604 struct ehea_rwqe *rwqe;
2605 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2606 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2607 struct sk_buff *skb;
2608 u32 lkey = pr->recv_mr.lkey;
2611 int i;
2612 int index;
2614 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2615 rwqe = ehea_get_next_rwqe(&qp, 2);
2616 rwqe->sg_list[0].l_key = lkey;
2617 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2618 skb = skba_rq2[index];
2619 if (skb)
2620 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2623 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2624 rwqe = ehea_get_next_rwqe(&qp, 3);
2625 rwqe->sg_list[0].l_key = lkey;
2626 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2627 skb = skba_rq3[index];
2628 if (skb)
2629 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2633 static int ehea_restart_qps(struct net_device *dev)
2635 struct ehea_port *port = netdev_priv(dev);
2636 struct ehea_adapter *adapter = port->adapter;
2637 int ret = 0;
2638 int i;
2640 struct hcp_modify_qp_cb0 *cb0;
2641 u64 hret;
2642 u64 dummy64 = 0;
2643 u16 dummy16 = 0;
2645 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2646 if (!cb0) {
2647 ret = -ENOMEM;
2648 goto out;
2651 for (i = 0; i < (port->num_def_qps); i++) {
2652 struct ehea_port_res *pr = &port->port_res[i];
2653 struct ehea_qp *qp = pr->qp;
2655 ret = ehea_gen_smrs(pr);
2656 if (ret) {
2657 netdev_err(dev, "creation of shared memory regions failed\n");
2658 goto out;
2661 ehea_update_rqs(qp, pr);
2663 /* Enable queue pair */
2664 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2665 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2666 cb0);
2667 if (hret != H_SUCCESS) {
2668 netdev_err(dev, "query_ehea_qp failed (1)\n");
2669 goto out;
2672 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2673 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2675 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2676 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2677 1), cb0, &dummy64,
2678 &dummy64, &dummy16, &dummy16);
2679 if (hret != H_SUCCESS) {
2680 netdev_err(dev, "modify_ehea_qp failed (1)\n");
2681 goto out;
2684 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2685 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2686 cb0);
2687 if (hret != H_SUCCESS) {
2688 netdev_err(dev, "query_ehea_qp failed (2)\n");
2689 goto out;
2692 /* refill entire queue */
2693 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2694 ehea_refill_rq2(pr, 0);
2695 ehea_refill_rq3(pr, 0);
2697 out:
2698 free_page((unsigned long)cb0);
2700 return ret;
2703 static void ehea_reset_port(struct work_struct *work)
2705 int ret;
2706 struct ehea_port *port =
2707 container_of(work, struct ehea_port, reset_task);
2708 struct net_device *dev = port->netdev;
2710 mutex_lock(&dlpar_mem_lock);
2711 port->resets++;
2712 mutex_lock(&port->port_lock);
2713 netif_tx_disable(dev);
2715 port_napi_disable(port);
2717 ehea_down(dev);
2719 ret = ehea_up(dev);
2720 if (ret)
2721 goto out;
2723 ehea_set_multicast_list(dev);
2725 netif_info(port, timer, dev, "reset successful\n");
2727 port_napi_enable(port);
2729 netif_tx_wake_all_queues(dev);
2730 out:
2731 mutex_unlock(&port->port_lock);
2732 mutex_unlock(&dlpar_mem_lock);
2735 static void ehea_rereg_mrs(void)
2737 int ret, i;
2738 struct ehea_adapter *adapter;
2740 pr_info("LPAR memory changed - re-initializing driver\n");
2742 list_for_each_entry(adapter, &adapter_list, list)
2743 if (adapter->active_ports) {
2744 /* Shutdown all ports */
2745 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2746 struct ehea_port *port = adapter->port[i];
2747 struct net_device *dev;
2749 if (!port)
2750 continue;
2752 dev = port->netdev;
2754 if (dev->flags & IFF_UP) {
2755 mutex_lock(&port->port_lock);
2756 netif_tx_disable(dev);
2757 ehea_flush_sq(port);
2758 ret = ehea_stop_qps(dev);
2759 if (ret) {
2760 mutex_unlock(&port->port_lock);
2761 goto out;
2763 port_napi_disable(port);
2764 mutex_unlock(&port->port_lock);
2766 reset_sq_restart_flag(port);
2769 /* Unregister old memory region */
2770 ret = ehea_rem_mr(&adapter->mr);
2771 if (ret) {
2772 pr_err("unregister MR failed - driver inoperable!\n");
2773 goto out;
2777 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2779 list_for_each_entry(adapter, &adapter_list, list)
2780 if (adapter->active_ports) {
2781 /* Register new memory region */
2782 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2783 if (ret) {
2784 pr_err("register MR failed - driver inoperable!\n");
2785 goto out;
2788 /* Restart all ports */
2789 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2790 struct ehea_port *port = adapter->port[i];
2792 if (port) {
2793 struct net_device *dev = port->netdev;
2795 if (dev->flags & IFF_UP) {
2796 mutex_lock(&port->port_lock);
2797 ret = ehea_restart_qps(dev);
2798 if (!ret) {
2799 check_sqs(port);
2800 port_napi_enable(port);
2801 netif_tx_wake_all_queues(dev);
2802 } else {
2803 netdev_err(dev, "Unable to restart QPS\n");
2805 mutex_unlock(&port->port_lock);
2810 pr_info("re-initializing driver complete\n");
2811 out:
2812 return;
2815 static void ehea_tx_watchdog(struct net_device *dev)
2817 struct ehea_port *port = netdev_priv(dev);
2819 if (netif_carrier_ok(dev) &&
2820 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2821 ehea_schedule_port_reset(port);
2824 static int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2826 struct hcp_query_ehea *cb;
2827 u64 hret;
2828 int ret;
2830 cb = (void *)get_zeroed_page(GFP_KERNEL);
2831 if (!cb) {
2832 ret = -ENOMEM;
2833 goto out;
2836 hret = ehea_h_query_ehea(adapter->handle, cb);
2838 if (hret != H_SUCCESS) {
2839 ret = -EIO;
2840 goto out_herr;
2843 adapter->max_mc_mac = cb->max_mc_mac - 1;
2844 ret = 0;
2846 out_herr:
2847 free_page((unsigned long)cb);
2848 out:
2849 return ret;
2852 static int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2854 struct hcp_ehea_port_cb4 *cb4;
2855 u64 hret;
2856 int ret = 0;
2858 *jumbo = 0;
2860 /* (Try to) enable *jumbo frames */
2861 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
2862 if (!cb4) {
2863 pr_err("no mem for cb4\n");
2864 ret = -ENOMEM;
2865 goto out;
2866 } else {
2867 hret = ehea_h_query_ehea_port(port->adapter->handle,
2868 port->logical_port_id,
2869 H_PORT_CB4,
2870 H_PORT_CB4_JUMBO, cb4);
2871 if (hret == H_SUCCESS) {
2872 if (cb4->jumbo_frame)
2873 *jumbo = 1;
2874 else {
2875 cb4->jumbo_frame = 1;
2876 hret = ehea_h_modify_ehea_port(port->adapter->
2877 handle,
2878 port->
2879 logical_port_id,
2880 H_PORT_CB4,
2881 H_PORT_CB4_JUMBO,
2882 cb4);
2883 if (hret == H_SUCCESS)
2884 *jumbo = 1;
2886 } else
2887 ret = -EINVAL;
2889 free_page((unsigned long)cb4);
2891 out:
2892 return ret;
2895 static ssize_t ehea_show_port_id(struct device *dev,
2896 struct device_attribute *attr, char *buf)
2898 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2899 return sprintf(buf, "%d", port->logical_port_id);
2902 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2903 NULL);
2905 static void logical_port_release(struct device *dev)
2907 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2908 of_node_put(port->ofdev.dev.of_node);
2911 static struct device *ehea_register_port(struct ehea_port *port,
2912 struct device_node *dn)
2914 int ret;
2916 port->ofdev.dev.of_node = of_node_get(dn);
2917 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
2918 port->ofdev.dev.bus = &ibmebus_bus_type;
2920 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
2921 port->ofdev.dev.release = logical_port_release;
2923 ret = of_device_register(&port->ofdev);
2924 if (ret) {
2925 pr_err("failed to register device. ret=%d\n", ret);
2926 goto out;
2929 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2930 if (ret) {
2931 pr_err("failed to register attributes, ret=%d\n", ret);
2932 goto out_unreg_of_dev;
2935 return &port->ofdev.dev;
2937 out_unreg_of_dev:
2938 of_device_unregister(&port->ofdev);
2939 out:
2940 return NULL;
2943 static void ehea_unregister_port(struct ehea_port *port)
2945 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2946 of_device_unregister(&port->ofdev);
2949 static const struct net_device_ops ehea_netdev_ops = {
2950 .ndo_open = ehea_open,
2951 .ndo_stop = ehea_stop,
2952 .ndo_start_xmit = ehea_start_xmit,
2953 #ifdef CONFIG_NET_POLL_CONTROLLER
2954 .ndo_poll_controller = ehea_netpoll,
2955 #endif
2956 .ndo_get_stats64 = ehea_get_stats64,
2957 .ndo_set_mac_address = ehea_set_mac_addr,
2958 .ndo_validate_addr = eth_validate_addr,
2959 .ndo_set_rx_mode = ehea_set_multicast_list,
2960 .ndo_change_mtu = ehea_change_mtu,
2961 .ndo_vlan_rx_add_vid = ehea_vlan_rx_add_vid,
2962 .ndo_vlan_rx_kill_vid = ehea_vlan_rx_kill_vid,
2963 .ndo_tx_timeout = ehea_tx_watchdog,
2966 static struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2967 u32 logical_port_id,
2968 struct device_node *dn)
2970 int ret;
2971 struct net_device *dev;
2972 struct ehea_port *port;
2973 struct device *port_dev;
2974 int jumbo;
2976 /* allocate memory for the port structures */
2977 dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
2979 if (!dev) {
2980 ret = -ENOMEM;
2981 goto out_err;
2984 port = netdev_priv(dev);
2986 mutex_init(&port->port_lock);
2987 port->state = EHEA_PORT_DOWN;
2988 port->sig_comp_iv = sq_entries / 10;
2990 port->adapter = adapter;
2991 port->netdev = dev;
2992 port->logical_port_id = logical_port_id;
2994 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2996 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2997 if (!port->mc_list) {
2998 ret = -ENOMEM;
2999 goto out_free_ethdev;
3002 INIT_LIST_HEAD(&port->mc_list->list);
3004 ret = ehea_sense_port_attr(port);
3005 if (ret)
3006 goto out_free_mc_list;
3008 netif_set_real_num_rx_queues(dev, port->num_def_qps);
3009 netif_set_real_num_tx_queues(dev, port->num_def_qps);
3011 port_dev = ehea_register_port(port, dn);
3012 if (!port_dev)
3013 goto out_free_mc_list;
3015 SET_NETDEV_DEV(dev, port_dev);
3017 /* initialize net_device structure */
3018 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3020 dev->netdev_ops = &ehea_netdev_ops;
3021 ehea_set_ethtool_ops(dev);
3023 dev->hw_features = NETIF_F_SG | NETIF_F_TSO |
3024 NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_CTAG_TX;
3025 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO |
3026 NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
3027 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3028 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM;
3029 dev->vlan_features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HIGHDMA |
3030 NETIF_F_IP_CSUM;
3031 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3033 INIT_WORK(&port->reset_task, ehea_reset_port);
3034 INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3036 init_waitqueue_head(&port->swqe_avail_wq);
3037 init_waitqueue_head(&port->restart_wq);
3039 memset(&port->stats, 0, sizeof(struct net_device_stats));
3040 ret = register_netdev(dev);
3041 if (ret) {
3042 pr_err("register_netdev failed. ret=%d\n", ret);
3043 goto out_unreg_port;
3046 ret = ehea_get_jumboframe_status(port, &jumbo);
3047 if (ret)
3048 netdev_err(dev, "failed determining jumbo frame status\n");
3050 netdev_info(dev, "Jumbo frames are %sabled\n",
3051 jumbo == 1 ? "en" : "dis");
3053 adapter->active_ports++;
3055 return port;
3057 out_unreg_port:
3058 ehea_unregister_port(port);
3060 out_free_mc_list:
3061 kfree(port->mc_list);
3063 out_free_ethdev:
3064 free_netdev(dev);
3066 out_err:
3067 pr_err("setting up logical port with id=%d failed, ret=%d\n",
3068 logical_port_id, ret);
3069 return NULL;
3072 static void ehea_shutdown_single_port(struct ehea_port *port)
3074 struct ehea_adapter *adapter = port->adapter;
3076 cancel_work_sync(&port->reset_task);
3077 cancel_delayed_work_sync(&port->stats_work);
3078 unregister_netdev(port->netdev);
3079 ehea_unregister_port(port);
3080 kfree(port->mc_list);
3081 free_netdev(port->netdev);
3082 adapter->active_ports--;
3085 static int ehea_setup_ports(struct ehea_adapter *adapter)
3087 struct device_node *lhea_dn;
3088 struct device_node *eth_dn = NULL;
3090 const u32 *dn_log_port_id;
3091 int i = 0;
3093 lhea_dn = adapter->ofdev->dev.of_node;
3094 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3096 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3097 NULL);
3098 if (!dn_log_port_id) {
3099 pr_err("bad device node: eth_dn name=%s\n",
3100 eth_dn->full_name);
3101 continue;
3104 if (ehea_add_adapter_mr(adapter)) {
3105 pr_err("creating MR failed\n");
3106 of_node_put(eth_dn);
3107 return -EIO;
3110 adapter->port[i] = ehea_setup_single_port(adapter,
3111 *dn_log_port_id,
3112 eth_dn);
3113 if (adapter->port[i])
3114 netdev_info(adapter->port[i]->netdev,
3115 "logical port id #%d\n", *dn_log_port_id);
3116 else
3117 ehea_remove_adapter_mr(adapter);
3119 i++;
3121 return 0;
3124 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3125 u32 logical_port_id)
3127 struct device_node *lhea_dn;
3128 struct device_node *eth_dn = NULL;
3129 const u32 *dn_log_port_id;
3131 lhea_dn = adapter->ofdev->dev.of_node;
3132 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3134 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3135 NULL);
3136 if (dn_log_port_id)
3137 if (*dn_log_port_id == logical_port_id)
3138 return eth_dn;
3141 return NULL;
3144 static ssize_t ehea_probe_port(struct device *dev,
3145 struct device_attribute *attr,
3146 const char *buf, size_t count)
3148 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3149 struct ehea_port *port;
3150 struct device_node *eth_dn = NULL;
3151 int i;
3153 u32 logical_port_id;
3155 sscanf(buf, "%d", &logical_port_id);
3157 port = ehea_get_port(adapter, logical_port_id);
3159 if (port) {
3160 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3161 logical_port_id);
3162 return -EINVAL;
3165 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3167 if (!eth_dn) {
3168 pr_info("no logical port with id %d found\n", logical_port_id);
3169 return -EINVAL;
3172 if (ehea_add_adapter_mr(adapter)) {
3173 pr_err("creating MR failed\n");
3174 return -EIO;
3177 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3179 of_node_put(eth_dn);
3181 if (port) {
3182 for (i = 0; i < EHEA_MAX_PORTS; i++)
3183 if (!adapter->port[i]) {
3184 adapter->port[i] = port;
3185 break;
3188 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3189 logical_port_id);
3190 } else {
3191 ehea_remove_adapter_mr(adapter);
3192 return -EIO;
3195 return (ssize_t) count;
3198 static ssize_t ehea_remove_port(struct device *dev,
3199 struct device_attribute *attr,
3200 const char *buf, size_t count)
3202 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3203 struct ehea_port *port;
3204 int i;
3205 u32 logical_port_id;
3207 sscanf(buf, "%d", &logical_port_id);
3209 port = ehea_get_port(adapter, logical_port_id);
3211 if (port) {
3212 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3213 logical_port_id);
3215 ehea_shutdown_single_port(port);
3217 for (i = 0; i < EHEA_MAX_PORTS; i++)
3218 if (adapter->port[i] == port) {
3219 adapter->port[i] = NULL;
3220 break;
3222 } else {
3223 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3224 logical_port_id);
3225 return -EINVAL;
3228 ehea_remove_adapter_mr(adapter);
3230 return (ssize_t) count;
3233 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3234 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3236 static int ehea_create_device_sysfs(struct platform_device *dev)
3238 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3239 if (ret)
3240 goto out;
3242 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3243 out:
3244 return ret;
3247 static void ehea_remove_device_sysfs(struct platform_device *dev)
3249 device_remove_file(&dev->dev, &dev_attr_probe_port);
3250 device_remove_file(&dev->dev, &dev_attr_remove_port);
3253 static int ehea_probe_adapter(struct platform_device *dev)
3255 struct ehea_adapter *adapter;
3256 const u64 *adapter_handle;
3257 int ret;
3258 int i;
3260 if (!dev || !dev->dev.of_node) {
3261 pr_err("Invalid ibmebus device probed\n");
3262 return -EINVAL;
3265 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3266 if (!adapter) {
3267 ret = -ENOMEM;
3268 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3269 goto out;
3272 list_add(&adapter->list, &adapter_list);
3274 adapter->ofdev = dev;
3276 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3277 NULL);
3278 if (adapter_handle)
3279 adapter->handle = *adapter_handle;
3281 if (!adapter->handle) {
3282 dev_err(&dev->dev, "failed getting handle for adapter"
3283 " '%s'\n", dev->dev.of_node->full_name);
3284 ret = -ENODEV;
3285 goto out_free_ad;
3288 adapter->pd = EHEA_PD_ID;
3290 platform_set_drvdata(dev, adapter);
3293 /* initialize adapter and ports */
3294 /* get adapter properties */
3295 ret = ehea_sense_adapter_attr(adapter);
3296 if (ret) {
3297 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3298 goto out_free_ad;
3301 adapter->neq = ehea_create_eq(adapter,
3302 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3303 if (!adapter->neq) {
3304 ret = -EIO;
3305 dev_err(&dev->dev, "NEQ creation failed\n");
3306 goto out_free_ad;
3309 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3310 (unsigned long)adapter);
3312 ret = ehea_create_device_sysfs(dev);
3313 if (ret)
3314 goto out_kill_eq;
3316 ret = ehea_setup_ports(adapter);
3317 if (ret) {
3318 dev_err(&dev->dev, "setup_ports failed\n");
3319 goto out_rem_dev_sysfs;
3322 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3323 ehea_interrupt_neq, IRQF_DISABLED,
3324 "ehea_neq", adapter);
3325 if (ret) {
3326 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3327 goto out_shutdown_ports;
3330 /* Handle any events that might be pending. */
3331 tasklet_hi_schedule(&adapter->neq_tasklet);
3333 ret = 0;
3334 goto out;
3336 out_shutdown_ports:
3337 for (i = 0; i < EHEA_MAX_PORTS; i++)
3338 if (adapter->port[i]) {
3339 ehea_shutdown_single_port(adapter->port[i]);
3340 adapter->port[i] = NULL;
3343 out_rem_dev_sysfs:
3344 ehea_remove_device_sysfs(dev);
3346 out_kill_eq:
3347 ehea_destroy_eq(adapter->neq);
3349 out_free_ad:
3350 list_del(&adapter->list);
3351 kfree(adapter);
3353 out:
3354 ehea_update_firmware_handles();
3356 return ret;
3359 static int ehea_remove(struct platform_device *dev)
3361 struct ehea_adapter *adapter = platform_get_drvdata(dev);
3362 int i;
3364 for (i = 0; i < EHEA_MAX_PORTS; i++)
3365 if (adapter->port[i]) {
3366 ehea_shutdown_single_port(adapter->port[i]);
3367 adapter->port[i] = NULL;
3370 ehea_remove_device_sysfs(dev);
3372 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3373 tasklet_kill(&adapter->neq_tasklet);
3375 ehea_destroy_eq(adapter->neq);
3376 ehea_remove_adapter_mr(adapter);
3377 list_del(&adapter->list);
3378 kfree(adapter);
3380 ehea_update_firmware_handles();
3382 return 0;
3385 static void ehea_crash_handler(void)
3387 int i;
3389 if (ehea_fw_handles.arr)
3390 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3391 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3392 ehea_fw_handles.arr[i].fwh,
3393 FORCE_FREE);
3395 if (ehea_bcmc_regs.arr)
3396 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3397 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3398 ehea_bcmc_regs.arr[i].port_id,
3399 ehea_bcmc_regs.arr[i].reg_type,
3400 ehea_bcmc_regs.arr[i].macaddr,
3401 0, H_DEREG_BCMC);
3404 static int ehea_mem_notifier(struct notifier_block *nb,
3405 unsigned long action, void *data)
3407 int ret = NOTIFY_BAD;
3408 struct memory_notify *arg = data;
3410 mutex_lock(&dlpar_mem_lock);
3412 switch (action) {
3413 case MEM_CANCEL_OFFLINE:
3414 pr_info("memory offlining canceled");
3415 /* Readd canceled memory block */
3416 case MEM_ONLINE:
3417 pr_info("memory is going online");
3418 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3419 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3420 goto out_unlock;
3421 ehea_rereg_mrs();
3422 break;
3423 case MEM_GOING_OFFLINE:
3424 pr_info("memory is going offline");
3425 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3426 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3427 goto out_unlock;
3428 ehea_rereg_mrs();
3429 break;
3430 default:
3431 break;
3434 ehea_update_firmware_handles();
3435 ret = NOTIFY_OK;
3437 out_unlock:
3438 mutex_unlock(&dlpar_mem_lock);
3439 return ret;
3442 static struct notifier_block ehea_mem_nb = {
3443 .notifier_call = ehea_mem_notifier,
3446 static int ehea_reboot_notifier(struct notifier_block *nb,
3447 unsigned long action, void *unused)
3449 if (action == SYS_RESTART) {
3450 pr_info("Reboot: freeing all eHEA resources\n");
3451 ibmebus_unregister_driver(&ehea_driver);
3453 return NOTIFY_DONE;
3456 static struct notifier_block ehea_reboot_nb = {
3457 .notifier_call = ehea_reboot_notifier,
3460 static int check_module_parm(void)
3462 int ret = 0;
3464 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3465 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3466 pr_info("Bad parameter: rq1_entries\n");
3467 ret = -EINVAL;
3469 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3470 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3471 pr_info("Bad parameter: rq2_entries\n");
3472 ret = -EINVAL;
3474 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3475 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3476 pr_info("Bad parameter: rq3_entries\n");
3477 ret = -EINVAL;
3479 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3480 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3481 pr_info("Bad parameter: sq_entries\n");
3482 ret = -EINVAL;
3485 return ret;
3488 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3489 char *buf)
3491 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3494 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3495 ehea_show_capabilities, NULL);
3497 static int __init ehea_module_init(void)
3499 int ret;
3501 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3503 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3504 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3506 mutex_init(&ehea_fw_handles.lock);
3507 spin_lock_init(&ehea_bcmc_regs.lock);
3509 ret = check_module_parm();
3510 if (ret)
3511 goto out;
3513 ret = ehea_create_busmap();
3514 if (ret)
3515 goto out;
3517 ret = register_reboot_notifier(&ehea_reboot_nb);
3518 if (ret)
3519 pr_info("failed registering reboot notifier\n");
3521 ret = register_memory_notifier(&ehea_mem_nb);
3522 if (ret)
3523 pr_info("failed registering memory remove notifier\n");
3525 ret = crash_shutdown_register(ehea_crash_handler);
3526 if (ret)
3527 pr_info("failed registering crash handler\n");
3529 ret = ibmebus_register_driver(&ehea_driver);
3530 if (ret) {
3531 pr_err("failed registering eHEA device driver on ebus\n");
3532 goto out2;
3535 ret = driver_create_file(&ehea_driver.driver,
3536 &driver_attr_capabilities);
3537 if (ret) {
3538 pr_err("failed to register capabilities attribute, ret=%d\n",
3539 ret);
3540 goto out3;
3543 return ret;
3545 out3:
3546 ibmebus_unregister_driver(&ehea_driver);
3547 out2:
3548 unregister_memory_notifier(&ehea_mem_nb);
3549 unregister_reboot_notifier(&ehea_reboot_nb);
3550 crash_shutdown_unregister(ehea_crash_handler);
3551 out:
3552 return ret;
3555 static void __exit ehea_module_exit(void)
3557 int ret;
3559 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3560 ibmebus_unregister_driver(&ehea_driver);
3561 unregister_reboot_notifier(&ehea_reboot_nb);
3562 ret = crash_shutdown_unregister(ehea_crash_handler);
3563 if (ret)
3564 pr_info("failed unregistering crash handler\n");
3565 unregister_memory_notifier(&ehea_mem_nb);
3566 kfree(ehea_fw_handles.arr);
3567 kfree(ehea_bcmc_regs.arr);
3568 ehea_destroy_busmap();
3571 module_init(ehea_module_init);
3572 module_exit(ehea_module_exit);