Merge 3.1-rc1 into usb-linus
[zen-stable.git] / drivers / net / ehea / ehea_main.c
blobbe2cb4ab8b4f4b09cbd36b8c4f5fc3444466e05f
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;
65 static int use_lro;
66 static int lro_max_aggr = EHEA_LRO_MAX_AGGR;
67 static int num_tx_qps = EHEA_NUM_TX_QP;
68 static int prop_carrier_state;
70 module_param(msg_level, int, 0);
71 module_param(rq1_entries, int, 0);
72 module_param(rq2_entries, int, 0);
73 module_param(rq3_entries, int, 0);
74 module_param(sq_entries, int, 0);
75 module_param(prop_carrier_state, int, 0);
76 module_param(use_mcs, int, 0);
77 module_param(use_lro, int, 0);
78 module_param(lro_max_aggr, int, 0);
79 module_param(num_tx_qps, int, 0);
81 MODULE_PARM_DESC(num_tx_qps, "Number of TX-QPS");
82 MODULE_PARM_DESC(msg_level, "msg_level");
83 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
84 "port to stack. 1:yes, 0:no. Default = 0 ");
85 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
86 "[2^x - 1], x = [6..14]. Default = "
87 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
88 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
89 "[2^x - 1], x = [6..14]. Default = "
90 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
91 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
92 "[2^x - 1], x = [6..14]. Default = "
93 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
94 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue "
95 "[2^x - 1], x = [6..14]. Default = "
96 __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
97 MODULE_PARM_DESC(use_mcs, " 0:NAPI, 1:Multiple receive queues, Default = 0 ");
99 MODULE_PARM_DESC(lro_max_aggr, " LRO: Max packets to be aggregated. Default = "
100 __MODULE_STRING(EHEA_LRO_MAX_AGGR));
101 MODULE_PARM_DESC(use_lro, " Large Receive Offload, 1: enable, 0: disable, "
102 "Default = 0");
104 static int port_name_cnt;
105 static LIST_HEAD(adapter_list);
106 static unsigned long ehea_driver_flags;
107 static DEFINE_MUTEX(dlpar_mem_lock);
108 struct ehea_fw_handle_array ehea_fw_handles;
109 struct ehea_bcmc_reg_array ehea_bcmc_regs;
112 static int __devinit ehea_probe_adapter(struct platform_device *dev,
113 const struct of_device_id *id);
115 static int __devexit ehea_remove(struct platform_device *dev);
117 static struct of_device_id ehea_device_table[] = {
119 .name = "lhea",
120 .compatible = "IBM,lhea",
124 MODULE_DEVICE_TABLE(of, ehea_device_table);
126 static struct of_platform_driver ehea_driver = {
127 .driver = {
128 .name = "ehea",
129 .owner = THIS_MODULE,
130 .of_match_table = ehea_device_table,
132 .probe = ehea_probe_adapter,
133 .remove = ehea_remove,
136 void ehea_dump(void *adr, int len, char *msg)
138 int x;
139 unsigned char *deb = adr;
140 for (x = 0; x < len; x += 16) {
141 pr_info("%s adr=%p ofs=%04x %016llx %016llx\n",
142 msg, deb, x, *((u64 *)&deb[0]), *((u64 *)&deb[8]));
143 deb += 16;
147 void ehea_schedule_port_reset(struct ehea_port *port)
149 if (!test_bit(__EHEA_DISABLE_PORT_RESET, &port->flags))
150 schedule_work(&port->reset_task);
153 static void ehea_update_firmware_handles(void)
155 struct ehea_fw_handle_entry *arr = NULL;
156 struct ehea_adapter *adapter;
157 int num_adapters = 0;
158 int num_ports = 0;
159 int num_portres = 0;
160 int i = 0;
161 int num_fw_handles, k, l;
163 /* Determine number of handles */
164 mutex_lock(&ehea_fw_handles.lock);
166 list_for_each_entry(adapter, &adapter_list, list) {
167 num_adapters++;
169 for (k = 0; k < EHEA_MAX_PORTS; k++) {
170 struct ehea_port *port = adapter->port[k];
172 if (!port || (port->state != EHEA_PORT_UP))
173 continue;
175 num_ports++;
176 num_portres += port->num_def_qps + port->num_add_tx_qps;
180 num_fw_handles = num_adapters * EHEA_NUM_ADAPTER_FW_HANDLES +
181 num_ports * EHEA_NUM_PORT_FW_HANDLES +
182 num_portres * EHEA_NUM_PORTRES_FW_HANDLES;
184 if (num_fw_handles) {
185 arr = kcalloc(num_fw_handles, sizeof(*arr), GFP_KERNEL);
186 if (!arr)
187 goto out; /* Keep the existing array */
188 } else
189 goto out_update;
191 list_for_each_entry(adapter, &adapter_list, list) {
192 if (num_adapters == 0)
193 break;
195 for (k = 0; k < EHEA_MAX_PORTS; k++) {
196 struct ehea_port *port = adapter->port[k];
198 if (!port || (port->state != EHEA_PORT_UP) ||
199 (num_ports == 0))
200 continue;
202 for (l = 0;
203 l < port->num_def_qps + port->num_add_tx_qps;
204 l++) {
205 struct ehea_port_res *pr = &port->port_res[l];
207 arr[i].adh = adapter->handle;
208 arr[i++].fwh = pr->qp->fw_handle;
209 arr[i].adh = adapter->handle;
210 arr[i++].fwh = pr->send_cq->fw_handle;
211 arr[i].adh = adapter->handle;
212 arr[i++].fwh = pr->recv_cq->fw_handle;
213 arr[i].adh = adapter->handle;
214 arr[i++].fwh = pr->eq->fw_handle;
215 arr[i].adh = adapter->handle;
216 arr[i++].fwh = pr->send_mr.handle;
217 arr[i].adh = adapter->handle;
218 arr[i++].fwh = pr->recv_mr.handle;
220 arr[i].adh = adapter->handle;
221 arr[i++].fwh = port->qp_eq->fw_handle;
222 num_ports--;
225 arr[i].adh = adapter->handle;
226 arr[i++].fwh = adapter->neq->fw_handle;
228 if (adapter->mr.handle) {
229 arr[i].adh = adapter->handle;
230 arr[i++].fwh = adapter->mr.handle;
232 num_adapters--;
235 out_update:
236 kfree(ehea_fw_handles.arr);
237 ehea_fw_handles.arr = arr;
238 ehea_fw_handles.num_entries = i;
239 out:
240 mutex_unlock(&ehea_fw_handles.lock);
243 static void ehea_update_bcmc_registrations(void)
245 unsigned long flags;
246 struct ehea_bcmc_reg_entry *arr = NULL;
247 struct ehea_adapter *adapter;
248 struct ehea_mc_list *mc_entry;
249 int num_registrations = 0;
250 int i = 0;
251 int k;
253 spin_lock_irqsave(&ehea_bcmc_regs.lock, flags);
255 /* Determine number of registrations */
256 list_for_each_entry(adapter, &adapter_list, list)
257 for (k = 0; k < EHEA_MAX_PORTS; k++) {
258 struct ehea_port *port = adapter->port[k];
260 if (!port || (port->state != EHEA_PORT_UP))
261 continue;
263 num_registrations += 2; /* Broadcast registrations */
265 list_for_each_entry(mc_entry, &port->mc_list->list,list)
266 num_registrations += 2;
269 if (num_registrations) {
270 arr = kcalloc(num_registrations, sizeof(*arr), GFP_ATOMIC);
271 if (!arr)
272 goto out; /* Keep the existing array */
273 } else
274 goto out_update;
276 list_for_each_entry(adapter, &adapter_list, list) {
277 for (k = 0; k < EHEA_MAX_PORTS; k++) {
278 struct ehea_port *port = adapter->port[k];
280 if (!port || (port->state != EHEA_PORT_UP))
281 continue;
283 if (num_registrations == 0)
284 goto out_update;
286 arr[i].adh = adapter->handle;
287 arr[i].port_id = port->logical_port_id;
288 arr[i].reg_type = EHEA_BCMC_BROADCAST |
289 EHEA_BCMC_UNTAGGED;
290 arr[i++].macaddr = port->mac_addr;
292 arr[i].adh = adapter->handle;
293 arr[i].port_id = port->logical_port_id;
294 arr[i].reg_type = EHEA_BCMC_BROADCAST |
295 EHEA_BCMC_VLANID_ALL;
296 arr[i++].macaddr = port->mac_addr;
297 num_registrations -= 2;
299 list_for_each_entry(mc_entry,
300 &port->mc_list->list, list) {
301 if (num_registrations == 0)
302 goto out_update;
304 arr[i].adh = adapter->handle;
305 arr[i].port_id = port->logical_port_id;
306 arr[i].reg_type = EHEA_BCMC_SCOPE_ALL |
307 EHEA_BCMC_MULTICAST |
308 EHEA_BCMC_UNTAGGED;
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_SCOPE_ALL |
314 EHEA_BCMC_MULTICAST |
315 EHEA_BCMC_VLANID_ALL;
316 arr[i++].macaddr = mc_entry->macaddr;
317 num_registrations -= 2;
322 out_update:
323 kfree(ehea_bcmc_regs.arr);
324 ehea_bcmc_regs.arr = arr;
325 ehea_bcmc_regs.num_entries = i;
326 out:
327 spin_unlock_irqrestore(&ehea_bcmc_regs.lock, flags);
330 static struct net_device_stats *ehea_get_stats(struct net_device *dev)
332 struct ehea_port *port = netdev_priv(dev);
333 struct net_device_stats *stats = &port->stats;
334 struct hcp_ehea_port_cb2 *cb2;
335 u64 hret, rx_packets, tx_packets, rx_bytes = 0, tx_bytes = 0;
336 int i;
338 memset(stats, 0, sizeof(*stats));
340 cb2 = (void *)get_zeroed_page(GFP_KERNEL);
341 if (!cb2) {
342 netdev_err(dev, "no mem for cb2\n");
343 goto out;
346 hret = ehea_h_query_ehea_port(port->adapter->handle,
347 port->logical_port_id,
348 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
349 if (hret != H_SUCCESS) {
350 netdev_err(dev, "query_ehea_port failed\n");
351 goto out_herr;
354 if (netif_msg_hw(port))
355 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
357 rx_packets = 0;
358 for (i = 0; i < port->num_def_qps; i++) {
359 rx_packets += port->port_res[i].rx_packets;
360 rx_bytes += port->port_res[i].rx_bytes;
363 tx_packets = 0;
364 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
365 tx_packets += port->port_res[i].tx_packets;
366 tx_bytes += port->port_res[i].tx_bytes;
369 stats->tx_packets = tx_packets;
370 stats->multicast = cb2->rxmcp;
371 stats->rx_errors = cb2->rxuerr;
372 stats->rx_bytes = rx_bytes;
373 stats->tx_bytes = tx_bytes;
374 stats->rx_packets = rx_packets;
376 out_herr:
377 free_page((unsigned long)cb2);
378 out:
379 return stats;
382 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
384 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
385 struct net_device *dev = pr->port->netdev;
386 int max_index_mask = pr->rq1_skba.len - 1;
387 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
388 int adder = 0;
389 int i;
391 pr->rq1_skba.os_skbs = 0;
393 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
394 if (nr_of_wqes > 0)
395 pr->rq1_skba.index = index;
396 pr->rq1_skba.os_skbs = fill_wqes;
397 return;
400 for (i = 0; i < fill_wqes; i++) {
401 if (!skb_arr_rq1[index]) {
402 skb_arr_rq1[index] = netdev_alloc_skb(dev,
403 EHEA_L_PKT_SIZE);
404 if (!skb_arr_rq1[index]) {
405 netdev_info(dev, "Unable to allocate enough skb in the array\n");
406 pr->rq1_skba.os_skbs = fill_wqes - i;
407 break;
410 index--;
411 index &= max_index_mask;
412 adder++;
415 if (adder == 0)
416 return;
418 /* Ring doorbell */
419 ehea_update_rq1a(pr->qp, adder);
422 static void ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
424 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
425 struct net_device *dev = pr->port->netdev;
426 int i;
428 if (nr_rq1a > pr->rq1_skba.len) {
429 netdev_err(dev, "NR_RQ1A bigger than skb array len\n");
430 return;
433 for (i = 0; i < nr_rq1a; i++) {
434 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
435 if (!skb_arr_rq1[i]) {
436 netdev_info(dev, "Not enough memory to allocate skb array\n");
437 break;
440 /* Ring doorbell */
441 ehea_update_rq1a(pr->qp, i - 1);
444 static int ehea_refill_rq_def(struct ehea_port_res *pr,
445 struct ehea_q_skb_arr *q_skba, int rq_nr,
446 int num_wqes, int wqe_type, int packet_size)
448 struct net_device *dev = pr->port->netdev;
449 struct ehea_qp *qp = pr->qp;
450 struct sk_buff **skb_arr = q_skba->arr;
451 struct ehea_rwqe *rwqe;
452 int i, index, max_index_mask, fill_wqes;
453 int adder = 0;
454 int ret = 0;
456 fill_wqes = q_skba->os_skbs + num_wqes;
457 q_skba->os_skbs = 0;
459 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
460 q_skba->os_skbs = fill_wqes;
461 return ret;
464 index = q_skba->index;
465 max_index_mask = q_skba->len - 1;
466 for (i = 0; i < fill_wqes; i++) {
467 u64 tmp_addr;
468 struct sk_buff *skb;
470 skb = netdev_alloc_skb_ip_align(dev, packet_size);
471 if (!skb) {
472 q_skba->os_skbs = fill_wqes - i;
473 if (q_skba->os_skbs == q_skba->len - 2) {
474 netdev_info(pr->port->netdev,
475 "rq%i ran dry - no mem for skb\n",
476 rq_nr);
477 ret = -ENOMEM;
479 break;
482 skb_arr[index] = skb;
483 tmp_addr = ehea_map_vaddr(skb->data);
484 if (tmp_addr == -1) {
485 dev_kfree_skb(skb);
486 q_skba->os_skbs = fill_wqes - i;
487 ret = 0;
488 break;
491 rwqe = ehea_get_next_rwqe(qp, rq_nr);
492 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
493 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
494 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
495 rwqe->sg_list[0].vaddr = tmp_addr;
496 rwqe->sg_list[0].len = packet_size;
497 rwqe->data_segments = 1;
499 index++;
500 index &= max_index_mask;
501 adder++;
504 q_skba->index = index;
505 if (adder == 0)
506 goto out;
508 /* Ring doorbell */
509 iosync();
510 if (rq_nr == 2)
511 ehea_update_rq2a(pr->qp, adder);
512 else
513 ehea_update_rq3a(pr->qp, adder);
514 out:
515 return ret;
519 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
521 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
522 nr_of_wqes, EHEA_RWQE2_TYPE,
523 EHEA_RQ2_PKT_SIZE);
527 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
529 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
530 nr_of_wqes, EHEA_RWQE3_TYPE,
531 EHEA_MAX_PACKET_SIZE);
534 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
536 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
537 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
538 return 0;
539 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
540 (cqe->header_length == 0))
541 return 0;
542 return -EINVAL;
545 static inline void ehea_fill_skb(struct net_device *dev,
546 struct sk_buff *skb, struct ehea_cqe *cqe)
548 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
550 skb_put(skb, length);
551 skb->protocol = eth_type_trans(skb, dev);
553 /* The packet was not an IPV4 packet so a complemented checksum was
554 calculated. The value is found in the Internet Checksum field. */
555 if (cqe->status & EHEA_CQE_BLIND_CKSUM) {
556 skb->ip_summed = CHECKSUM_COMPLETE;
557 skb->csum = csum_unfold(~cqe->inet_checksum_value);
558 } else
559 skb->ip_summed = CHECKSUM_UNNECESSARY;
562 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
563 int arr_len,
564 struct ehea_cqe *cqe)
566 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
567 struct sk_buff *skb;
568 void *pref;
569 int x;
571 x = skb_index + 1;
572 x &= (arr_len - 1);
574 pref = skb_array[x];
575 if (pref) {
576 prefetchw(pref);
577 prefetchw(pref + EHEA_CACHE_LINE);
579 pref = (skb_array[x]->data);
580 prefetch(pref);
581 prefetch(pref + EHEA_CACHE_LINE);
582 prefetch(pref + EHEA_CACHE_LINE * 2);
583 prefetch(pref + EHEA_CACHE_LINE * 3);
586 skb = skb_array[skb_index];
587 skb_array[skb_index] = NULL;
588 return skb;
591 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
592 int arr_len, int wqe_index)
594 struct sk_buff *skb;
595 void *pref;
596 int x;
598 x = wqe_index + 1;
599 x &= (arr_len - 1);
601 pref = skb_array[x];
602 if (pref) {
603 prefetchw(pref);
604 prefetchw(pref + EHEA_CACHE_LINE);
606 pref = (skb_array[x]->data);
607 prefetchw(pref);
608 prefetchw(pref + EHEA_CACHE_LINE);
611 skb = skb_array[wqe_index];
612 skb_array[wqe_index] = NULL;
613 return skb;
616 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
617 struct ehea_cqe *cqe, int *processed_rq2,
618 int *processed_rq3)
620 struct sk_buff *skb;
622 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
623 pr->p_stats.err_tcp_cksum++;
624 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
625 pr->p_stats.err_ip_cksum++;
626 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
627 pr->p_stats.err_frame_crc++;
629 if (rq == 2) {
630 *processed_rq2 += 1;
631 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
632 dev_kfree_skb(skb);
633 } else if (rq == 3) {
634 *processed_rq3 += 1;
635 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
636 dev_kfree_skb(skb);
639 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
640 if (netif_msg_rx_err(pr->port)) {
641 pr_err("Critical receive error for QP %d. Resetting port.\n",
642 pr->qp->init_attr.qp_nr);
643 ehea_dump(cqe, sizeof(*cqe), "CQE");
645 ehea_schedule_port_reset(pr->port);
646 return 1;
649 return 0;
652 static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
653 void **tcph, u64 *hdr_flags, void *priv)
655 struct ehea_cqe *cqe = priv;
656 unsigned int ip_len;
657 struct iphdr *iph;
659 /* non tcp/udp packets */
660 if (!cqe->header_length)
661 return -1;
663 /* non tcp packet */
664 skb_reset_network_header(skb);
665 iph = ip_hdr(skb);
666 if (iph->protocol != IPPROTO_TCP)
667 return -1;
669 ip_len = ip_hdrlen(skb);
670 skb_set_transport_header(skb, ip_len);
671 *tcph = tcp_hdr(skb);
673 /* check if ip header and tcp header are complete */
674 if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
675 return -1;
677 *hdr_flags = LRO_IPV4 | LRO_TCP;
678 *iphdr = iph;
680 return 0;
683 static void ehea_proc_skb(struct ehea_port_res *pr, struct ehea_cqe *cqe,
684 struct sk_buff *skb)
686 if (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
687 __vlan_hwaccel_put_tag(skb, cqe->vlan_tag);
689 if (skb->dev->features & NETIF_F_LRO)
690 lro_receive_skb(&pr->lro_mgr, skb, cqe);
691 else
692 netif_receive_skb(skb);
695 static int ehea_proc_rwqes(struct net_device *dev,
696 struct ehea_port_res *pr,
697 int budget)
699 struct ehea_port *port = pr->port;
700 struct ehea_qp *qp = pr->qp;
701 struct ehea_cqe *cqe;
702 struct sk_buff *skb;
703 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
704 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
705 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
706 int skb_arr_rq1_len = pr->rq1_skba.len;
707 int skb_arr_rq2_len = pr->rq2_skba.len;
708 int skb_arr_rq3_len = pr->rq3_skba.len;
709 int processed, processed_rq1, processed_rq2, processed_rq3;
710 u64 processed_bytes = 0;
711 int wqe_index, last_wqe_index, rq, port_reset;
713 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
714 last_wqe_index = 0;
716 cqe = ehea_poll_rq1(qp, &wqe_index);
717 while ((processed < budget) && cqe) {
718 ehea_inc_rq1(qp);
719 processed_rq1++;
720 processed++;
721 if (netif_msg_rx_status(port))
722 ehea_dump(cqe, sizeof(*cqe), "CQE");
724 last_wqe_index = wqe_index;
725 rmb();
726 if (!ehea_check_cqe(cqe, &rq)) {
727 if (rq == 1) {
728 /* LL RQ1 */
729 skb = get_skb_by_index_ll(skb_arr_rq1,
730 skb_arr_rq1_len,
731 wqe_index);
732 if (unlikely(!skb)) {
733 netif_info(port, rx_err, dev,
734 "LL rq1: skb=NULL\n");
736 skb = netdev_alloc_skb(dev,
737 EHEA_L_PKT_SIZE);
738 if (!skb) {
739 netdev_err(dev, "Not enough memory to allocate skb\n");
740 break;
743 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
744 cqe->num_bytes_transfered - 4);
745 ehea_fill_skb(dev, skb, cqe);
746 } else if (rq == 2) {
747 /* RQ2 */
748 skb = get_skb_by_index(skb_arr_rq2,
749 skb_arr_rq2_len, cqe);
750 if (unlikely(!skb)) {
751 netif_err(port, rx_err, dev,
752 "rq2: skb=NULL\n");
753 break;
755 ehea_fill_skb(dev, skb, cqe);
756 processed_rq2++;
757 } else {
758 /* RQ3 */
759 skb = get_skb_by_index(skb_arr_rq3,
760 skb_arr_rq3_len, cqe);
761 if (unlikely(!skb)) {
762 netif_err(port, rx_err, dev,
763 "rq3: skb=NULL\n");
764 break;
766 ehea_fill_skb(dev, skb, cqe);
767 processed_rq3++;
770 processed_bytes += skb->len;
771 ehea_proc_skb(pr, cqe, skb);
772 } else {
773 pr->p_stats.poll_receive_errors++;
774 port_reset = ehea_treat_poll_error(pr, rq, cqe,
775 &processed_rq2,
776 &processed_rq3);
777 if (port_reset)
778 break;
780 cqe = ehea_poll_rq1(qp, &wqe_index);
782 if (dev->features & NETIF_F_LRO)
783 lro_flush_all(&pr->lro_mgr);
785 pr->rx_packets += processed;
786 pr->rx_bytes += processed_bytes;
788 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
789 ehea_refill_rq2(pr, processed_rq2);
790 ehea_refill_rq3(pr, processed_rq3);
792 return processed;
795 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
797 static void reset_sq_restart_flag(struct ehea_port *port)
799 int i;
801 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
802 struct ehea_port_res *pr = &port->port_res[i];
803 pr->sq_restart_flag = 0;
805 wake_up(&port->restart_wq);
808 static void check_sqs(struct ehea_port *port)
810 struct ehea_swqe *swqe;
811 int swqe_index;
812 int i, k;
814 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
815 struct ehea_port_res *pr = &port->port_res[i];
816 int ret;
817 k = 0;
818 swqe = ehea_get_swqe(pr->qp, &swqe_index);
819 memset(swqe, 0, SWQE_HEADER_SIZE);
820 atomic_dec(&pr->swqe_avail);
822 swqe->tx_control |= EHEA_SWQE_PURGE;
823 swqe->wr_id = SWQE_RESTART_CHECK;
824 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
825 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
826 swqe->immediate_data_length = 80;
828 ehea_post_swqe(pr->qp, swqe);
830 ret = wait_event_timeout(port->restart_wq,
831 pr->sq_restart_flag == 0,
832 msecs_to_jiffies(100));
834 if (!ret) {
835 pr_err("HW/SW queues out of sync\n");
836 ehea_schedule_port_reset(pr->port);
837 return;
843 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
845 struct sk_buff *skb;
846 struct ehea_cq *send_cq = pr->send_cq;
847 struct ehea_cqe *cqe;
848 int quota = my_quota;
849 int cqe_counter = 0;
850 int swqe_av = 0;
851 int index;
852 unsigned long flags;
854 cqe = ehea_poll_cq(send_cq);
855 while (cqe && (quota > 0)) {
856 ehea_inc_cq(send_cq);
858 cqe_counter++;
859 rmb();
861 if (cqe->wr_id == SWQE_RESTART_CHECK) {
862 pr->sq_restart_flag = 1;
863 swqe_av++;
864 break;
867 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
868 pr_err("Bad send completion status=0x%04X\n",
869 cqe->status);
871 if (netif_msg_tx_err(pr->port))
872 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
874 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
875 pr_err("Resetting port\n");
876 ehea_schedule_port_reset(pr->port);
877 break;
881 if (netif_msg_tx_done(pr->port))
882 ehea_dump(cqe, sizeof(*cqe), "CQE");
884 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
885 == EHEA_SWQE2_TYPE)) {
887 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
888 skb = pr->sq_skba.arr[index];
889 dev_kfree_skb(skb);
890 pr->sq_skba.arr[index] = NULL;
893 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
894 quota--;
896 cqe = ehea_poll_cq(send_cq);
899 ehea_update_feca(send_cq, cqe_counter);
900 atomic_add(swqe_av, &pr->swqe_avail);
902 spin_lock_irqsave(&pr->netif_queue, flags);
904 if (pr->queue_stopped && (atomic_read(&pr->swqe_avail)
905 >= pr->swqe_refill_th)) {
906 netif_wake_queue(pr->port->netdev);
907 pr->queue_stopped = 0;
909 spin_unlock_irqrestore(&pr->netif_queue, flags);
910 wake_up(&pr->port->swqe_avail_wq);
912 return cqe;
915 #define EHEA_NAPI_POLL_NUM_BEFORE_IRQ 16
916 #define EHEA_POLL_MAX_CQES 65535
918 static int ehea_poll(struct napi_struct *napi, int budget)
920 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
921 napi);
922 struct net_device *dev = pr->port->netdev;
923 struct ehea_cqe *cqe;
924 struct ehea_cqe *cqe_skb = NULL;
925 int force_irq, wqe_index;
926 int rx = 0;
928 force_irq = (pr->poll_counter > EHEA_NAPI_POLL_NUM_BEFORE_IRQ);
929 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
931 if (!force_irq)
932 rx += ehea_proc_rwqes(dev, pr, budget - rx);
934 while ((rx != budget) || force_irq) {
935 pr->poll_counter = 0;
936 force_irq = 0;
937 napi_complete(napi);
938 ehea_reset_cq_ep(pr->recv_cq);
939 ehea_reset_cq_ep(pr->send_cq);
940 ehea_reset_cq_n1(pr->recv_cq);
941 ehea_reset_cq_n1(pr->send_cq);
942 rmb();
943 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
944 cqe_skb = ehea_poll_cq(pr->send_cq);
946 if (!cqe && !cqe_skb)
947 return rx;
949 if (!napi_reschedule(napi))
950 return rx;
952 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
953 rx += ehea_proc_rwqes(dev, pr, budget - rx);
956 pr->poll_counter++;
957 return rx;
960 #ifdef CONFIG_NET_POLL_CONTROLLER
961 static void ehea_netpoll(struct net_device *dev)
963 struct ehea_port *port = netdev_priv(dev);
964 int i;
966 for (i = 0; i < port->num_def_qps; i++)
967 napi_schedule(&port->port_res[i].napi);
969 #endif
971 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
973 struct ehea_port_res *pr = param;
975 napi_schedule(&pr->napi);
977 return IRQ_HANDLED;
980 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
982 struct ehea_port *port = param;
983 struct ehea_eqe *eqe;
984 struct ehea_qp *qp;
985 u32 qp_token;
986 u64 resource_type, aer, aerr;
987 int reset_port = 0;
989 eqe = ehea_poll_eq(port->qp_eq);
991 while (eqe) {
992 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
993 pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
994 eqe->entry, qp_token);
996 qp = port->port_res[qp_token].qp;
998 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
999 &aer, &aerr);
1001 if (resource_type == EHEA_AER_RESTYPE_QP) {
1002 if ((aer & EHEA_AER_RESET_MASK) ||
1003 (aerr & EHEA_AERR_RESET_MASK))
1004 reset_port = 1;
1005 } else
1006 reset_port = 1; /* Reset in case of CQ or EQ error */
1008 eqe = ehea_poll_eq(port->qp_eq);
1011 if (reset_port) {
1012 pr_err("Resetting port\n");
1013 ehea_schedule_port_reset(port);
1016 return IRQ_HANDLED;
1019 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
1020 int logical_port)
1022 int i;
1024 for (i = 0; i < EHEA_MAX_PORTS; i++)
1025 if (adapter->port[i])
1026 if (adapter->port[i]->logical_port_id == logical_port)
1027 return adapter->port[i];
1028 return NULL;
1031 int ehea_sense_port_attr(struct ehea_port *port)
1033 int ret;
1034 u64 hret;
1035 struct hcp_ehea_port_cb0 *cb0;
1037 /* may be called via ehea_neq_tasklet() */
1038 cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
1039 if (!cb0) {
1040 pr_err("no mem for cb0\n");
1041 ret = -ENOMEM;
1042 goto out;
1045 hret = ehea_h_query_ehea_port(port->adapter->handle,
1046 port->logical_port_id, H_PORT_CB0,
1047 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
1048 cb0);
1049 if (hret != H_SUCCESS) {
1050 ret = -EIO;
1051 goto out_free;
1054 /* MAC address */
1055 port->mac_addr = cb0->port_mac_addr << 16;
1057 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1058 ret = -EADDRNOTAVAIL;
1059 goto out_free;
1062 /* Port speed */
1063 switch (cb0->port_speed) {
1064 case H_SPEED_10M_H:
1065 port->port_speed = EHEA_SPEED_10M;
1066 port->full_duplex = 0;
1067 break;
1068 case H_SPEED_10M_F:
1069 port->port_speed = EHEA_SPEED_10M;
1070 port->full_duplex = 1;
1071 break;
1072 case H_SPEED_100M_H:
1073 port->port_speed = EHEA_SPEED_100M;
1074 port->full_duplex = 0;
1075 break;
1076 case H_SPEED_100M_F:
1077 port->port_speed = EHEA_SPEED_100M;
1078 port->full_duplex = 1;
1079 break;
1080 case H_SPEED_1G_F:
1081 port->port_speed = EHEA_SPEED_1G;
1082 port->full_duplex = 1;
1083 break;
1084 case H_SPEED_10G_F:
1085 port->port_speed = EHEA_SPEED_10G;
1086 port->full_duplex = 1;
1087 break;
1088 default:
1089 port->port_speed = 0;
1090 port->full_duplex = 0;
1091 break;
1094 port->autoneg = 1;
1095 port->num_mcs = cb0->num_default_qps;
1097 /* Number of default QPs */
1098 if (use_mcs)
1099 port->num_def_qps = cb0->num_default_qps;
1100 else
1101 port->num_def_qps = 1;
1103 if (!port->num_def_qps) {
1104 ret = -EINVAL;
1105 goto out_free;
1108 port->num_tx_qps = num_tx_qps;
1110 if (port->num_def_qps >= port->num_tx_qps)
1111 port->num_add_tx_qps = 0;
1112 else
1113 port->num_add_tx_qps = port->num_tx_qps - port->num_def_qps;
1115 ret = 0;
1116 out_free:
1117 if (ret || netif_msg_probe(port))
1118 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1119 free_page((unsigned long)cb0);
1120 out:
1121 return ret;
1124 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1126 struct hcp_ehea_port_cb4 *cb4;
1127 u64 hret;
1128 int ret = 0;
1130 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1131 if (!cb4) {
1132 pr_err("no mem for cb4\n");
1133 ret = -ENOMEM;
1134 goto out;
1137 cb4->port_speed = port_speed;
1139 netif_carrier_off(port->netdev);
1141 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1142 port->logical_port_id,
1143 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1144 if (hret == H_SUCCESS) {
1145 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1147 hret = ehea_h_query_ehea_port(port->adapter->handle,
1148 port->logical_port_id,
1149 H_PORT_CB4, H_PORT_CB4_SPEED,
1150 cb4);
1151 if (hret == H_SUCCESS) {
1152 switch (cb4->port_speed) {
1153 case H_SPEED_10M_H:
1154 port->port_speed = EHEA_SPEED_10M;
1155 port->full_duplex = 0;
1156 break;
1157 case H_SPEED_10M_F:
1158 port->port_speed = EHEA_SPEED_10M;
1159 port->full_duplex = 1;
1160 break;
1161 case H_SPEED_100M_H:
1162 port->port_speed = EHEA_SPEED_100M;
1163 port->full_duplex = 0;
1164 break;
1165 case H_SPEED_100M_F:
1166 port->port_speed = EHEA_SPEED_100M;
1167 port->full_duplex = 1;
1168 break;
1169 case H_SPEED_1G_F:
1170 port->port_speed = EHEA_SPEED_1G;
1171 port->full_duplex = 1;
1172 break;
1173 case H_SPEED_10G_F:
1174 port->port_speed = EHEA_SPEED_10G;
1175 port->full_duplex = 1;
1176 break;
1177 default:
1178 port->port_speed = 0;
1179 port->full_duplex = 0;
1180 break;
1182 } else {
1183 pr_err("Failed sensing port speed\n");
1184 ret = -EIO;
1186 } else {
1187 if (hret == H_AUTHORITY) {
1188 pr_info("Hypervisor denied setting port speed\n");
1189 ret = -EPERM;
1190 } else {
1191 ret = -EIO;
1192 pr_err("Failed setting port speed\n");
1195 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1196 netif_carrier_on(port->netdev);
1198 free_page((unsigned long)cb4);
1199 out:
1200 return ret;
1203 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1205 int ret;
1206 u8 ec;
1207 u8 portnum;
1208 struct ehea_port *port;
1209 struct net_device *dev;
1211 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1212 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1213 port = ehea_get_port(adapter, portnum);
1214 dev = port->netdev;
1216 switch (ec) {
1217 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1219 if (!port) {
1220 netdev_err(dev, "unknown portnum %x\n", portnum);
1221 break;
1224 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1225 if (!netif_carrier_ok(dev)) {
1226 ret = ehea_sense_port_attr(port);
1227 if (ret) {
1228 netdev_err(dev, "failed resensing port attributes\n");
1229 break;
1232 netif_info(port, link, dev,
1233 "Logical port up: %dMbps %s Duplex\n",
1234 port->port_speed,
1235 port->full_duplex == 1 ?
1236 "Full" : "Half");
1238 netif_carrier_on(dev);
1239 netif_wake_queue(dev);
1241 } else
1242 if (netif_carrier_ok(dev)) {
1243 netif_info(port, link, dev,
1244 "Logical port down\n");
1245 netif_carrier_off(dev);
1246 netif_stop_queue(dev);
1249 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1250 port->phy_link = EHEA_PHY_LINK_UP;
1251 netif_info(port, link, dev,
1252 "Physical port up\n");
1253 if (prop_carrier_state)
1254 netif_carrier_on(dev);
1255 } else {
1256 port->phy_link = EHEA_PHY_LINK_DOWN;
1257 netif_info(port, link, dev,
1258 "Physical port down\n");
1259 if (prop_carrier_state)
1260 netif_carrier_off(dev);
1263 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1264 netdev_info(dev,
1265 "External switch port is primary port\n");
1266 else
1267 netdev_info(dev,
1268 "External switch port is backup port\n");
1270 break;
1271 case EHEA_EC_ADAPTER_MALFUNC:
1272 netdev_err(dev, "Adapter malfunction\n");
1273 break;
1274 case EHEA_EC_PORT_MALFUNC:
1275 netdev_info(dev, "Port malfunction\n");
1276 netif_carrier_off(dev);
1277 netif_stop_queue(dev);
1278 break;
1279 default:
1280 netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1281 break;
1285 static void ehea_neq_tasklet(unsigned long data)
1287 struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1288 struct ehea_eqe *eqe;
1289 u64 event_mask;
1291 eqe = ehea_poll_eq(adapter->neq);
1292 pr_debug("eqe=%p\n", eqe);
1294 while (eqe) {
1295 pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1296 ehea_parse_eqe(adapter, eqe->entry);
1297 eqe = ehea_poll_eq(adapter->neq);
1298 pr_debug("next eqe=%p\n", eqe);
1301 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1302 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1303 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1305 ehea_h_reset_events(adapter->handle,
1306 adapter->neq->fw_handle, event_mask);
1309 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1311 struct ehea_adapter *adapter = param;
1312 tasklet_hi_schedule(&adapter->neq_tasklet);
1313 return IRQ_HANDLED;
1317 static int ehea_fill_port_res(struct ehea_port_res *pr)
1319 int ret;
1320 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1322 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1324 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1326 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1328 return ret;
1331 static int ehea_reg_interrupts(struct net_device *dev)
1333 struct ehea_port *port = netdev_priv(dev);
1334 struct ehea_port_res *pr;
1335 int i, ret;
1338 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1339 dev->name);
1341 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1342 ehea_qp_aff_irq_handler,
1343 IRQF_DISABLED, port->int_aff_name, port);
1344 if (ret) {
1345 netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1346 port->qp_eq->attr.ist1);
1347 goto out_free_qpeq;
1350 netif_info(port, ifup, dev,
1351 "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1352 port->qp_eq->attr.ist1);
1355 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1356 pr = &port->port_res[i];
1357 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1358 "%s-queue%d", dev->name, i);
1359 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1360 ehea_recv_irq_handler,
1361 IRQF_DISABLED, pr->int_send_name,
1362 pr);
1363 if (ret) {
1364 netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1365 i, pr->eq->attr.ist1);
1366 goto out_free_req;
1368 netif_info(port, ifup, dev,
1369 "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1370 pr->eq->attr.ist1, i);
1372 out:
1373 return ret;
1376 out_free_req:
1377 while (--i >= 0) {
1378 u32 ist = port->port_res[i].eq->attr.ist1;
1379 ibmebus_free_irq(ist, &port->port_res[i]);
1382 out_free_qpeq:
1383 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1384 i = port->num_def_qps;
1386 goto out;
1390 static void ehea_free_interrupts(struct net_device *dev)
1392 struct ehea_port *port = netdev_priv(dev);
1393 struct ehea_port_res *pr;
1394 int i;
1396 /* send */
1398 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1399 pr = &port->port_res[i];
1400 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1401 netif_info(port, intr, dev,
1402 "free send irq for res %d with handle 0x%X\n",
1403 i, pr->eq->attr.ist1);
1406 /* associated events */
1407 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1408 netif_info(port, intr, dev,
1409 "associated event interrupt for handle 0x%X freed\n",
1410 port->qp_eq->attr.ist1);
1413 static int ehea_configure_port(struct ehea_port *port)
1415 int ret, i;
1416 u64 hret, mask;
1417 struct hcp_ehea_port_cb0 *cb0;
1419 ret = -ENOMEM;
1420 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1421 if (!cb0)
1422 goto out;
1424 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1425 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1426 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1427 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1428 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1429 PXLY_RC_VLAN_FILTER)
1430 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1432 for (i = 0; i < port->num_mcs; i++)
1433 if (use_mcs)
1434 cb0->default_qpn_arr[i] =
1435 port->port_res[i].qp->init_attr.qp_nr;
1436 else
1437 cb0->default_qpn_arr[i] =
1438 port->port_res[0].qp->init_attr.qp_nr;
1440 if (netif_msg_ifup(port))
1441 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1443 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1444 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1446 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1447 port->logical_port_id,
1448 H_PORT_CB0, mask, cb0);
1449 ret = -EIO;
1450 if (hret != H_SUCCESS)
1451 goto out_free;
1453 ret = 0;
1455 out_free:
1456 free_page((unsigned long)cb0);
1457 out:
1458 return ret;
1461 int ehea_gen_smrs(struct ehea_port_res *pr)
1463 int ret;
1464 struct ehea_adapter *adapter = pr->port->adapter;
1466 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1467 if (ret)
1468 goto out;
1470 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1471 if (ret)
1472 goto out_free;
1474 return 0;
1476 out_free:
1477 ehea_rem_mr(&pr->send_mr);
1478 out:
1479 pr_err("Generating SMRS failed\n");
1480 return -EIO;
1483 int ehea_rem_smrs(struct ehea_port_res *pr)
1485 if ((ehea_rem_mr(&pr->send_mr)) ||
1486 (ehea_rem_mr(&pr->recv_mr)))
1487 return -EIO;
1488 else
1489 return 0;
1492 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1494 int arr_size = sizeof(void *) * max_q_entries;
1496 q_skba->arr = vzalloc(arr_size);
1497 if (!q_skba->arr)
1498 return -ENOMEM;
1500 q_skba->len = max_q_entries;
1501 q_skba->index = 0;
1502 q_skba->os_skbs = 0;
1504 return 0;
1507 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1508 struct port_res_cfg *pr_cfg, int queue_token)
1510 struct ehea_adapter *adapter = port->adapter;
1511 enum ehea_eq_type eq_type = EHEA_EQ;
1512 struct ehea_qp_init_attr *init_attr = NULL;
1513 int ret = -EIO;
1514 u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1516 tx_bytes = pr->tx_bytes;
1517 tx_packets = pr->tx_packets;
1518 rx_bytes = pr->rx_bytes;
1519 rx_packets = pr->rx_packets;
1521 memset(pr, 0, sizeof(struct ehea_port_res));
1523 pr->tx_bytes = rx_bytes;
1524 pr->tx_packets = tx_packets;
1525 pr->rx_bytes = rx_bytes;
1526 pr->rx_packets = rx_packets;
1528 pr->port = port;
1529 spin_lock_init(&pr->xmit_lock);
1530 spin_lock_init(&pr->netif_queue);
1532 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1533 if (!pr->eq) {
1534 pr_err("create_eq failed (eq)\n");
1535 goto out_free;
1538 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1539 pr->eq->fw_handle,
1540 port->logical_port_id);
1541 if (!pr->recv_cq) {
1542 pr_err("create_cq failed (cq_recv)\n");
1543 goto out_free;
1546 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1547 pr->eq->fw_handle,
1548 port->logical_port_id);
1549 if (!pr->send_cq) {
1550 pr_err("create_cq failed (cq_send)\n");
1551 goto out_free;
1554 if (netif_msg_ifup(port))
1555 pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1556 pr->send_cq->attr.act_nr_of_cqes,
1557 pr->recv_cq->attr.act_nr_of_cqes);
1559 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1560 if (!init_attr) {
1561 ret = -ENOMEM;
1562 pr_err("no mem for ehea_qp_init_attr\n");
1563 goto out_free;
1566 init_attr->low_lat_rq1 = 1;
1567 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1568 init_attr->rq_count = 3;
1569 init_attr->qp_token = queue_token;
1570 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1571 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1572 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1573 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1574 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1575 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1576 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1577 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1578 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1579 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1580 init_attr->port_nr = port->logical_port_id;
1581 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1582 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1583 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1585 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1586 if (!pr->qp) {
1587 pr_err("create_qp failed\n");
1588 ret = -EIO;
1589 goto out_free;
1592 if (netif_msg_ifup(port))
1593 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",
1594 init_attr->qp_nr,
1595 init_attr->act_nr_send_wqes,
1596 init_attr->act_nr_rwqes_rq1,
1597 init_attr->act_nr_rwqes_rq2,
1598 init_attr->act_nr_rwqes_rq3);
1600 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1602 ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1603 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1604 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1605 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1606 if (ret)
1607 goto out_free;
1609 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1610 if (ehea_gen_smrs(pr) != 0) {
1611 ret = -EIO;
1612 goto out_free;
1615 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1617 kfree(init_attr);
1619 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1621 pr->lro_mgr.max_aggr = pr->port->lro_max_aggr;
1622 pr->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1623 pr->lro_mgr.lro_arr = pr->lro_desc;
1624 pr->lro_mgr.get_skb_header = get_skb_hdr;
1625 pr->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1626 pr->lro_mgr.dev = port->netdev;
1627 pr->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1628 pr->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1630 ret = 0;
1631 goto out;
1633 out_free:
1634 kfree(init_attr);
1635 vfree(pr->sq_skba.arr);
1636 vfree(pr->rq1_skba.arr);
1637 vfree(pr->rq2_skba.arr);
1638 vfree(pr->rq3_skba.arr);
1639 ehea_destroy_qp(pr->qp);
1640 ehea_destroy_cq(pr->send_cq);
1641 ehea_destroy_cq(pr->recv_cq);
1642 ehea_destroy_eq(pr->eq);
1643 out:
1644 return ret;
1647 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1649 int ret, i;
1651 if (pr->qp)
1652 netif_napi_del(&pr->napi);
1654 ret = ehea_destroy_qp(pr->qp);
1656 if (!ret) {
1657 ehea_destroy_cq(pr->send_cq);
1658 ehea_destroy_cq(pr->recv_cq);
1659 ehea_destroy_eq(pr->eq);
1661 for (i = 0; i < pr->rq1_skba.len; i++)
1662 if (pr->rq1_skba.arr[i])
1663 dev_kfree_skb(pr->rq1_skba.arr[i]);
1665 for (i = 0; i < pr->rq2_skba.len; i++)
1666 if (pr->rq2_skba.arr[i])
1667 dev_kfree_skb(pr->rq2_skba.arr[i]);
1669 for (i = 0; i < pr->rq3_skba.len; i++)
1670 if (pr->rq3_skba.arr[i])
1671 dev_kfree_skb(pr->rq3_skba.arr[i]);
1673 for (i = 0; i < pr->sq_skba.len; i++)
1674 if (pr->sq_skba.arr[i])
1675 dev_kfree_skb(pr->sq_skba.arr[i]);
1677 vfree(pr->rq1_skba.arr);
1678 vfree(pr->rq2_skba.arr);
1679 vfree(pr->rq3_skba.arr);
1680 vfree(pr->sq_skba.arr);
1681 ret = ehea_rem_smrs(pr);
1683 return ret;
1687 * The write_* functions store information in swqe which is used by
1688 * the hardware to calculate the ip/tcp/udp checksum
1691 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1692 const struct sk_buff *skb)
1694 swqe->ip_start = skb_network_offset(skb);
1695 swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1698 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1699 const struct sk_buff *skb)
1701 swqe->tcp_offset =
1702 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1704 swqe->tcp_end = (u16)skb->len - 1;
1707 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1708 const struct sk_buff *skb)
1710 swqe->tcp_offset =
1711 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1713 swqe->tcp_end = (u16)skb->len - 1;
1717 static void write_swqe2_TSO(struct sk_buff *skb,
1718 struct ehea_swqe *swqe, u32 lkey)
1720 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1721 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1722 int skb_data_size = skb_headlen(skb);
1723 int headersize;
1725 /* Packet is TCP with TSO enabled */
1726 swqe->tx_control |= EHEA_SWQE_TSO;
1727 swqe->mss = skb_shinfo(skb)->gso_size;
1728 /* copy only eth/ip/tcp headers to immediate data and
1729 * the rest of skb->data to sg1entry
1731 headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1733 skb_data_size = skb_headlen(skb);
1735 if (skb_data_size >= headersize) {
1736 /* copy immediate data */
1737 skb_copy_from_linear_data(skb, imm_data, headersize);
1738 swqe->immediate_data_length = headersize;
1740 if (skb_data_size > headersize) {
1741 /* set sg1entry data */
1742 sg1entry->l_key = lkey;
1743 sg1entry->len = skb_data_size - headersize;
1744 sg1entry->vaddr =
1745 ehea_map_vaddr(skb->data + headersize);
1746 swqe->descriptors++;
1748 } else
1749 pr_err("cannot handle fragmented headers\n");
1752 static void write_swqe2_nonTSO(struct sk_buff *skb,
1753 struct ehea_swqe *swqe, u32 lkey)
1755 int skb_data_size = skb_headlen(skb);
1756 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1757 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1759 /* Packet is any nonTSO type
1761 * Copy as much as possible skb->data to immediate data and
1762 * the rest to sg1entry
1764 if (skb_data_size >= SWQE2_MAX_IMM) {
1765 /* copy immediate data */
1766 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1768 swqe->immediate_data_length = SWQE2_MAX_IMM;
1770 if (skb_data_size > SWQE2_MAX_IMM) {
1771 /* copy sg1entry data */
1772 sg1entry->l_key = lkey;
1773 sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1774 sg1entry->vaddr =
1775 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1776 swqe->descriptors++;
1778 } else {
1779 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1780 swqe->immediate_data_length = skb_data_size;
1784 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1785 struct ehea_swqe *swqe, u32 lkey)
1787 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1788 skb_frag_t *frag;
1789 int nfrags, sg1entry_contains_frag_data, i;
1791 nfrags = skb_shinfo(skb)->nr_frags;
1792 sg1entry = &swqe->u.immdata_desc.sg_entry;
1793 sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1794 swqe->descriptors = 0;
1795 sg1entry_contains_frag_data = 0;
1797 if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1798 write_swqe2_TSO(skb, swqe, lkey);
1799 else
1800 write_swqe2_nonTSO(skb, swqe, lkey);
1802 /* write descriptors */
1803 if (nfrags > 0) {
1804 if (swqe->descriptors == 0) {
1805 /* sg1entry not yet used */
1806 frag = &skb_shinfo(skb)->frags[0];
1808 /* copy sg1entry data */
1809 sg1entry->l_key = lkey;
1810 sg1entry->len = frag->size;
1811 sg1entry->vaddr =
1812 ehea_map_vaddr(page_address(frag->page)
1813 + frag->page_offset);
1814 swqe->descriptors++;
1815 sg1entry_contains_frag_data = 1;
1818 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1820 frag = &skb_shinfo(skb)->frags[i];
1821 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1823 sgentry->l_key = lkey;
1824 sgentry->len = frag->size;
1825 sgentry->vaddr =
1826 ehea_map_vaddr(page_address(frag->page)
1827 + frag->page_offset);
1828 swqe->descriptors++;
1833 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1835 int ret = 0;
1836 u64 hret;
1837 u8 reg_type;
1839 /* De/Register untagged packets */
1840 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1841 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1842 port->logical_port_id,
1843 reg_type, port->mac_addr, 0, hcallid);
1844 if (hret != H_SUCCESS) {
1845 pr_err("%sregistering bc address failed (tagged)\n",
1846 hcallid == H_REG_BCMC ? "" : "de");
1847 ret = -EIO;
1848 goto out_herr;
1851 /* De/Register VLAN packets */
1852 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1853 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1854 port->logical_port_id,
1855 reg_type, port->mac_addr, 0, hcallid);
1856 if (hret != H_SUCCESS) {
1857 pr_err("%sregistering bc address failed (vlan)\n",
1858 hcallid == H_REG_BCMC ? "" : "de");
1859 ret = -EIO;
1861 out_herr:
1862 return ret;
1865 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1867 struct ehea_port *port = netdev_priv(dev);
1868 struct sockaddr *mac_addr = sa;
1869 struct hcp_ehea_port_cb0 *cb0;
1870 int ret;
1871 u64 hret;
1873 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1874 ret = -EADDRNOTAVAIL;
1875 goto out;
1878 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1879 if (!cb0) {
1880 pr_err("no mem for cb0\n");
1881 ret = -ENOMEM;
1882 goto out;
1885 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1887 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1889 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1890 port->logical_port_id, H_PORT_CB0,
1891 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1892 if (hret != H_SUCCESS) {
1893 ret = -EIO;
1894 goto out_free;
1897 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1899 /* Deregister old MAC in pHYP */
1900 if (port->state == EHEA_PORT_UP) {
1901 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1902 if (ret)
1903 goto out_upregs;
1906 port->mac_addr = cb0->port_mac_addr << 16;
1908 /* Register new MAC in pHYP */
1909 if (port->state == EHEA_PORT_UP) {
1910 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1911 if (ret)
1912 goto out_upregs;
1915 ret = 0;
1917 out_upregs:
1918 ehea_update_bcmc_registrations();
1919 out_free:
1920 free_page((unsigned long)cb0);
1921 out:
1922 return ret;
1925 static void ehea_promiscuous_error(u64 hret, int enable)
1927 if (hret == H_AUTHORITY)
1928 pr_info("Hypervisor denied %sabling promiscuous mode\n",
1929 enable == 1 ? "en" : "dis");
1930 else
1931 pr_err("failed %sabling promiscuous mode\n",
1932 enable == 1 ? "en" : "dis");
1935 static void ehea_promiscuous(struct net_device *dev, int enable)
1937 struct ehea_port *port = netdev_priv(dev);
1938 struct hcp_ehea_port_cb7 *cb7;
1939 u64 hret;
1941 if (enable == port->promisc)
1942 return;
1944 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1945 if (!cb7) {
1946 pr_err("no mem for cb7\n");
1947 goto out;
1950 /* Modify Pxs_DUCQPN in CB7 */
1951 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1953 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1954 port->logical_port_id,
1955 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1956 if (hret) {
1957 ehea_promiscuous_error(hret, enable);
1958 goto out;
1961 port->promisc = enable;
1962 out:
1963 free_page((unsigned long)cb7);
1966 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1967 u32 hcallid)
1969 u64 hret;
1970 u8 reg_type;
1972 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1973 | EHEA_BCMC_UNTAGGED;
1975 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1976 port->logical_port_id,
1977 reg_type, mc_mac_addr, 0, hcallid);
1978 if (hret)
1979 goto out;
1981 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1982 | EHEA_BCMC_VLANID_ALL;
1984 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1985 port->logical_port_id,
1986 reg_type, mc_mac_addr, 0, hcallid);
1987 out:
1988 return hret;
1991 static int ehea_drop_multicast_list(struct net_device *dev)
1993 struct ehea_port *port = netdev_priv(dev);
1994 struct ehea_mc_list *mc_entry = port->mc_list;
1995 struct list_head *pos;
1996 struct list_head *temp;
1997 int ret = 0;
1998 u64 hret;
2000 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
2001 mc_entry = list_entry(pos, struct ehea_mc_list, list);
2003 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
2004 H_DEREG_BCMC);
2005 if (hret) {
2006 pr_err("failed deregistering mcast MAC\n");
2007 ret = -EIO;
2010 list_del(pos);
2011 kfree(mc_entry);
2013 return ret;
2016 static void ehea_allmulti(struct net_device *dev, int enable)
2018 struct ehea_port *port = netdev_priv(dev);
2019 u64 hret;
2021 if (!port->allmulti) {
2022 if (enable) {
2023 /* Enable ALLMULTI */
2024 ehea_drop_multicast_list(dev);
2025 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
2026 if (!hret)
2027 port->allmulti = 1;
2028 else
2029 netdev_err(dev,
2030 "failed enabling IFF_ALLMULTI\n");
2032 } else
2033 if (!enable) {
2034 /* Disable ALLMULTI */
2035 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
2036 if (!hret)
2037 port->allmulti = 0;
2038 else
2039 netdev_err(dev,
2040 "failed disabling IFF_ALLMULTI\n");
2044 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
2046 struct ehea_mc_list *ehea_mcl_entry;
2047 u64 hret;
2049 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
2050 if (!ehea_mcl_entry) {
2051 pr_err("no mem for mcl_entry\n");
2052 return;
2055 INIT_LIST_HEAD(&ehea_mcl_entry->list);
2057 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
2059 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
2060 H_REG_BCMC);
2061 if (!hret)
2062 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
2063 else {
2064 pr_err("failed registering mcast MAC\n");
2065 kfree(ehea_mcl_entry);
2069 static void ehea_set_multicast_list(struct net_device *dev)
2071 struct ehea_port *port = netdev_priv(dev);
2072 struct netdev_hw_addr *ha;
2073 int ret;
2075 if (port->promisc) {
2076 ehea_promiscuous(dev, 1);
2077 return;
2079 ehea_promiscuous(dev, 0);
2081 if (dev->flags & IFF_ALLMULTI) {
2082 ehea_allmulti(dev, 1);
2083 goto out;
2085 ehea_allmulti(dev, 0);
2087 if (!netdev_mc_empty(dev)) {
2088 ret = ehea_drop_multicast_list(dev);
2089 if (ret) {
2090 /* Dropping the current multicast list failed.
2091 * Enabling ALL_MULTI is the best we can do.
2093 ehea_allmulti(dev, 1);
2096 if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
2097 pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
2098 port->adapter->max_mc_mac);
2099 goto out;
2102 netdev_for_each_mc_addr(ha, dev)
2103 ehea_add_multicast_entry(port, ha->addr);
2106 out:
2107 ehea_update_bcmc_registrations();
2110 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
2112 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
2113 return -EINVAL;
2114 dev->mtu = new_mtu;
2115 return 0;
2118 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2119 struct ehea_swqe *swqe, u32 lkey)
2121 if (skb->protocol == htons(ETH_P_IP)) {
2122 const struct iphdr *iph = ip_hdr(skb);
2124 /* IPv4 */
2125 swqe->tx_control |= EHEA_SWQE_CRC
2126 | EHEA_SWQE_IP_CHECKSUM
2127 | EHEA_SWQE_TCP_CHECKSUM
2128 | EHEA_SWQE_IMM_DATA_PRESENT
2129 | EHEA_SWQE_DESCRIPTORS_PRESENT;
2131 write_ip_start_end(swqe, skb);
2133 if (iph->protocol == IPPROTO_UDP) {
2134 if ((iph->frag_off & IP_MF) ||
2135 (iph->frag_off & IP_OFFSET))
2136 /* IP fragment, so don't change cs */
2137 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
2138 else
2139 write_udp_offset_end(swqe, skb);
2140 } else if (iph->protocol == IPPROTO_TCP) {
2141 write_tcp_offset_end(swqe, skb);
2144 /* icmp (big data) and ip segmentation packets (all other ip
2145 packets) do not require any special handling */
2147 } else {
2148 /* Other Ethernet Protocol */
2149 swqe->tx_control |= EHEA_SWQE_CRC
2150 | EHEA_SWQE_IMM_DATA_PRESENT
2151 | EHEA_SWQE_DESCRIPTORS_PRESENT;
2154 write_swqe2_data(skb, dev, swqe, lkey);
2157 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2158 struct ehea_swqe *swqe)
2160 int nfrags = skb_shinfo(skb)->nr_frags;
2161 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2162 skb_frag_t *frag;
2163 int i;
2165 if (skb->protocol == htons(ETH_P_IP)) {
2166 const struct iphdr *iph = ip_hdr(skb);
2168 /* IPv4 */
2169 write_ip_start_end(swqe, skb);
2171 if (iph->protocol == IPPROTO_TCP) {
2172 swqe->tx_control |= EHEA_SWQE_CRC
2173 | EHEA_SWQE_IP_CHECKSUM
2174 | EHEA_SWQE_TCP_CHECKSUM
2175 | EHEA_SWQE_IMM_DATA_PRESENT;
2177 write_tcp_offset_end(swqe, skb);
2179 } else if (iph->protocol == IPPROTO_UDP) {
2180 if ((iph->frag_off & IP_MF) ||
2181 (iph->frag_off & IP_OFFSET))
2182 /* IP fragment, so don't change cs */
2183 swqe->tx_control |= EHEA_SWQE_CRC
2184 | EHEA_SWQE_IMM_DATA_PRESENT;
2185 else {
2186 swqe->tx_control |= EHEA_SWQE_CRC
2187 | EHEA_SWQE_IP_CHECKSUM
2188 | EHEA_SWQE_TCP_CHECKSUM
2189 | EHEA_SWQE_IMM_DATA_PRESENT;
2191 write_udp_offset_end(swqe, skb);
2193 } else {
2194 /* icmp (big data) and
2195 ip segmentation packets (all other ip packets) */
2196 swqe->tx_control |= EHEA_SWQE_CRC
2197 | EHEA_SWQE_IP_CHECKSUM
2198 | EHEA_SWQE_IMM_DATA_PRESENT;
2200 } else {
2201 /* Other Ethernet Protocol */
2202 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
2204 /* copy (immediate) data */
2205 if (nfrags == 0) {
2206 /* data is in a single piece */
2207 skb_copy_from_linear_data(skb, imm_data, skb->len);
2208 } else {
2209 /* first copy data from the skb->data buffer ... */
2210 skb_copy_from_linear_data(skb, imm_data,
2211 skb_headlen(skb));
2212 imm_data += skb_headlen(skb);
2214 /* ... then copy data from the fragments */
2215 for (i = 0; i < nfrags; i++) {
2216 frag = &skb_shinfo(skb)->frags[i];
2217 memcpy(imm_data,
2218 page_address(frag->page) + frag->page_offset,
2219 frag->size);
2220 imm_data += frag->size;
2223 swqe->immediate_data_length = skb->len;
2224 dev_kfree_skb(skb);
2227 static inline int ehea_hash_skb(struct sk_buff *skb, int num_qps)
2229 struct tcphdr *tcp;
2230 u32 tmp;
2232 if ((skb->protocol == htons(ETH_P_IP)) &&
2233 (ip_hdr(skb)->protocol == IPPROTO_TCP)) {
2234 tcp = (struct tcphdr *)(skb_network_header(skb) +
2235 (ip_hdr(skb)->ihl * 4));
2236 tmp = (tcp->source + (tcp->dest << 16)) % 31;
2237 tmp += ip_hdr(skb)->daddr % 31;
2238 return tmp % num_qps;
2239 } else
2240 return 0;
2243 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2245 struct ehea_port *port = netdev_priv(dev);
2246 struct ehea_swqe *swqe;
2247 unsigned long flags;
2248 u32 lkey;
2249 int swqe_index;
2250 struct ehea_port_res *pr;
2252 pr = &port->port_res[ehea_hash_skb(skb, port->num_tx_qps)];
2254 if (!spin_trylock(&pr->xmit_lock))
2255 return NETDEV_TX_BUSY;
2257 if (pr->queue_stopped) {
2258 spin_unlock(&pr->xmit_lock);
2259 return NETDEV_TX_BUSY;
2262 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2263 memset(swqe, 0, SWQE_HEADER_SIZE);
2264 atomic_dec(&pr->swqe_avail);
2266 if (vlan_tx_tag_present(skb)) {
2267 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2268 swqe->vlan_tag = vlan_tx_tag_get(skb);
2271 pr->tx_packets++;
2272 pr->tx_bytes += skb->len;
2274 if (skb->len <= SWQE3_MAX_IMM) {
2275 u32 sig_iv = port->sig_comp_iv;
2276 u32 swqe_num = pr->swqe_id_counter;
2277 ehea_xmit3(skb, dev, swqe);
2278 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2279 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2280 if (pr->swqe_ll_count >= (sig_iv - 1)) {
2281 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2282 sig_iv);
2283 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2284 pr->swqe_ll_count = 0;
2285 } else
2286 pr->swqe_ll_count += 1;
2287 } else {
2288 swqe->wr_id =
2289 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2290 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2291 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2292 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2293 pr->sq_skba.arr[pr->sq_skba.index] = skb;
2295 pr->sq_skba.index++;
2296 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2298 lkey = pr->send_mr.lkey;
2299 ehea_xmit2(skb, dev, swqe, lkey);
2300 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2302 pr->swqe_id_counter += 1;
2304 netif_info(port, tx_queued, dev,
2305 "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2306 if (netif_msg_tx_queued(port))
2307 ehea_dump(swqe, 512, "swqe");
2309 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2310 netif_stop_queue(dev);
2311 swqe->tx_control |= EHEA_SWQE_PURGE;
2314 ehea_post_swqe(pr->qp, swqe);
2316 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2317 spin_lock_irqsave(&pr->netif_queue, flags);
2318 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2319 pr->p_stats.queue_stopped++;
2320 netif_stop_queue(dev);
2321 pr->queue_stopped = 1;
2323 spin_unlock_irqrestore(&pr->netif_queue, flags);
2325 dev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
2326 spin_unlock(&pr->xmit_lock);
2328 return NETDEV_TX_OK;
2331 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
2333 struct ehea_port *port = netdev_priv(dev);
2334 struct ehea_adapter *adapter = port->adapter;
2335 struct hcp_ehea_port_cb1 *cb1;
2336 int index;
2337 u64 hret;
2339 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2340 if (!cb1) {
2341 pr_err("no mem for cb1\n");
2342 goto out;
2345 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2346 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2347 if (hret != H_SUCCESS) {
2348 pr_err("query_ehea_port failed\n");
2349 goto out;
2352 index = (vid / 64);
2353 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2355 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2356 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2357 if (hret != H_SUCCESS)
2358 pr_err("modify_ehea_port failed\n");
2359 out:
2360 free_page((unsigned long)cb1);
2361 return;
2364 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2366 struct ehea_port *port = netdev_priv(dev);
2367 struct ehea_adapter *adapter = port->adapter;
2368 struct hcp_ehea_port_cb1 *cb1;
2369 int index;
2370 u64 hret;
2372 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2373 if (!cb1) {
2374 pr_err("no mem for cb1\n");
2375 goto out;
2378 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2379 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2380 if (hret != H_SUCCESS) {
2381 pr_err("query_ehea_port failed\n");
2382 goto out;
2385 index = (vid / 64);
2386 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2388 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2389 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2390 if (hret != H_SUCCESS)
2391 pr_err("modify_ehea_port failed\n");
2392 out:
2393 free_page((unsigned long)cb1);
2396 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2398 int ret = -EIO;
2399 u64 hret;
2400 u16 dummy16 = 0;
2401 u64 dummy64 = 0;
2402 struct hcp_modify_qp_cb0 *cb0;
2404 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2405 if (!cb0) {
2406 ret = -ENOMEM;
2407 goto out;
2410 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2411 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2412 if (hret != H_SUCCESS) {
2413 pr_err("query_ehea_qp failed (1)\n");
2414 goto out;
2417 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2418 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2419 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2420 &dummy64, &dummy64, &dummy16, &dummy16);
2421 if (hret != H_SUCCESS) {
2422 pr_err("modify_ehea_qp failed (1)\n");
2423 goto out;
2426 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2427 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2428 if (hret != H_SUCCESS) {
2429 pr_err("query_ehea_qp failed (2)\n");
2430 goto out;
2433 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2434 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2435 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2436 &dummy64, &dummy64, &dummy16, &dummy16);
2437 if (hret != H_SUCCESS) {
2438 pr_err("modify_ehea_qp failed (2)\n");
2439 goto out;
2442 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2443 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2444 if (hret != H_SUCCESS) {
2445 pr_err("query_ehea_qp failed (3)\n");
2446 goto out;
2449 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2450 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2451 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2452 &dummy64, &dummy64, &dummy16, &dummy16);
2453 if (hret != H_SUCCESS) {
2454 pr_err("modify_ehea_qp failed (3)\n");
2455 goto out;
2458 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2459 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2460 if (hret != H_SUCCESS) {
2461 pr_err("query_ehea_qp failed (4)\n");
2462 goto out;
2465 ret = 0;
2466 out:
2467 free_page((unsigned long)cb0);
2468 return ret;
2471 static int ehea_port_res_setup(struct ehea_port *port, int def_qps,
2472 int add_tx_qps)
2474 int ret, i;
2475 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2476 enum ehea_eq_type eq_type = EHEA_EQ;
2478 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2479 EHEA_MAX_ENTRIES_EQ, 1);
2480 if (!port->qp_eq) {
2481 ret = -EINVAL;
2482 pr_err("ehea_create_eq failed (qp_eq)\n");
2483 goto out_kill_eq;
2486 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2487 pr_cfg.max_entries_scq = sq_entries * 2;
2488 pr_cfg.max_entries_sq = sq_entries;
2489 pr_cfg.max_entries_rq1 = rq1_entries;
2490 pr_cfg.max_entries_rq2 = rq2_entries;
2491 pr_cfg.max_entries_rq3 = rq3_entries;
2493 pr_cfg_small_rx.max_entries_rcq = 1;
2494 pr_cfg_small_rx.max_entries_scq = sq_entries;
2495 pr_cfg_small_rx.max_entries_sq = sq_entries;
2496 pr_cfg_small_rx.max_entries_rq1 = 1;
2497 pr_cfg_small_rx.max_entries_rq2 = 1;
2498 pr_cfg_small_rx.max_entries_rq3 = 1;
2500 for (i = 0; i < def_qps; i++) {
2501 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2502 if (ret)
2503 goto out_clean_pr;
2505 for (i = def_qps; i < def_qps + add_tx_qps; i++) {
2506 ret = ehea_init_port_res(port, &port->port_res[i],
2507 &pr_cfg_small_rx, i);
2508 if (ret)
2509 goto out_clean_pr;
2512 return 0;
2514 out_clean_pr:
2515 while (--i >= 0)
2516 ehea_clean_portres(port, &port->port_res[i]);
2518 out_kill_eq:
2519 ehea_destroy_eq(port->qp_eq);
2520 return ret;
2523 static int ehea_clean_all_portres(struct ehea_port *port)
2525 int ret = 0;
2526 int i;
2528 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2529 ret |= ehea_clean_portres(port, &port->port_res[i]);
2531 ret |= ehea_destroy_eq(port->qp_eq);
2533 return ret;
2536 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2538 if (adapter->active_ports)
2539 return;
2541 ehea_rem_mr(&adapter->mr);
2544 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2546 if (adapter->active_ports)
2547 return 0;
2549 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2552 static int ehea_up(struct net_device *dev)
2554 int ret, i;
2555 struct ehea_port *port = netdev_priv(dev);
2557 if (port->state == EHEA_PORT_UP)
2558 return 0;
2560 ret = ehea_port_res_setup(port, port->num_def_qps,
2561 port->num_add_tx_qps);
2562 if (ret) {
2563 netdev_err(dev, "port_res_failed\n");
2564 goto out;
2567 /* Set default QP for this port */
2568 ret = ehea_configure_port(port);
2569 if (ret) {
2570 netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2571 goto out_clean_pr;
2574 ret = ehea_reg_interrupts(dev);
2575 if (ret) {
2576 netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2577 goto out_clean_pr;
2580 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2581 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2582 if (ret) {
2583 netdev_err(dev, "activate_qp failed\n");
2584 goto out_free_irqs;
2588 for (i = 0; i < port->num_def_qps; i++) {
2589 ret = ehea_fill_port_res(&port->port_res[i]);
2590 if (ret) {
2591 netdev_err(dev, "out_free_irqs\n");
2592 goto out_free_irqs;
2596 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2597 if (ret) {
2598 ret = -EIO;
2599 goto out_free_irqs;
2602 port->state = EHEA_PORT_UP;
2604 ret = 0;
2605 goto out;
2607 out_free_irqs:
2608 ehea_free_interrupts(dev);
2610 out_clean_pr:
2611 ehea_clean_all_portres(port);
2612 out:
2613 if (ret)
2614 netdev_info(dev, "Failed starting. ret=%i\n", ret);
2616 ehea_update_bcmc_registrations();
2617 ehea_update_firmware_handles();
2619 return ret;
2622 static void port_napi_disable(struct ehea_port *port)
2624 int i;
2626 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2627 napi_disable(&port->port_res[i].napi);
2630 static void port_napi_enable(struct ehea_port *port)
2632 int i;
2634 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2635 napi_enable(&port->port_res[i].napi);
2638 static int ehea_open(struct net_device *dev)
2640 int ret;
2641 struct ehea_port *port = netdev_priv(dev);
2643 mutex_lock(&port->port_lock);
2645 netif_info(port, ifup, dev, "enabling port\n");
2647 ret = ehea_up(dev);
2648 if (!ret) {
2649 port_napi_enable(port);
2650 netif_start_queue(dev);
2653 mutex_unlock(&port->port_lock);
2655 return ret;
2658 static int ehea_down(struct net_device *dev)
2660 int ret;
2661 struct ehea_port *port = netdev_priv(dev);
2663 if (port->state == EHEA_PORT_DOWN)
2664 return 0;
2666 ehea_drop_multicast_list(dev);
2667 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2669 ehea_free_interrupts(dev);
2671 port->state = EHEA_PORT_DOWN;
2673 ehea_update_bcmc_registrations();
2675 ret = ehea_clean_all_portres(port);
2676 if (ret)
2677 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2679 ehea_update_firmware_handles();
2681 return ret;
2684 static int ehea_stop(struct net_device *dev)
2686 int ret;
2687 struct ehea_port *port = netdev_priv(dev);
2689 netif_info(port, ifdown, dev, "disabling port\n");
2691 set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2692 cancel_work_sync(&port->reset_task);
2693 mutex_lock(&port->port_lock);
2694 netif_stop_queue(dev);
2695 port_napi_disable(port);
2696 ret = ehea_down(dev);
2697 mutex_unlock(&port->port_lock);
2698 clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2699 return ret;
2702 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2704 struct ehea_qp qp = *orig_qp;
2705 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2706 struct ehea_swqe *swqe;
2707 int wqe_index;
2708 int i;
2710 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2711 swqe = ehea_get_swqe(&qp, &wqe_index);
2712 swqe->tx_control |= EHEA_SWQE_PURGE;
2716 static void ehea_flush_sq(struct ehea_port *port)
2718 int i;
2720 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2721 struct ehea_port_res *pr = &port->port_res[i];
2722 int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2723 int ret;
2725 ret = wait_event_timeout(port->swqe_avail_wq,
2726 atomic_read(&pr->swqe_avail) >= swqe_max,
2727 msecs_to_jiffies(100));
2729 if (!ret) {
2730 pr_err("WARNING: sq not flushed completely\n");
2731 break;
2736 int ehea_stop_qps(struct net_device *dev)
2738 struct ehea_port *port = netdev_priv(dev);
2739 struct ehea_adapter *adapter = port->adapter;
2740 struct hcp_modify_qp_cb0 *cb0;
2741 int ret = -EIO;
2742 int dret;
2743 int i;
2744 u64 hret;
2745 u64 dummy64 = 0;
2746 u16 dummy16 = 0;
2748 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2749 if (!cb0) {
2750 ret = -ENOMEM;
2751 goto out;
2754 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2755 struct ehea_port_res *pr = &port->port_res[i];
2756 struct ehea_qp *qp = pr->qp;
2758 /* Purge send queue */
2759 ehea_purge_sq(qp);
2761 /* Disable queue pair */
2762 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2763 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2764 cb0);
2765 if (hret != H_SUCCESS) {
2766 pr_err("query_ehea_qp failed (1)\n");
2767 goto out;
2770 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2771 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2773 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2774 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2775 1), cb0, &dummy64,
2776 &dummy64, &dummy16, &dummy16);
2777 if (hret != H_SUCCESS) {
2778 pr_err("modify_ehea_qp failed (1)\n");
2779 goto out;
2782 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2783 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2784 cb0);
2785 if (hret != H_SUCCESS) {
2786 pr_err("query_ehea_qp failed (2)\n");
2787 goto out;
2790 /* deregister shared memory regions */
2791 dret = ehea_rem_smrs(pr);
2792 if (dret) {
2793 pr_err("unreg shared memory region failed\n");
2794 goto out;
2798 ret = 0;
2799 out:
2800 free_page((unsigned long)cb0);
2802 return ret;
2805 void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2807 struct ehea_qp qp = *orig_qp;
2808 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2809 struct ehea_rwqe *rwqe;
2810 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2811 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2812 struct sk_buff *skb;
2813 u32 lkey = pr->recv_mr.lkey;
2816 int i;
2817 int index;
2819 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2820 rwqe = ehea_get_next_rwqe(&qp, 2);
2821 rwqe->sg_list[0].l_key = lkey;
2822 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2823 skb = skba_rq2[index];
2824 if (skb)
2825 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2828 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2829 rwqe = ehea_get_next_rwqe(&qp, 3);
2830 rwqe->sg_list[0].l_key = lkey;
2831 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2832 skb = skba_rq3[index];
2833 if (skb)
2834 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2838 int ehea_restart_qps(struct net_device *dev)
2840 struct ehea_port *port = netdev_priv(dev);
2841 struct ehea_adapter *adapter = port->adapter;
2842 int ret = 0;
2843 int i;
2845 struct hcp_modify_qp_cb0 *cb0;
2846 u64 hret;
2847 u64 dummy64 = 0;
2848 u16 dummy16 = 0;
2850 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2851 if (!cb0) {
2852 ret = -ENOMEM;
2853 goto out;
2856 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2857 struct ehea_port_res *pr = &port->port_res[i];
2858 struct ehea_qp *qp = pr->qp;
2860 ret = ehea_gen_smrs(pr);
2861 if (ret) {
2862 netdev_err(dev, "creation of shared memory regions failed\n");
2863 goto out;
2866 ehea_update_rqs(qp, pr);
2868 /* Enable queue pair */
2869 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2870 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2871 cb0);
2872 if (hret != H_SUCCESS) {
2873 netdev_err(dev, "query_ehea_qp failed (1)\n");
2874 goto out;
2877 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2878 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2880 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2881 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2882 1), cb0, &dummy64,
2883 &dummy64, &dummy16, &dummy16);
2884 if (hret != H_SUCCESS) {
2885 netdev_err(dev, "modify_ehea_qp failed (1)\n");
2886 goto out;
2889 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2890 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2891 cb0);
2892 if (hret != H_SUCCESS) {
2893 netdev_err(dev, "query_ehea_qp failed (2)\n");
2894 goto out;
2897 /* refill entire queue */
2898 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2899 ehea_refill_rq2(pr, 0);
2900 ehea_refill_rq3(pr, 0);
2902 out:
2903 free_page((unsigned long)cb0);
2905 return ret;
2908 static void ehea_reset_port(struct work_struct *work)
2910 int ret;
2911 struct ehea_port *port =
2912 container_of(work, struct ehea_port, reset_task);
2913 struct net_device *dev = port->netdev;
2915 mutex_lock(&dlpar_mem_lock);
2916 port->resets++;
2917 mutex_lock(&port->port_lock);
2918 netif_stop_queue(dev);
2920 port_napi_disable(port);
2922 ehea_down(dev);
2924 ret = ehea_up(dev);
2925 if (ret)
2926 goto out;
2928 ehea_set_multicast_list(dev);
2930 netif_info(port, timer, dev, "reset successful\n");
2932 port_napi_enable(port);
2934 netif_wake_queue(dev);
2935 out:
2936 mutex_unlock(&port->port_lock);
2937 mutex_unlock(&dlpar_mem_lock);
2940 static void ehea_rereg_mrs(void)
2942 int ret, i;
2943 struct ehea_adapter *adapter;
2945 pr_info("LPAR memory changed - re-initializing driver\n");
2947 list_for_each_entry(adapter, &adapter_list, list)
2948 if (adapter->active_ports) {
2949 /* Shutdown all ports */
2950 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2951 struct ehea_port *port = adapter->port[i];
2952 struct net_device *dev;
2954 if (!port)
2955 continue;
2957 dev = port->netdev;
2959 if (dev->flags & IFF_UP) {
2960 mutex_lock(&port->port_lock);
2961 netif_stop_queue(dev);
2962 ehea_flush_sq(port);
2963 ret = ehea_stop_qps(dev);
2964 if (ret) {
2965 mutex_unlock(&port->port_lock);
2966 goto out;
2968 port_napi_disable(port);
2969 mutex_unlock(&port->port_lock);
2971 reset_sq_restart_flag(port);
2974 /* Unregister old memory region */
2975 ret = ehea_rem_mr(&adapter->mr);
2976 if (ret) {
2977 pr_err("unregister MR failed - driver inoperable!\n");
2978 goto out;
2982 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2984 list_for_each_entry(adapter, &adapter_list, list)
2985 if (adapter->active_ports) {
2986 /* Register new memory region */
2987 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2988 if (ret) {
2989 pr_err("register MR failed - driver inoperable!\n");
2990 goto out;
2993 /* Restart all ports */
2994 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2995 struct ehea_port *port = adapter->port[i];
2997 if (port) {
2998 struct net_device *dev = port->netdev;
3000 if (dev->flags & IFF_UP) {
3001 mutex_lock(&port->port_lock);
3002 ret = ehea_restart_qps(dev);
3003 if (!ret) {
3004 check_sqs(port);
3005 port_napi_enable(port);
3006 netif_wake_queue(dev);
3007 } else {
3008 netdev_err(dev, "Unable to restart QPS\n");
3010 mutex_unlock(&port->port_lock);
3015 pr_info("re-initializing driver complete\n");
3016 out:
3017 return;
3020 static void ehea_tx_watchdog(struct net_device *dev)
3022 struct ehea_port *port = netdev_priv(dev);
3024 if (netif_carrier_ok(dev) &&
3025 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
3026 ehea_schedule_port_reset(port);
3029 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
3031 struct hcp_query_ehea *cb;
3032 u64 hret;
3033 int ret;
3035 cb = (void *)get_zeroed_page(GFP_KERNEL);
3036 if (!cb) {
3037 ret = -ENOMEM;
3038 goto out;
3041 hret = ehea_h_query_ehea(adapter->handle, cb);
3043 if (hret != H_SUCCESS) {
3044 ret = -EIO;
3045 goto out_herr;
3048 adapter->max_mc_mac = cb->max_mc_mac - 1;
3049 ret = 0;
3051 out_herr:
3052 free_page((unsigned long)cb);
3053 out:
3054 return ret;
3057 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
3059 struct hcp_ehea_port_cb4 *cb4;
3060 u64 hret;
3061 int ret = 0;
3063 *jumbo = 0;
3065 /* (Try to) enable *jumbo frames */
3066 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
3067 if (!cb4) {
3068 pr_err("no mem for cb4\n");
3069 ret = -ENOMEM;
3070 goto out;
3071 } else {
3072 hret = ehea_h_query_ehea_port(port->adapter->handle,
3073 port->logical_port_id,
3074 H_PORT_CB4,
3075 H_PORT_CB4_JUMBO, cb4);
3076 if (hret == H_SUCCESS) {
3077 if (cb4->jumbo_frame)
3078 *jumbo = 1;
3079 else {
3080 cb4->jumbo_frame = 1;
3081 hret = ehea_h_modify_ehea_port(port->adapter->
3082 handle,
3083 port->
3084 logical_port_id,
3085 H_PORT_CB4,
3086 H_PORT_CB4_JUMBO,
3087 cb4);
3088 if (hret == H_SUCCESS)
3089 *jumbo = 1;
3091 } else
3092 ret = -EINVAL;
3094 free_page((unsigned long)cb4);
3096 out:
3097 return ret;
3100 static ssize_t ehea_show_port_id(struct device *dev,
3101 struct device_attribute *attr, char *buf)
3103 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
3104 return sprintf(buf, "%d", port->logical_port_id);
3107 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
3108 NULL);
3110 static void __devinit logical_port_release(struct device *dev)
3112 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
3113 of_node_put(port->ofdev.dev.of_node);
3116 static struct device *ehea_register_port(struct ehea_port *port,
3117 struct device_node *dn)
3119 int ret;
3121 port->ofdev.dev.of_node = of_node_get(dn);
3122 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
3123 port->ofdev.dev.bus = &ibmebus_bus_type;
3125 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
3126 port->ofdev.dev.release = logical_port_release;
3128 ret = of_device_register(&port->ofdev);
3129 if (ret) {
3130 pr_err("failed to register device. ret=%d\n", ret);
3131 goto out;
3134 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
3135 if (ret) {
3136 pr_err("failed to register attributes, ret=%d\n", ret);
3137 goto out_unreg_of_dev;
3140 return &port->ofdev.dev;
3142 out_unreg_of_dev:
3143 of_device_unregister(&port->ofdev);
3144 out:
3145 return NULL;
3148 static void ehea_unregister_port(struct ehea_port *port)
3150 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
3151 of_device_unregister(&port->ofdev);
3154 static const struct net_device_ops ehea_netdev_ops = {
3155 .ndo_open = ehea_open,
3156 .ndo_stop = ehea_stop,
3157 .ndo_start_xmit = ehea_start_xmit,
3158 #ifdef CONFIG_NET_POLL_CONTROLLER
3159 .ndo_poll_controller = ehea_netpoll,
3160 #endif
3161 .ndo_get_stats = ehea_get_stats,
3162 .ndo_set_mac_address = ehea_set_mac_addr,
3163 .ndo_validate_addr = eth_validate_addr,
3164 .ndo_set_multicast_list = ehea_set_multicast_list,
3165 .ndo_change_mtu = ehea_change_mtu,
3166 .ndo_vlan_rx_add_vid = ehea_vlan_rx_add_vid,
3167 .ndo_vlan_rx_kill_vid = ehea_vlan_rx_kill_vid,
3168 .ndo_tx_timeout = ehea_tx_watchdog,
3171 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
3172 u32 logical_port_id,
3173 struct device_node *dn)
3175 int ret;
3176 struct net_device *dev;
3177 struct ehea_port *port;
3178 struct device *port_dev;
3179 int jumbo;
3181 /* allocate memory for the port structures */
3182 dev = alloc_etherdev(sizeof(struct ehea_port));
3184 if (!dev) {
3185 pr_err("no mem for net_device\n");
3186 ret = -ENOMEM;
3187 goto out_err;
3190 port = netdev_priv(dev);
3192 mutex_init(&port->port_lock);
3193 port->state = EHEA_PORT_DOWN;
3194 port->sig_comp_iv = sq_entries / 10;
3196 port->adapter = adapter;
3197 port->netdev = dev;
3198 port->logical_port_id = logical_port_id;
3200 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
3202 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
3203 if (!port->mc_list) {
3204 ret = -ENOMEM;
3205 goto out_free_ethdev;
3208 INIT_LIST_HEAD(&port->mc_list->list);
3210 ret = ehea_sense_port_attr(port);
3211 if (ret)
3212 goto out_free_mc_list;
3214 port_dev = ehea_register_port(port, dn);
3215 if (!port_dev)
3216 goto out_free_mc_list;
3218 SET_NETDEV_DEV(dev, port_dev);
3220 /* initialize net_device structure */
3221 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3223 dev->netdev_ops = &ehea_netdev_ops;
3224 ehea_set_ethtool_ops(dev);
3226 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
3227 | NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_TX | NETIF_F_LRO;
3228 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
3229 | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_TX
3230 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
3231 | NETIF_F_LLTX | NETIF_F_RXCSUM;
3232 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3234 if (use_lro)
3235 dev->features |= NETIF_F_LRO;
3237 INIT_WORK(&port->reset_task, ehea_reset_port);
3239 init_waitqueue_head(&port->swqe_avail_wq);
3240 init_waitqueue_head(&port->restart_wq);
3242 ret = register_netdev(dev);
3243 if (ret) {
3244 pr_err("register_netdev failed. ret=%d\n", ret);
3245 goto out_unreg_port;
3248 port->lro_max_aggr = lro_max_aggr;
3250 ret = ehea_get_jumboframe_status(port, &jumbo);
3251 if (ret)
3252 netdev_err(dev, "failed determining jumbo frame status\n");
3254 netdev_info(dev, "Jumbo frames are %sabled\n",
3255 jumbo == 1 ? "en" : "dis");
3257 adapter->active_ports++;
3259 return port;
3261 out_unreg_port:
3262 ehea_unregister_port(port);
3264 out_free_mc_list:
3265 kfree(port->mc_list);
3267 out_free_ethdev:
3268 free_netdev(dev);
3270 out_err:
3271 pr_err("setting up logical port with id=%d failed, ret=%d\n",
3272 logical_port_id, ret);
3273 return NULL;
3276 static void ehea_shutdown_single_port(struct ehea_port *port)
3278 struct ehea_adapter *adapter = port->adapter;
3280 cancel_work_sync(&port->reset_task);
3281 unregister_netdev(port->netdev);
3282 ehea_unregister_port(port);
3283 kfree(port->mc_list);
3284 free_netdev(port->netdev);
3285 adapter->active_ports--;
3288 static int ehea_setup_ports(struct ehea_adapter *adapter)
3290 struct device_node *lhea_dn;
3291 struct device_node *eth_dn = NULL;
3293 const u32 *dn_log_port_id;
3294 int i = 0;
3296 lhea_dn = adapter->ofdev->dev.of_node;
3297 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3299 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3300 NULL);
3301 if (!dn_log_port_id) {
3302 pr_err("bad device node: eth_dn name=%s\n",
3303 eth_dn->full_name);
3304 continue;
3307 if (ehea_add_adapter_mr(adapter)) {
3308 pr_err("creating MR failed\n");
3309 of_node_put(eth_dn);
3310 return -EIO;
3313 adapter->port[i] = ehea_setup_single_port(adapter,
3314 *dn_log_port_id,
3315 eth_dn);
3316 if (adapter->port[i])
3317 netdev_info(adapter->port[i]->netdev,
3318 "logical port id #%d\n", *dn_log_port_id);
3319 else
3320 ehea_remove_adapter_mr(adapter);
3322 i++;
3324 return 0;
3327 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3328 u32 logical_port_id)
3330 struct device_node *lhea_dn;
3331 struct device_node *eth_dn = NULL;
3332 const u32 *dn_log_port_id;
3334 lhea_dn = adapter->ofdev->dev.of_node;
3335 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3337 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3338 NULL);
3339 if (dn_log_port_id)
3340 if (*dn_log_port_id == logical_port_id)
3341 return eth_dn;
3344 return NULL;
3347 static ssize_t ehea_probe_port(struct device *dev,
3348 struct device_attribute *attr,
3349 const char *buf, size_t count)
3351 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3352 struct ehea_port *port;
3353 struct device_node *eth_dn = NULL;
3354 int i;
3356 u32 logical_port_id;
3358 sscanf(buf, "%d", &logical_port_id);
3360 port = ehea_get_port(adapter, logical_port_id);
3362 if (port) {
3363 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3364 logical_port_id);
3365 return -EINVAL;
3368 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3370 if (!eth_dn) {
3371 pr_info("no logical port with id %d found\n", logical_port_id);
3372 return -EINVAL;
3375 if (ehea_add_adapter_mr(adapter)) {
3376 pr_err("creating MR failed\n");
3377 return -EIO;
3380 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3382 of_node_put(eth_dn);
3384 if (port) {
3385 for (i = 0; i < EHEA_MAX_PORTS; i++)
3386 if (!adapter->port[i]) {
3387 adapter->port[i] = port;
3388 break;
3391 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3392 logical_port_id);
3393 } else {
3394 ehea_remove_adapter_mr(adapter);
3395 return -EIO;
3398 return (ssize_t) count;
3401 static ssize_t ehea_remove_port(struct device *dev,
3402 struct device_attribute *attr,
3403 const char *buf, size_t count)
3405 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3406 struct ehea_port *port;
3407 int i;
3408 u32 logical_port_id;
3410 sscanf(buf, "%d", &logical_port_id);
3412 port = ehea_get_port(adapter, logical_port_id);
3414 if (port) {
3415 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3416 logical_port_id);
3418 ehea_shutdown_single_port(port);
3420 for (i = 0; i < EHEA_MAX_PORTS; i++)
3421 if (adapter->port[i] == port) {
3422 adapter->port[i] = NULL;
3423 break;
3425 } else {
3426 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3427 logical_port_id);
3428 return -EINVAL;
3431 ehea_remove_adapter_mr(adapter);
3433 return (ssize_t) count;
3436 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3437 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3439 int ehea_create_device_sysfs(struct platform_device *dev)
3441 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3442 if (ret)
3443 goto out;
3445 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3446 out:
3447 return ret;
3450 void ehea_remove_device_sysfs(struct platform_device *dev)
3452 device_remove_file(&dev->dev, &dev_attr_probe_port);
3453 device_remove_file(&dev->dev, &dev_attr_remove_port);
3456 static int __devinit ehea_probe_adapter(struct platform_device *dev,
3457 const struct of_device_id *id)
3459 struct ehea_adapter *adapter;
3460 const u64 *adapter_handle;
3461 int ret;
3463 if (!dev || !dev->dev.of_node) {
3464 pr_err("Invalid ibmebus device probed\n");
3465 return -EINVAL;
3468 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3469 if (!adapter) {
3470 ret = -ENOMEM;
3471 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3472 goto out;
3475 list_add(&adapter->list, &adapter_list);
3477 adapter->ofdev = dev;
3479 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3480 NULL);
3481 if (adapter_handle)
3482 adapter->handle = *adapter_handle;
3484 if (!adapter->handle) {
3485 dev_err(&dev->dev, "failed getting handle for adapter"
3486 " '%s'\n", dev->dev.of_node->full_name);
3487 ret = -ENODEV;
3488 goto out_free_ad;
3491 adapter->pd = EHEA_PD_ID;
3493 dev_set_drvdata(&dev->dev, adapter);
3496 /* initialize adapter and ports */
3497 /* get adapter properties */
3498 ret = ehea_sense_adapter_attr(adapter);
3499 if (ret) {
3500 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3501 goto out_free_ad;
3504 adapter->neq = ehea_create_eq(adapter,
3505 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3506 if (!adapter->neq) {
3507 ret = -EIO;
3508 dev_err(&dev->dev, "NEQ creation failed\n");
3509 goto out_free_ad;
3512 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3513 (unsigned long)adapter);
3515 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3516 ehea_interrupt_neq, IRQF_DISABLED,
3517 "ehea_neq", adapter);
3518 if (ret) {
3519 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3520 goto out_kill_eq;
3523 ret = ehea_create_device_sysfs(dev);
3524 if (ret)
3525 goto out_free_irq;
3527 ret = ehea_setup_ports(adapter);
3528 if (ret) {
3529 dev_err(&dev->dev, "setup_ports failed\n");
3530 goto out_rem_dev_sysfs;
3533 ret = 0;
3534 goto out;
3536 out_rem_dev_sysfs:
3537 ehea_remove_device_sysfs(dev);
3539 out_free_irq:
3540 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3542 out_kill_eq:
3543 ehea_destroy_eq(adapter->neq);
3545 out_free_ad:
3546 list_del(&adapter->list);
3547 kfree(adapter);
3549 out:
3550 ehea_update_firmware_handles();
3552 return ret;
3555 static int __devexit ehea_remove(struct platform_device *dev)
3557 struct ehea_adapter *adapter = dev_get_drvdata(&dev->dev);
3558 int i;
3560 for (i = 0; i < EHEA_MAX_PORTS; i++)
3561 if (adapter->port[i]) {
3562 ehea_shutdown_single_port(adapter->port[i]);
3563 adapter->port[i] = NULL;
3566 ehea_remove_device_sysfs(dev);
3568 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3569 tasklet_kill(&adapter->neq_tasklet);
3571 ehea_destroy_eq(adapter->neq);
3572 ehea_remove_adapter_mr(adapter);
3573 list_del(&adapter->list);
3574 kfree(adapter);
3576 ehea_update_firmware_handles();
3578 return 0;
3581 void ehea_crash_handler(void)
3583 int i;
3585 if (ehea_fw_handles.arr)
3586 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3587 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3588 ehea_fw_handles.arr[i].fwh,
3589 FORCE_FREE);
3591 if (ehea_bcmc_regs.arr)
3592 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3593 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3594 ehea_bcmc_regs.arr[i].port_id,
3595 ehea_bcmc_regs.arr[i].reg_type,
3596 ehea_bcmc_regs.arr[i].macaddr,
3597 0, H_DEREG_BCMC);
3600 static int ehea_mem_notifier(struct notifier_block *nb,
3601 unsigned long action, void *data)
3603 int ret = NOTIFY_BAD;
3604 struct memory_notify *arg = data;
3606 mutex_lock(&dlpar_mem_lock);
3608 switch (action) {
3609 case MEM_CANCEL_OFFLINE:
3610 pr_info("memory offlining canceled");
3611 /* Readd canceled memory block */
3612 case MEM_ONLINE:
3613 pr_info("memory is going online");
3614 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3615 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3616 goto out_unlock;
3617 ehea_rereg_mrs();
3618 break;
3619 case MEM_GOING_OFFLINE:
3620 pr_info("memory is going offline");
3621 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3622 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3623 goto out_unlock;
3624 ehea_rereg_mrs();
3625 break;
3626 default:
3627 break;
3630 ehea_update_firmware_handles();
3631 ret = NOTIFY_OK;
3633 out_unlock:
3634 mutex_unlock(&dlpar_mem_lock);
3635 return ret;
3638 static struct notifier_block ehea_mem_nb = {
3639 .notifier_call = ehea_mem_notifier,
3642 static int ehea_reboot_notifier(struct notifier_block *nb,
3643 unsigned long action, void *unused)
3645 if (action == SYS_RESTART) {
3646 pr_info("Reboot: freeing all eHEA resources\n");
3647 ibmebus_unregister_driver(&ehea_driver);
3649 return NOTIFY_DONE;
3652 static struct notifier_block ehea_reboot_nb = {
3653 .notifier_call = ehea_reboot_notifier,
3656 static int check_module_parm(void)
3658 int ret = 0;
3660 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3661 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3662 pr_info("Bad parameter: rq1_entries\n");
3663 ret = -EINVAL;
3665 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3666 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3667 pr_info("Bad parameter: rq2_entries\n");
3668 ret = -EINVAL;
3670 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3671 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3672 pr_info("Bad parameter: rq3_entries\n");
3673 ret = -EINVAL;
3675 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3676 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3677 pr_info("Bad parameter: sq_entries\n");
3678 ret = -EINVAL;
3681 return ret;
3684 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3685 char *buf)
3687 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3690 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3691 ehea_show_capabilities, NULL);
3693 int __init ehea_module_init(void)
3695 int ret;
3697 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3699 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3700 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3702 mutex_init(&ehea_fw_handles.lock);
3703 spin_lock_init(&ehea_bcmc_regs.lock);
3705 ret = check_module_parm();
3706 if (ret)
3707 goto out;
3709 ret = ehea_create_busmap();
3710 if (ret)
3711 goto out;
3713 ret = register_reboot_notifier(&ehea_reboot_nb);
3714 if (ret)
3715 pr_info("failed registering reboot notifier\n");
3717 ret = register_memory_notifier(&ehea_mem_nb);
3718 if (ret)
3719 pr_info("failed registering memory remove notifier\n");
3721 ret = crash_shutdown_register(ehea_crash_handler);
3722 if (ret)
3723 pr_info("failed registering crash handler\n");
3725 ret = ibmebus_register_driver(&ehea_driver);
3726 if (ret) {
3727 pr_err("failed registering eHEA device driver on ebus\n");
3728 goto out2;
3731 ret = driver_create_file(&ehea_driver.driver,
3732 &driver_attr_capabilities);
3733 if (ret) {
3734 pr_err("failed to register capabilities attribute, ret=%d\n",
3735 ret);
3736 goto out3;
3739 return ret;
3741 out3:
3742 ibmebus_unregister_driver(&ehea_driver);
3743 out2:
3744 unregister_memory_notifier(&ehea_mem_nb);
3745 unregister_reboot_notifier(&ehea_reboot_nb);
3746 crash_shutdown_unregister(ehea_crash_handler);
3747 out:
3748 return ret;
3751 static void __exit ehea_module_exit(void)
3753 int ret;
3755 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3756 ibmebus_unregister_driver(&ehea_driver);
3757 unregister_reboot_notifier(&ehea_reboot_nb);
3758 ret = crash_shutdown_unregister(ehea_crash_handler);
3759 if (ret)
3760 pr_info("failed unregistering crash handler\n");
3761 unregister_memory_notifier(&ehea_mem_nb);
3762 kfree(ehea_fw_handles.arr);
3763 kfree(ehea_bcmc_regs.arr);
3764 ehea_destroy_busmap();
3767 module_init(ehea_module_init);
3768 module_exit(ehea_module_exit);