Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / ethernet / ibm / ehea / ehea_main.c
blobe6893cdfd13b9bd16ce628b7fc697f2ef940a188
1 /*
2 * linux/drivers/net/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 = [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");
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 __devinit ehea_probe_adapter(struct platform_device *dev,
102 const struct of_device_id *id);
104 static int __devexit ehea_remove(struct platform_device *dev);
106 static struct of_device_id ehea_device_table[] = {
108 .name = "lhea",
109 .compatible = "IBM,lhea",
113 MODULE_DEVICE_TABLE(of, ehea_device_table);
115 static struct of_platform_driver ehea_driver = {
116 .driver = {
117 .name = "ehea",
118 .owner = THIS_MODULE,
119 .of_match_table = ehea_device_table,
121 .probe = ehea_probe_adapter,
122 .remove = ehea_remove,
125 void ehea_dump(void *adr, int len, char *msg)
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;
136 static void ehea_schedule_port_reset(struct ehea_port *port)
138 if (!test_bit(__EHEA_DISABLE_PORT_RESET, &port->flags))
139 schedule_work(&port->reset_task);
142 static void ehea_update_firmware_handles(void)
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;
152 /* Determine number of handles */
153 mutex_lock(&ehea_fw_handles.lock);
155 list_for_each_entry(adapter, &adapter_list, list) {
156 num_adapters++;
158 for (k = 0; k < EHEA_MAX_PORTS; k++) {
159 struct ehea_port *port = adapter->port[k];
161 if (!port || (port->state != EHEA_PORT_UP))
162 continue;
164 num_ports++;
165 num_portres += port->num_def_qps;
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;
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;
180 list_for_each_entry(adapter, &adapter_list, list) {
181 if (num_adapters == 0)
182 break;
184 for (k = 0; k < EHEA_MAX_PORTS; k++) {
185 struct ehea_port *port = adapter->port[k];
187 if (!port || (port->state != EHEA_PORT_UP) ||
188 (num_ports == 0))
189 continue;
191 for (l = 0; l < port->num_def_qps; l++) {
192 struct ehea_port_res *pr = &port->port_res[l];
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;
207 arr[i].adh = adapter->handle;
208 arr[i++].fwh = port->qp_eq->fw_handle;
209 num_ports--;
212 arr[i].adh = adapter->handle;
213 arr[i++].fwh = adapter->neq->fw_handle;
215 if (adapter->mr.handle) {
216 arr[i].adh = adapter->handle;
217 arr[i++].fwh = adapter->mr.handle;
219 num_adapters--;
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);
230 static void ehea_update_bcmc_registrations(void)
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;
240 spin_lock_irqsave(&ehea_bcmc_regs.lock, flags);
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];
247 if (!port || (port->state != EHEA_PORT_UP))
248 continue;
250 num_registrations += 2; /* Broadcast registrations */
252 list_for_each_entry(mc_entry, &port->mc_list->list,list)
253 num_registrations += 2;
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;
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];
267 if (!port || (port->state != EHEA_PORT_UP))
268 continue;
270 if (num_registrations == 0)
271 goto out_update;
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;
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;
286 list_for_each_entry(mc_entry,
287 &port->mc_list->list, list) {
288 if (num_registrations == 0)
289 goto out_update;
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;
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;
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);
317 static struct rtnl_link_stats64 *ehea_get_stats64(struct net_device *dev,
318 struct rtnl_link_stats64 *stats)
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;
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;
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;
334 stats->tx_packets = tx_packets;
335 stats->rx_bytes = rx_bytes;
336 stats->tx_bytes = tx_bytes;
337 stats->rx_packets = rx_packets;
339 stats->multicast = port->stats.multicast;
340 stats->rx_errors = port->stats.rx_errors;
341 return stats;
344 static void ehea_update_stats(struct work_struct *work)
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;
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;
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;
367 if (netif_msg_hw(port))
368 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
370 stats->multicast = cb2->rxmcp;
371 stats->rx_errors = cb2->rxuerr;
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)));
380 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
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;
389 pr->rq1_skba.os_skbs = 0;
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;
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;
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 netdev_info(dev, "Not enough memory to allocate skb array\n");
435 break;
438 /* Ring doorbell */
439 ehea_update_rq1a(pr->qp, i - 1);
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)
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;
454 fill_wqes = q_skba->os_skbs + num_wqes;
455 q_skba->os_skbs = 0;
457 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
458 q_skba->os_skbs = fill_wqes;
459 return ret;
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;
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;
477 break;
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;
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;
497 index++;
498 index &= max_index_mask;
499 adder++;
502 q_skba->index = index;
503 if (adder == 0)
504 goto out;
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;
517 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
519 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
520 nr_of_wqes, EHEA_RWQE2_TYPE,
521 EHEA_RQ2_PKT_SIZE);
525 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
527 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
528 nr_of_wqes, EHEA_RWQE3_TYPE,
529 EHEA_MAX_PACKET_SIZE);
532 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
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;
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)
547 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
549 skb_put(skb, length);
550 skb->protocol = eth_type_trans(skb, dev);
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;
560 skb_record_rx_queue(skb, pr - &pr->port->port_res[0]);
563 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
564 int arr_len,
565 struct ehea_cqe *cqe)
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;
572 x = skb_index + 1;
573 x &= (arr_len - 1);
575 pref = skb_array[x];
576 if (pref) {
577 prefetchw(pref);
578 prefetchw(pref + EHEA_CACHE_LINE);
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);
587 skb = skb_array[skb_index];
588 skb_array[skb_index] = NULL;
589 return skb;
592 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
593 int arr_len, int wqe_index)
595 struct sk_buff *skb;
596 void *pref;
597 int x;
599 x = wqe_index + 1;
600 x &= (arr_len - 1);
602 pref = skb_array[x];
603 if (pref) {
604 prefetchw(pref);
605 prefetchw(pref + EHEA_CACHE_LINE);
607 pref = (skb_array[x]->data);
608 prefetchw(pref);
609 prefetchw(pref + EHEA_CACHE_LINE);
612 skb = skb_array[wqe_index];
613 skb_array[wqe_index] = NULL;
614 return skb;
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)
621 struct sk_buff *skb;
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++;
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);
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");
646 ehea_schedule_port_reset(pr->port);
647 return 1;
650 return 0;
653 static int ehea_proc_rwqes(struct net_device *dev,
654 struct ehea_port_res *pr,
655 int budget)
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;
671 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
672 last_wqe_index = 0;
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");
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");
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;
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;
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;
724 ehea_fill_skb(dev, skb, cqe, pr);
725 processed_rq3++;
728 processed_bytes += skb->len;
730 if (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
731 __vlan_hwaccel_put_tag(skb, cqe->vlan_tag);
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;
742 cqe = ehea_poll_rq1(qp, &wqe_index);
745 pr->rx_packets += processed;
746 pr->rx_bytes += processed_bytes;
748 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
749 ehea_refill_rq2(pr, processed_rq2);
750 ehea_refill_rq3(pr, processed_rq3);
752 return processed;
755 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
757 static void reset_sq_restart_flag(struct ehea_port *port)
759 int i;
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;
765 wake_up(&port->restart_wq);
768 static void check_sqs(struct ehea_port *port)
770 struct ehea_swqe *swqe;
771 int swqe_index;
772 int i, k;
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);
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;
788 ehea_post_swqe(pr->qp, swqe);
790 ret = wait_event_timeout(port->restart_wq,
791 pr->sq_restart_flag == 0,
792 msecs_to_jiffies(100));
794 if (!ret) {
795 pr_err("HW/SW queues out of sync\n");
796 ehea_schedule_port_reset(pr->port);
797 return;
803 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
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]);
815 cqe = ehea_poll_cq(send_cq);
816 while (cqe && (quota > 0)) {
817 ehea_inc_cq(send_cq);
819 cqe_counter++;
820 rmb();
822 if (cqe->wr_id == SWQE_RESTART_CHECK) {
823 pr->sq_restart_flag = 1;
824 swqe_av++;
825 break;
828 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
829 pr_err("Bad send completion status=0x%04X\n",
830 cqe->status);
832 if (netif_msg_tx_err(pr->port))
833 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
835 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
836 pr_err("Resetting port\n");
837 ehea_schedule_port_reset(pr->port);
838 break;
842 if (netif_msg_tx_done(pr->port))
843 ehea_dump(cqe, sizeof(*cqe), "CQE");
845 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
846 == EHEA_SWQE2_TYPE)) {
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;
854 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
855 quota--;
857 cqe = ehea_poll_cq(send_cq);
860 ehea_update_feca(send_cq, cqe_counter);
861 atomic_add(swqe_av, &pr->swqe_avail);
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);
872 wake_up(&pr->port->swqe_avail_wq);
874 return cqe;
877 #define EHEA_POLL_MAX_CQES 65535
879 static int ehea_poll(struct napi_struct *napi, int budget)
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;
889 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
890 rx += ehea_proc_rwqes(dev, pr, budget - rx);
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);
902 if (!cqe && !cqe_skb)
903 return rx;
905 if (!napi_reschedule(napi))
906 return rx;
908 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
909 rx += ehea_proc_rwqes(dev, pr, budget - rx);
912 return rx;
915 #ifdef CONFIG_NET_POLL_CONTROLLER
916 static void ehea_netpoll(struct net_device *dev)
918 struct ehea_port *port = netdev_priv(dev);
919 int i;
921 for (i = 0; i < port->num_def_qps; i++)
922 napi_schedule(&port->port_res[i].napi);
924 #endif
926 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
928 struct ehea_port_res *pr = param;
930 napi_schedule(&pr->napi);
932 return IRQ_HANDLED;
935 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
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;
944 eqe = ehea_poll_eq(port->qp_eq);
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);
951 qp = port->port_res[qp_token].qp;
953 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
954 &aer, &aerr);
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 */
963 eqe = ehea_poll_eq(port->qp_eq);
966 if (reset_port) {
967 pr_err("Resetting port\n");
968 ehea_schedule_port_reset(port);
971 return IRQ_HANDLED;
974 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
975 int logical_port)
977 int i;
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;
986 int ehea_sense_port_attr(struct ehea_port *port)
988 int ret;
989 u64 hret;
990 struct hcp_ehea_port_cb0 *cb0;
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;
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;
1009 /* MAC address */
1010 port->mac_addr = cb0->port_mac_addr << 16;
1012 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1013 ret = -EADDRNOTAVAIL;
1014 goto out_free;
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;
1049 port->autoneg = 1;
1050 port->num_mcs = cb0->num_default_qps;
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;
1058 if (!port->num_def_qps) {
1059 ret = -EINVAL;
1060 goto out_free;
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;
1072 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1074 struct hcp_ehea_port_cb4 *cb4;
1075 u64 hret;
1076 int ret = 0;
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;
1085 cb4->port_speed = port_speed;
1087 netif_carrier_off(port->netdev);
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;
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;
1130 } else {
1131 pr_err("Failed sensing port speed\n");
1132 ret = -EIO;
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");
1143 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1144 netif_carrier_on(port->netdev);
1146 free_page((unsigned long)cb4);
1147 out:
1148 return ret;
1151 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1153 int ret;
1154 u8 ec;
1155 u8 portnum;
1156 struct ehea_port *port;
1157 struct net_device *dev;
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;
1164 switch (ec) {
1165 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1167 if (!port) {
1168 netdev_err(dev, "unknown portnum %x\n", portnum);
1169 break;
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;
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");
1186 netif_carrier_on(dev);
1187 netif_wake_queue(dev);
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);
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);
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");
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;
1233 static void ehea_neq_tasklet(unsigned long data)
1235 struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1236 struct ehea_eqe *eqe;
1237 u64 event_mask;
1239 eqe = ehea_poll_eq(adapter->neq);
1240 pr_debug("eqe=%p\n", eqe);
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);
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);
1253 ehea_h_reset_events(adapter->handle,
1254 adapter->neq->fw_handle, event_mask);
1257 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1259 struct ehea_adapter *adapter = param;
1260 tasklet_hi_schedule(&adapter->neq_tasklet);
1261 return IRQ_HANDLED;
1265 static int ehea_fill_port_res(struct ehea_port_res *pr)
1267 int ret;
1268 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1270 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1272 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1274 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1276 return ret;
1279 static int ehea_reg_interrupts(struct net_device *dev)
1281 struct ehea_port *port = netdev_priv(dev);
1282 struct ehea_port_res *pr;
1283 int i, ret;
1286 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1287 dev->name);
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;
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);
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;
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);
1320 out:
1321 return ret;
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]);
1330 out_free_qpeq:
1331 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1332 i = port->num_def_qps;
1334 goto out;
1338 static void ehea_free_interrupts(struct net_device *dev)
1340 struct ehea_port *port = netdev_priv(dev);
1341 struct ehea_port_res *pr;
1342 int i;
1344 /* send */
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);
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);
1361 static int ehea_configure_port(struct ehea_port *port)
1363 int ret, i;
1364 u64 hret, mask;
1365 struct hcp_ehea_port_cb0 *cb0;
1367 ret = -ENOMEM;
1368 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1369 if (!cb0)
1370 goto out;
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);
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;
1388 if (netif_msg_ifup(port))
1389 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1391 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1392 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
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;
1401 ret = 0;
1403 out_free:
1404 free_page((unsigned long)cb0);
1405 out:
1406 return ret;
1409 static int ehea_gen_smrs(struct ehea_port_res *pr)
1411 int ret;
1412 struct ehea_adapter *adapter = pr->port->adapter;
1414 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1415 if (ret)
1416 goto out;
1418 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1419 if (ret)
1420 goto out_free;
1422 return 0;
1424 out_free:
1425 ehea_rem_mr(&pr->send_mr);
1426 out:
1427 pr_err("Generating SMRS failed\n");
1428 return -EIO;
1431 static int ehea_rem_smrs(struct ehea_port_res *pr)
1433 if ((ehea_rem_mr(&pr->send_mr)) ||
1434 (ehea_rem_mr(&pr->recv_mr)))
1435 return -EIO;
1436 else
1437 return 0;
1440 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1442 int arr_size = sizeof(void *) * max_q_entries;
1444 q_skba->arr = vzalloc(arr_size);
1445 if (!q_skba->arr)
1446 return -ENOMEM;
1448 q_skba->len = max_q_entries;
1449 q_skba->index = 0;
1450 q_skba->os_skbs = 0;
1452 return 0;
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)
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;
1464 tx_bytes = pr->tx_bytes;
1465 tx_packets = pr->tx_packets;
1466 rx_bytes = pr->rx_bytes;
1467 rx_packets = pr->rx_packets;
1469 memset(pr, 0, sizeof(struct ehea_port_res));
1471 pr->tx_bytes = rx_bytes;
1472 pr->tx_packets = tx_packets;
1473 pr->rx_bytes = rx_bytes;
1474 pr->rx_packets = rx_packets;
1476 pr->port = port;
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;
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;
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;
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);
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;
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;
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;
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);
1546 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
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;
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;
1561 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1563 kfree(init_attr);
1565 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1567 ret = 0;
1568 goto out;
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;
1584 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1586 int ret, i;
1588 if (pr->qp)
1589 netif_napi_del(&pr->napi);
1591 ret = ehea_destroy_qp(pr->qp);
1593 if (!ret) {
1594 ehea_destroy_cq(pr->send_cq);
1595 ehea_destroy_cq(pr->recv_cq);
1596 ehea_destroy_eq(pr->eq);
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]);
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]);
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]);
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]);
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);
1620 return ret;
1623 static void write_swqe2_immediate(struct sk_buff *skb, struct ehea_swqe *swqe,
1624 u32 lkey)
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;
1631 swqe->descriptors = 0;
1633 if (skb_is_gso(skb)) {
1634 swqe->tx_control |= EHEA_SWQE_TSO;
1635 swqe->mss = skb_shinfo(skb)->gso_size;
1637 * For TSO packets we only copy the headers into the
1638 * immediate area.
1640 immediate_len = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
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;
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++;
1654 } else {
1655 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1656 swqe->immediate_data_length = skb_data_size;
1660 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1661 struct ehea_swqe *swqe, u32 lkey)
1663 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1664 skb_frag_t *frag;
1665 int nfrags, sg1entry_contains_frag_data, i;
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;
1672 write_swqe2_immediate(skb, swqe, lkey);
1674 /* write descriptors */
1675 if (nfrags > 0) {
1676 if (swqe->descriptors == 0) {
1677 /* sg1entry not yet used */
1678 frag = &skb_shinfo(skb)->frags[0];
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;
1689 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1691 frag = &skb_shinfo(skb)->frags[i];
1692 sgentry = &sg_list[i - sg1entry_contains_frag_data];
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++;
1702 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1704 int ret = 0;
1705 u64 hret;
1706 u8 reg_type;
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;
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;
1730 out_herr:
1731 return ret;
1734 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
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;
1742 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1743 ret = -EADDRNOTAVAIL;
1744 goto out;
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;
1754 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1756 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
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;
1766 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
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;
1775 port->mac_addr = cb0->port_mac_addr << 16;
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;
1784 ret = 0;
1786 out_upregs:
1787 ehea_update_bcmc_registrations();
1788 out_free:
1789 free_page((unsigned long)cb0);
1790 out:
1791 return ret;
1794 static void ehea_promiscuous_error(u64 hret, int enable)
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");
1804 static void ehea_promiscuous(struct net_device *dev, int enable)
1806 struct ehea_port *port = netdev_priv(dev);
1807 struct hcp_ehea_port_cb7 *cb7;
1808 u64 hret;
1810 if (enable == port->promisc)
1811 return;
1813 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1814 if (!cb7) {
1815 pr_err("no mem for cb7\n");
1816 goto out;
1819 /* Modify Pxs_DUCQPN in CB7 */
1820 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
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;
1830 port->promisc = enable;
1831 out:
1832 free_page((unsigned long)cb7);
1835 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1836 u32 hcallid)
1838 u64 hret;
1839 u8 reg_type;
1841 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1842 | EHEA_BCMC_UNTAGGED;
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;
1850 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1851 | EHEA_BCMC_VLANID_ALL;
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;
1860 static int ehea_drop_multicast_list(struct net_device *dev)
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;
1869 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1870 mc_entry = list_entry(pos, struct ehea_mc_list, list);
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;
1879 list_del(pos);
1880 kfree(mc_entry);
1882 return ret;
1885 static void ehea_allmulti(struct net_device *dev, int enable)
1887 struct ehea_port *port = netdev_priv(dev);
1888 u64 hret;
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");
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");
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 pr_err("no mem for mcl_entry\n");
1921 return;
1924 INIT_LIST_HEAD(&ehea_mcl_entry->list);
1926 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
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);
1938 static void ehea_set_multicast_list(struct net_device *dev)
1940 struct ehea_port *port = netdev_priv(dev);
1941 struct netdev_hw_addr *ha;
1942 int ret;
1944 if (port->promisc) {
1945 ehea_promiscuous(dev, 1);
1946 return;
1948 ehea_promiscuous(dev, 0);
1950 if (dev->flags & IFF_ALLMULTI) {
1951 ehea_allmulti(dev, 1);
1952 goto out;
1954 ehea_allmulti(dev, 0);
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.
1962 ehea_allmulti(dev, 1);
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;
1971 netdev_for_each_mc_addr(ha, dev)
1972 ehea_add_multicast_entry(port, ha->addr);
1975 out:
1976 ehea_update_bcmc_registrations();
1979 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1981 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1982 return -EINVAL;
1983 dev->mtu = new_mtu;
1984 return 0;
1987 static void xmit_common(struct sk_buff *skb, struct ehea_swqe *swqe)
1989 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT | EHEA_SWQE_CRC;
1991 if (skb->protocol != htons(ETH_P_IP))
1992 return;
1994 if (skb->ip_summed == CHECKSUM_PARTIAL)
1995 swqe->tx_control |= EHEA_SWQE_IP_CHECKSUM;
1997 swqe->ip_start = skb_network_offset(skb);
1998 swqe->ip_end = swqe->ip_start + ip_hdrlen(skb) - 1;
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;
2005 swqe->tcp_offset = swqe->ip_end + 1 +
2006 offsetof(struct udphdr, check);
2007 break;
2009 case IPPROTO_TCP:
2010 if (skb->ip_summed == CHECKSUM_PARTIAL)
2011 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
2013 swqe->tcp_offset = swqe->ip_end + 1 +
2014 offsetof(struct tcphdr, check);
2015 break;
2019 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2020 struct ehea_swqe *swqe, u32 lkey)
2022 swqe->tx_control |= EHEA_SWQE_DESCRIPTORS_PRESENT;
2024 xmit_common(skb, swqe);
2026 write_swqe2_data(skb, dev, swqe, lkey);
2029 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2030 struct ehea_swqe *swqe)
2032 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2034 xmit_common(skb, swqe);
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);
2041 swqe->immediate_data_length = skb->len;
2042 dev_kfree_skb(skb);
2045 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
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;
2054 pr = &port->port_res[skb_get_queue_mapping(skb)];
2055 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2057 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2058 memset(swqe, 0, SWQE_HEADER_SIZE);
2059 atomic_dec(&pr->swqe_avail);
2061 if (vlan_tx_tag_present(skb)) {
2062 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2063 swqe->vlan_tag = vlan_tx_tag_get(skb);
2066 pr->tx_packets++;
2067 pr->tx_bytes += skb->len;
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;
2090 pr->sq_skba.index++;
2091 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2093 lkey = pr->send_mr.lkey;
2094 ehea_xmit2(skb, dev, swqe, lkey);
2095 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2097 pr->swqe_id_counter += 1;
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");
2104 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2105 netif_tx_stop_queue(txq);
2106 swqe->tx_control |= EHEA_SWQE_PURGE;
2109 ehea_post_swqe(pr->qp, swqe);
2111 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2112 pr->p_stats.queue_stopped++;
2113 netif_tx_stop_queue(txq);
2116 return NETDEV_TX_OK;
2119 static int ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
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;
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;
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;
2143 index = (vid / 64);
2144 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
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;
2152 out:
2153 free_page((unsigned long)cb1);
2154 return err;
2157 static int ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
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;
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;
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;
2181 index = (vid / 64);
2182 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
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;
2190 out:
2191 free_page((unsigned long)cb1);
2192 return err;
2195 static int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2197 int ret = -EIO;
2198 u64 hret;
2199 u16 dummy16 = 0;
2200 u64 dummy64 = 0;
2201 struct hcp_modify_qp_cb0 *cb0;
2203 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2204 if (!cb0) {
2205 ret = -ENOMEM;
2206 goto out;
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;
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;
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;
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;
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;
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;
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;
2264 ret = 0;
2265 out:
2266 free_page((unsigned long)cb0);
2267 return ret;
2270 static int ehea_port_res_setup(struct ehea_port *port, int def_qps)
2272 int ret, i;
2273 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2274 enum ehea_eq_type eq_type = EHEA_EQ;
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;
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;
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;
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;
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;
2310 return 0;
2312 out_clean_pr:
2313 while (--i >= 0)
2314 ehea_clean_portres(port, &port->port_res[i]);
2316 out_kill_eq:
2317 ehea_destroy_eq(port->qp_eq);
2318 return ret;
2321 static int ehea_clean_all_portres(struct ehea_port *port)
2323 int ret = 0;
2324 int i;
2326 for (i = 0; i < port->num_def_qps; i++)
2327 ret |= ehea_clean_portres(port, &port->port_res[i]);
2329 ret |= ehea_destroy_eq(port->qp_eq);
2331 return ret;
2334 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2336 if (adapter->active_ports)
2337 return;
2339 ehea_rem_mr(&adapter->mr);
2342 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2344 if (adapter->active_ports)
2345 return 0;
2347 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2350 static int ehea_up(struct net_device *dev)
2352 int ret, i;
2353 struct ehea_port *port = netdev_priv(dev);
2355 if (port->state == EHEA_PORT_UP)
2356 return 0;
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;
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;
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;
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;
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;
2393 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2394 if (ret) {
2395 ret = -EIO;
2396 goto out_free_irqs;
2399 port->state = EHEA_PORT_UP;
2401 ret = 0;
2402 goto out;
2404 out_free_irqs:
2405 ehea_free_interrupts(dev);
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);
2413 ehea_update_bcmc_registrations();
2414 ehea_update_firmware_handles();
2416 return ret;
2419 static void port_napi_disable(struct ehea_port *port)
2421 int i;
2423 for (i = 0; i < port->num_def_qps; i++)
2424 napi_disable(&port->port_res[i].napi);
2427 static void port_napi_enable(struct ehea_port *port)
2429 int i;
2431 for (i = 0; i < port->num_def_qps; i++)
2432 napi_enable(&port->port_res[i].napi);
2435 static int ehea_open(struct net_device *dev)
2437 int ret;
2438 struct ehea_port *port = netdev_priv(dev);
2440 mutex_lock(&port->port_lock);
2442 netif_info(port, ifup, dev, "enabling port\n");
2444 ret = ehea_up(dev);
2445 if (!ret) {
2446 port_napi_enable(port);
2447 netif_tx_start_all_queues(dev);
2450 mutex_unlock(&port->port_lock);
2451 schedule_delayed_work(&port->stats_work,
2452 round_jiffies_relative(msecs_to_jiffies(1000)));
2454 return ret;
2457 static int ehea_down(struct net_device *dev)
2459 int ret;
2460 struct ehea_port *port = netdev_priv(dev);
2462 if (port->state == EHEA_PORT_DOWN)
2463 return 0;
2465 ehea_drop_multicast_list(dev);
2466 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2468 ehea_free_interrupts(dev);
2470 port->state = EHEA_PORT_DOWN;
2472 ehea_update_bcmc_registrations();
2474 ret = ehea_clean_all_portres(port);
2475 if (ret)
2476 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2478 ehea_update_firmware_handles();
2480 return ret;
2483 static int ehea_stop(struct net_device *dev)
2485 int ret;
2486 struct ehea_port *port = netdev_priv(dev);
2488 netif_info(port, ifdown, dev, "disabling port\n");
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;
2502 static void ehea_purge_sq(struct ehea_qp *orig_qp)
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;
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;
2516 static void ehea_flush_sq(struct ehea_port *port)
2518 int i;
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;
2525 ret = wait_event_timeout(port->swqe_avail_wq,
2526 atomic_read(&pr->swqe_avail) >= swqe_max,
2527 msecs_to_jiffies(100));
2529 if (!ret) {
2530 pr_err("WARNING: sq not flushed completely\n");
2531 break;
2536 static int ehea_stop_qps(struct net_device *dev)
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;
2548 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2549 if (!cb0) {
2550 ret = -ENOMEM;
2551 goto out;
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;
2558 /* Purge send queue */
2559 ehea_purge_sq(qp);
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;
2570 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2571 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
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;
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;
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;
2598 ret = 0;
2599 out:
2600 free_page((unsigned long)cb0);
2602 return ret;
2605 static void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
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;
2616 int i;
2617 int index;
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);
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);
2638 static int ehea_restart_qps(struct net_device *dev)
2640 struct ehea_port *port = netdev_priv(dev);
2641 struct ehea_adapter *adapter = port->adapter;
2642 int ret = 0;
2643 int i;
2645 struct hcp_modify_qp_cb0 *cb0;
2646 u64 hret;
2647 u64 dummy64 = 0;
2648 u16 dummy16 = 0;
2650 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2651 if (!cb0) {
2652 ret = -ENOMEM;
2653 goto out;
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;
2660 ret = ehea_gen_smrs(pr);
2661 if (ret) {
2662 netdev_err(dev, "creation of shared memory regions failed\n");
2663 goto out;
2666 ehea_update_rqs(qp, pr);
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;
2677 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2678 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
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;
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;
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);
2702 out:
2703 free_page((unsigned long)cb0);
2705 return ret;
2708 static void ehea_reset_port(struct work_struct *work)
2710 int ret;
2711 struct ehea_port *port =
2712 container_of(work, struct ehea_port, reset_task);
2713 struct net_device *dev = port->netdev;
2715 mutex_lock(&dlpar_mem_lock);
2716 port->resets++;
2717 mutex_lock(&port->port_lock);
2718 netif_tx_disable(dev);
2720 port_napi_disable(port);
2722 ehea_down(dev);
2724 ret = ehea_up(dev);
2725 if (ret)
2726 goto out;
2728 ehea_set_multicast_list(dev);
2730 netif_info(port, timer, dev, "reset successful\n");
2732 port_napi_enable(port);
2734 netif_tx_wake_all_queues(dev);
2735 out:
2736 mutex_unlock(&port->port_lock);
2737 mutex_unlock(&dlpar_mem_lock);
2740 static void ehea_rereg_mrs(void)
2742 int ret, i;
2743 struct ehea_adapter *adapter;
2745 pr_info("LPAR memory changed - re-initializing driver\n");
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;
2754 if (!port)
2755 continue;
2757 dev = port->netdev;
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;
2768 port_napi_disable(port);
2769 mutex_unlock(&port->port_lock);
2771 reset_sq_restart_flag(port);
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;
2782 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
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;
2793 /* Restart all ports */
2794 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2795 struct ehea_port *port = adapter->port[i];
2797 if (port) {
2798 struct net_device *dev = port->netdev;
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");
2810 mutex_unlock(&port->port_lock);
2815 pr_info("re-initializing driver complete\n");
2816 out:
2817 return;
2820 static void ehea_tx_watchdog(struct net_device *dev)
2822 struct ehea_port *port = netdev_priv(dev);
2824 if (netif_carrier_ok(dev) &&
2825 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2826 ehea_schedule_port_reset(port);
2829 static int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2831 struct hcp_query_ehea *cb;
2832 u64 hret;
2833 int ret;
2835 cb = (void *)get_zeroed_page(GFP_KERNEL);
2836 if (!cb) {
2837 ret = -ENOMEM;
2838 goto out;
2841 hret = ehea_h_query_ehea(adapter->handle, cb);
2843 if (hret != H_SUCCESS) {
2844 ret = -EIO;
2845 goto out_herr;
2848 adapter->max_mc_mac = cb->max_mc_mac - 1;
2849 ret = 0;
2851 out_herr:
2852 free_page((unsigned long)cb);
2853 out:
2854 return ret;
2857 static int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2859 struct hcp_ehea_port_cb4 *cb4;
2860 u64 hret;
2861 int ret = 0;
2863 *jumbo = 0;
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;
2891 } else
2892 ret = -EINVAL;
2894 free_page((unsigned long)cb4);
2896 out:
2897 return ret;
2900 static ssize_t ehea_show_port_id(struct device *dev,
2901 struct device_attribute *attr, char *buf)
2903 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2904 return sprintf(buf, "%d", port->logical_port_id);
2907 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2908 NULL);
2910 static void __devinit logical_port_release(struct device *dev)
2912 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2913 of_node_put(port->ofdev.dev.of_node);
2916 static struct device *ehea_register_port(struct ehea_port *port,
2917 struct device_node *dn)
2919 int ret;
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;
2925 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
2926 port->ofdev.dev.release = logical_port_release;
2928 ret = of_device_register(&port->ofdev);
2929 if (ret) {
2930 pr_err("failed to register device. ret=%d\n", ret);
2931 goto out;
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;
2940 return &port->ofdev.dev;
2942 out_unreg_of_dev:
2943 of_device_unregister(&port->ofdev);
2944 out:
2945 return NULL;
2948 static void ehea_unregister_port(struct ehea_port *port)
2950 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2951 of_device_unregister(&port->ofdev);
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,
2971 static struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2972 u32 logical_port_id,
2973 struct device_node *dn)
2975 int ret;
2976 struct net_device *dev;
2977 struct ehea_port *port;
2978 struct device *port_dev;
2979 int jumbo;
2981 /* allocate memory for the port structures */
2982 dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
2984 if (!dev) {
2985 pr_err("no mem for net_device\n");
2986 ret = -ENOMEM;
2987 goto out_err;
2990 port = netdev_priv(dev);
2992 mutex_init(&port->port_lock);
2993 port->state = EHEA_PORT_DOWN;
2994 port->sig_comp_iv = sq_entries / 10;
2996 port->adapter = adapter;
2997 port->netdev = dev;
2998 port->logical_port_id = logical_port_id;
3000 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
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;
3008 INIT_LIST_HEAD(&port->mc_list->list);
3010 ret = ehea_sense_port_attr(port);
3011 if (ret)
3012 goto out_free_mc_list;
3014 netif_set_real_num_rx_queues(dev, port->num_def_qps);
3015 netif_set_real_num_tx_queues(dev, port->num_def_qps);
3017 port_dev = ehea_register_port(port, dn);
3018 if (!port_dev)
3019 goto out_free_mc_list;
3021 SET_NETDEV_DEV(dev, port_dev);
3023 /* initialize net_device structure */
3024 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3026 dev->netdev_ops = &ehea_netdev_ops;
3027 ehea_set_ethtool_ops(dev);
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;
3039 INIT_WORK(&port->reset_task, ehea_reset_port);
3040 INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3042 init_waitqueue_head(&port->swqe_avail_wq);
3043 init_waitqueue_head(&port->restart_wq);
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;
3052 ret = ehea_get_jumboframe_status(port, &jumbo);
3053 if (ret)
3054 netdev_err(dev, "failed determining jumbo frame status\n");
3056 netdev_info(dev, "Jumbo frames are %sabled\n",
3057 jumbo == 1 ? "en" : "dis");
3059 adapter->active_ports++;
3061 return port;
3063 out_unreg_port:
3064 ehea_unregister_port(port);
3066 out_free_mc_list:
3067 kfree(port->mc_list);
3069 out_free_ethdev:
3070 free_netdev(dev);
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;
3078 static void ehea_shutdown_single_port(struct ehea_port *port)
3080 struct ehea_adapter *adapter = port->adapter;
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--;
3091 static int ehea_setup_ports(struct ehea_adapter *adapter)
3093 struct device_node *lhea_dn;
3094 struct device_node *eth_dn = NULL;
3096 const u32 *dn_log_port_id;
3097 int i = 0;
3099 lhea_dn = adapter->ofdev->dev.of_node;
3100 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3102 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3103 NULL);
3104 if (!dn_log_port_id) {
3105 pr_err("bad device node: eth_dn name=%s\n",
3106 eth_dn->full_name);
3107 continue;
3110 if (ehea_add_adapter_mr(adapter)) {
3111 pr_err("creating MR failed\n");
3112 of_node_put(eth_dn);
3113 return -EIO;
3116 adapter->port[i] = ehea_setup_single_port(adapter,
3117 *dn_log_port_id,
3118 eth_dn);
3119 if (adapter->port[i])
3120 netdev_info(adapter->port[i]->netdev,
3121 "logical port id #%d\n", *dn_log_port_id);
3122 else
3123 ehea_remove_adapter_mr(adapter);
3125 i++;
3127 return 0;
3130 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3131 u32 logical_port_id)
3133 struct device_node *lhea_dn;
3134 struct device_node *eth_dn = NULL;
3135 const u32 *dn_log_port_id;
3137 lhea_dn = adapter->ofdev->dev.of_node;
3138 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3140 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3141 NULL);
3142 if (dn_log_port_id)
3143 if (*dn_log_port_id == logical_port_id)
3144 return eth_dn;
3147 return NULL;
3150 static ssize_t ehea_probe_port(struct device *dev,
3151 struct device_attribute *attr,
3152 const char *buf, size_t count)
3154 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3155 struct ehea_port *port;
3156 struct device_node *eth_dn = NULL;
3157 int i;
3159 u32 logical_port_id;
3161 sscanf(buf, "%d", &logical_port_id);
3163 port = ehea_get_port(adapter, logical_port_id);
3165 if (port) {
3166 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3167 logical_port_id);
3168 return -EINVAL;
3171 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3173 if (!eth_dn) {
3174 pr_info("no logical port with id %d found\n", logical_port_id);
3175 return -EINVAL;
3178 if (ehea_add_adapter_mr(adapter)) {
3179 pr_err("creating MR failed\n");
3180 return -EIO;
3183 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3185 of_node_put(eth_dn);
3187 if (port) {
3188 for (i = 0; i < EHEA_MAX_PORTS; i++)
3189 if (!adapter->port[i]) {
3190 adapter->port[i] = port;
3191 break;
3194 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3195 logical_port_id);
3196 } else {
3197 ehea_remove_adapter_mr(adapter);
3198 return -EIO;
3201 return (ssize_t) count;
3204 static ssize_t ehea_remove_port(struct device *dev,
3205 struct device_attribute *attr,
3206 const char *buf, size_t count)
3208 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3209 struct ehea_port *port;
3210 int i;
3211 u32 logical_port_id;
3213 sscanf(buf, "%d", &logical_port_id);
3215 port = ehea_get_port(adapter, logical_port_id);
3217 if (port) {
3218 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3219 logical_port_id);
3221 ehea_shutdown_single_port(port);
3223 for (i = 0; i < EHEA_MAX_PORTS; i++)
3224 if (adapter->port[i] == port) {
3225 adapter->port[i] = NULL;
3226 break;
3228 } else {
3229 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3230 logical_port_id);
3231 return -EINVAL;
3234 ehea_remove_adapter_mr(adapter);
3236 return (ssize_t) count;
3239 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3240 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3242 static int ehea_create_device_sysfs(struct platform_device *dev)
3244 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3245 if (ret)
3246 goto out;
3248 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3249 out:
3250 return ret;
3253 static void ehea_remove_device_sysfs(struct platform_device *dev)
3255 device_remove_file(&dev->dev, &dev_attr_probe_port);
3256 device_remove_file(&dev->dev, &dev_attr_remove_port);
3259 static int __devinit ehea_probe_adapter(struct platform_device *dev,
3260 const struct of_device_id *id)
3262 struct ehea_adapter *adapter;
3263 const u64 *adapter_handle;
3264 int ret;
3266 if (!dev || !dev->dev.of_node) {
3267 pr_err("Invalid ibmebus device probed\n");
3268 return -EINVAL;
3271 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3272 if (!adapter) {
3273 ret = -ENOMEM;
3274 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3275 goto out;
3278 list_add(&adapter->list, &adapter_list);
3280 adapter->ofdev = dev;
3282 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3283 NULL);
3284 if (adapter_handle)
3285 adapter->handle = *adapter_handle;
3287 if (!adapter->handle) {
3288 dev_err(&dev->dev, "failed getting handle for adapter"
3289 " '%s'\n", dev->dev.of_node->full_name);
3290 ret = -ENODEV;
3291 goto out_free_ad;
3294 adapter->pd = EHEA_PD_ID;
3296 dev_set_drvdata(&dev->dev, adapter);
3299 /* initialize adapter and ports */
3300 /* get adapter properties */
3301 ret = ehea_sense_adapter_attr(adapter);
3302 if (ret) {
3303 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3304 goto out_free_ad;
3307 adapter->neq = ehea_create_eq(adapter,
3308 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3309 if (!adapter->neq) {
3310 ret = -EIO;
3311 dev_err(&dev->dev, "NEQ creation failed\n");
3312 goto out_free_ad;
3315 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3316 (unsigned long)adapter);
3318 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3319 ehea_interrupt_neq, IRQF_DISABLED,
3320 "ehea_neq", adapter);
3321 if (ret) {
3322 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3323 goto out_kill_eq;
3326 ret = ehea_create_device_sysfs(dev);
3327 if (ret)
3328 goto out_free_irq;
3330 ret = ehea_setup_ports(adapter);
3331 if (ret) {
3332 dev_err(&dev->dev, "setup_ports failed\n");
3333 goto out_rem_dev_sysfs;
3336 ret = 0;
3337 goto out;
3339 out_rem_dev_sysfs:
3340 ehea_remove_device_sysfs(dev);
3342 out_free_irq:
3343 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3345 out_kill_eq:
3346 ehea_destroy_eq(adapter->neq);
3348 out_free_ad:
3349 list_del(&adapter->list);
3350 kfree(adapter);
3352 out:
3353 ehea_update_firmware_handles();
3355 return ret;
3358 static int __devexit ehea_remove(struct platform_device *dev)
3360 struct ehea_adapter *adapter = dev_get_drvdata(&dev->dev);
3361 int i;
3363 for (i = 0; i < EHEA_MAX_PORTS; i++)
3364 if (adapter->port[i]) {
3365 ehea_shutdown_single_port(adapter->port[i]);
3366 adapter->port[i] = NULL;
3369 ehea_remove_device_sysfs(dev);
3371 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3372 tasklet_kill(&adapter->neq_tasklet);
3374 ehea_destroy_eq(adapter->neq);
3375 ehea_remove_adapter_mr(adapter);
3376 list_del(&adapter->list);
3377 kfree(adapter);
3379 ehea_update_firmware_handles();
3381 return 0;
3384 static void ehea_crash_handler(void)
3386 int i;
3388 if (ehea_fw_handles.arr)
3389 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3390 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3391 ehea_fw_handles.arr[i].fwh,
3392 FORCE_FREE);
3394 if (ehea_bcmc_regs.arr)
3395 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3396 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3397 ehea_bcmc_regs.arr[i].port_id,
3398 ehea_bcmc_regs.arr[i].reg_type,
3399 ehea_bcmc_regs.arr[i].macaddr,
3400 0, H_DEREG_BCMC);
3403 static int ehea_mem_notifier(struct notifier_block *nb,
3404 unsigned long action, void *data)
3406 int ret = NOTIFY_BAD;
3407 struct memory_notify *arg = data;
3409 mutex_lock(&dlpar_mem_lock);
3411 switch (action) {
3412 case MEM_CANCEL_OFFLINE:
3413 pr_info("memory offlining canceled");
3414 /* Readd canceled memory block */
3415 case MEM_ONLINE:
3416 pr_info("memory is going online");
3417 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3418 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3419 goto out_unlock;
3420 ehea_rereg_mrs();
3421 break;
3422 case MEM_GOING_OFFLINE:
3423 pr_info("memory is going offline");
3424 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3425 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3426 goto out_unlock;
3427 ehea_rereg_mrs();
3428 break;
3429 default:
3430 break;
3433 ehea_update_firmware_handles();
3434 ret = NOTIFY_OK;
3436 out_unlock:
3437 mutex_unlock(&dlpar_mem_lock);
3438 return ret;
3441 static struct notifier_block ehea_mem_nb = {
3442 .notifier_call = ehea_mem_notifier,
3445 static int ehea_reboot_notifier(struct notifier_block *nb,
3446 unsigned long action, void *unused)
3448 if (action == SYS_RESTART) {
3449 pr_info("Reboot: freeing all eHEA resources\n");
3450 ibmebus_unregister_driver(&ehea_driver);
3452 return NOTIFY_DONE;
3455 static struct notifier_block ehea_reboot_nb = {
3456 .notifier_call = ehea_reboot_notifier,
3459 static int check_module_parm(void)
3461 int ret = 0;
3463 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3464 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3465 pr_info("Bad parameter: rq1_entries\n");
3466 ret = -EINVAL;
3468 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3469 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3470 pr_info("Bad parameter: rq2_entries\n");
3471 ret = -EINVAL;
3473 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3474 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3475 pr_info("Bad parameter: rq3_entries\n");
3476 ret = -EINVAL;
3478 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3479 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3480 pr_info("Bad parameter: sq_entries\n");
3481 ret = -EINVAL;
3484 return ret;
3487 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3488 char *buf)
3490 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3493 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3494 ehea_show_capabilities, NULL);
3496 static int __init ehea_module_init(void)
3498 int ret;
3500 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3502 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3503 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3505 mutex_init(&ehea_fw_handles.lock);
3506 spin_lock_init(&ehea_bcmc_regs.lock);
3508 ret = check_module_parm();
3509 if (ret)
3510 goto out;
3512 ret = ehea_create_busmap();
3513 if (ret)
3514 goto out;
3516 ret = register_reboot_notifier(&ehea_reboot_nb);
3517 if (ret)
3518 pr_info("failed registering reboot notifier\n");
3520 ret = register_memory_notifier(&ehea_mem_nb);
3521 if (ret)
3522 pr_info("failed registering memory remove notifier\n");
3524 ret = crash_shutdown_register(ehea_crash_handler);
3525 if (ret)
3526 pr_info("failed registering crash handler\n");
3528 ret = ibmebus_register_driver(&ehea_driver);
3529 if (ret) {
3530 pr_err("failed registering eHEA device driver on ebus\n");
3531 goto out2;
3534 ret = driver_create_file(&ehea_driver.driver,
3535 &driver_attr_capabilities);
3536 if (ret) {
3537 pr_err("failed to register capabilities attribute, ret=%d\n",
3538 ret);
3539 goto out3;
3542 return ret;
3544 out3:
3545 ibmebus_unregister_driver(&ehea_driver);
3546 out2:
3547 unregister_memory_notifier(&ehea_mem_nb);
3548 unregister_reboot_notifier(&ehea_reboot_nb);
3549 crash_shutdown_unregister(ehea_crash_handler);
3550 out:
3551 return ret;
3554 static void __exit ehea_module_exit(void)
3556 int ret;
3558 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3559 ibmebus_unregister_driver(&ehea_driver);
3560 unregister_reboot_notifier(&ehea_reboot_nb);
3561 ret = crash_shutdown_unregister(ehea_crash_handler);
3562 if (ret)
3563 pr_info("failed unregistering crash handler\n");
3564 unregister_memory_notifier(&ehea_mem_nb);
3565 kfree(ehea_fw_handles.arr);
3566 kfree(ehea_bcmc_regs.arr);
3567 ehea_destroy_busmap();
3570 module_init(ehea_module_init);
3571 module_exit(ehea_module_exit);