Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / net / ethernet / ibm / ehea / ehea_main.c
blob54efa9a5167b86021802b98900e6ff216fa10a73
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/device.h>
32 #include <linux/in.h>
33 #include <linux/ip.h>
34 #include <linux/tcp.h>
35 #include <linux/udp.h>
36 #include <linux/if.h>
37 #include <linux/list.h>
38 #include <linux/slab.h>
39 #include <linux/if_ether.h>
40 #include <linux/notifier.h>
41 #include <linux/reboot.h>
42 #include <linux/memory.h>
43 #include <asm/kexec.h>
44 #include <linux/mutex.h>
45 #include <linux/prefetch.h>
47 #include <net/ip.h>
49 #include "ehea.h"
50 #include "ehea_qmr.h"
51 #include "ehea_phyp.h"
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
56 MODULE_DESCRIPTION("IBM eServer HEA Driver");
57 MODULE_VERSION(DRV_VERSION);
60 static int msg_level = -1;
61 static int rq1_entries = EHEA_DEF_ENTRIES_RQ1;
62 static int rq2_entries = EHEA_DEF_ENTRIES_RQ2;
63 static int rq3_entries = EHEA_DEF_ENTRIES_RQ3;
64 static int sq_entries = EHEA_DEF_ENTRIES_SQ;
65 static int use_mcs = 1;
66 static int prop_carrier_state;
68 module_param(msg_level, int, 0);
69 module_param(rq1_entries, int, 0);
70 module_param(rq2_entries, int, 0);
71 module_param(rq3_entries, int, 0);
72 module_param(sq_entries, int, 0);
73 module_param(prop_carrier_state, int, 0);
74 module_param(use_mcs, int, 0);
76 MODULE_PARM_DESC(msg_level, "msg_level");
77 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
78 "port to stack. 1:yes, 0:no. Default = 0 ");
79 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
80 "[2^x - 1], x = [7..14]. Default = "
81 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
82 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
83 "[2^x - 1], x = [7..14]. Default = "
84 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
85 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
86 "[2^x - 1], x = [7..14]. Default = "
87 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
88 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue "
89 "[2^x - 1], x = [7..14]. Default = "
90 __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
91 MODULE_PARM_DESC(use_mcs, " Multiple receive queues, 1: enable, 0: disable, "
92 "Default = 1");
94 static int port_name_cnt;
95 static LIST_HEAD(adapter_list);
96 static unsigned long ehea_driver_flags;
97 static DEFINE_MUTEX(dlpar_mem_lock);
98 static struct ehea_fw_handle_array ehea_fw_handles;
99 static struct ehea_bcmc_reg_array ehea_bcmc_regs;
102 static int ehea_probe_adapter(struct platform_device *dev);
104 static int ehea_remove(struct platform_device *dev);
106 static const struct of_device_id ehea_module_device_table[] = {
108 .name = "lhea",
109 .compatible = "IBM,lhea",
112 .type = "network",
113 .compatible = "IBM,lhea-ethernet",
117 MODULE_DEVICE_TABLE(of, ehea_module_device_table);
119 static const struct of_device_id ehea_device_table[] = {
121 .name = "lhea",
122 .compatible = "IBM,lhea",
127 static struct platform_driver ehea_driver = {
128 .driver = {
129 .name = "ehea",
130 .owner = THIS_MODULE,
131 .of_match_table = ehea_device_table,
133 .probe = ehea_probe_adapter,
134 .remove = ehea_remove,
137 void ehea_dump(void *adr, int len, char *msg)
139 int x;
140 unsigned char *deb = adr;
141 for (x = 0; x < len; x += 16) {
142 pr_info("%s adr=%p ofs=%04x %016llx %016llx\n",
143 msg, deb, x, *((u64 *)&deb[0]), *((u64 *)&deb[8]));
144 deb += 16;
148 static void ehea_schedule_port_reset(struct ehea_port *port)
150 if (!test_bit(__EHEA_DISABLE_PORT_RESET, &port->flags))
151 schedule_work(&port->reset_task);
154 static void ehea_update_firmware_handles(void)
156 struct ehea_fw_handle_entry *arr = NULL;
157 struct ehea_adapter *adapter;
158 int num_adapters = 0;
159 int num_ports = 0;
160 int num_portres = 0;
161 int i = 0;
162 int num_fw_handles, k, l;
164 /* Determine number of handles */
165 mutex_lock(&ehea_fw_handles.lock);
167 list_for_each_entry(adapter, &adapter_list, list) {
168 num_adapters++;
170 for (k = 0; k < EHEA_MAX_PORTS; k++) {
171 struct ehea_port *port = adapter->port[k];
173 if (!port || (port->state != EHEA_PORT_UP))
174 continue;
176 num_ports++;
177 num_portres += port->num_def_qps;
181 num_fw_handles = num_adapters * EHEA_NUM_ADAPTER_FW_HANDLES +
182 num_ports * EHEA_NUM_PORT_FW_HANDLES +
183 num_portres * EHEA_NUM_PORTRES_FW_HANDLES;
185 if (num_fw_handles) {
186 arr = kcalloc(num_fw_handles, sizeof(*arr), GFP_KERNEL);
187 if (!arr)
188 goto out; /* Keep the existing array */
189 } else
190 goto out_update;
192 list_for_each_entry(adapter, &adapter_list, list) {
193 if (num_adapters == 0)
194 break;
196 for (k = 0; k < EHEA_MAX_PORTS; k++) {
197 struct ehea_port *port = adapter->port[k];
199 if (!port || (port->state != EHEA_PORT_UP) ||
200 (num_ports == 0))
201 continue;
203 for (l = 0; l < port->num_def_qps; l++) {
204 struct ehea_port_res *pr = &port->port_res[l];
206 arr[i].adh = adapter->handle;
207 arr[i++].fwh = pr->qp->fw_handle;
208 arr[i].adh = adapter->handle;
209 arr[i++].fwh = pr->send_cq->fw_handle;
210 arr[i].adh = adapter->handle;
211 arr[i++].fwh = pr->recv_cq->fw_handle;
212 arr[i].adh = adapter->handle;
213 arr[i++].fwh = pr->eq->fw_handle;
214 arr[i].adh = adapter->handle;
215 arr[i++].fwh = pr->send_mr.handle;
216 arr[i].adh = adapter->handle;
217 arr[i++].fwh = pr->recv_mr.handle;
219 arr[i].adh = adapter->handle;
220 arr[i++].fwh = port->qp_eq->fw_handle;
221 num_ports--;
224 arr[i].adh = adapter->handle;
225 arr[i++].fwh = adapter->neq->fw_handle;
227 if (adapter->mr.handle) {
228 arr[i].adh = adapter->handle;
229 arr[i++].fwh = adapter->mr.handle;
231 num_adapters--;
234 out_update:
235 kfree(ehea_fw_handles.arr);
236 ehea_fw_handles.arr = arr;
237 ehea_fw_handles.num_entries = i;
238 out:
239 mutex_unlock(&ehea_fw_handles.lock);
242 static void ehea_update_bcmc_registrations(void)
244 unsigned long flags;
245 struct ehea_bcmc_reg_entry *arr = NULL;
246 struct ehea_adapter *adapter;
247 struct ehea_mc_list *mc_entry;
248 int num_registrations = 0;
249 int i = 0;
250 int k;
252 spin_lock_irqsave(&ehea_bcmc_regs.lock, flags);
254 /* Determine number of registrations */
255 list_for_each_entry(adapter, &adapter_list, list)
256 for (k = 0; k < EHEA_MAX_PORTS; k++) {
257 struct ehea_port *port = adapter->port[k];
259 if (!port || (port->state != EHEA_PORT_UP))
260 continue;
262 num_registrations += 2; /* Broadcast registrations */
264 list_for_each_entry(mc_entry, &port->mc_list->list,list)
265 num_registrations += 2;
268 if (num_registrations) {
269 arr = kcalloc(num_registrations, sizeof(*arr), GFP_ATOMIC);
270 if (!arr)
271 goto out; /* Keep the existing array */
272 } else
273 goto out_update;
275 list_for_each_entry(adapter, &adapter_list, list) {
276 for (k = 0; k < EHEA_MAX_PORTS; k++) {
277 struct ehea_port *port = adapter->port[k];
279 if (!port || (port->state != EHEA_PORT_UP))
280 continue;
282 if (num_registrations == 0)
283 goto out_update;
285 arr[i].adh = adapter->handle;
286 arr[i].port_id = port->logical_port_id;
287 arr[i].reg_type = EHEA_BCMC_BROADCAST |
288 EHEA_BCMC_UNTAGGED;
289 arr[i++].macaddr = port->mac_addr;
291 arr[i].adh = adapter->handle;
292 arr[i].port_id = port->logical_port_id;
293 arr[i].reg_type = EHEA_BCMC_BROADCAST |
294 EHEA_BCMC_VLANID_ALL;
295 arr[i++].macaddr = port->mac_addr;
296 num_registrations -= 2;
298 list_for_each_entry(mc_entry,
299 &port->mc_list->list, list) {
300 if (num_registrations == 0)
301 goto out_update;
303 arr[i].adh = adapter->handle;
304 arr[i].port_id = port->logical_port_id;
305 arr[i].reg_type = EHEA_BCMC_MULTICAST |
306 EHEA_BCMC_UNTAGGED;
307 if (mc_entry->macaddr == 0)
308 arr[i].reg_type |= EHEA_BCMC_SCOPE_ALL;
309 arr[i++].macaddr = mc_entry->macaddr;
311 arr[i].adh = adapter->handle;
312 arr[i].port_id = port->logical_port_id;
313 arr[i].reg_type = EHEA_BCMC_MULTICAST |
314 EHEA_BCMC_VLANID_ALL;
315 if (mc_entry->macaddr == 0)
316 arr[i].reg_type |= EHEA_BCMC_SCOPE_ALL;
317 arr[i++].macaddr = mc_entry->macaddr;
318 num_registrations -= 2;
323 out_update:
324 kfree(ehea_bcmc_regs.arr);
325 ehea_bcmc_regs.arr = arr;
326 ehea_bcmc_regs.num_entries = i;
327 out:
328 spin_unlock_irqrestore(&ehea_bcmc_regs.lock, flags);
331 static struct rtnl_link_stats64 *ehea_get_stats64(struct net_device *dev,
332 struct rtnl_link_stats64 *stats)
334 struct ehea_port *port = netdev_priv(dev);
335 u64 rx_packets = 0, tx_packets = 0, rx_bytes = 0, tx_bytes = 0;
336 int i;
338 for (i = 0; i < port->num_def_qps; i++) {
339 rx_packets += port->port_res[i].rx_packets;
340 rx_bytes += port->port_res[i].rx_bytes;
343 for (i = 0; i < port->num_def_qps; i++) {
344 tx_packets += port->port_res[i].tx_packets;
345 tx_bytes += port->port_res[i].tx_bytes;
348 stats->tx_packets = tx_packets;
349 stats->rx_bytes = rx_bytes;
350 stats->tx_bytes = tx_bytes;
351 stats->rx_packets = rx_packets;
353 stats->multicast = port->stats.multicast;
354 stats->rx_errors = port->stats.rx_errors;
355 return stats;
358 static void ehea_update_stats(struct work_struct *work)
360 struct ehea_port *port =
361 container_of(work, struct ehea_port, stats_work.work);
362 struct net_device *dev = port->netdev;
363 struct rtnl_link_stats64 *stats = &port->stats;
364 struct hcp_ehea_port_cb2 *cb2;
365 u64 hret;
367 cb2 = (void *)get_zeroed_page(GFP_KERNEL);
368 if (!cb2) {
369 netdev_err(dev, "No mem for cb2. Some interface statistics were not updated\n");
370 goto resched;
373 hret = ehea_h_query_ehea_port(port->adapter->handle,
374 port->logical_port_id,
375 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
376 if (hret != H_SUCCESS) {
377 netdev_err(dev, "query_ehea_port failed\n");
378 goto out_herr;
381 if (netif_msg_hw(port))
382 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
384 stats->multicast = cb2->rxmcp;
385 stats->rx_errors = cb2->rxuerr;
387 out_herr:
388 free_page((unsigned long)cb2);
389 resched:
390 schedule_delayed_work(&port->stats_work,
391 round_jiffies_relative(msecs_to_jiffies(1000)));
394 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
396 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
397 struct net_device *dev = pr->port->netdev;
398 int max_index_mask = pr->rq1_skba.len - 1;
399 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
400 int adder = 0;
401 int i;
403 pr->rq1_skba.os_skbs = 0;
405 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
406 if (nr_of_wqes > 0)
407 pr->rq1_skba.index = index;
408 pr->rq1_skba.os_skbs = fill_wqes;
409 return;
412 for (i = 0; i < fill_wqes; i++) {
413 if (!skb_arr_rq1[index]) {
414 skb_arr_rq1[index] = netdev_alloc_skb(dev,
415 EHEA_L_PKT_SIZE);
416 if (!skb_arr_rq1[index]) {
417 pr->rq1_skba.os_skbs = fill_wqes - i;
418 break;
421 index--;
422 index &= max_index_mask;
423 adder++;
426 if (adder == 0)
427 return;
429 /* Ring doorbell */
430 ehea_update_rq1a(pr->qp, adder);
433 static void ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
435 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
436 struct net_device *dev = pr->port->netdev;
437 int i;
439 if (nr_rq1a > pr->rq1_skba.len) {
440 netdev_err(dev, "NR_RQ1A bigger than skb array len\n");
441 return;
444 for (i = 0; i < nr_rq1a; i++) {
445 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
446 if (!skb_arr_rq1[i])
447 break;
449 /* Ring doorbell */
450 ehea_update_rq1a(pr->qp, i - 1);
453 static int ehea_refill_rq_def(struct ehea_port_res *pr,
454 struct ehea_q_skb_arr *q_skba, int rq_nr,
455 int num_wqes, int wqe_type, int packet_size)
457 struct net_device *dev = pr->port->netdev;
458 struct ehea_qp *qp = pr->qp;
459 struct sk_buff **skb_arr = q_skba->arr;
460 struct ehea_rwqe *rwqe;
461 int i, index, max_index_mask, fill_wqes;
462 int adder = 0;
463 int ret = 0;
465 fill_wqes = q_skba->os_skbs + num_wqes;
466 q_skba->os_skbs = 0;
468 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
469 q_skba->os_skbs = fill_wqes;
470 return ret;
473 index = q_skba->index;
474 max_index_mask = q_skba->len - 1;
475 for (i = 0; i < fill_wqes; i++) {
476 u64 tmp_addr;
477 struct sk_buff *skb;
479 skb = netdev_alloc_skb_ip_align(dev, packet_size);
480 if (!skb) {
481 q_skba->os_skbs = fill_wqes - i;
482 if (q_skba->os_skbs == q_skba->len - 2) {
483 netdev_info(pr->port->netdev,
484 "rq%i ran dry - no mem for skb\n",
485 rq_nr);
486 ret = -ENOMEM;
488 break;
491 skb_arr[index] = skb;
492 tmp_addr = ehea_map_vaddr(skb->data);
493 if (tmp_addr == -1) {
494 dev_consume_skb_any(skb);
495 q_skba->os_skbs = fill_wqes - i;
496 ret = 0;
497 break;
500 rwqe = ehea_get_next_rwqe(qp, rq_nr);
501 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
502 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
503 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
504 rwqe->sg_list[0].vaddr = tmp_addr;
505 rwqe->sg_list[0].len = packet_size;
506 rwqe->data_segments = 1;
508 index++;
509 index &= max_index_mask;
510 adder++;
513 q_skba->index = index;
514 if (adder == 0)
515 goto out;
517 /* Ring doorbell */
518 iosync();
519 if (rq_nr == 2)
520 ehea_update_rq2a(pr->qp, adder);
521 else
522 ehea_update_rq3a(pr->qp, adder);
523 out:
524 return ret;
528 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
530 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
531 nr_of_wqes, EHEA_RWQE2_TYPE,
532 EHEA_RQ2_PKT_SIZE);
536 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
538 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
539 nr_of_wqes, EHEA_RWQE3_TYPE,
540 EHEA_MAX_PACKET_SIZE);
543 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
545 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
546 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
547 return 0;
548 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
549 (cqe->header_length == 0))
550 return 0;
551 return -EINVAL;
554 static inline void ehea_fill_skb(struct net_device *dev,
555 struct sk_buff *skb, struct ehea_cqe *cqe,
556 struct ehea_port_res *pr)
558 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
560 skb_put(skb, length);
561 skb->protocol = eth_type_trans(skb, dev);
563 /* The packet was not an IPV4 packet so a complemented checksum was
564 calculated. The value is found in the Internet Checksum field. */
565 if (cqe->status & EHEA_CQE_BLIND_CKSUM) {
566 skb->ip_summed = CHECKSUM_COMPLETE;
567 skb->csum = csum_unfold(~cqe->inet_checksum_value);
568 } else
569 skb->ip_summed = CHECKSUM_UNNECESSARY;
571 skb_record_rx_queue(skb, pr - &pr->port->port_res[0]);
574 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
575 int arr_len,
576 struct ehea_cqe *cqe)
578 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
579 struct sk_buff *skb;
580 void *pref;
581 int x;
583 x = skb_index + 1;
584 x &= (arr_len - 1);
586 pref = skb_array[x];
587 if (pref) {
588 prefetchw(pref);
589 prefetchw(pref + EHEA_CACHE_LINE);
591 pref = (skb_array[x]->data);
592 prefetch(pref);
593 prefetch(pref + EHEA_CACHE_LINE);
594 prefetch(pref + EHEA_CACHE_LINE * 2);
595 prefetch(pref + EHEA_CACHE_LINE * 3);
598 skb = skb_array[skb_index];
599 skb_array[skb_index] = NULL;
600 return skb;
603 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
604 int arr_len, int wqe_index)
606 struct sk_buff *skb;
607 void *pref;
608 int x;
610 x = wqe_index + 1;
611 x &= (arr_len - 1);
613 pref = skb_array[x];
614 if (pref) {
615 prefetchw(pref);
616 prefetchw(pref + EHEA_CACHE_LINE);
618 pref = (skb_array[x]->data);
619 prefetchw(pref);
620 prefetchw(pref + EHEA_CACHE_LINE);
623 skb = skb_array[wqe_index];
624 skb_array[wqe_index] = NULL;
625 return skb;
628 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
629 struct ehea_cqe *cqe, int *processed_rq2,
630 int *processed_rq3)
632 struct sk_buff *skb;
634 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
635 pr->p_stats.err_tcp_cksum++;
636 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
637 pr->p_stats.err_ip_cksum++;
638 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
639 pr->p_stats.err_frame_crc++;
641 if (rq == 2) {
642 *processed_rq2 += 1;
643 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
644 dev_kfree_skb(skb);
645 } else if (rq == 3) {
646 *processed_rq3 += 1;
647 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
648 dev_kfree_skb(skb);
651 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
652 if (netif_msg_rx_err(pr->port)) {
653 pr_err("Critical receive error for QP %d. Resetting port.\n",
654 pr->qp->init_attr.qp_nr);
655 ehea_dump(cqe, sizeof(*cqe), "CQE");
657 ehea_schedule_port_reset(pr->port);
658 return 1;
661 return 0;
664 static int ehea_proc_rwqes(struct net_device *dev,
665 struct ehea_port_res *pr,
666 int budget)
668 struct ehea_port *port = pr->port;
669 struct ehea_qp *qp = pr->qp;
670 struct ehea_cqe *cqe;
671 struct sk_buff *skb;
672 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
673 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
674 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
675 int skb_arr_rq1_len = pr->rq1_skba.len;
676 int skb_arr_rq2_len = pr->rq2_skba.len;
677 int skb_arr_rq3_len = pr->rq3_skba.len;
678 int processed, processed_rq1, processed_rq2, processed_rq3;
679 u64 processed_bytes = 0;
680 int wqe_index, last_wqe_index, rq, port_reset;
682 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
683 last_wqe_index = 0;
685 cqe = ehea_poll_rq1(qp, &wqe_index);
686 while ((processed < budget) && cqe) {
687 ehea_inc_rq1(qp);
688 processed_rq1++;
689 processed++;
690 if (netif_msg_rx_status(port))
691 ehea_dump(cqe, sizeof(*cqe), "CQE");
693 last_wqe_index = wqe_index;
694 rmb();
695 if (!ehea_check_cqe(cqe, &rq)) {
696 if (rq == 1) {
697 /* LL RQ1 */
698 skb = get_skb_by_index_ll(skb_arr_rq1,
699 skb_arr_rq1_len,
700 wqe_index);
701 if (unlikely(!skb)) {
702 netif_info(port, rx_err, dev,
703 "LL rq1: skb=NULL\n");
705 skb = netdev_alloc_skb(dev,
706 EHEA_L_PKT_SIZE);
707 if (!skb)
708 break;
710 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
711 cqe->num_bytes_transfered - 4);
712 ehea_fill_skb(dev, skb, cqe, pr);
713 } else if (rq == 2) {
714 /* RQ2 */
715 skb = get_skb_by_index(skb_arr_rq2,
716 skb_arr_rq2_len, cqe);
717 if (unlikely(!skb)) {
718 netif_err(port, rx_err, dev,
719 "rq2: skb=NULL\n");
720 break;
722 ehea_fill_skb(dev, skb, cqe, pr);
723 processed_rq2++;
724 } else {
725 /* RQ3 */
726 skb = get_skb_by_index(skb_arr_rq3,
727 skb_arr_rq3_len, cqe);
728 if (unlikely(!skb)) {
729 netif_err(port, rx_err, dev,
730 "rq3: skb=NULL\n");
731 break;
733 ehea_fill_skb(dev, skb, cqe, pr);
734 processed_rq3++;
737 processed_bytes += skb->len;
739 if (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
740 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
741 cqe->vlan_tag);
743 napi_gro_receive(&pr->napi, skb);
744 } else {
745 pr->p_stats.poll_receive_errors++;
746 port_reset = ehea_treat_poll_error(pr, rq, cqe,
747 &processed_rq2,
748 &processed_rq3);
749 if (port_reset)
750 break;
752 cqe = ehea_poll_rq1(qp, &wqe_index);
755 pr->rx_packets += processed;
756 pr->rx_bytes += processed_bytes;
758 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
759 ehea_refill_rq2(pr, processed_rq2);
760 ehea_refill_rq3(pr, processed_rq3);
762 return processed;
765 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
767 static void reset_sq_restart_flag(struct ehea_port *port)
769 int i;
771 for (i = 0; i < port->num_def_qps; i++) {
772 struct ehea_port_res *pr = &port->port_res[i];
773 pr->sq_restart_flag = 0;
775 wake_up(&port->restart_wq);
778 static void check_sqs(struct ehea_port *port)
780 struct ehea_swqe *swqe;
781 int swqe_index;
782 int i, k;
784 for (i = 0; i < port->num_def_qps; i++) {
785 struct ehea_port_res *pr = &port->port_res[i];
786 int ret;
787 k = 0;
788 swqe = ehea_get_swqe(pr->qp, &swqe_index);
789 memset(swqe, 0, SWQE_HEADER_SIZE);
790 atomic_dec(&pr->swqe_avail);
792 swqe->tx_control |= EHEA_SWQE_PURGE;
793 swqe->wr_id = SWQE_RESTART_CHECK;
794 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
795 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
796 swqe->immediate_data_length = 80;
798 ehea_post_swqe(pr->qp, swqe);
800 ret = wait_event_timeout(port->restart_wq,
801 pr->sq_restart_flag == 0,
802 msecs_to_jiffies(100));
804 if (!ret) {
805 pr_err("HW/SW queues out of sync\n");
806 ehea_schedule_port_reset(pr->port);
807 return;
813 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
815 struct sk_buff *skb;
816 struct ehea_cq *send_cq = pr->send_cq;
817 struct ehea_cqe *cqe;
818 int quota = my_quota;
819 int cqe_counter = 0;
820 int swqe_av = 0;
821 int index;
822 struct netdev_queue *txq = netdev_get_tx_queue(pr->port->netdev,
823 pr - &pr->port->port_res[0]);
825 cqe = ehea_poll_cq(send_cq);
826 while (cqe && (quota > 0)) {
827 ehea_inc_cq(send_cq);
829 cqe_counter++;
830 rmb();
832 if (cqe->wr_id == SWQE_RESTART_CHECK) {
833 pr->sq_restart_flag = 1;
834 swqe_av++;
835 break;
838 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
839 pr_err("Bad send completion status=0x%04X\n",
840 cqe->status);
842 if (netif_msg_tx_err(pr->port))
843 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
845 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
846 pr_err("Resetting port\n");
847 ehea_schedule_port_reset(pr->port);
848 break;
852 if (netif_msg_tx_done(pr->port))
853 ehea_dump(cqe, sizeof(*cqe), "CQE");
855 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
856 == EHEA_SWQE2_TYPE)) {
858 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
859 skb = pr->sq_skba.arr[index];
860 dev_consume_skb_any(skb);
861 pr->sq_skba.arr[index] = NULL;
864 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
865 quota--;
867 cqe = ehea_poll_cq(send_cq);
870 ehea_update_feca(send_cq, cqe_counter);
871 atomic_add(swqe_av, &pr->swqe_avail);
873 if (unlikely(netif_tx_queue_stopped(txq) &&
874 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))) {
875 __netif_tx_lock(txq, smp_processor_id());
876 if (netif_tx_queue_stopped(txq) &&
877 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))
878 netif_tx_wake_queue(txq);
879 __netif_tx_unlock(txq);
882 wake_up(&pr->port->swqe_avail_wq);
884 return cqe;
887 #define EHEA_POLL_MAX_CQES 65535
889 static int ehea_poll(struct napi_struct *napi, int budget)
891 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
892 napi);
893 struct net_device *dev = pr->port->netdev;
894 struct ehea_cqe *cqe;
895 struct ehea_cqe *cqe_skb = NULL;
896 int wqe_index;
897 int rx = 0;
899 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
900 rx += ehea_proc_rwqes(dev, pr, budget - rx);
902 while (rx != budget) {
903 napi_complete(napi);
904 ehea_reset_cq_ep(pr->recv_cq);
905 ehea_reset_cq_ep(pr->send_cq);
906 ehea_reset_cq_n1(pr->recv_cq);
907 ehea_reset_cq_n1(pr->send_cq);
908 rmb();
909 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
910 cqe_skb = ehea_poll_cq(pr->send_cq);
912 if (!cqe && !cqe_skb)
913 return rx;
915 if (!napi_reschedule(napi))
916 return rx;
918 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
919 rx += ehea_proc_rwqes(dev, pr, budget - rx);
922 return rx;
925 #ifdef CONFIG_NET_POLL_CONTROLLER
926 static void ehea_netpoll(struct net_device *dev)
928 struct ehea_port *port = netdev_priv(dev);
929 int i;
931 for (i = 0; i < port->num_def_qps; i++)
932 napi_schedule(&port->port_res[i].napi);
934 #endif
936 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
938 struct ehea_port_res *pr = param;
940 napi_schedule(&pr->napi);
942 return IRQ_HANDLED;
945 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
947 struct ehea_port *port = param;
948 struct ehea_eqe *eqe;
949 struct ehea_qp *qp;
950 u32 qp_token;
951 u64 resource_type, aer, aerr;
952 int reset_port = 0;
954 eqe = ehea_poll_eq(port->qp_eq);
956 while (eqe) {
957 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
958 pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
959 eqe->entry, qp_token);
961 qp = port->port_res[qp_token].qp;
963 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
964 &aer, &aerr);
966 if (resource_type == EHEA_AER_RESTYPE_QP) {
967 if ((aer & EHEA_AER_RESET_MASK) ||
968 (aerr & EHEA_AERR_RESET_MASK))
969 reset_port = 1;
970 } else
971 reset_port = 1; /* Reset in case of CQ or EQ error */
973 eqe = ehea_poll_eq(port->qp_eq);
976 if (reset_port) {
977 pr_err("Resetting port\n");
978 ehea_schedule_port_reset(port);
981 return IRQ_HANDLED;
984 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
985 int logical_port)
987 int i;
989 for (i = 0; i < EHEA_MAX_PORTS; i++)
990 if (adapter->port[i])
991 if (adapter->port[i]->logical_port_id == logical_port)
992 return adapter->port[i];
993 return NULL;
996 int ehea_sense_port_attr(struct ehea_port *port)
998 int ret;
999 u64 hret;
1000 struct hcp_ehea_port_cb0 *cb0;
1002 /* may be called via ehea_neq_tasklet() */
1003 cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
1004 if (!cb0) {
1005 pr_err("no mem for cb0\n");
1006 ret = -ENOMEM;
1007 goto out;
1010 hret = ehea_h_query_ehea_port(port->adapter->handle,
1011 port->logical_port_id, H_PORT_CB0,
1012 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
1013 cb0);
1014 if (hret != H_SUCCESS) {
1015 ret = -EIO;
1016 goto out_free;
1019 /* MAC address */
1020 port->mac_addr = cb0->port_mac_addr << 16;
1022 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1023 ret = -EADDRNOTAVAIL;
1024 goto out_free;
1027 /* Port speed */
1028 switch (cb0->port_speed) {
1029 case H_SPEED_10M_H:
1030 port->port_speed = EHEA_SPEED_10M;
1031 port->full_duplex = 0;
1032 break;
1033 case H_SPEED_10M_F:
1034 port->port_speed = EHEA_SPEED_10M;
1035 port->full_duplex = 1;
1036 break;
1037 case H_SPEED_100M_H:
1038 port->port_speed = EHEA_SPEED_100M;
1039 port->full_duplex = 0;
1040 break;
1041 case H_SPEED_100M_F:
1042 port->port_speed = EHEA_SPEED_100M;
1043 port->full_duplex = 1;
1044 break;
1045 case H_SPEED_1G_F:
1046 port->port_speed = EHEA_SPEED_1G;
1047 port->full_duplex = 1;
1048 break;
1049 case H_SPEED_10G_F:
1050 port->port_speed = EHEA_SPEED_10G;
1051 port->full_duplex = 1;
1052 break;
1053 default:
1054 port->port_speed = 0;
1055 port->full_duplex = 0;
1056 break;
1059 port->autoneg = 1;
1060 port->num_mcs = cb0->num_default_qps;
1062 /* Number of default QPs */
1063 if (use_mcs)
1064 port->num_def_qps = cb0->num_default_qps;
1065 else
1066 port->num_def_qps = 1;
1068 if (!port->num_def_qps) {
1069 ret = -EINVAL;
1070 goto out_free;
1073 ret = 0;
1074 out_free:
1075 if (ret || netif_msg_probe(port))
1076 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1077 free_page((unsigned long)cb0);
1078 out:
1079 return ret;
1082 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1084 struct hcp_ehea_port_cb4 *cb4;
1085 u64 hret;
1086 int ret = 0;
1088 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1089 if (!cb4) {
1090 pr_err("no mem for cb4\n");
1091 ret = -ENOMEM;
1092 goto out;
1095 cb4->port_speed = port_speed;
1097 netif_carrier_off(port->netdev);
1099 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1100 port->logical_port_id,
1101 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1102 if (hret == H_SUCCESS) {
1103 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1105 hret = ehea_h_query_ehea_port(port->adapter->handle,
1106 port->logical_port_id,
1107 H_PORT_CB4, H_PORT_CB4_SPEED,
1108 cb4);
1109 if (hret == H_SUCCESS) {
1110 switch (cb4->port_speed) {
1111 case H_SPEED_10M_H:
1112 port->port_speed = EHEA_SPEED_10M;
1113 port->full_duplex = 0;
1114 break;
1115 case H_SPEED_10M_F:
1116 port->port_speed = EHEA_SPEED_10M;
1117 port->full_duplex = 1;
1118 break;
1119 case H_SPEED_100M_H:
1120 port->port_speed = EHEA_SPEED_100M;
1121 port->full_duplex = 0;
1122 break;
1123 case H_SPEED_100M_F:
1124 port->port_speed = EHEA_SPEED_100M;
1125 port->full_duplex = 1;
1126 break;
1127 case H_SPEED_1G_F:
1128 port->port_speed = EHEA_SPEED_1G;
1129 port->full_duplex = 1;
1130 break;
1131 case H_SPEED_10G_F:
1132 port->port_speed = EHEA_SPEED_10G;
1133 port->full_duplex = 1;
1134 break;
1135 default:
1136 port->port_speed = 0;
1137 port->full_duplex = 0;
1138 break;
1140 } else {
1141 pr_err("Failed sensing port speed\n");
1142 ret = -EIO;
1144 } else {
1145 if (hret == H_AUTHORITY) {
1146 pr_info("Hypervisor denied setting port speed\n");
1147 ret = -EPERM;
1148 } else {
1149 ret = -EIO;
1150 pr_err("Failed setting port speed\n");
1153 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1154 netif_carrier_on(port->netdev);
1156 free_page((unsigned long)cb4);
1157 out:
1158 return ret;
1161 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1163 int ret;
1164 u8 ec;
1165 u8 portnum;
1166 struct ehea_port *port;
1167 struct net_device *dev;
1169 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1170 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1171 port = ehea_get_port(adapter, portnum);
1172 if (!port) {
1173 netdev_err(NULL, "unknown portnum %x\n", portnum);
1174 return;
1176 dev = port->netdev;
1178 switch (ec) {
1179 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1181 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1182 if (!netif_carrier_ok(dev)) {
1183 ret = ehea_sense_port_attr(port);
1184 if (ret) {
1185 netdev_err(dev, "failed resensing port attributes\n");
1186 break;
1189 netif_info(port, link, dev,
1190 "Logical port up: %dMbps %s Duplex\n",
1191 port->port_speed,
1192 port->full_duplex == 1 ?
1193 "Full" : "Half");
1195 netif_carrier_on(dev);
1196 netif_wake_queue(dev);
1198 } else
1199 if (netif_carrier_ok(dev)) {
1200 netif_info(port, link, dev,
1201 "Logical port down\n");
1202 netif_carrier_off(dev);
1203 netif_tx_disable(dev);
1206 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1207 port->phy_link = EHEA_PHY_LINK_UP;
1208 netif_info(port, link, dev,
1209 "Physical port up\n");
1210 if (prop_carrier_state)
1211 netif_carrier_on(dev);
1212 } else {
1213 port->phy_link = EHEA_PHY_LINK_DOWN;
1214 netif_info(port, link, dev,
1215 "Physical port down\n");
1216 if (prop_carrier_state)
1217 netif_carrier_off(dev);
1220 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1221 netdev_info(dev,
1222 "External switch port is primary port\n");
1223 else
1224 netdev_info(dev,
1225 "External switch port is backup port\n");
1227 break;
1228 case EHEA_EC_ADAPTER_MALFUNC:
1229 netdev_err(dev, "Adapter malfunction\n");
1230 break;
1231 case EHEA_EC_PORT_MALFUNC:
1232 netdev_info(dev, "Port malfunction\n");
1233 netif_carrier_off(dev);
1234 netif_tx_disable(dev);
1235 break;
1236 default:
1237 netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1238 break;
1242 static void ehea_neq_tasklet(unsigned long data)
1244 struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1245 struct ehea_eqe *eqe;
1246 u64 event_mask;
1248 eqe = ehea_poll_eq(adapter->neq);
1249 pr_debug("eqe=%p\n", eqe);
1251 while (eqe) {
1252 pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1253 ehea_parse_eqe(adapter, eqe->entry);
1254 eqe = ehea_poll_eq(adapter->neq);
1255 pr_debug("next eqe=%p\n", eqe);
1258 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1259 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1260 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1262 ehea_h_reset_events(adapter->handle,
1263 adapter->neq->fw_handle, event_mask);
1266 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1268 struct ehea_adapter *adapter = param;
1269 tasklet_hi_schedule(&adapter->neq_tasklet);
1270 return IRQ_HANDLED;
1274 static int ehea_fill_port_res(struct ehea_port_res *pr)
1276 int ret;
1277 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1279 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1281 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1283 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1285 return ret;
1288 static int ehea_reg_interrupts(struct net_device *dev)
1290 struct ehea_port *port = netdev_priv(dev);
1291 struct ehea_port_res *pr;
1292 int i, ret;
1295 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1296 dev->name);
1298 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1299 ehea_qp_aff_irq_handler,
1300 0, port->int_aff_name, port);
1301 if (ret) {
1302 netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1303 port->qp_eq->attr.ist1);
1304 goto out_free_qpeq;
1307 netif_info(port, ifup, dev,
1308 "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1309 port->qp_eq->attr.ist1);
1312 for (i = 0; i < port->num_def_qps; i++) {
1313 pr = &port->port_res[i];
1314 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1315 "%s-queue%d", dev->name, i);
1316 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1317 ehea_recv_irq_handler,
1318 0, pr->int_send_name, pr);
1319 if (ret) {
1320 netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1321 i, pr->eq->attr.ist1);
1322 goto out_free_req;
1324 netif_info(port, ifup, dev,
1325 "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1326 pr->eq->attr.ist1, i);
1328 out:
1329 return ret;
1332 out_free_req:
1333 while (--i >= 0) {
1334 u32 ist = port->port_res[i].eq->attr.ist1;
1335 ibmebus_free_irq(ist, &port->port_res[i]);
1338 out_free_qpeq:
1339 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1340 i = port->num_def_qps;
1342 goto out;
1346 static void ehea_free_interrupts(struct net_device *dev)
1348 struct ehea_port *port = netdev_priv(dev);
1349 struct ehea_port_res *pr;
1350 int i;
1352 /* send */
1354 for (i = 0; i < port->num_def_qps; i++) {
1355 pr = &port->port_res[i];
1356 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1357 netif_info(port, intr, dev,
1358 "free send irq for res %d with handle 0x%X\n",
1359 i, pr->eq->attr.ist1);
1362 /* associated events */
1363 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1364 netif_info(port, intr, dev,
1365 "associated event interrupt for handle 0x%X freed\n",
1366 port->qp_eq->attr.ist1);
1369 static int ehea_configure_port(struct ehea_port *port)
1371 int ret, i;
1372 u64 hret, mask;
1373 struct hcp_ehea_port_cb0 *cb0;
1375 ret = -ENOMEM;
1376 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1377 if (!cb0)
1378 goto out;
1380 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1381 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1382 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1383 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1384 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1385 PXLY_RC_VLAN_FILTER)
1386 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1388 for (i = 0; i < port->num_mcs; i++)
1389 if (use_mcs)
1390 cb0->default_qpn_arr[i] =
1391 port->port_res[i].qp->init_attr.qp_nr;
1392 else
1393 cb0->default_qpn_arr[i] =
1394 port->port_res[0].qp->init_attr.qp_nr;
1396 if (netif_msg_ifup(port))
1397 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1399 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1400 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1402 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1403 port->logical_port_id,
1404 H_PORT_CB0, mask, cb0);
1405 ret = -EIO;
1406 if (hret != H_SUCCESS)
1407 goto out_free;
1409 ret = 0;
1411 out_free:
1412 free_page((unsigned long)cb0);
1413 out:
1414 return ret;
1417 static int ehea_gen_smrs(struct ehea_port_res *pr)
1419 int ret;
1420 struct ehea_adapter *adapter = pr->port->adapter;
1422 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1423 if (ret)
1424 goto out;
1426 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1427 if (ret)
1428 goto out_free;
1430 return 0;
1432 out_free:
1433 ehea_rem_mr(&pr->send_mr);
1434 out:
1435 pr_err("Generating SMRS failed\n");
1436 return -EIO;
1439 static int ehea_rem_smrs(struct ehea_port_res *pr)
1441 if ((ehea_rem_mr(&pr->send_mr)) ||
1442 (ehea_rem_mr(&pr->recv_mr)))
1443 return -EIO;
1444 else
1445 return 0;
1448 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1450 int arr_size = sizeof(void *) * max_q_entries;
1452 q_skba->arr = vzalloc(arr_size);
1453 if (!q_skba->arr)
1454 return -ENOMEM;
1456 q_skba->len = max_q_entries;
1457 q_skba->index = 0;
1458 q_skba->os_skbs = 0;
1460 return 0;
1463 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1464 struct port_res_cfg *pr_cfg, int queue_token)
1466 struct ehea_adapter *adapter = port->adapter;
1467 enum ehea_eq_type eq_type = EHEA_EQ;
1468 struct ehea_qp_init_attr *init_attr = NULL;
1469 int ret = -EIO;
1470 u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1472 tx_bytes = pr->tx_bytes;
1473 tx_packets = pr->tx_packets;
1474 rx_bytes = pr->rx_bytes;
1475 rx_packets = pr->rx_packets;
1477 memset(pr, 0, sizeof(struct ehea_port_res));
1479 pr->tx_bytes = rx_bytes;
1480 pr->tx_packets = tx_packets;
1481 pr->rx_bytes = rx_bytes;
1482 pr->rx_packets = rx_packets;
1484 pr->port = port;
1486 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1487 if (!pr->eq) {
1488 pr_err("create_eq failed (eq)\n");
1489 goto out_free;
1492 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1493 pr->eq->fw_handle,
1494 port->logical_port_id);
1495 if (!pr->recv_cq) {
1496 pr_err("create_cq failed (cq_recv)\n");
1497 goto out_free;
1500 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1501 pr->eq->fw_handle,
1502 port->logical_port_id);
1503 if (!pr->send_cq) {
1504 pr_err("create_cq failed (cq_send)\n");
1505 goto out_free;
1508 if (netif_msg_ifup(port))
1509 pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1510 pr->send_cq->attr.act_nr_of_cqes,
1511 pr->recv_cq->attr.act_nr_of_cqes);
1513 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1514 if (!init_attr) {
1515 ret = -ENOMEM;
1516 pr_err("no mem for ehea_qp_init_attr\n");
1517 goto out_free;
1520 init_attr->low_lat_rq1 = 1;
1521 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1522 init_attr->rq_count = 3;
1523 init_attr->qp_token = queue_token;
1524 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1525 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1526 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1527 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1528 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1529 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1530 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1531 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1532 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1533 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1534 init_attr->port_nr = port->logical_port_id;
1535 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1536 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1537 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1539 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1540 if (!pr->qp) {
1541 pr_err("create_qp failed\n");
1542 ret = -EIO;
1543 goto out_free;
1546 if (netif_msg_ifup(port))
1547 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",
1548 init_attr->qp_nr,
1549 init_attr->act_nr_send_wqes,
1550 init_attr->act_nr_rwqes_rq1,
1551 init_attr->act_nr_rwqes_rq2,
1552 init_attr->act_nr_rwqes_rq3);
1554 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1556 ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1557 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1558 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1559 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1560 if (ret)
1561 goto out_free;
1563 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1564 if (ehea_gen_smrs(pr) != 0) {
1565 ret = -EIO;
1566 goto out_free;
1569 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1571 kfree(init_attr);
1573 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1575 ret = 0;
1576 goto out;
1578 out_free:
1579 kfree(init_attr);
1580 vfree(pr->sq_skba.arr);
1581 vfree(pr->rq1_skba.arr);
1582 vfree(pr->rq2_skba.arr);
1583 vfree(pr->rq3_skba.arr);
1584 ehea_destroy_qp(pr->qp);
1585 ehea_destroy_cq(pr->send_cq);
1586 ehea_destroy_cq(pr->recv_cq);
1587 ehea_destroy_eq(pr->eq);
1588 out:
1589 return ret;
1592 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1594 int ret, i;
1596 if (pr->qp)
1597 netif_napi_del(&pr->napi);
1599 ret = ehea_destroy_qp(pr->qp);
1601 if (!ret) {
1602 ehea_destroy_cq(pr->send_cq);
1603 ehea_destroy_cq(pr->recv_cq);
1604 ehea_destroy_eq(pr->eq);
1606 for (i = 0; i < pr->rq1_skba.len; i++)
1607 if (pr->rq1_skba.arr[i])
1608 dev_kfree_skb(pr->rq1_skba.arr[i]);
1610 for (i = 0; i < pr->rq2_skba.len; i++)
1611 if (pr->rq2_skba.arr[i])
1612 dev_kfree_skb(pr->rq2_skba.arr[i]);
1614 for (i = 0; i < pr->rq3_skba.len; i++)
1615 if (pr->rq3_skba.arr[i])
1616 dev_kfree_skb(pr->rq3_skba.arr[i]);
1618 for (i = 0; i < pr->sq_skba.len; i++)
1619 if (pr->sq_skba.arr[i])
1620 dev_kfree_skb(pr->sq_skba.arr[i]);
1622 vfree(pr->rq1_skba.arr);
1623 vfree(pr->rq2_skba.arr);
1624 vfree(pr->rq3_skba.arr);
1625 vfree(pr->sq_skba.arr);
1626 ret = ehea_rem_smrs(pr);
1628 return ret;
1631 static void write_swqe2_immediate(struct sk_buff *skb, struct ehea_swqe *swqe,
1632 u32 lkey)
1634 int skb_data_size = skb_headlen(skb);
1635 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1636 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1637 unsigned int immediate_len = SWQE2_MAX_IMM;
1639 swqe->descriptors = 0;
1641 if (skb_is_gso(skb)) {
1642 swqe->tx_control |= EHEA_SWQE_TSO;
1643 swqe->mss = skb_shinfo(skb)->gso_size;
1645 * For TSO packets we only copy the headers into the
1646 * immediate area.
1648 immediate_len = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1651 if (skb_is_gso(skb) || skb_data_size >= SWQE2_MAX_IMM) {
1652 skb_copy_from_linear_data(skb, imm_data, immediate_len);
1653 swqe->immediate_data_length = immediate_len;
1655 if (skb_data_size > immediate_len) {
1656 sg1entry->l_key = lkey;
1657 sg1entry->len = skb_data_size - immediate_len;
1658 sg1entry->vaddr =
1659 ehea_map_vaddr(skb->data + immediate_len);
1660 swqe->descriptors++;
1662 } else {
1663 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1664 swqe->immediate_data_length = skb_data_size;
1668 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1669 struct ehea_swqe *swqe, u32 lkey)
1671 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1672 skb_frag_t *frag;
1673 int nfrags, sg1entry_contains_frag_data, i;
1675 nfrags = skb_shinfo(skb)->nr_frags;
1676 sg1entry = &swqe->u.immdata_desc.sg_entry;
1677 sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1678 sg1entry_contains_frag_data = 0;
1680 write_swqe2_immediate(skb, swqe, lkey);
1682 /* write descriptors */
1683 if (nfrags > 0) {
1684 if (swqe->descriptors == 0) {
1685 /* sg1entry not yet used */
1686 frag = &skb_shinfo(skb)->frags[0];
1688 /* copy sg1entry data */
1689 sg1entry->l_key = lkey;
1690 sg1entry->len = skb_frag_size(frag);
1691 sg1entry->vaddr =
1692 ehea_map_vaddr(skb_frag_address(frag));
1693 swqe->descriptors++;
1694 sg1entry_contains_frag_data = 1;
1697 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1699 frag = &skb_shinfo(skb)->frags[i];
1700 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1702 sgentry->l_key = lkey;
1703 sgentry->len = skb_frag_size(frag);
1704 sgentry->vaddr = ehea_map_vaddr(skb_frag_address(frag));
1705 swqe->descriptors++;
1710 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1712 int ret = 0;
1713 u64 hret;
1714 u8 reg_type;
1716 /* De/Register untagged packets */
1717 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1718 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1719 port->logical_port_id,
1720 reg_type, port->mac_addr, 0, hcallid);
1721 if (hret != H_SUCCESS) {
1722 pr_err("%sregistering bc address failed (tagged)\n",
1723 hcallid == H_REG_BCMC ? "" : "de");
1724 ret = -EIO;
1725 goto out_herr;
1728 /* De/Register VLAN packets */
1729 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1730 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1731 port->logical_port_id,
1732 reg_type, port->mac_addr, 0, hcallid);
1733 if (hret != H_SUCCESS) {
1734 pr_err("%sregistering bc address failed (vlan)\n",
1735 hcallid == H_REG_BCMC ? "" : "de");
1736 ret = -EIO;
1738 out_herr:
1739 return ret;
1742 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1744 struct ehea_port *port = netdev_priv(dev);
1745 struct sockaddr *mac_addr = sa;
1746 struct hcp_ehea_port_cb0 *cb0;
1747 int ret;
1748 u64 hret;
1750 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1751 ret = -EADDRNOTAVAIL;
1752 goto out;
1755 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1756 if (!cb0) {
1757 pr_err("no mem for cb0\n");
1758 ret = -ENOMEM;
1759 goto out;
1762 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1764 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1766 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1767 port->logical_port_id, H_PORT_CB0,
1768 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1769 if (hret != H_SUCCESS) {
1770 ret = -EIO;
1771 goto out_free;
1774 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1776 /* Deregister old MAC in pHYP */
1777 if (port->state == EHEA_PORT_UP) {
1778 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1779 if (ret)
1780 goto out_upregs;
1783 port->mac_addr = cb0->port_mac_addr << 16;
1785 /* Register new MAC in pHYP */
1786 if (port->state == EHEA_PORT_UP) {
1787 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1788 if (ret)
1789 goto out_upregs;
1792 ret = 0;
1794 out_upregs:
1795 ehea_update_bcmc_registrations();
1796 out_free:
1797 free_page((unsigned long)cb0);
1798 out:
1799 return ret;
1802 static void ehea_promiscuous_error(u64 hret, int enable)
1804 if (hret == H_AUTHORITY)
1805 pr_info("Hypervisor denied %sabling promiscuous mode\n",
1806 enable == 1 ? "en" : "dis");
1807 else
1808 pr_err("failed %sabling promiscuous mode\n",
1809 enable == 1 ? "en" : "dis");
1812 static void ehea_promiscuous(struct net_device *dev, int enable)
1814 struct ehea_port *port = netdev_priv(dev);
1815 struct hcp_ehea_port_cb7 *cb7;
1816 u64 hret;
1818 if (enable == port->promisc)
1819 return;
1821 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1822 if (!cb7) {
1823 pr_err("no mem for cb7\n");
1824 goto out;
1827 /* Modify Pxs_DUCQPN in CB7 */
1828 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1830 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1831 port->logical_port_id,
1832 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1833 if (hret) {
1834 ehea_promiscuous_error(hret, enable);
1835 goto out;
1838 port->promisc = enable;
1839 out:
1840 free_page((unsigned long)cb7);
1843 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1844 u32 hcallid)
1846 u64 hret;
1847 u8 reg_type;
1849 reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_UNTAGGED;
1850 if (mc_mac_addr == 0)
1851 reg_type |= EHEA_BCMC_SCOPE_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 if (hret)
1857 goto out;
1859 reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_VLANID_ALL;
1860 if (mc_mac_addr == 0)
1861 reg_type |= EHEA_BCMC_SCOPE_ALL;
1863 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1864 port->logical_port_id,
1865 reg_type, mc_mac_addr, 0, hcallid);
1866 out:
1867 return hret;
1870 static int ehea_drop_multicast_list(struct net_device *dev)
1872 struct ehea_port *port = netdev_priv(dev);
1873 struct ehea_mc_list *mc_entry = port->mc_list;
1874 struct list_head *pos;
1875 struct list_head *temp;
1876 int ret = 0;
1877 u64 hret;
1879 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1880 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1882 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1883 H_DEREG_BCMC);
1884 if (hret) {
1885 pr_err("failed deregistering mcast MAC\n");
1886 ret = -EIO;
1889 list_del(pos);
1890 kfree(mc_entry);
1892 return ret;
1895 static void ehea_allmulti(struct net_device *dev, int enable)
1897 struct ehea_port *port = netdev_priv(dev);
1898 u64 hret;
1900 if (!port->allmulti) {
1901 if (enable) {
1902 /* Enable ALLMULTI */
1903 ehea_drop_multicast_list(dev);
1904 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1905 if (!hret)
1906 port->allmulti = 1;
1907 else
1908 netdev_err(dev,
1909 "failed enabling IFF_ALLMULTI\n");
1911 } else {
1912 if (!enable) {
1913 /* Disable ALLMULTI */
1914 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1915 if (!hret)
1916 port->allmulti = 0;
1917 else
1918 netdev_err(dev,
1919 "failed disabling IFF_ALLMULTI\n");
1924 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
1926 struct ehea_mc_list *ehea_mcl_entry;
1927 u64 hret;
1929 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1930 if (!ehea_mcl_entry)
1931 return;
1933 INIT_LIST_HEAD(&ehea_mcl_entry->list);
1935 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1937 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1938 H_REG_BCMC);
1939 if (!hret)
1940 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1941 else {
1942 pr_err("failed registering mcast MAC\n");
1943 kfree(ehea_mcl_entry);
1947 static void ehea_set_multicast_list(struct net_device *dev)
1949 struct ehea_port *port = netdev_priv(dev);
1950 struct netdev_hw_addr *ha;
1951 int ret;
1953 ehea_promiscuous(dev, !!(dev->flags & IFF_PROMISC));
1955 if (dev->flags & IFF_ALLMULTI) {
1956 ehea_allmulti(dev, 1);
1957 goto out;
1959 ehea_allmulti(dev, 0);
1961 if (!netdev_mc_empty(dev)) {
1962 ret = ehea_drop_multicast_list(dev);
1963 if (ret) {
1964 /* Dropping the current multicast list failed.
1965 * Enabling ALL_MULTI is the best we can do.
1967 ehea_allmulti(dev, 1);
1970 if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
1971 pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
1972 port->adapter->max_mc_mac);
1973 goto out;
1976 netdev_for_each_mc_addr(ha, dev)
1977 ehea_add_multicast_entry(port, ha->addr);
1980 out:
1981 ehea_update_bcmc_registrations();
1984 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1986 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1987 return -EINVAL;
1988 dev->mtu = new_mtu;
1989 return 0;
1992 static void xmit_common(struct sk_buff *skb, struct ehea_swqe *swqe)
1994 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT | EHEA_SWQE_CRC;
1996 if (vlan_get_protocol(skb) != htons(ETH_P_IP))
1997 return;
1999 if (skb->ip_summed == CHECKSUM_PARTIAL)
2000 swqe->tx_control |= EHEA_SWQE_IP_CHECKSUM;
2002 swqe->ip_start = skb_network_offset(skb);
2003 swqe->ip_end = swqe->ip_start + ip_hdrlen(skb) - 1;
2005 switch (ip_hdr(skb)->protocol) {
2006 case IPPROTO_UDP:
2007 if (skb->ip_summed == CHECKSUM_PARTIAL)
2008 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
2010 swqe->tcp_offset = swqe->ip_end + 1 +
2011 offsetof(struct udphdr, check);
2012 break;
2014 case IPPROTO_TCP:
2015 if (skb->ip_summed == CHECKSUM_PARTIAL)
2016 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
2018 swqe->tcp_offset = swqe->ip_end + 1 +
2019 offsetof(struct tcphdr, check);
2020 break;
2024 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2025 struct ehea_swqe *swqe, u32 lkey)
2027 swqe->tx_control |= EHEA_SWQE_DESCRIPTORS_PRESENT;
2029 xmit_common(skb, swqe);
2031 write_swqe2_data(skb, dev, swqe, lkey);
2034 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2035 struct ehea_swqe *swqe)
2037 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2039 xmit_common(skb, swqe);
2041 if (!skb->data_len)
2042 skb_copy_from_linear_data(skb, imm_data, skb->len);
2043 else
2044 skb_copy_bits(skb, 0, imm_data, skb->len);
2046 swqe->immediate_data_length = skb->len;
2047 dev_consume_skb_any(skb);
2050 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2052 struct ehea_port *port = netdev_priv(dev);
2053 struct ehea_swqe *swqe;
2054 u32 lkey;
2055 int swqe_index;
2056 struct ehea_port_res *pr;
2057 struct netdev_queue *txq;
2059 pr = &port->port_res[skb_get_queue_mapping(skb)];
2060 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2062 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2063 memset(swqe, 0, SWQE_HEADER_SIZE);
2064 atomic_dec(&pr->swqe_avail);
2066 if (skb_vlan_tag_present(skb)) {
2067 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2068 swqe->vlan_tag = skb_vlan_tag_get(skb);
2071 pr->tx_packets++;
2072 pr->tx_bytes += skb->len;
2074 if (skb->len <= SWQE3_MAX_IMM) {
2075 u32 sig_iv = port->sig_comp_iv;
2076 u32 swqe_num = pr->swqe_id_counter;
2077 ehea_xmit3(skb, dev, swqe);
2078 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2079 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2080 if (pr->swqe_ll_count >= (sig_iv - 1)) {
2081 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2082 sig_iv);
2083 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2084 pr->swqe_ll_count = 0;
2085 } else
2086 pr->swqe_ll_count += 1;
2087 } else {
2088 swqe->wr_id =
2089 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2090 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2091 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2092 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2093 pr->sq_skba.arr[pr->sq_skba.index] = skb;
2095 pr->sq_skba.index++;
2096 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2098 lkey = pr->send_mr.lkey;
2099 ehea_xmit2(skb, dev, swqe, lkey);
2100 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2102 pr->swqe_id_counter += 1;
2104 netif_info(port, tx_queued, dev,
2105 "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2106 if (netif_msg_tx_queued(port))
2107 ehea_dump(swqe, 512, "swqe");
2109 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2110 netif_tx_stop_queue(txq);
2111 swqe->tx_control |= EHEA_SWQE_PURGE;
2114 ehea_post_swqe(pr->qp, swqe);
2116 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2117 pr->p_stats.queue_stopped++;
2118 netif_tx_stop_queue(txq);
2121 return NETDEV_TX_OK;
2124 static int ehea_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
2126 struct ehea_port *port = netdev_priv(dev);
2127 struct ehea_adapter *adapter = port->adapter;
2128 struct hcp_ehea_port_cb1 *cb1;
2129 int index;
2130 u64 hret;
2131 int err = 0;
2133 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2134 if (!cb1) {
2135 pr_err("no mem for cb1\n");
2136 err = -ENOMEM;
2137 goto out;
2140 hret = ehea_h_query_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("query_ehea_port failed\n");
2144 err = -EINVAL;
2145 goto out;
2148 index = (vid / 64);
2149 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2151 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2152 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2153 if (hret != H_SUCCESS) {
2154 pr_err("modify_ehea_port failed\n");
2155 err = -EINVAL;
2157 out:
2158 free_page((unsigned long)cb1);
2159 return err;
2162 static int ehea_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
2164 struct ehea_port *port = netdev_priv(dev);
2165 struct ehea_adapter *adapter = port->adapter;
2166 struct hcp_ehea_port_cb1 *cb1;
2167 int index;
2168 u64 hret;
2169 int err = 0;
2171 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2172 if (!cb1) {
2173 pr_err("no mem for cb1\n");
2174 err = -ENOMEM;
2175 goto out;
2178 hret = ehea_h_query_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("query_ehea_port failed\n");
2182 err = -EINVAL;
2183 goto out;
2186 index = (vid / 64);
2187 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2189 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2190 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2191 if (hret != H_SUCCESS) {
2192 pr_err("modify_ehea_port failed\n");
2193 err = -EINVAL;
2195 out:
2196 free_page((unsigned long)cb1);
2197 return err;
2200 static int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2202 int ret = -EIO;
2203 u64 hret;
2204 u16 dummy16 = 0;
2205 u64 dummy64 = 0;
2206 struct hcp_modify_qp_cb0 *cb0;
2208 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2209 if (!cb0) {
2210 ret = -ENOMEM;
2211 goto out;
2214 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2215 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2216 if (hret != H_SUCCESS) {
2217 pr_err("query_ehea_qp failed (1)\n");
2218 goto out;
2221 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2222 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2223 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2224 &dummy64, &dummy64, &dummy16, &dummy16);
2225 if (hret != H_SUCCESS) {
2226 pr_err("modify_ehea_qp failed (1)\n");
2227 goto out;
2230 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2231 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2232 if (hret != H_SUCCESS) {
2233 pr_err("query_ehea_qp failed (2)\n");
2234 goto out;
2237 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2238 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2239 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2240 &dummy64, &dummy64, &dummy16, &dummy16);
2241 if (hret != H_SUCCESS) {
2242 pr_err("modify_ehea_qp failed (2)\n");
2243 goto out;
2246 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2247 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2248 if (hret != H_SUCCESS) {
2249 pr_err("query_ehea_qp failed (3)\n");
2250 goto out;
2253 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2254 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2255 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2256 &dummy64, &dummy64, &dummy16, &dummy16);
2257 if (hret != H_SUCCESS) {
2258 pr_err("modify_ehea_qp failed (3)\n");
2259 goto out;
2262 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2263 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2264 if (hret != H_SUCCESS) {
2265 pr_err("query_ehea_qp failed (4)\n");
2266 goto out;
2269 ret = 0;
2270 out:
2271 free_page((unsigned long)cb0);
2272 return ret;
2275 static int ehea_port_res_setup(struct ehea_port *port, int def_qps)
2277 int ret, i;
2278 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2279 enum ehea_eq_type eq_type = EHEA_EQ;
2281 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2282 EHEA_MAX_ENTRIES_EQ, 1);
2283 if (!port->qp_eq) {
2284 ret = -EINVAL;
2285 pr_err("ehea_create_eq failed (qp_eq)\n");
2286 goto out_kill_eq;
2289 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2290 pr_cfg.max_entries_scq = sq_entries * 2;
2291 pr_cfg.max_entries_sq = sq_entries;
2292 pr_cfg.max_entries_rq1 = rq1_entries;
2293 pr_cfg.max_entries_rq2 = rq2_entries;
2294 pr_cfg.max_entries_rq3 = rq3_entries;
2296 pr_cfg_small_rx.max_entries_rcq = 1;
2297 pr_cfg_small_rx.max_entries_scq = sq_entries;
2298 pr_cfg_small_rx.max_entries_sq = sq_entries;
2299 pr_cfg_small_rx.max_entries_rq1 = 1;
2300 pr_cfg_small_rx.max_entries_rq2 = 1;
2301 pr_cfg_small_rx.max_entries_rq3 = 1;
2303 for (i = 0; i < def_qps; i++) {
2304 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2305 if (ret)
2306 goto out_clean_pr;
2308 for (i = def_qps; i < def_qps; i++) {
2309 ret = ehea_init_port_res(port, &port->port_res[i],
2310 &pr_cfg_small_rx, i);
2311 if (ret)
2312 goto out_clean_pr;
2315 return 0;
2317 out_clean_pr:
2318 while (--i >= 0)
2319 ehea_clean_portres(port, &port->port_res[i]);
2321 out_kill_eq:
2322 ehea_destroy_eq(port->qp_eq);
2323 return ret;
2326 static int ehea_clean_all_portres(struct ehea_port *port)
2328 int ret = 0;
2329 int i;
2331 for (i = 0; i < port->num_def_qps; i++)
2332 ret |= ehea_clean_portres(port, &port->port_res[i]);
2334 ret |= ehea_destroy_eq(port->qp_eq);
2336 return ret;
2339 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2341 if (adapter->active_ports)
2342 return;
2344 ehea_rem_mr(&adapter->mr);
2347 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2349 if (adapter->active_ports)
2350 return 0;
2352 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2355 static int ehea_up(struct net_device *dev)
2357 int ret, i;
2358 struct ehea_port *port = netdev_priv(dev);
2360 if (port->state == EHEA_PORT_UP)
2361 return 0;
2363 ret = ehea_port_res_setup(port, port->num_def_qps);
2364 if (ret) {
2365 netdev_err(dev, "port_res_failed\n");
2366 goto out;
2369 /* Set default QP for this port */
2370 ret = ehea_configure_port(port);
2371 if (ret) {
2372 netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2373 goto out_clean_pr;
2376 ret = ehea_reg_interrupts(dev);
2377 if (ret) {
2378 netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2379 goto out_clean_pr;
2382 for (i = 0; i < port->num_def_qps; i++) {
2383 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2384 if (ret) {
2385 netdev_err(dev, "activate_qp failed\n");
2386 goto out_free_irqs;
2390 for (i = 0; i < port->num_def_qps; i++) {
2391 ret = ehea_fill_port_res(&port->port_res[i]);
2392 if (ret) {
2393 netdev_err(dev, "out_free_irqs\n");
2394 goto out_free_irqs;
2398 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2399 if (ret) {
2400 ret = -EIO;
2401 goto out_free_irqs;
2404 port->state = EHEA_PORT_UP;
2406 ret = 0;
2407 goto out;
2409 out_free_irqs:
2410 ehea_free_interrupts(dev);
2412 out_clean_pr:
2413 ehea_clean_all_portres(port);
2414 out:
2415 if (ret)
2416 netdev_info(dev, "Failed starting. ret=%i\n", ret);
2418 ehea_update_bcmc_registrations();
2419 ehea_update_firmware_handles();
2421 return ret;
2424 static void port_napi_disable(struct ehea_port *port)
2426 int i;
2428 for (i = 0; i < port->num_def_qps; i++)
2429 napi_disable(&port->port_res[i].napi);
2432 static void port_napi_enable(struct ehea_port *port)
2434 int i;
2436 for (i = 0; i < port->num_def_qps; i++)
2437 napi_enable(&port->port_res[i].napi);
2440 static int ehea_open(struct net_device *dev)
2442 int ret;
2443 struct ehea_port *port = netdev_priv(dev);
2445 mutex_lock(&port->port_lock);
2447 netif_info(port, ifup, dev, "enabling port\n");
2449 ret = ehea_up(dev);
2450 if (!ret) {
2451 port_napi_enable(port);
2452 netif_tx_start_all_queues(dev);
2455 mutex_unlock(&port->port_lock);
2456 schedule_delayed_work(&port->stats_work,
2457 round_jiffies_relative(msecs_to_jiffies(1000)));
2459 return ret;
2462 static int ehea_down(struct net_device *dev)
2464 int ret;
2465 struct ehea_port *port = netdev_priv(dev);
2467 if (port->state == EHEA_PORT_DOWN)
2468 return 0;
2470 ehea_drop_multicast_list(dev);
2471 ehea_allmulti(dev, 0);
2472 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2474 ehea_free_interrupts(dev);
2476 port->state = EHEA_PORT_DOWN;
2478 ehea_update_bcmc_registrations();
2480 ret = ehea_clean_all_portres(port);
2481 if (ret)
2482 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2484 ehea_update_firmware_handles();
2486 return ret;
2489 static int ehea_stop(struct net_device *dev)
2491 int ret;
2492 struct ehea_port *port = netdev_priv(dev);
2494 netif_info(port, ifdown, dev, "disabling port\n");
2496 set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2497 cancel_work_sync(&port->reset_task);
2498 cancel_delayed_work_sync(&port->stats_work);
2499 mutex_lock(&port->port_lock);
2500 netif_tx_stop_all_queues(dev);
2501 port_napi_disable(port);
2502 ret = ehea_down(dev);
2503 mutex_unlock(&port->port_lock);
2504 clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2505 return ret;
2508 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2510 struct ehea_qp qp = *orig_qp;
2511 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2512 struct ehea_swqe *swqe;
2513 int wqe_index;
2514 int i;
2516 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2517 swqe = ehea_get_swqe(&qp, &wqe_index);
2518 swqe->tx_control |= EHEA_SWQE_PURGE;
2522 static void ehea_flush_sq(struct ehea_port *port)
2524 int i;
2526 for (i = 0; i < port->num_def_qps; i++) {
2527 struct ehea_port_res *pr = &port->port_res[i];
2528 int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2529 int ret;
2531 ret = wait_event_timeout(port->swqe_avail_wq,
2532 atomic_read(&pr->swqe_avail) >= swqe_max,
2533 msecs_to_jiffies(100));
2535 if (!ret) {
2536 pr_err("WARNING: sq not flushed completely\n");
2537 break;
2542 static int ehea_stop_qps(struct net_device *dev)
2544 struct ehea_port *port = netdev_priv(dev);
2545 struct ehea_adapter *adapter = port->adapter;
2546 struct hcp_modify_qp_cb0 *cb0;
2547 int ret = -EIO;
2548 int dret;
2549 int i;
2550 u64 hret;
2551 u64 dummy64 = 0;
2552 u16 dummy16 = 0;
2554 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2555 if (!cb0) {
2556 ret = -ENOMEM;
2557 goto out;
2560 for (i = 0; i < (port->num_def_qps); i++) {
2561 struct ehea_port_res *pr = &port->port_res[i];
2562 struct ehea_qp *qp = pr->qp;
2564 /* Purge send queue */
2565 ehea_purge_sq(qp);
2567 /* Disable queue pair */
2568 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2569 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2570 cb0);
2571 if (hret != H_SUCCESS) {
2572 pr_err("query_ehea_qp failed (1)\n");
2573 goto out;
2576 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2577 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2579 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2580 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2581 1), cb0, &dummy64,
2582 &dummy64, &dummy16, &dummy16);
2583 if (hret != H_SUCCESS) {
2584 pr_err("modify_ehea_qp failed (1)\n");
2585 goto out;
2588 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2589 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2590 cb0);
2591 if (hret != H_SUCCESS) {
2592 pr_err("query_ehea_qp failed (2)\n");
2593 goto out;
2596 /* deregister shared memory regions */
2597 dret = ehea_rem_smrs(pr);
2598 if (dret) {
2599 pr_err("unreg shared memory region failed\n");
2600 goto out;
2604 ret = 0;
2605 out:
2606 free_page((unsigned long)cb0);
2608 return ret;
2611 static void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2613 struct ehea_qp qp = *orig_qp;
2614 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2615 struct ehea_rwqe *rwqe;
2616 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2617 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2618 struct sk_buff *skb;
2619 u32 lkey = pr->recv_mr.lkey;
2622 int i;
2623 int index;
2625 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2626 rwqe = ehea_get_next_rwqe(&qp, 2);
2627 rwqe->sg_list[0].l_key = lkey;
2628 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2629 skb = skba_rq2[index];
2630 if (skb)
2631 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2634 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2635 rwqe = ehea_get_next_rwqe(&qp, 3);
2636 rwqe->sg_list[0].l_key = lkey;
2637 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2638 skb = skba_rq3[index];
2639 if (skb)
2640 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2644 static int ehea_restart_qps(struct net_device *dev)
2646 struct ehea_port *port = netdev_priv(dev);
2647 struct ehea_adapter *adapter = port->adapter;
2648 int ret = 0;
2649 int i;
2651 struct hcp_modify_qp_cb0 *cb0;
2652 u64 hret;
2653 u64 dummy64 = 0;
2654 u16 dummy16 = 0;
2656 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2657 if (!cb0) {
2658 ret = -ENOMEM;
2659 goto out;
2662 for (i = 0; i < (port->num_def_qps); i++) {
2663 struct ehea_port_res *pr = &port->port_res[i];
2664 struct ehea_qp *qp = pr->qp;
2666 ret = ehea_gen_smrs(pr);
2667 if (ret) {
2668 netdev_err(dev, "creation of shared memory regions failed\n");
2669 goto out;
2672 ehea_update_rqs(qp, pr);
2674 /* Enable queue pair */
2675 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2676 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2677 cb0);
2678 if (hret != H_SUCCESS) {
2679 netdev_err(dev, "query_ehea_qp failed (1)\n");
2680 goto out;
2683 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2684 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2686 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2687 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2688 1), cb0, &dummy64,
2689 &dummy64, &dummy16, &dummy16);
2690 if (hret != H_SUCCESS) {
2691 netdev_err(dev, "modify_ehea_qp failed (1)\n");
2692 goto out;
2695 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2696 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2697 cb0);
2698 if (hret != H_SUCCESS) {
2699 netdev_err(dev, "query_ehea_qp failed (2)\n");
2700 goto out;
2703 /* refill entire queue */
2704 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2705 ehea_refill_rq2(pr, 0);
2706 ehea_refill_rq3(pr, 0);
2708 out:
2709 free_page((unsigned long)cb0);
2711 return ret;
2714 static void ehea_reset_port(struct work_struct *work)
2716 int ret;
2717 struct ehea_port *port =
2718 container_of(work, struct ehea_port, reset_task);
2719 struct net_device *dev = port->netdev;
2721 mutex_lock(&dlpar_mem_lock);
2722 port->resets++;
2723 mutex_lock(&port->port_lock);
2724 netif_tx_disable(dev);
2726 port_napi_disable(port);
2728 ehea_down(dev);
2730 ret = ehea_up(dev);
2731 if (ret)
2732 goto out;
2734 ehea_set_multicast_list(dev);
2736 netif_info(port, timer, dev, "reset successful\n");
2738 port_napi_enable(port);
2740 netif_tx_wake_all_queues(dev);
2741 out:
2742 mutex_unlock(&port->port_lock);
2743 mutex_unlock(&dlpar_mem_lock);
2746 static void ehea_rereg_mrs(void)
2748 int ret, i;
2749 struct ehea_adapter *adapter;
2751 pr_info("LPAR memory changed - re-initializing driver\n");
2753 list_for_each_entry(adapter, &adapter_list, list)
2754 if (adapter->active_ports) {
2755 /* Shutdown all ports */
2756 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2757 struct ehea_port *port = adapter->port[i];
2758 struct net_device *dev;
2760 if (!port)
2761 continue;
2763 dev = port->netdev;
2765 if (dev->flags & IFF_UP) {
2766 mutex_lock(&port->port_lock);
2767 netif_tx_disable(dev);
2768 ehea_flush_sq(port);
2769 ret = ehea_stop_qps(dev);
2770 if (ret) {
2771 mutex_unlock(&port->port_lock);
2772 goto out;
2774 port_napi_disable(port);
2775 mutex_unlock(&port->port_lock);
2777 reset_sq_restart_flag(port);
2780 /* Unregister old memory region */
2781 ret = ehea_rem_mr(&adapter->mr);
2782 if (ret) {
2783 pr_err("unregister MR failed - driver inoperable!\n");
2784 goto out;
2788 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2790 list_for_each_entry(adapter, &adapter_list, list)
2791 if (adapter->active_ports) {
2792 /* Register new memory region */
2793 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2794 if (ret) {
2795 pr_err("register MR failed - driver inoperable!\n");
2796 goto out;
2799 /* Restart all ports */
2800 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2801 struct ehea_port *port = adapter->port[i];
2803 if (port) {
2804 struct net_device *dev = port->netdev;
2806 if (dev->flags & IFF_UP) {
2807 mutex_lock(&port->port_lock);
2808 ret = ehea_restart_qps(dev);
2809 if (!ret) {
2810 check_sqs(port);
2811 port_napi_enable(port);
2812 netif_tx_wake_all_queues(dev);
2813 } else {
2814 netdev_err(dev, "Unable to restart QPS\n");
2816 mutex_unlock(&port->port_lock);
2821 pr_info("re-initializing driver complete\n");
2822 out:
2823 return;
2826 static void ehea_tx_watchdog(struct net_device *dev)
2828 struct ehea_port *port = netdev_priv(dev);
2830 if (netif_carrier_ok(dev) &&
2831 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2832 ehea_schedule_port_reset(port);
2835 static int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2837 struct hcp_query_ehea *cb;
2838 u64 hret;
2839 int ret;
2841 cb = (void *)get_zeroed_page(GFP_KERNEL);
2842 if (!cb) {
2843 ret = -ENOMEM;
2844 goto out;
2847 hret = ehea_h_query_ehea(adapter->handle, cb);
2849 if (hret != H_SUCCESS) {
2850 ret = -EIO;
2851 goto out_herr;
2854 adapter->max_mc_mac = cb->max_mc_mac - 1;
2855 ret = 0;
2857 out_herr:
2858 free_page((unsigned long)cb);
2859 out:
2860 return ret;
2863 static int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2865 struct hcp_ehea_port_cb4 *cb4;
2866 u64 hret;
2867 int ret = 0;
2869 *jumbo = 0;
2871 /* (Try to) enable *jumbo frames */
2872 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
2873 if (!cb4) {
2874 pr_err("no mem for cb4\n");
2875 ret = -ENOMEM;
2876 goto out;
2877 } else {
2878 hret = ehea_h_query_ehea_port(port->adapter->handle,
2879 port->logical_port_id,
2880 H_PORT_CB4,
2881 H_PORT_CB4_JUMBO, cb4);
2882 if (hret == H_SUCCESS) {
2883 if (cb4->jumbo_frame)
2884 *jumbo = 1;
2885 else {
2886 cb4->jumbo_frame = 1;
2887 hret = ehea_h_modify_ehea_port(port->adapter->
2888 handle,
2889 port->
2890 logical_port_id,
2891 H_PORT_CB4,
2892 H_PORT_CB4_JUMBO,
2893 cb4);
2894 if (hret == H_SUCCESS)
2895 *jumbo = 1;
2897 } else
2898 ret = -EINVAL;
2900 free_page((unsigned long)cb4);
2902 out:
2903 return ret;
2906 static ssize_t ehea_show_port_id(struct device *dev,
2907 struct device_attribute *attr, char *buf)
2909 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2910 return sprintf(buf, "%d", port->logical_port_id);
2913 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2914 NULL);
2916 static void logical_port_release(struct device *dev)
2918 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2919 of_node_put(port->ofdev.dev.of_node);
2922 static struct device *ehea_register_port(struct ehea_port *port,
2923 struct device_node *dn)
2925 int ret;
2927 port->ofdev.dev.of_node = of_node_get(dn);
2928 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
2929 port->ofdev.dev.bus = &ibmebus_bus_type;
2931 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
2932 port->ofdev.dev.release = logical_port_release;
2934 ret = of_device_register(&port->ofdev);
2935 if (ret) {
2936 pr_err("failed to register device. ret=%d\n", ret);
2937 goto out;
2940 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2941 if (ret) {
2942 pr_err("failed to register attributes, ret=%d\n", ret);
2943 goto out_unreg_of_dev;
2946 return &port->ofdev.dev;
2948 out_unreg_of_dev:
2949 of_device_unregister(&port->ofdev);
2950 out:
2951 return NULL;
2954 static void ehea_unregister_port(struct ehea_port *port)
2956 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2957 of_device_unregister(&port->ofdev);
2960 static const struct net_device_ops ehea_netdev_ops = {
2961 .ndo_open = ehea_open,
2962 .ndo_stop = ehea_stop,
2963 .ndo_start_xmit = ehea_start_xmit,
2964 #ifdef CONFIG_NET_POLL_CONTROLLER
2965 .ndo_poll_controller = ehea_netpoll,
2966 #endif
2967 .ndo_get_stats64 = ehea_get_stats64,
2968 .ndo_set_mac_address = ehea_set_mac_addr,
2969 .ndo_validate_addr = eth_validate_addr,
2970 .ndo_set_rx_mode = ehea_set_multicast_list,
2971 .ndo_change_mtu = ehea_change_mtu,
2972 .ndo_vlan_rx_add_vid = ehea_vlan_rx_add_vid,
2973 .ndo_vlan_rx_kill_vid = ehea_vlan_rx_kill_vid,
2974 .ndo_tx_timeout = ehea_tx_watchdog,
2977 static struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2978 u32 logical_port_id,
2979 struct device_node *dn)
2981 int ret;
2982 struct net_device *dev;
2983 struct ehea_port *port;
2984 struct device *port_dev;
2985 int jumbo;
2987 /* allocate memory for the port structures */
2988 dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
2990 if (!dev) {
2991 ret = -ENOMEM;
2992 goto out_err;
2995 port = netdev_priv(dev);
2997 mutex_init(&port->port_lock);
2998 port->state = EHEA_PORT_DOWN;
2999 port->sig_comp_iv = sq_entries / 10;
3001 port->adapter = adapter;
3002 port->netdev = dev;
3003 port->logical_port_id = logical_port_id;
3005 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
3007 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
3008 if (!port->mc_list) {
3009 ret = -ENOMEM;
3010 goto out_free_ethdev;
3013 INIT_LIST_HEAD(&port->mc_list->list);
3015 ret = ehea_sense_port_attr(port);
3016 if (ret)
3017 goto out_free_mc_list;
3019 netif_set_real_num_rx_queues(dev, port->num_def_qps);
3020 netif_set_real_num_tx_queues(dev, port->num_def_qps);
3022 port_dev = ehea_register_port(port, dn);
3023 if (!port_dev)
3024 goto out_free_mc_list;
3026 SET_NETDEV_DEV(dev, port_dev);
3028 /* initialize net_device structure */
3029 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3031 dev->netdev_ops = &ehea_netdev_ops;
3032 ehea_set_ethtool_ops(dev);
3034 dev->hw_features = NETIF_F_SG | NETIF_F_TSO |
3035 NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_CTAG_TX;
3036 dev->features = NETIF_F_SG | NETIF_F_TSO |
3037 NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
3038 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3039 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM;
3040 dev->vlan_features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HIGHDMA |
3041 NETIF_F_IP_CSUM;
3042 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3044 INIT_WORK(&port->reset_task, ehea_reset_port);
3045 INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3047 init_waitqueue_head(&port->swqe_avail_wq);
3048 init_waitqueue_head(&port->restart_wq);
3050 memset(&port->stats, 0, sizeof(struct net_device_stats));
3051 ret = register_netdev(dev);
3052 if (ret) {
3053 pr_err("register_netdev failed. ret=%d\n", ret);
3054 goto out_unreg_port;
3057 ret = ehea_get_jumboframe_status(port, &jumbo);
3058 if (ret)
3059 netdev_err(dev, "failed determining jumbo frame status\n");
3061 netdev_info(dev, "Jumbo frames are %sabled\n",
3062 jumbo == 1 ? "en" : "dis");
3064 adapter->active_ports++;
3066 return port;
3068 out_unreg_port:
3069 ehea_unregister_port(port);
3071 out_free_mc_list:
3072 kfree(port->mc_list);
3074 out_free_ethdev:
3075 free_netdev(dev);
3077 out_err:
3078 pr_err("setting up logical port with id=%d failed, ret=%d\n",
3079 logical_port_id, ret);
3080 return NULL;
3083 static void ehea_shutdown_single_port(struct ehea_port *port)
3085 struct ehea_adapter *adapter = port->adapter;
3087 cancel_work_sync(&port->reset_task);
3088 cancel_delayed_work_sync(&port->stats_work);
3089 unregister_netdev(port->netdev);
3090 ehea_unregister_port(port);
3091 kfree(port->mc_list);
3092 free_netdev(port->netdev);
3093 adapter->active_ports--;
3096 static int ehea_setup_ports(struct ehea_adapter *adapter)
3098 struct device_node *lhea_dn;
3099 struct device_node *eth_dn = NULL;
3101 const u32 *dn_log_port_id;
3102 int i = 0;
3104 lhea_dn = adapter->ofdev->dev.of_node;
3105 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3107 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3108 NULL);
3109 if (!dn_log_port_id) {
3110 pr_err("bad device node: eth_dn name=%s\n",
3111 eth_dn->full_name);
3112 continue;
3115 if (ehea_add_adapter_mr(adapter)) {
3116 pr_err("creating MR failed\n");
3117 of_node_put(eth_dn);
3118 return -EIO;
3121 adapter->port[i] = ehea_setup_single_port(adapter,
3122 *dn_log_port_id,
3123 eth_dn);
3124 if (adapter->port[i])
3125 netdev_info(adapter->port[i]->netdev,
3126 "logical port id #%d\n", *dn_log_port_id);
3127 else
3128 ehea_remove_adapter_mr(adapter);
3130 i++;
3132 return 0;
3135 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3136 u32 logical_port_id)
3138 struct device_node *lhea_dn;
3139 struct device_node *eth_dn = NULL;
3140 const u32 *dn_log_port_id;
3142 lhea_dn = adapter->ofdev->dev.of_node;
3143 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3145 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3146 NULL);
3147 if (dn_log_port_id)
3148 if (*dn_log_port_id == logical_port_id)
3149 return eth_dn;
3152 return NULL;
3155 static ssize_t ehea_probe_port(struct device *dev,
3156 struct device_attribute *attr,
3157 const char *buf, size_t count)
3159 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3160 struct ehea_port *port;
3161 struct device_node *eth_dn = NULL;
3162 int i;
3164 u32 logical_port_id;
3166 sscanf(buf, "%d", &logical_port_id);
3168 port = ehea_get_port(adapter, logical_port_id);
3170 if (port) {
3171 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3172 logical_port_id);
3173 return -EINVAL;
3176 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3178 if (!eth_dn) {
3179 pr_info("no logical port with id %d found\n", logical_port_id);
3180 return -EINVAL;
3183 if (ehea_add_adapter_mr(adapter)) {
3184 pr_err("creating MR failed\n");
3185 return -EIO;
3188 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3190 of_node_put(eth_dn);
3192 if (port) {
3193 for (i = 0; i < EHEA_MAX_PORTS; i++)
3194 if (!adapter->port[i]) {
3195 adapter->port[i] = port;
3196 break;
3199 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3200 logical_port_id);
3201 } else {
3202 ehea_remove_adapter_mr(adapter);
3203 return -EIO;
3206 return (ssize_t) count;
3209 static ssize_t ehea_remove_port(struct device *dev,
3210 struct device_attribute *attr,
3211 const char *buf, size_t count)
3213 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3214 struct ehea_port *port;
3215 int i;
3216 u32 logical_port_id;
3218 sscanf(buf, "%d", &logical_port_id);
3220 port = ehea_get_port(adapter, logical_port_id);
3222 if (port) {
3223 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3224 logical_port_id);
3226 ehea_shutdown_single_port(port);
3228 for (i = 0; i < EHEA_MAX_PORTS; i++)
3229 if (adapter->port[i] == port) {
3230 adapter->port[i] = NULL;
3231 break;
3233 } else {
3234 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3235 logical_port_id);
3236 return -EINVAL;
3239 ehea_remove_adapter_mr(adapter);
3241 return (ssize_t) count;
3244 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3245 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3247 static int ehea_create_device_sysfs(struct platform_device *dev)
3249 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3250 if (ret)
3251 goto out;
3253 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3254 out:
3255 return ret;
3258 static void ehea_remove_device_sysfs(struct platform_device *dev)
3260 device_remove_file(&dev->dev, &dev_attr_probe_port);
3261 device_remove_file(&dev->dev, &dev_attr_remove_port);
3264 static int ehea_reboot_notifier(struct notifier_block *nb,
3265 unsigned long action, void *unused)
3267 if (action == SYS_RESTART) {
3268 pr_info("Reboot: freeing all eHEA resources\n");
3269 ibmebus_unregister_driver(&ehea_driver);
3271 return NOTIFY_DONE;
3274 static struct notifier_block ehea_reboot_nb = {
3275 .notifier_call = ehea_reboot_notifier,
3278 static int ehea_mem_notifier(struct notifier_block *nb,
3279 unsigned long action, void *data)
3281 int ret = NOTIFY_BAD;
3282 struct memory_notify *arg = data;
3284 mutex_lock(&dlpar_mem_lock);
3286 switch (action) {
3287 case MEM_CANCEL_OFFLINE:
3288 pr_info("memory offlining canceled");
3289 /* Fall through: re-add canceled memory block */
3291 case MEM_ONLINE:
3292 pr_info("memory is going online");
3293 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3294 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3295 goto out_unlock;
3296 ehea_rereg_mrs();
3297 break;
3299 case MEM_GOING_OFFLINE:
3300 pr_info("memory is going offline");
3301 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3302 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3303 goto out_unlock;
3304 ehea_rereg_mrs();
3305 break;
3307 default:
3308 break;
3311 ehea_update_firmware_handles();
3312 ret = NOTIFY_OK;
3314 out_unlock:
3315 mutex_unlock(&dlpar_mem_lock);
3316 return ret;
3319 static struct notifier_block ehea_mem_nb = {
3320 .notifier_call = ehea_mem_notifier,
3323 static void ehea_crash_handler(void)
3325 int i;
3327 if (ehea_fw_handles.arr)
3328 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3329 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3330 ehea_fw_handles.arr[i].fwh,
3331 FORCE_FREE);
3333 if (ehea_bcmc_regs.arr)
3334 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3335 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3336 ehea_bcmc_regs.arr[i].port_id,
3337 ehea_bcmc_regs.arr[i].reg_type,
3338 ehea_bcmc_regs.arr[i].macaddr,
3339 0, H_DEREG_BCMC);
3342 static atomic_t ehea_memory_hooks_registered;
3344 /* Register memory hooks on probe of first adapter */
3345 static int ehea_register_memory_hooks(void)
3347 int ret = 0;
3349 if (atomic_inc_return(&ehea_memory_hooks_registered) > 1)
3350 return 0;
3352 ret = ehea_create_busmap();
3353 if (ret) {
3354 pr_info("ehea_create_busmap failed\n");
3355 goto out;
3358 ret = register_reboot_notifier(&ehea_reboot_nb);
3359 if (ret) {
3360 pr_info("register_reboot_notifier failed\n");
3361 goto out;
3364 ret = register_memory_notifier(&ehea_mem_nb);
3365 if (ret) {
3366 pr_info("register_memory_notifier failed\n");
3367 goto out2;
3370 ret = crash_shutdown_register(ehea_crash_handler);
3371 if (ret) {
3372 pr_info("crash_shutdown_register failed\n");
3373 goto out3;
3376 return 0;
3378 out3:
3379 unregister_memory_notifier(&ehea_mem_nb);
3380 out2:
3381 unregister_reboot_notifier(&ehea_reboot_nb);
3382 out:
3383 atomic_dec(&ehea_memory_hooks_registered);
3384 return ret;
3387 static void ehea_unregister_memory_hooks(void)
3389 /* Only remove the hooks if we've registered them */
3390 if (atomic_read(&ehea_memory_hooks_registered) == 0)
3391 return;
3393 unregister_reboot_notifier(&ehea_reboot_nb);
3394 if (crash_shutdown_unregister(ehea_crash_handler))
3395 pr_info("failed unregistering crash handler\n");
3396 unregister_memory_notifier(&ehea_mem_nb);
3399 static int ehea_probe_adapter(struct platform_device *dev)
3401 struct ehea_adapter *adapter;
3402 const u64 *adapter_handle;
3403 int ret;
3404 int i;
3406 ret = ehea_register_memory_hooks();
3407 if (ret)
3408 return ret;
3410 if (!dev || !dev->dev.of_node) {
3411 pr_err("Invalid ibmebus device probed\n");
3412 return -EINVAL;
3415 adapter = devm_kzalloc(&dev->dev, sizeof(*adapter), GFP_KERNEL);
3416 if (!adapter) {
3417 ret = -ENOMEM;
3418 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3419 goto out;
3422 list_add(&adapter->list, &adapter_list);
3424 adapter->ofdev = dev;
3426 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3427 NULL);
3428 if (adapter_handle)
3429 adapter->handle = *adapter_handle;
3431 if (!adapter->handle) {
3432 dev_err(&dev->dev, "failed getting handle for adapter"
3433 " '%s'\n", dev->dev.of_node->full_name);
3434 ret = -ENODEV;
3435 goto out_free_ad;
3438 adapter->pd = EHEA_PD_ID;
3440 platform_set_drvdata(dev, adapter);
3443 /* initialize adapter and ports */
3444 /* get adapter properties */
3445 ret = ehea_sense_adapter_attr(adapter);
3446 if (ret) {
3447 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3448 goto out_free_ad;
3451 adapter->neq = ehea_create_eq(adapter,
3452 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3453 if (!adapter->neq) {
3454 ret = -EIO;
3455 dev_err(&dev->dev, "NEQ creation failed\n");
3456 goto out_free_ad;
3459 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3460 (unsigned long)adapter);
3462 ret = ehea_create_device_sysfs(dev);
3463 if (ret)
3464 goto out_kill_eq;
3466 ret = ehea_setup_ports(adapter);
3467 if (ret) {
3468 dev_err(&dev->dev, "setup_ports failed\n");
3469 goto out_rem_dev_sysfs;
3472 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3473 ehea_interrupt_neq, 0,
3474 "ehea_neq", adapter);
3475 if (ret) {
3476 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3477 goto out_shutdown_ports;
3480 /* Handle any events that might be pending. */
3481 tasklet_hi_schedule(&adapter->neq_tasklet);
3483 ret = 0;
3484 goto out;
3486 out_shutdown_ports:
3487 for (i = 0; i < EHEA_MAX_PORTS; i++)
3488 if (adapter->port[i]) {
3489 ehea_shutdown_single_port(adapter->port[i]);
3490 adapter->port[i] = NULL;
3493 out_rem_dev_sysfs:
3494 ehea_remove_device_sysfs(dev);
3496 out_kill_eq:
3497 ehea_destroy_eq(adapter->neq);
3499 out_free_ad:
3500 list_del(&adapter->list);
3502 out:
3503 ehea_update_firmware_handles();
3505 return ret;
3508 static int ehea_remove(struct platform_device *dev)
3510 struct ehea_adapter *adapter = platform_get_drvdata(dev);
3511 int i;
3513 for (i = 0; i < EHEA_MAX_PORTS; i++)
3514 if (adapter->port[i]) {
3515 ehea_shutdown_single_port(adapter->port[i]);
3516 adapter->port[i] = NULL;
3519 ehea_remove_device_sysfs(dev);
3521 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3522 tasklet_kill(&adapter->neq_tasklet);
3524 ehea_destroy_eq(adapter->neq);
3525 ehea_remove_adapter_mr(adapter);
3526 list_del(&adapter->list);
3528 ehea_update_firmware_handles();
3530 return 0;
3533 static int check_module_parm(void)
3535 int ret = 0;
3537 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3538 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3539 pr_info("Bad parameter: rq1_entries\n");
3540 ret = -EINVAL;
3542 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3543 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3544 pr_info("Bad parameter: rq2_entries\n");
3545 ret = -EINVAL;
3547 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3548 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3549 pr_info("Bad parameter: rq3_entries\n");
3550 ret = -EINVAL;
3552 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3553 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3554 pr_info("Bad parameter: sq_entries\n");
3555 ret = -EINVAL;
3558 return ret;
3561 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3562 char *buf)
3564 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3567 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3568 ehea_show_capabilities, NULL);
3570 static int __init ehea_module_init(void)
3572 int ret;
3574 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3576 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3577 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3579 mutex_init(&ehea_fw_handles.lock);
3580 spin_lock_init(&ehea_bcmc_regs.lock);
3582 ret = check_module_parm();
3583 if (ret)
3584 goto out;
3586 ret = ibmebus_register_driver(&ehea_driver);
3587 if (ret) {
3588 pr_err("failed registering eHEA device driver on ebus\n");
3589 goto out;
3592 ret = driver_create_file(&ehea_driver.driver,
3593 &driver_attr_capabilities);
3594 if (ret) {
3595 pr_err("failed to register capabilities attribute, ret=%d\n",
3596 ret);
3597 goto out2;
3600 return ret;
3602 out2:
3603 ibmebus_unregister_driver(&ehea_driver);
3604 out:
3605 return ret;
3608 static void __exit ehea_module_exit(void)
3610 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3611 ibmebus_unregister_driver(&ehea_driver);
3612 ehea_unregister_memory_hooks();
3613 kfree(ehea_fw_handles.arr);
3614 kfree(ehea_bcmc_regs.arr);
3615 ehea_destroy_busmap();
3618 module_init(ehea_module_init);
3619 module_exit(ehea_module_exit);