Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[linux-2.6/next.git] / drivers / net / ehea / ehea_main.c
blob6a0a8fca62bc58aeaa8354abc1f2c6d67175e659
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 int vlan_extracted = ((cqe->status & EHEA_CQE_VLAN_TAG_XTRACT) &&
687 pr->port->vgrp);
689 if (skb->dev->features & NETIF_F_LRO) {
690 if (vlan_extracted)
691 lro_vlan_hwaccel_receive_skb(&pr->lro_mgr, skb,
692 pr->port->vgrp,
693 cqe->vlan_tag,
694 cqe);
695 else
696 lro_receive_skb(&pr->lro_mgr, skb, cqe);
697 } else {
698 if (vlan_extracted)
699 vlan_hwaccel_receive_skb(skb, pr->port->vgrp,
700 cqe->vlan_tag);
701 else
702 netif_receive_skb(skb);
706 static int ehea_proc_rwqes(struct net_device *dev,
707 struct ehea_port_res *pr,
708 int budget)
710 struct ehea_port *port = pr->port;
711 struct ehea_qp *qp = pr->qp;
712 struct ehea_cqe *cqe;
713 struct sk_buff *skb;
714 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
715 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
716 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
717 int skb_arr_rq1_len = pr->rq1_skba.len;
718 int skb_arr_rq2_len = pr->rq2_skba.len;
719 int skb_arr_rq3_len = pr->rq3_skba.len;
720 int processed, processed_rq1, processed_rq2, processed_rq3;
721 u64 processed_bytes = 0;
722 int wqe_index, last_wqe_index, rq, port_reset;
724 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
725 last_wqe_index = 0;
727 cqe = ehea_poll_rq1(qp, &wqe_index);
728 while ((processed < budget) && cqe) {
729 ehea_inc_rq1(qp);
730 processed_rq1++;
731 processed++;
732 if (netif_msg_rx_status(port))
733 ehea_dump(cqe, sizeof(*cqe), "CQE");
735 last_wqe_index = wqe_index;
736 rmb();
737 if (!ehea_check_cqe(cqe, &rq)) {
738 if (rq == 1) {
739 /* LL RQ1 */
740 skb = get_skb_by_index_ll(skb_arr_rq1,
741 skb_arr_rq1_len,
742 wqe_index);
743 if (unlikely(!skb)) {
744 netif_info(port, rx_err, dev,
745 "LL rq1: skb=NULL\n");
747 skb = netdev_alloc_skb(dev,
748 EHEA_L_PKT_SIZE);
749 if (!skb) {
750 netdev_err(dev, "Not enough memory to allocate skb\n");
751 break;
754 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
755 cqe->num_bytes_transfered - 4);
756 ehea_fill_skb(dev, skb, cqe);
757 } else if (rq == 2) {
758 /* RQ2 */
759 skb = get_skb_by_index(skb_arr_rq2,
760 skb_arr_rq2_len, cqe);
761 if (unlikely(!skb)) {
762 netif_err(port, rx_err, dev,
763 "rq2: skb=NULL\n");
764 break;
766 ehea_fill_skb(dev, skb, cqe);
767 processed_rq2++;
768 } else {
769 /* RQ3 */
770 skb = get_skb_by_index(skb_arr_rq3,
771 skb_arr_rq3_len, cqe);
772 if (unlikely(!skb)) {
773 netif_err(port, rx_err, dev,
774 "rq3: skb=NULL\n");
775 break;
777 ehea_fill_skb(dev, skb, cqe);
778 processed_rq3++;
781 processed_bytes += skb->len;
782 ehea_proc_skb(pr, cqe, skb);
783 } else {
784 pr->p_stats.poll_receive_errors++;
785 port_reset = ehea_treat_poll_error(pr, rq, cqe,
786 &processed_rq2,
787 &processed_rq3);
788 if (port_reset)
789 break;
791 cqe = ehea_poll_rq1(qp, &wqe_index);
793 if (dev->features & NETIF_F_LRO)
794 lro_flush_all(&pr->lro_mgr);
796 pr->rx_packets += processed;
797 pr->rx_bytes += processed_bytes;
799 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
800 ehea_refill_rq2(pr, processed_rq2);
801 ehea_refill_rq3(pr, processed_rq3);
803 return processed;
806 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
808 static void reset_sq_restart_flag(struct ehea_port *port)
810 int i;
812 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
813 struct ehea_port_res *pr = &port->port_res[i];
814 pr->sq_restart_flag = 0;
816 wake_up(&port->restart_wq);
819 static void check_sqs(struct ehea_port *port)
821 struct ehea_swqe *swqe;
822 int swqe_index;
823 int i, k;
825 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
826 struct ehea_port_res *pr = &port->port_res[i];
827 int ret;
828 k = 0;
829 swqe = ehea_get_swqe(pr->qp, &swqe_index);
830 memset(swqe, 0, SWQE_HEADER_SIZE);
831 atomic_dec(&pr->swqe_avail);
833 swqe->tx_control |= EHEA_SWQE_PURGE;
834 swqe->wr_id = SWQE_RESTART_CHECK;
835 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
836 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
837 swqe->immediate_data_length = 80;
839 ehea_post_swqe(pr->qp, swqe);
841 ret = wait_event_timeout(port->restart_wq,
842 pr->sq_restart_flag == 0,
843 msecs_to_jiffies(100));
845 if (!ret) {
846 pr_err("HW/SW queues out of sync\n");
847 ehea_schedule_port_reset(pr->port);
848 return;
854 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
856 struct sk_buff *skb;
857 struct ehea_cq *send_cq = pr->send_cq;
858 struct ehea_cqe *cqe;
859 int quota = my_quota;
860 int cqe_counter = 0;
861 int swqe_av = 0;
862 int index;
863 unsigned long flags;
865 cqe = ehea_poll_cq(send_cq);
866 while (cqe && (quota > 0)) {
867 ehea_inc_cq(send_cq);
869 cqe_counter++;
870 rmb();
872 if (cqe->wr_id == SWQE_RESTART_CHECK) {
873 pr->sq_restart_flag = 1;
874 swqe_av++;
875 break;
878 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
879 pr_err("Bad send completion status=0x%04X\n",
880 cqe->status);
882 if (netif_msg_tx_err(pr->port))
883 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
885 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
886 pr_err("Resetting port\n");
887 ehea_schedule_port_reset(pr->port);
888 break;
892 if (netif_msg_tx_done(pr->port))
893 ehea_dump(cqe, sizeof(*cqe), "CQE");
895 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
896 == EHEA_SWQE2_TYPE)) {
898 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
899 skb = pr->sq_skba.arr[index];
900 dev_kfree_skb(skb);
901 pr->sq_skba.arr[index] = NULL;
904 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
905 quota--;
907 cqe = ehea_poll_cq(send_cq);
910 ehea_update_feca(send_cq, cqe_counter);
911 atomic_add(swqe_av, &pr->swqe_avail);
913 spin_lock_irqsave(&pr->netif_queue, flags);
915 if (pr->queue_stopped && (atomic_read(&pr->swqe_avail)
916 >= pr->swqe_refill_th)) {
917 netif_wake_queue(pr->port->netdev);
918 pr->queue_stopped = 0;
920 spin_unlock_irqrestore(&pr->netif_queue, flags);
921 wake_up(&pr->port->swqe_avail_wq);
923 return cqe;
926 #define EHEA_NAPI_POLL_NUM_BEFORE_IRQ 16
927 #define EHEA_POLL_MAX_CQES 65535
929 static int ehea_poll(struct napi_struct *napi, int budget)
931 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
932 napi);
933 struct net_device *dev = pr->port->netdev;
934 struct ehea_cqe *cqe;
935 struct ehea_cqe *cqe_skb = NULL;
936 int force_irq, wqe_index;
937 int rx = 0;
939 force_irq = (pr->poll_counter > EHEA_NAPI_POLL_NUM_BEFORE_IRQ);
940 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
942 if (!force_irq)
943 rx += ehea_proc_rwqes(dev, pr, budget - rx);
945 while ((rx != budget) || force_irq) {
946 pr->poll_counter = 0;
947 force_irq = 0;
948 napi_complete(napi);
949 ehea_reset_cq_ep(pr->recv_cq);
950 ehea_reset_cq_ep(pr->send_cq);
951 ehea_reset_cq_n1(pr->recv_cq);
952 ehea_reset_cq_n1(pr->send_cq);
953 rmb();
954 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
955 cqe_skb = ehea_poll_cq(pr->send_cq);
957 if (!cqe && !cqe_skb)
958 return rx;
960 if (!napi_reschedule(napi))
961 return rx;
963 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
964 rx += ehea_proc_rwqes(dev, pr, budget - rx);
967 pr->poll_counter++;
968 return rx;
971 #ifdef CONFIG_NET_POLL_CONTROLLER
972 static void ehea_netpoll(struct net_device *dev)
974 struct ehea_port *port = netdev_priv(dev);
975 int i;
977 for (i = 0; i < port->num_def_qps; i++)
978 napi_schedule(&port->port_res[i].napi);
980 #endif
982 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
984 struct ehea_port_res *pr = param;
986 napi_schedule(&pr->napi);
988 return IRQ_HANDLED;
991 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
993 struct ehea_port *port = param;
994 struct ehea_eqe *eqe;
995 struct ehea_qp *qp;
996 u32 qp_token;
997 u64 resource_type, aer, aerr;
998 int reset_port = 0;
1000 eqe = ehea_poll_eq(port->qp_eq);
1002 while (eqe) {
1003 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
1004 pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
1005 eqe->entry, qp_token);
1007 qp = port->port_res[qp_token].qp;
1009 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
1010 &aer, &aerr);
1012 if (resource_type == EHEA_AER_RESTYPE_QP) {
1013 if ((aer & EHEA_AER_RESET_MASK) ||
1014 (aerr & EHEA_AERR_RESET_MASK))
1015 reset_port = 1;
1016 } else
1017 reset_port = 1; /* Reset in case of CQ or EQ error */
1019 eqe = ehea_poll_eq(port->qp_eq);
1022 if (reset_port) {
1023 pr_err("Resetting port\n");
1024 ehea_schedule_port_reset(port);
1027 return IRQ_HANDLED;
1030 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
1031 int logical_port)
1033 int i;
1035 for (i = 0; i < EHEA_MAX_PORTS; i++)
1036 if (adapter->port[i])
1037 if (adapter->port[i]->logical_port_id == logical_port)
1038 return adapter->port[i];
1039 return NULL;
1042 int ehea_sense_port_attr(struct ehea_port *port)
1044 int ret;
1045 u64 hret;
1046 struct hcp_ehea_port_cb0 *cb0;
1048 /* may be called via ehea_neq_tasklet() */
1049 cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
1050 if (!cb0) {
1051 pr_err("no mem for cb0\n");
1052 ret = -ENOMEM;
1053 goto out;
1056 hret = ehea_h_query_ehea_port(port->adapter->handle,
1057 port->logical_port_id, H_PORT_CB0,
1058 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
1059 cb0);
1060 if (hret != H_SUCCESS) {
1061 ret = -EIO;
1062 goto out_free;
1065 /* MAC address */
1066 port->mac_addr = cb0->port_mac_addr << 16;
1068 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1069 ret = -EADDRNOTAVAIL;
1070 goto out_free;
1073 /* Port speed */
1074 switch (cb0->port_speed) {
1075 case H_SPEED_10M_H:
1076 port->port_speed = EHEA_SPEED_10M;
1077 port->full_duplex = 0;
1078 break;
1079 case H_SPEED_10M_F:
1080 port->port_speed = EHEA_SPEED_10M;
1081 port->full_duplex = 1;
1082 break;
1083 case H_SPEED_100M_H:
1084 port->port_speed = EHEA_SPEED_100M;
1085 port->full_duplex = 0;
1086 break;
1087 case H_SPEED_100M_F:
1088 port->port_speed = EHEA_SPEED_100M;
1089 port->full_duplex = 1;
1090 break;
1091 case H_SPEED_1G_F:
1092 port->port_speed = EHEA_SPEED_1G;
1093 port->full_duplex = 1;
1094 break;
1095 case H_SPEED_10G_F:
1096 port->port_speed = EHEA_SPEED_10G;
1097 port->full_duplex = 1;
1098 break;
1099 default:
1100 port->port_speed = 0;
1101 port->full_duplex = 0;
1102 break;
1105 port->autoneg = 1;
1106 port->num_mcs = cb0->num_default_qps;
1108 /* Number of default QPs */
1109 if (use_mcs)
1110 port->num_def_qps = cb0->num_default_qps;
1111 else
1112 port->num_def_qps = 1;
1114 if (!port->num_def_qps) {
1115 ret = -EINVAL;
1116 goto out_free;
1119 port->num_tx_qps = num_tx_qps;
1121 if (port->num_def_qps >= port->num_tx_qps)
1122 port->num_add_tx_qps = 0;
1123 else
1124 port->num_add_tx_qps = port->num_tx_qps - port->num_def_qps;
1126 ret = 0;
1127 out_free:
1128 if (ret || netif_msg_probe(port))
1129 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1130 free_page((unsigned long)cb0);
1131 out:
1132 return ret;
1135 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1137 struct hcp_ehea_port_cb4 *cb4;
1138 u64 hret;
1139 int ret = 0;
1141 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1142 if (!cb4) {
1143 pr_err("no mem for cb4\n");
1144 ret = -ENOMEM;
1145 goto out;
1148 cb4->port_speed = port_speed;
1150 netif_carrier_off(port->netdev);
1152 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1153 port->logical_port_id,
1154 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1155 if (hret == H_SUCCESS) {
1156 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1158 hret = ehea_h_query_ehea_port(port->adapter->handle,
1159 port->logical_port_id,
1160 H_PORT_CB4, H_PORT_CB4_SPEED,
1161 cb4);
1162 if (hret == H_SUCCESS) {
1163 switch (cb4->port_speed) {
1164 case H_SPEED_10M_H:
1165 port->port_speed = EHEA_SPEED_10M;
1166 port->full_duplex = 0;
1167 break;
1168 case H_SPEED_10M_F:
1169 port->port_speed = EHEA_SPEED_10M;
1170 port->full_duplex = 1;
1171 break;
1172 case H_SPEED_100M_H:
1173 port->port_speed = EHEA_SPEED_100M;
1174 port->full_duplex = 0;
1175 break;
1176 case H_SPEED_100M_F:
1177 port->port_speed = EHEA_SPEED_100M;
1178 port->full_duplex = 1;
1179 break;
1180 case H_SPEED_1G_F:
1181 port->port_speed = EHEA_SPEED_1G;
1182 port->full_duplex = 1;
1183 break;
1184 case H_SPEED_10G_F:
1185 port->port_speed = EHEA_SPEED_10G;
1186 port->full_duplex = 1;
1187 break;
1188 default:
1189 port->port_speed = 0;
1190 port->full_duplex = 0;
1191 break;
1193 } else {
1194 pr_err("Failed sensing port speed\n");
1195 ret = -EIO;
1197 } else {
1198 if (hret == H_AUTHORITY) {
1199 pr_info("Hypervisor denied setting port speed\n");
1200 ret = -EPERM;
1201 } else {
1202 ret = -EIO;
1203 pr_err("Failed setting port speed\n");
1206 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1207 netif_carrier_on(port->netdev);
1209 free_page((unsigned long)cb4);
1210 out:
1211 return ret;
1214 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1216 int ret;
1217 u8 ec;
1218 u8 portnum;
1219 struct ehea_port *port;
1220 struct net_device *dev;
1222 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1223 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1224 port = ehea_get_port(adapter, portnum);
1225 dev = port->netdev;
1227 switch (ec) {
1228 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1230 if (!port) {
1231 netdev_err(dev, "unknown portnum %x\n", portnum);
1232 break;
1235 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1236 if (!netif_carrier_ok(dev)) {
1237 ret = ehea_sense_port_attr(port);
1238 if (ret) {
1239 netdev_err(dev, "failed resensing port attributes\n");
1240 break;
1243 netif_info(port, link, dev,
1244 "Logical port up: %dMbps %s Duplex\n",
1245 port->port_speed,
1246 port->full_duplex == 1 ?
1247 "Full" : "Half");
1249 netif_carrier_on(dev);
1250 netif_wake_queue(dev);
1252 } else
1253 if (netif_carrier_ok(dev)) {
1254 netif_info(port, link, dev,
1255 "Logical port down\n");
1256 netif_carrier_off(dev);
1257 netif_stop_queue(dev);
1260 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1261 port->phy_link = EHEA_PHY_LINK_UP;
1262 netif_info(port, link, dev,
1263 "Physical port up\n");
1264 if (prop_carrier_state)
1265 netif_carrier_on(dev);
1266 } else {
1267 port->phy_link = EHEA_PHY_LINK_DOWN;
1268 netif_info(port, link, dev,
1269 "Physical port down\n");
1270 if (prop_carrier_state)
1271 netif_carrier_off(dev);
1274 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1275 netdev_info(dev,
1276 "External switch port is primary port\n");
1277 else
1278 netdev_info(dev,
1279 "External switch port is backup port\n");
1281 break;
1282 case EHEA_EC_ADAPTER_MALFUNC:
1283 netdev_err(dev, "Adapter malfunction\n");
1284 break;
1285 case EHEA_EC_PORT_MALFUNC:
1286 netdev_info(dev, "Port malfunction\n");
1287 netif_carrier_off(dev);
1288 netif_stop_queue(dev);
1289 break;
1290 default:
1291 netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1292 break;
1296 static void ehea_neq_tasklet(unsigned long data)
1298 struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1299 struct ehea_eqe *eqe;
1300 u64 event_mask;
1302 eqe = ehea_poll_eq(adapter->neq);
1303 pr_debug("eqe=%p\n", eqe);
1305 while (eqe) {
1306 pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1307 ehea_parse_eqe(adapter, eqe->entry);
1308 eqe = ehea_poll_eq(adapter->neq);
1309 pr_debug("next eqe=%p\n", eqe);
1312 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1313 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1314 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1316 ehea_h_reset_events(adapter->handle,
1317 adapter->neq->fw_handle, event_mask);
1320 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1322 struct ehea_adapter *adapter = param;
1323 tasklet_hi_schedule(&adapter->neq_tasklet);
1324 return IRQ_HANDLED;
1328 static int ehea_fill_port_res(struct ehea_port_res *pr)
1330 int ret;
1331 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1333 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1335 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1337 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1339 return ret;
1342 static int ehea_reg_interrupts(struct net_device *dev)
1344 struct ehea_port *port = netdev_priv(dev);
1345 struct ehea_port_res *pr;
1346 int i, ret;
1349 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1350 dev->name);
1352 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1353 ehea_qp_aff_irq_handler,
1354 IRQF_DISABLED, port->int_aff_name, port);
1355 if (ret) {
1356 netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1357 port->qp_eq->attr.ist1);
1358 goto out_free_qpeq;
1361 netif_info(port, ifup, dev,
1362 "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1363 port->qp_eq->attr.ist1);
1366 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1367 pr = &port->port_res[i];
1368 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1369 "%s-queue%d", dev->name, i);
1370 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1371 ehea_recv_irq_handler,
1372 IRQF_DISABLED, pr->int_send_name,
1373 pr);
1374 if (ret) {
1375 netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1376 i, pr->eq->attr.ist1);
1377 goto out_free_req;
1379 netif_info(port, ifup, dev,
1380 "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1381 pr->eq->attr.ist1, i);
1383 out:
1384 return ret;
1387 out_free_req:
1388 while (--i >= 0) {
1389 u32 ist = port->port_res[i].eq->attr.ist1;
1390 ibmebus_free_irq(ist, &port->port_res[i]);
1393 out_free_qpeq:
1394 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1395 i = port->num_def_qps;
1397 goto out;
1401 static void ehea_free_interrupts(struct net_device *dev)
1403 struct ehea_port *port = netdev_priv(dev);
1404 struct ehea_port_res *pr;
1405 int i;
1407 /* send */
1409 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1410 pr = &port->port_res[i];
1411 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1412 netif_info(port, intr, dev,
1413 "free send irq for res %d with handle 0x%X\n",
1414 i, pr->eq->attr.ist1);
1417 /* associated events */
1418 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1419 netif_info(port, intr, dev,
1420 "associated event interrupt for handle 0x%X freed\n",
1421 port->qp_eq->attr.ist1);
1424 static int ehea_configure_port(struct ehea_port *port)
1426 int ret, i;
1427 u64 hret, mask;
1428 struct hcp_ehea_port_cb0 *cb0;
1430 ret = -ENOMEM;
1431 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1432 if (!cb0)
1433 goto out;
1435 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1436 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1437 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1438 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1439 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1440 PXLY_RC_VLAN_FILTER)
1441 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1443 for (i = 0; i < port->num_mcs; i++)
1444 if (use_mcs)
1445 cb0->default_qpn_arr[i] =
1446 port->port_res[i].qp->init_attr.qp_nr;
1447 else
1448 cb0->default_qpn_arr[i] =
1449 port->port_res[0].qp->init_attr.qp_nr;
1451 if (netif_msg_ifup(port))
1452 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1454 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1455 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1457 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1458 port->logical_port_id,
1459 H_PORT_CB0, mask, cb0);
1460 ret = -EIO;
1461 if (hret != H_SUCCESS)
1462 goto out_free;
1464 ret = 0;
1466 out_free:
1467 free_page((unsigned long)cb0);
1468 out:
1469 return ret;
1472 int ehea_gen_smrs(struct ehea_port_res *pr)
1474 int ret;
1475 struct ehea_adapter *adapter = pr->port->adapter;
1477 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1478 if (ret)
1479 goto out;
1481 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1482 if (ret)
1483 goto out_free;
1485 return 0;
1487 out_free:
1488 ehea_rem_mr(&pr->send_mr);
1489 out:
1490 pr_err("Generating SMRS failed\n");
1491 return -EIO;
1494 int ehea_rem_smrs(struct ehea_port_res *pr)
1496 if ((ehea_rem_mr(&pr->send_mr)) ||
1497 (ehea_rem_mr(&pr->recv_mr)))
1498 return -EIO;
1499 else
1500 return 0;
1503 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1505 int arr_size = sizeof(void *) * max_q_entries;
1507 q_skba->arr = vzalloc(arr_size);
1508 if (!q_skba->arr)
1509 return -ENOMEM;
1511 q_skba->len = max_q_entries;
1512 q_skba->index = 0;
1513 q_skba->os_skbs = 0;
1515 return 0;
1518 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1519 struct port_res_cfg *pr_cfg, int queue_token)
1521 struct ehea_adapter *adapter = port->adapter;
1522 enum ehea_eq_type eq_type = EHEA_EQ;
1523 struct ehea_qp_init_attr *init_attr = NULL;
1524 int ret = -EIO;
1525 u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1527 tx_bytes = pr->tx_bytes;
1528 tx_packets = pr->tx_packets;
1529 rx_bytes = pr->rx_bytes;
1530 rx_packets = pr->rx_packets;
1532 memset(pr, 0, sizeof(struct ehea_port_res));
1534 pr->tx_bytes = rx_bytes;
1535 pr->tx_packets = tx_packets;
1536 pr->rx_bytes = rx_bytes;
1537 pr->rx_packets = rx_packets;
1539 pr->port = port;
1540 spin_lock_init(&pr->xmit_lock);
1541 spin_lock_init(&pr->netif_queue);
1543 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1544 if (!pr->eq) {
1545 pr_err("create_eq failed (eq)\n");
1546 goto out_free;
1549 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1550 pr->eq->fw_handle,
1551 port->logical_port_id);
1552 if (!pr->recv_cq) {
1553 pr_err("create_cq failed (cq_recv)\n");
1554 goto out_free;
1557 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1558 pr->eq->fw_handle,
1559 port->logical_port_id);
1560 if (!pr->send_cq) {
1561 pr_err("create_cq failed (cq_send)\n");
1562 goto out_free;
1565 if (netif_msg_ifup(port))
1566 pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1567 pr->send_cq->attr.act_nr_of_cqes,
1568 pr->recv_cq->attr.act_nr_of_cqes);
1570 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1571 if (!init_attr) {
1572 ret = -ENOMEM;
1573 pr_err("no mem for ehea_qp_init_attr\n");
1574 goto out_free;
1577 init_attr->low_lat_rq1 = 1;
1578 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1579 init_attr->rq_count = 3;
1580 init_attr->qp_token = queue_token;
1581 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1582 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1583 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1584 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1585 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1586 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1587 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1588 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1589 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1590 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1591 init_attr->port_nr = port->logical_port_id;
1592 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1593 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1594 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1596 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1597 if (!pr->qp) {
1598 pr_err("create_qp failed\n");
1599 ret = -EIO;
1600 goto out_free;
1603 if (netif_msg_ifup(port))
1604 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",
1605 init_attr->qp_nr,
1606 init_attr->act_nr_send_wqes,
1607 init_attr->act_nr_rwqes_rq1,
1608 init_attr->act_nr_rwqes_rq2,
1609 init_attr->act_nr_rwqes_rq3);
1611 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1613 ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1614 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1615 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1616 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1617 if (ret)
1618 goto out_free;
1620 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1621 if (ehea_gen_smrs(pr) != 0) {
1622 ret = -EIO;
1623 goto out_free;
1626 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1628 kfree(init_attr);
1630 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1632 pr->lro_mgr.max_aggr = pr->port->lro_max_aggr;
1633 pr->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1634 pr->lro_mgr.lro_arr = pr->lro_desc;
1635 pr->lro_mgr.get_skb_header = get_skb_hdr;
1636 pr->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1637 pr->lro_mgr.dev = port->netdev;
1638 pr->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1639 pr->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1641 ret = 0;
1642 goto out;
1644 out_free:
1645 kfree(init_attr);
1646 vfree(pr->sq_skba.arr);
1647 vfree(pr->rq1_skba.arr);
1648 vfree(pr->rq2_skba.arr);
1649 vfree(pr->rq3_skba.arr);
1650 ehea_destroy_qp(pr->qp);
1651 ehea_destroy_cq(pr->send_cq);
1652 ehea_destroy_cq(pr->recv_cq);
1653 ehea_destroy_eq(pr->eq);
1654 out:
1655 return ret;
1658 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1660 int ret, i;
1662 if (pr->qp)
1663 netif_napi_del(&pr->napi);
1665 ret = ehea_destroy_qp(pr->qp);
1667 if (!ret) {
1668 ehea_destroy_cq(pr->send_cq);
1669 ehea_destroy_cq(pr->recv_cq);
1670 ehea_destroy_eq(pr->eq);
1672 for (i = 0; i < pr->rq1_skba.len; i++)
1673 if (pr->rq1_skba.arr[i])
1674 dev_kfree_skb(pr->rq1_skba.arr[i]);
1676 for (i = 0; i < pr->rq2_skba.len; i++)
1677 if (pr->rq2_skba.arr[i])
1678 dev_kfree_skb(pr->rq2_skba.arr[i]);
1680 for (i = 0; i < pr->rq3_skba.len; i++)
1681 if (pr->rq3_skba.arr[i])
1682 dev_kfree_skb(pr->rq3_skba.arr[i]);
1684 for (i = 0; i < pr->sq_skba.len; i++)
1685 if (pr->sq_skba.arr[i])
1686 dev_kfree_skb(pr->sq_skba.arr[i]);
1688 vfree(pr->rq1_skba.arr);
1689 vfree(pr->rq2_skba.arr);
1690 vfree(pr->rq3_skba.arr);
1691 vfree(pr->sq_skba.arr);
1692 ret = ehea_rem_smrs(pr);
1694 return ret;
1698 * The write_* functions store information in swqe which is used by
1699 * the hardware to calculate the ip/tcp/udp checksum
1702 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1703 const struct sk_buff *skb)
1705 swqe->ip_start = skb_network_offset(skb);
1706 swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1709 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1710 const struct sk_buff *skb)
1712 swqe->tcp_offset =
1713 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1715 swqe->tcp_end = (u16)skb->len - 1;
1718 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1719 const struct sk_buff *skb)
1721 swqe->tcp_offset =
1722 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1724 swqe->tcp_end = (u16)skb->len - 1;
1728 static void write_swqe2_TSO(struct sk_buff *skb,
1729 struct ehea_swqe *swqe, u32 lkey)
1731 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1732 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1733 int skb_data_size = skb_headlen(skb);
1734 int headersize;
1736 /* Packet is TCP with TSO enabled */
1737 swqe->tx_control |= EHEA_SWQE_TSO;
1738 swqe->mss = skb_shinfo(skb)->gso_size;
1739 /* copy only eth/ip/tcp headers to immediate data and
1740 * the rest of skb->data to sg1entry
1742 headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1744 skb_data_size = skb_headlen(skb);
1746 if (skb_data_size >= headersize) {
1747 /* copy immediate data */
1748 skb_copy_from_linear_data(skb, imm_data, headersize);
1749 swqe->immediate_data_length = headersize;
1751 if (skb_data_size > headersize) {
1752 /* set sg1entry data */
1753 sg1entry->l_key = lkey;
1754 sg1entry->len = skb_data_size - headersize;
1755 sg1entry->vaddr =
1756 ehea_map_vaddr(skb->data + headersize);
1757 swqe->descriptors++;
1759 } else
1760 pr_err("cannot handle fragmented headers\n");
1763 static void write_swqe2_nonTSO(struct sk_buff *skb,
1764 struct ehea_swqe *swqe, u32 lkey)
1766 int skb_data_size = skb_headlen(skb);
1767 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1768 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1770 /* Packet is any nonTSO type
1772 * Copy as much as possible skb->data to immediate data and
1773 * the rest to sg1entry
1775 if (skb_data_size >= SWQE2_MAX_IMM) {
1776 /* copy immediate data */
1777 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1779 swqe->immediate_data_length = SWQE2_MAX_IMM;
1781 if (skb_data_size > SWQE2_MAX_IMM) {
1782 /* copy sg1entry data */
1783 sg1entry->l_key = lkey;
1784 sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1785 sg1entry->vaddr =
1786 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1787 swqe->descriptors++;
1789 } else {
1790 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1791 swqe->immediate_data_length = skb_data_size;
1795 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1796 struct ehea_swqe *swqe, u32 lkey)
1798 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1799 skb_frag_t *frag;
1800 int nfrags, sg1entry_contains_frag_data, i;
1802 nfrags = skb_shinfo(skb)->nr_frags;
1803 sg1entry = &swqe->u.immdata_desc.sg_entry;
1804 sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1805 swqe->descriptors = 0;
1806 sg1entry_contains_frag_data = 0;
1808 if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1809 write_swqe2_TSO(skb, swqe, lkey);
1810 else
1811 write_swqe2_nonTSO(skb, swqe, lkey);
1813 /* write descriptors */
1814 if (nfrags > 0) {
1815 if (swqe->descriptors == 0) {
1816 /* sg1entry not yet used */
1817 frag = &skb_shinfo(skb)->frags[0];
1819 /* copy sg1entry data */
1820 sg1entry->l_key = lkey;
1821 sg1entry->len = frag->size;
1822 sg1entry->vaddr =
1823 ehea_map_vaddr(page_address(frag->page)
1824 + frag->page_offset);
1825 swqe->descriptors++;
1826 sg1entry_contains_frag_data = 1;
1829 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1831 frag = &skb_shinfo(skb)->frags[i];
1832 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1834 sgentry->l_key = lkey;
1835 sgentry->len = frag->size;
1836 sgentry->vaddr =
1837 ehea_map_vaddr(page_address(frag->page)
1838 + frag->page_offset);
1839 swqe->descriptors++;
1844 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1846 int ret = 0;
1847 u64 hret;
1848 u8 reg_type;
1850 /* De/Register untagged packets */
1851 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1852 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1853 port->logical_port_id,
1854 reg_type, port->mac_addr, 0, hcallid);
1855 if (hret != H_SUCCESS) {
1856 pr_err("%sregistering bc address failed (tagged)\n",
1857 hcallid == H_REG_BCMC ? "" : "de");
1858 ret = -EIO;
1859 goto out_herr;
1862 /* De/Register VLAN packets */
1863 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1864 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1865 port->logical_port_id,
1866 reg_type, port->mac_addr, 0, hcallid);
1867 if (hret != H_SUCCESS) {
1868 pr_err("%sregistering bc address failed (vlan)\n",
1869 hcallid == H_REG_BCMC ? "" : "de");
1870 ret = -EIO;
1872 out_herr:
1873 return ret;
1876 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1878 struct ehea_port *port = netdev_priv(dev);
1879 struct sockaddr *mac_addr = sa;
1880 struct hcp_ehea_port_cb0 *cb0;
1881 int ret;
1882 u64 hret;
1884 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1885 ret = -EADDRNOTAVAIL;
1886 goto out;
1889 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1890 if (!cb0) {
1891 pr_err("no mem for cb0\n");
1892 ret = -ENOMEM;
1893 goto out;
1896 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1898 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1900 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1901 port->logical_port_id, H_PORT_CB0,
1902 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1903 if (hret != H_SUCCESS) {
1904 ret = -EIO;
1905 goto out_free;
1908 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1910 /* Deregister old MAC in pHYP */
1911 if (port->state == EHEA_PORT_UP) {
1912 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1913 if (ret)
1914 goto out_upregs;
1917 port->mac_addr = cb0->port_mac_addr << 16;
1919 /* Register new MAC in pHYP */
1920 if (port->state == EHEA_PORT_UP) {
1921 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1922 if (ret)
1923 goto out_upregs;
1926 ret = 0;
1928 out_upregs:
1929 ehea_update_bcmc_registrations();
1930 out_free:
1931 free_page((unsigned long)cb0);
1932 out:
1933 return ret;
1936 static void ehea_promiscuous_error(u64 hret, int enable)
1938 if (hret == H_AUTHORITY)
1939 pr_info("Hypervisor denied %sabling promiscuous mode\n",
1940 enable == 1 ? "en" : "dis");
1941 else
1942 pr_err("failed %sabling promiscuous mode\n",
1943 enable == 1 ? "en" : "dis");
1946 static void ehea_promiscuous(struct net_device *dev, int enable)
1948 struct ehea_port *port = netdev_priv(dev);
1949 struct hcp_ehea_port_cb7 *cb7;
1950 u64 hret;
1952 if (enable == port->promisc)
1953 return;
1955 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1956 if (!cb7) {
1957 pr_err("no mem for cb7\n");
1958 goto out;
1961 /* Modify Pxs_DUCQPN in CB7 */
1962 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1964 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1965 port->logical_port_id,
1966 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1967 if (hret) {
1968 ehea_promiscuous_error(hret, enable);
1969 goto out;
1972 port->promisc = enable;
1973 out:
1974 free_page((unsigned long)cb7);
1977 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1978 u32 hcallid)
1980 u64 hret;
1981 u8 reg_type;
1983 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1984 | EHEA_BCMC_UNTAGGED;
1986 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1987 port->logical_port_id,
1988 reg_type, mc_mac_addr, 0, hcallid);
1989 if (hret)
1990 goto out;
1992 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1993 | EHEA_BCMC_VLANID_ALL;
1995 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1996 port->logical_port_id,
1997 reg_type, mc_mac_addr, 0, hcallid);
1998 out:
1999 return hret;
2002 static int ehea_drop_multicast_list(struct net_device *dev)
2004 struct ehea_port *port = netdev_priv(dev);
2005 struct ehea_mc_list *mc_entry = port->mc_list;
2006 struct list_head *pos;
2007 struct list_head *temp;
2008 int ret = 0;
2009 u64 hret;
2011 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
2012 mc_entry = list_entry(pos, struct ehea_mc_list, list);
2014 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
2015 H_DEREG_BCMC);
2016 if (hret) {
2017 pr_err("failed deregistering mcast MAC\n");
2018 ret = -EIO;
2021 list_del(pos);
2022 kfree(mc_entry);
2024 return ret;
2027 static void ehea_allmulti(struct net_device *dev, int enable)
2029 struct ehea_port *port = netdev_priv(dev);
2030 u64 hret;
2032 if (!port->allmulti) {
2033 if (enable) {
2034 /* Enable ALLMULTI */
2035 ehea_drop_multicast_list(dev);
2036 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
2037 if (!hret)
2038 port->allmulti = 1;
2039 else
2040 netdev_err(dev,
2041 "failed enabling IFF_ALLMULTI\n");
2043 } else
2044 if (!enable) {
2045 /* Disable ALLMULTI */
2046 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
2047 if (!hret)
2048 port->allmulti = 0;
2049 else
2050 netdev_err(dev,
2051 "failed disabling IFF_ALLMULTI\n");
2055 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
2057 struct ehea_mc_list *ehea_mcl_entry;
2058 u64 hret;
2060 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
2061 if (!ehea_mcl_entry) {
2062 pr_err("no mem for mcl_entry\n");
2063 return;
2066 INIT_LIST_HEAD(&ehea_mcl_entry->list);
2068 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
2070 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
2071 H_REG_BCMC);
2072 if (!hret)
2073 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
2074 else {
2075 pr_err("failed registering mcast MAC\n");
2076 kfree(ehea_mcl_entry);
2080 static void ehea_set_multicast_list(struct net_device *dev)
2082 struct ehea_port *port = netdev_priv(dev);
2083 struct netdev_hw_addr *ha;
2084 int ret;
2086 if (dev->flags & IFF_PROMISC) {
2087 ehea_promiscuous(dev, 1);
2088 return;
2090 ehea_promiscuous(dev, 0);
2092 if (dev->flags & IFF_ALLMULTI) {
2093 ehea_allmulti(dev, 1);
2094 goto out;
2096 ehea_allmulti(dev, 0);
2098 if (!netdev_mc_empty(dev)) {
2099 ret = ehea_drop_multicast_list(dev);
2100 if (ret) {
2101 /* Dropping the current multicast list failed.
2102 * Enabling ALL_MULTI is the best we can do.
2104 ehea_allmulti(dev, 1);
2107 if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
2108 pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
2109 port->adapter->max_mc_mac);
2110 goto out;
2113 netdev_for_each_mc_addr(ha, dev)
2114 ehea_add_multicast_entry(port, ha->addr);
2117 out:
2118 ehea_update_bcmc_registrations();
2121 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
2123 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
2124 return -EINVAL;
2125 dev->mtu = new_mtu;
2126 return 0;
2129 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2130 struct ehea_swqe *swqe, u32 lkey)
2132 if (skb->protocol == htons(ETH_P_IP)) {
2133 const struct iphdr *iph = ip_hdr(skb);
2135 /* IPv4 */
2136 swqe->tx_control |= EHEA_SWQE_CRC
2137 | EHEA_SWQE_IP_CHECKSUM
2138 | EHEA_SWQE_TCP_CHECKSUM
2139 | EHEA_SWQE_IMM_DATA_PRESENT
2140 | EHEA_SWQE_DESCRIPTORS_PRESENT;
2142 write_ip_start_end(swqe, skb);
2144 if (iph->protocol == IPPROTO_UDP) {
2145 if ((iph->frag_off & IP_MF) ||
2146 (iph->frag_off & IP_OFFSET))
2147 /* IP fragment, so don't change cs */
2148 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
2149 else
2150 write_udp_offset_end(swqe, skb);
2151 } else if (iph->protocol == IPPROTO_TCP) {
2152 write_tcp_offset_end(swqe, skb);
2155 /* icmp (big data) and ip segmentation packets (all other ip
2156 packets) do not require any special handling */
2158 } else {
2159 /* Other Ethernet Protocol */
2160 swqe->tx_control |= EHEA_SWQE_CRC
2161 | EHEA_SWQE_IMM_DATA_PRESENT
2162 | EHEA_SWQE_DESCRIPTORS_PRESENT;
2165 write_swqe2_data(skb, dev, swqe, lkey);
2168 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2169 struct ehea_swqe *swqe)
2171 int nfrags = skb_shinfo(skb)->nr_frags;
2172 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2173 skb_frag_t *frag;
2174 int i;
2176 if (skb->protocol == htons(ETH_P_IP)) {
2177 const struct iphdr *iph = ip_hdr(skb);
2179 /* IPv4 */
2180 write_ip_start_end(swqe, skb);
2182 if (iph->protocol == IPPROTO_TCP) {
2183 swqe->tx_control |= EHEA_SWQE_CRC
2184 | EHEA_SWQE_IP_CHECKSUM
2185 | EHEA_SWQE_TCP_CHECKSUM
2186 | EHEA_SWQE_IMM_DATA_PRESENT;
2188 write_tcp_offset_end(swqe, skb);
2190 } else if (iph->protocol == IPPROTO_UDP) {
2191 if ((iph->frag_off & IP_MF) ||
2192 (iph->frag_off & IP_OFFSET))
2193 /* IP fragment, so don't change cs */
2194 swqe->tx_control |= EHEA_SWQE_CRC
2195 | EHEA_SWQE_IMM_DATA_PRESENT;
2196 else {
2197 swqe->tx_control |= EHEA_SWQE_CRC
2198 | EHEA_SWQE_IP_CHECKSUM
2199 | EHEA_SWQE_TCP_CHECKSUM
2200 | EHEA_SWQE_IMM_DATA_PRESENT;
2202 write_udp_offset_end(swqe, skb);
2204 } else {
2205 /* icmp (big data) and
2206 ip segmentation packets (all other ip packets) */
2207 swqe->tx_control |= EHEA_SWQE_CRC
2208 | EHEA_SWQE_IP_CHECKSUM
2209 | EHEA_SWQE_IMM_DATA_PRESENT;
2211 } else {
2212 /* Other Ethernet Protocol */
2213 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
2215 /* copy (immediate) data */
2216 if (nfrags == 0) {
2217 /* data is in a single piece */
2218 skb_copy_from_linear_data(skb, imm_data, skb->len);
2219 } else {
2220 /* first copy data from the skb->data buffer ... */
2221 skb_copy_from_linear_data(skb, imm_data,
2222 skb_headlen(skb));
2223 imm_data += skb_headlen(skb);
2225 /* ... then copy data from the fragments */
2226 for (i = 0; i < nfrags; i++) {
2227 frag = &skb_shinfo(skb)->frags[i];
2228 memcpy(imm_data,
2229 page_address(frag->page) + frag->page_offset,
2230 frag->size);
2231 imm_data += frag->size;
2234 swqe->immediate_data_length = skb->len;
2235 dev_kfree_skb(skb);
2238 static inline int ehea_hash_skb(struct sk_buff *skb, int num_qps)
2240 struct tcphdr *tcp;
2241 u32 tmp;
2243 if ((skb->protocol == htons(ETH_P_IP)) &&
2244 (ip_hdr(skb)->protocol == IPPROTO_TCP)) {
2245 tcp = (struct tcphdr *)(skb_network_header(skb) +
2246 (ip_hdr(skb)->ihl * 4));
2247 tmp = (tcp->source + (tcp->dest << 16)) % 31;
2248 tmp += ip_hdr(skb)->daddr % 31;
2249 return tmp % num_qps;
2250 } else
2251 return 0;
2254 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2256 struct ehea_port *port = netdev_priv(dev);
2257 struct ehea_swqe *swqe;
2258 unsigned long flags;
2259 u32 lkey;
2260 int swqe_index;
2261 struct ehea_port_res *pr;
2263 pr = &port->port_res[ehea_hash_skb(skb, port->num_tx_qps)];
2265 if (!spin_trylock(&pr->xmit_lock))
2266 return NETDEV_TX_BUSY;
2268 if (pr->queue_stopped) {
2269 spin_unlock(&pr->xmit_lock);
2270 return NETDEV_TX_BUSY;
2273 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2274 memset(swqe, 0, SWQE_HEADER_SIZE);
2275 atomic_dec(&pr->swqe_avail);
2277 if (vlan_tx_tag_present(skb)) {
2278 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2279 swqe->vlan_tag = vlan_tx_tag_get(skb);
2282 pr->tx_packets++;
2283 pr->tx_bytes += skb->len;
2285 if (skb->len <= SWQE3_MAX_IMM) {
2286 u32 sig_iv = port->sig_comp_iv;
2287 u32 swqe_num = pr->swqe_id_counter;
2288 ehea_xmit3(skb, dev, swqe);
2289 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2290 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2291 if (pr->swqe_ll_count >= (sig_iv - 1)) {
2292 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2293 sig_iv);
2294 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2295 pr->swqe_ll_count = 0;
2296 } else
2297 pr->swqe_ll_count += 1;
2298 } else {
2299 swqe->wr_id =
2300 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2301 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2302 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2303 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2304 pr->sq_skba.arr[pr->sq_skba.index] = skb;
2306 pr->sq_skba.index++;
2307 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2309 lkey = pr->send_mr.lkey;
2310 ehea_xmit2(skb, dev, swqe, lkey);
2311 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2313 pr->swqe_id_counter += 1;
2315 netif_info(port, tx_queued, dev,
2316 "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2317 if (netif_msg_tx_queued(port))
2318 ehea_dump(swqe, 512, "swqe");
2320 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2321 netif_stop_queue(dev);
2322 swqe->tx_control |= EHEA_SWQE_PURGE;
2325 ehea_post_swqe(pr->qp, swqe);
2327 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2328 spin_lock_irqsave(&pr->netif_queue, flags);
2329 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2330 pr->p_stats.queue_stopped++;
2331 netif_stop_queue(dev);
2332 pr->queue_stopped = 1;
2334 spin_unlock_irqrestore(&pr->netif_queue, flags);
2336 dev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
2337 spin_unlock(&pr->xmit_lock);
2339 return NETDEV_TX_OK;
2342 static void ehea_vlan_rx_register(struct net_device *dev,
2343 struct vlan_group *grp)
2345 struct ehea_port *port = netdev_priv(dev);
2346 struct ehea_adapter *adapter = port->adapter;
2347 struct hcp_ehea_port_cb1 *cb1;
2348 u64 hret;
2350 port->vgrp = grp;
2352 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2353 if (!cb1) {
2354 pr_err("no mem for cb1\n");
2355 goto out;
2358 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2359 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2360 if (hret != H_SUCCESS)
2361 pr_err("modify_ehea_port failed\n");
2363 free_page((unsigned long)cb1);
2364 out:
2365 return;
2368 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
2370 struct ehea_port *port = netdev_priv(dev);
2371 struct ehea_adapter *adapter = port->adapter;
2372 struct hcp_ehea_port_cb1 *cb1;
2373 int index;
2374 u64 hret;
2376 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2377 if (!cb1) {
2378 pr_err("no mem for cb1\n");
2379 goto out;
2382 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2383 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2384 if (hret != H_SUCCESS) {
2385 pr_err("query_ehea_port failed\n");
2386 goto out;
2389 index = (vid / 64);
2390 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2392 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2393 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2394 if (hret != H_SUCCESS)
2395 pr_err("modify_ehea_port failed\n");
2396 out:
2397 free_page((unsigned long)cb1);
2398 return;
2401 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2403 struct ehea_port *port = netdev_priv(dev);
2404 struct ehea_adapter *adapter = port->adapter;
2405 struct hcp_ehea_port_cb1 *cb1;
2406 int index;
2407 u64 hret;
2409 vlan_group_set_device(port->vgrp, vid, NULL);
2411 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2412 if (!cb1) {
2413 pr_err("no mem for cb1\n");
2414 goto out;
2417 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2418 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2419 if (hret != H_SUCCESS) {
2420 pr_err("query_ehea_port failed\n");
2421 goto out;
2424 index = (vid / 64);
2425 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2427 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2428 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2429 if (hret != H_SUCCESS)
2430 pr_err("modify_ehea_port failed\n");
2431 out:
2432 free_page((unsigned long)cb1);
2435 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2437 int ret = -EIO;
2438 u64 hret;
2439 u16 dummy16 = 0;
2440 u64 dummy64 = 0;
2441 struct hcp_modify_qp_cb0 *cb0;
2443 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2444 if (!cb0) {
2445 ret = -ENOMEM;
2446 goto out;
2449 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2450 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2451 if (hret != H_SUCCESS) {
2452 pr_err("query_ehea_qp failed (1)\n");
2453 goto out;
2456 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2457 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2458 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2459 &dummy64, &dummy64, &dummy16, &dummy16);
2460 if (hret != H_SUCCESS) {
2461 pr_err("modify_ehea_qp failed (1)\n");
2462 goto out;
2465 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2466 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2467 if (hret != H_SUCCESS) {
2468 pr_err("query_ehea_qp failed (2)\n");
2469 goto out;
2472 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2473 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2474 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2475 &dummy64, &dummy64, &dummy16, &dummy16);
2476 if (hret != H_SUCCESS) {
2477 pr_err("modify_ehea_qp failed (2)\n");
2478 goto out;
2481 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2482 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2483 if (hret != H_SUCCESS) {
2484 pr_err("query_ehea_qp failed (3)\n");
2485 goto out;
2488 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2489 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2490 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2491 &dummy64, &dummy64, &dummy16, &dummy16);
2492 if (hret != H_SUCCESS) {
2493 pr_err("modify_ehea_qp failed (3)\n");
2494 goto out;
2497 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2498 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2499 if (hret != H_SUCCESS) {
2500 pr_err("query_ehea_qp failed (4)\n");
2501 goto out;
2504 ret = 0;
2505 out:
2506 free_page((unsigned long)cb0);
2507 return ret;
2510 static int ehea_port_res_setup(struct ehea_port *port, int def_qps,
2511 int add_tx_qps)
2513 int ret, i;
2514 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2515 enum ehea_eq_type eq_type = EHEA_EQ;
2517 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2518 EHEA_MAX_ENTRIES_EQ, 1);
2519 if (!port->qp_eq) {
2520 ret = -EINVAL;
2521 pr_err("ehea_create_eq failed (qp_eq)\n");
2522 goto out_kill_eq;
2525 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2526 pr_cfg.max_entries_scq = sq_entries * 2;
2527 pr_cfg.max_entries_sq = sq_entries;
2528 pr_cfg.max_entries_rq1 = rq1_entries;
2529 pr_cfg.max_entries_rq2 = rq2_entries;
2530 pr_cfg.max_entries_rq3 = rq3_entries;
2532 pr_cfg_small_rx.max_entries_rcq = 1;
2533 pr_cfg_small_rx.max_entries_scq = sq_entries;
2534 pr_cfg_small_rx.max_entries_sq = sq_entries;
2535 pr_cfg_small_rx.max_entries_rq1 = 1;
2536 pr_cfg_small_rx.max_entries_rq2 = 1;
2537 pr_cfg_small_rx.max_entries_rq3 = 1;
2539 for (i = 0; i < def_qps; i++) {
2540 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2541 if (ret)
2542 goto out_clean_pr;
2544 for (i = def_qps; i < def_qps + add_tx_qps; i++) {
2545 ret = ehea_init_port_res(port, &port->port_res[i],
2546 &pr_cfg_small_rx, i);
2547 if (ret)
2548 goto out_clean_pr;
2551 return 0;
2553 out_clean_pr:
2554 while (--i >= 0)
2555 ehea_clean_portres(port, &port->port_res[i]);
2557 out_kill_eq:
2558 ehea_destroy_eq(port->qp_eq);
2559 return ret;
2562 static int ehea_clean_all_portres(struct ehea_port *port)
2564 int ret = 0;
2565 int i;
2567 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2568 ret |= ehea_clean_portres(port, &port->port_res[i]);
2570 ret |= ehea_destroy_eq(port->qp_eq);
2572 return ret;
2575 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2577 if (adapter->active_ports)
2578 return;
2580 ehea_rem_mr(&adapter->mr);
2583 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2585 if (adapter->active_ports)
2586 return 0;
2588 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2591 static int ehea_up(struct net_device *dev)
2593 int ret, i;
2594 struct ehea_port *port = netdev_priv(dev);
2596 if (port->state == EHEA_PORT_UP)
2597 return 0;
2599 ret = ehea_port_res_setup(port, port->num_def_qps,
2600 port->num_add_tx_qps);
2601 if (ret) {
2602 netdev_err(dev, "port_res_failed\n");
2603 goto out;
2606 /* Set default QP for this port */
2607 ret = ehea_configure_port(port);
2608 if (ret) {
2609 netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2610 goto out_clean_pr;
2613 ret = ehea_reg_interrupts(dev);
2614 if (ret) {
2615 netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2616 goto out_clean_pr;
2619 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2620 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2621 if (ret) {
2622 netdev_err(dev, "activate_qp failed\n");
2623 goto out_free_irqs;
2627 for (i = 0; i < port->num_def_qps; i++) {
2628 ret = ehea_fill_port_res(&port->port_res[i]);
2629 if (ret) {
2630 netdev_err(dev, "out_free_irqs\n");
2631 goto out_free_irqs;
2635 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2636 if (ret) {
2637 ret = -EIO;
2638 goto out_free_irqs;
2641 port->state = EHEA_PORT_UP;
2643 ret = 0;
2644 goto out;
2646 out_free_irqs:
2647 ehea_free_interrupts(dev);
2649 out_clean_pr:
2650 ehea_clean_all_portres(port);
2651 out:
2652 if (ret)
2653 netdev_info(dev, "Failed starting. ret=%i\n", ret);
2655 ehea_update_bcmc_registrations();
2656 ehea_update_firmware_handles();
2658 return ret;
2661 static void port_napi_disable(struct ehea_port *port)
2663 int i;
2665 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2666 napi_disable(&port->port_res[i].napi);
2669 static void port_napi_enable(struct ehea_port *port)
2671 int i;
2673 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2674 napi_enable(&port->port_res[i].napi);
2677 static int ehea_open(struct net_device *dev)
2679 int ret;
2680 struct ehea_port *port = netdev_priv(dev);
2682 mutex_lock(&port->port_lock);
2684 netif_info(port, ifup, dev, "enabling port\n");
2686 ret = ehea_up(dev);
2687 if (!ret) {
2688 port_napi_enable(port);
2689 netif_start_queue(dev);
2692 mutex_unlock(&port->port_lock);
2694 return ret;
2697 static int ehea_down(struct net_device *dev)
2699 int ret;
2700 struct ehea_port *port = netdev_priv(dev);
2702 if (port->state == EHEA_PORT_DOWN)
2703 return 0;
2705 ehea_drop_multicast_list(dev);
2706 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2708 ehea_free_interrupts(dev);
2710 port->state = EHEA_PORT_DOWN;
2712 ehea_update_bcmc_registrations();
2714 ret = ehea_clean_all_portres(port);
2715 if (ret)
2716 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2718 ehea_update_firmware_handles();
2720 return ret;
2723 static int ehea_stop(struct net_device *dev)
2725 int ret;
2726 struct ehea_port *port = netdev_priv(dev);
2728 netif_info(port, ifdown, dev, "disabling port\n");
2730 set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2731 cancel_work_sync(&port->reset_task);
2732 mutex_lock(&port->port_lock);
2733 netif_stop_queue(dev);
2734 port_napi_disable(port);
2735 ret = ehea_down(dev);
2736 mutex_unlock(&port->port_lock);
2737 clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2738 return ret;
2741 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2743 struct ehea_qp qp = *orig_qp;
2744 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2745 struct ehea_swqe *swqe;
2746 int wqe_index;
2747 int i;
2749 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2750 swqe = ehea_get_swqe(&qp, &wqe_index);
2751 swqe->tx_control |= EHEA_SWQE_PURGE;
2755 static void ehea_flush_sq(struct ehea_port *port)
2757 int i;
2759 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2760 struct ehea_port_res *pr = &port->port_res[i];
2761 int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2762 int ret;
2764 ret = wait_event_timeout(port->swqe_avail_wq,
2765 atomic_read(&pr->swqe_avail) >= swqe_max,
2766 msecs_to_jiffies(100));
2768 if (!ret) {
2769 pr_err("WARNING: sq not flushed completely\n");
2770 break;
2775 int ehea_stop_qps(struct net_device *dev)
2777 struct ehea_port *port = netdev_priv(dev);
2778 struct ehea_adapter *adapter = port->adapter;
2779 struct hcp_modify_qp_cb0 *cb0;
2780 int ret = -EIO;
2781 int dret;
2782 int i;
2783 u64 hret;
2784 u64 dummy64 = 0;
2785 u16 dummy16 = 0;
2787 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2788 if (!cb0) {
2789 ret = -ENOMEM;
2790 goto out;
2793 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2794 struct ehea_port_res *pr = &port->port_res[i];
2795 struct ehea_qp *qp = pr->qp;
2797 /* Purge send queue */
2798 ehea_purge_sq(qp);
2800 /* Disable queue pair */
2801 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2802 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2803 cb0);
2804 if (hret != H_SUCCESS) {
2805 pr_err("query_ehea_qp failed (1)\n");
2806 goto out;
2809 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2810 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2812 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2813 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2814 1), cb0, &dummy64,
2815 &dummy64, &dummy16, &dummy16);
2816 if (hret != H_SUCCESS) {
2817 pr_err("modify_ehea_qp failed (1)\n");
2818 goto out;
2821 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2822 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2823 cb0);
2824 if (hret != H_SUCCESS) {
2825 pr_err("query_ehea_qp failed (2)\n");
2826 goto out;
2829 /* deregister shared memory regions */
2830 dret = ehea_rem_smrs(pr);
2831 if (dret) {
2832 pr_err("unreg shared memory region failed\n");
2833 goto out;
2837 ret = 0;
2838 out:
2839 free_page((unsigned long)cb0);
2841 return ret;
2844 void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2846 struct ehea_qp qp = *orig_qp;
2847 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2848 struct ehea_rwqe *rwqe;
2849 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2850 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2851 struct sk_buff *skb;
2852 u32 lkey = pr->recv_mr.lkey;
2855 int i;
2856 int index;
2858 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2859 rwqe = ehea_get_next_rwqe(&qp, 2);
2860 rwqe->sg_list[0].l_key = lkey;
2861 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2862 skb = skba_rq2[index];
2863 if (skb)
2864 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2867 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2868 rwqe = ehea_get_next_rwqe(&qp, 3);
2869 rwqe->sg_list[0].l_key = lkey;
2870 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2871 skb = skba_rq3[index];
2872 if (skb)
2873 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2877 int ehea_restart_qps(struct net_device *dev)
2879 struct ehea_port *port = netdev_priv(dev);
2880 struct ehea_adapter *adapter = port->adapter;
2881 int ret = 0;
2882 int i;
2884 struct hcp_modify_qp_cb0 *cb0;
2885 u64 hret;
2886 u64 dummy64 = 0;
2887 u16 dummy16 = 0;
2889 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2890 if (!cb0) {
2891 ret = -ENOMEM;
2892 goto out;
2895 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2896 struct ehea_port_res *pr = &port->port_res[i];
2897 struct ehea_qp *qp = pr->qp;
2899 ret = ehea_gen_smrs(pr);
2900 if (ret) {
2901 netdev_err(dev, "creation of shared memory regions failed\n");
2902 goto out;
2905 ehea_update_rqs(qp, pr);
2907 /* Enable queue pair */
2908 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2909 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2910 cb0);
2911 if (hret != H_SUCCESS) {
2912 netdev_err(dev, "query_ehea_qp failed (1)\n");
2913 goto out;
2916 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2917 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2919 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2920 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2921 1), cb0, &dummy64,
2922 &dummy64, &dummy16, &dummy16);
2923 if (hret != H_SUCCESS) {
2924 netdev_err(dev, "modify_ehea_qp failed (1)\n");
2925 goto out;
2928 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2929 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2930 cb0);
2931 if (hret != H_SUCCESS) {
2932 netdev_err(dev, "query_ehea_qp failed (2)\n");
2933 goto out;
2936 /* refill entire queue */
2937 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2938 ehea_refill_rq2(pr, 0);
2939 ehea_refill_rq3(pr, 0);
2941 out:
2942 free_page((unsigned long)cb0);
2944 return ret;
2947 static void ehea_reset_port(struct work_struct *work)
2949 int ret;
2950 struct ehea_port *port =
2951 container_of(work, struct ehea_port, reset_task);
2952 struct net_device *dev = port->netdev;
2954 mutex_lock(&dlpar_mem_lock);
2955 port->resets++;
2956 mutex_lock(&port->port_lock);
2957 netif_stop_queue(dev);
2959 port_napi_disable(port);
2961 ehea_down(dev);
2963 ret = ehea_up(dev);
2964 if (ret)
2965 goto out;
2967 ehea_set_multicast_list(dev);
2969 netif_info(port, timer, dev, "reset successful\n");
2971 port_napi_enable(port);
2973 netif_wake_queue(dev);
2974 out:
2975 mutex_unlock(&port->port_lock);
2976 mutex_unlock(&dlpar_mem_lock);
2979 static void ehea_rereg_mrs(void)
2981 int ret, i;
2982 struct ehea_adapter *adapter;
2984 pr_info("LPAR memory changed - re-initializing driver\n");
2986 list_for_each_entry(adapter, &adapter_list, list)
2987 if (adapter->active_ports) {
2988 /* Shutdown all ports */
2989 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2990 struct ehea_port *port = adapter->port[i];
2991 struct net_device *dev;
2993 if (!port)
2994 continue;
2996 dev = port->netdev;
2998 if (dev->flags & IFF_UP) {
2999 mutex_lock(&port->port_lock);
3000 netif_stop_queue(dev);
3001 ehea_flush_sq(port);
3002 ret = ehea_stop_qps(dev);
3003 if (ret) {
3004 mutex_unlock(&port->port_lock);
3005 goto out;
3007 port_napi_disable(port);
3008 mutex_unlock(&port->port_lock);
3010 reset_sq_restart_flag(port);
3013 /* Unregister old memory region */
3014 ret = ehea_rem_mr(&adapter->mr);
3015 if (ret) {
3016 pr_err("unregister MR failed - driver inoperable!\n");
3017 goto out;
3021 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3023 list_for_each_entry(adapter, &adapter_list, list)
3024 if (adapter->active_ports) {
3025 /* Register new memory region */
3026 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
3027 if (ret) {
3028 pr_err("register MR failed - driver inoperable!\n");
3029 goto out;
3032 /* Restart all ports */
3033 for (i = 0; i < EHEA_MAX_PORTS; i++) {
3034 struct ehea_port *port = adapter->port[i];
3036 if (port) {
3037 struct net_device *dev = port->netdev;
3039 if (dev->flags & IFF_UP) {
3040 mutex_lock(&port->port_lock);
3041 ret = ehea_restart_qps(dev);
3042 if (!ret) {
3043 check_sqs(port);
3044 port_napi_enable(port);
3045 netif_wake_queue(dev);
3046 } else {
3047 netdev_err(dev, "Unable to restart QPS\n");
3049 mutex_unlock(&port->port_lock);
3054 pr_info("re-initializing driver complete\n");
3055 out:
3056 return;
3059 static void ehea_tx_watchdog(struct net_device *dev)
3061 struct ehea_port *port = netdev_priv(dev);
3063 if (netif_carrier_ok(dev) &&
3064 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
3065 ehea_schedule_port_reset(port);
3068 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
3070 struct hcp_query_ehea *cb;
3071 u64 hret;
3072 int ret;
3074 cb = (void *)get_zeroed_page(GFP_KERNEL);
3075 if (!cb) {
3076 ret = -ENOMEM;
3077 goto out;
3080 hret = ehea_h_query_ehea(adapter->handle, cb);
3082 if (hret != H_SUCCESS) {
3083 ret = -EIO;
3084 goto out_herr;
3087 adapter->max_mc_mac = cb->max_mc_mac - 1;
3088 ret = 0;
3090 out_herr:
3091 free_page((unsigned long)cb);
3092 out:
3093 return ret;
3096 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
3098 struct hcp_ehea_port_cb4 *cb4;
3099 u64 hret;
3100 int ret = 0;
3102 *jumbo = 0;
3104 /* (Try to) enable *jumbo frames */
3105 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
3106 if (!cb4) {
3107 pr_err("no mem for cb4\n");
3108 ret = -ENOMEM;
3109 goto out;
3110 } else {
3111 hret = ehea_h_query_ehea_port(port->adapter->handle,
3112 port->logical_port_id,
3113 H_PORT_CB4,
3114 H_PORT_CB4_JUMBO, cb4);
3115 if (hret == H_SUCCESS) {
3116 if (cb4->jumbo_frame)
3117 *jumbo = 1;
3118 else {
3119 cb4->jumbo_frame = 1;
3120 hret = ehea_h_modify_ehea_port(port->adapter->
3121 handle,
3122 port->
3123 logical_port_id,
3124 H_PORT_CB4,
3125 H_PORT_CB4_JUMBO,
3126 cb4);
3127 if (hret == H_SUCCESS)
3128 *jumbo = 1;
3130 } else
3131 ret = -EINVAL;
3133 free_page((unsigned long)cb4);
3135 out:
3136 return ret;
3139 static ssize_t ehea_show_port_id(struct device *dev,
3140 struct device_attribute *attr, char *buf)
3142 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
3143 return sprintf(buf, "%d", port->logical_port_id);
3146 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
3147 NULL);
3149 static void __devinit logical_port_release(struct device *dev)
3151 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
3152 of_node_put(port->ofdev.dev.of_node);
3155 static struct device *ehea_register_port(struct ehea_port *port,
3156 struct device_node *dn)
3158 int ret;
3160 port->ofdev.dev.of_node = of_node_get(dn);
3161 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
3162 port->ofdev.dev.bus = &ibmebus_bus_type;
3164 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
3165 port->ofdev.dev.release = logical_port_release;
3167 ret = of_device_register(&port->ofdev);
3168 if (ret) {
3169 pr_err("failed to register device. ret=%d\n", ret);
3170 goto out;
3173 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
3174 if (ret) {
3175 pr_err("failed to register attributes, ret=%d\n", ret);
3176 goto out_unreg_of_dev;
3179 return &port->ofdev.dev;
3181 out_unreg_of_dev:
3182 of_device_unregister(&port->ofdev);
3183 out:
3184 return NULL;
3187 static void ehea_unregister_port(struct ehea_port *port)
3189 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
3190 of_device_unregister(&port->ofdev);
3193 static const struct net_device_ops ehea_netdev_ops = {
3194 .ndo_open = ehea_open,
3195 .ndo_stop = ehea_stop,
3196 .ndo_start_xmit = ehea_start_xmit,
3197 #ifdef CONFIG_NET_POLL_CONTROLLER
3198 .ndo_poll_controller = ehea_netpoll,
3199 #endif
3200 .ndo_get_stats = ehea_get_stats,
3201 .ndo_set_mac_address = ehea_set_mac_addr,
3202 .ndo_validate_addr = eth_validate_addr,
3203 .ndo_set_multicast_list = ehea_set_multicast_list,
3204 .ndo_change_mtu = ehea_change_mtu,
3205 .ndo_vlan_rx_register = ehea_vlan_rx_register,
3206 .ndo_vlan_rx_add_vid = ehea_vlan_rx_add_vid,
3207 .ndo_vlan_rx_kill_vid = ehea_vlan_rx_kill_vid,
3208 .ndo_tx_timeout = ehea_tx_watchdog,
3211 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
3212 u32 logical_port_id,
3213 struct device_node *dn)
3215 int ret;
3216 struct net_device *dev;
3217 struct ehea_port *port;
3218 struct device *port_dev;
3219 int jumbo;
3221 /* allocate memory for the port structures */
3222 dev = alloc_etherdev(sizeof(struct ehea_port));
3224 if (!dev) {
3225 pr_err("no mem for net_device\n");
3226 ret = -ENOMEM;
3227 goto out_err;
3230 port = netdev_priv(dev);
3232 mutex_init(&port->port_lock);
3233 port->state = EHEA_PORT_DOWN;
3234 port->sig_comp_iv = sq_entries / 10;
3236 port->adapter = adapter;
3237 port->netdev = dev;
3238 port->logical_port_id = logical_port_id;
3240 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
3242 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
3243 if (!port->mc_list) {
3244 ret = -ENOMEM;
3245 goto out_free_ethdev;
3248 INIT_LIST_HEAD(&port->mc_list->list);
3250 ret = ehea_sense_port_attr(port);
3251 if (ret)
3252 goto out_free_mc_list;
3254 port_dev = ehea_register_port(port, dn);
3255 if (!port_dev)
3256 goto out_free_mc_list;
3258 SET_NETDEV_DEV(dev, port_dev);
3260 /* initialize net_device structure */
3261 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3263 dev->netdev_ops = &ehea_netdev_ops;
3264 ehea_set_ethtool_ops(dev);
3266 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
3267 | NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_TX | NETIF_F_LRO;
3268 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
3269 | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_TX
3270 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
3271 | NETIF_F_LLTX | NETIF_F_RXCSUM;
3272 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3274 if (use_lro)
3275 dev->features |= NETIF_F_LRO;
3277 INIT_WORK(&port->reset_task, ehea_reset_port);
3279 init_waitqueue_head(&port->swqe_avail_wq);
3280 init_waitqueue_head(&port->restart_wq);
3282 ret = register_netdev(dev);
3283 if (ret) {
3284 pr_err("register_netdev failed. ret=%d\n", ret);
3285 goto out_unreg_port;
3288 port->lro_max_aggr = lro_max_aggr;
3290 ret = ehea_get_jumboframe_status(port, &jumbo);
3291 if (ret)
3292 netdev_err(dev, "failed determining jumbo frame status\n");
3294 netdev_info(dev, "Jumbo frames are %sabled\n",
3295 jumbo == 1 ? "en" : "dis");
3297 adapter->active_ports++;
3299 return port;
3301 out_unreg_port:
3302 ehea_unregister_port(port);
3304 out_free_mc_list:
3305 kfree(port->mc_list);
3307 out_free_ethdev:
3308 free_netdev(dev);
3310 out_err:
3311 pr_err("setting up logical port with id=%d failed, ret=%d\n",
3312 logical_port_id, ret);
3313 return NULL;
3316 static void ehea_shutdown_single_port(struct ehea_port *port)
3318 struct ehea_adapter *adapter = port->adapter;
3320 cancel_work_sync(&port->reset_task);
3321 unregister_netdev(port->netdev);
3322 ehea_unregister_port(port);
3323 kfree(port->mc_list);
3324 free_netdev(port->netdev);
3325 adapter->active_ports--;
3328 static int ehea_setup_ports(struct ehea_adapter *adapter)
3330 struct device_node *lhea_dn;
3331 struct device_node *eth_dn = NULL;
3333 const u32 *dn_log_port_id;
3334 int i = 0;
3336 lhea_dn = adapter->ofdev->dev.of_node;
3337 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3339 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3340 NULL);
3341 if (!dn_log_port_id) {
3342 pr_err("bad device node: eth_dn name=%s\n",
3343 eth_dn->full_name);
3344 continue;
3347 if (ehea_add_adapter_mr(adapter)) {
3348 pr_err("creating MR failed\n");
3349 of_node_put(eth_dn);
3350 return -EIO;
3353 adapter->port[i] = ehea_setup_single_port(adapter,
3354 *dn_log_port_id,
3355 eth_dn);
3356 if (adapter->port[i])
3357 netdev_info(adapter->port[i]->netdev,
3358 "logical port id #%d\n", *dn_log_port_id);
3359 else
3360 ehea_remove_adapter_mr(adapter);
3362 i++;
3364 return 0;
3367 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3368 u32 logical_port_id)
3370 struct device_node *lhea_dn;
3371 struct device_node *eth_dn = NULL;
3372 const u32 *dn_log_port_id;
3374 lhea_dn = adapter->ofdev->dev.of_node;
3375 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3377 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3378 NULL);
3379 if (dn_log_port_id)
3380 if (*dn_log_port_id == logical_port_id)
3381 return eth_dn;
3384 return NULL;
3387 static ssize_t ehea_probe_port(struct device *dev,
3388 struct device_attribute *attr,
3389 const char *buf, size_t count)
3391 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3392 struct ehea_port *port;
3393 struct device_node *eth_dn = NULL;
3394 int i;
3396 u32 logical_port_id;
3398 sscanf(buf, "%d", &logical_port_id);
3400 port = ehea_get_port(adapter, logical_port_id);
3402 if (port) {
3403 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3404 logical_port_id);
3405 return -EINVAL;
3408 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3410 if (!eth_dn) {
3411 pr_info("no logical port with id %d found\n", logical_port_id);
3412 return -EINVAL;
3415 if (ehea_add_adapter_mr(adapter)) {
3416 pr_err("creating MR failed\n");
3417 return -EIO;
3420 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3422 of_node_put(eth_dn);
3424 if (port) {
3425 for (i = 0; i < EHEA_MAX_PORTS; i++)
3426 if (!adapter->port[i]) {
3427 adapter->port[i] = port;
3428 break;
3431 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3432 logical_port_id);
3433 } else {
3434 ehea_remove_adapter_mr(adapter);
3435 return -EIO;
3438 return (ssize_t) count;
3441 static ssize_t ehea_remove_port(struct device *dev,
3442 struct device_attribute *attr,
3443 const char *buf, size_t count)
3445 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3446 struct ehea_port *port;
3447 int i;
3448 u32 logical_port_id;
3450 sscanf(buf, "%d", &logical_port_id);
3452 port = ehea_get_port(adapter, logical_port_id);
3454 if (port) {
3455 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3456 logical_port_id);
3458 ehea_shutdown_single_port(port);
3460 for (i = 0; i < EHEA_MAX_PORTS; i++)
3461 if (adapter->port[i] == port) {
3462 adapter->port[i] = NULL;
3463 break;
3465 } else {
3466 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3467 logical_port_id);
3468 return -EINVAL;
3471 ehea_remove_adapter_mr(adapter);
3473 return (ssize_t) count;
3476 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3477 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3479 int ehea_create_device_sysfs(struct platform_device *dev)
3481 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3482 if (ret)
3483 goto out;
3485 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3486 out:
3487 return ret;
3490 void ehea_remove_device_sysfs(struct platform_device *dev)
3492 device_remove_file(&dev->dev, &dev_attr_probe_port);
3493 device_remove_file(&dev->dev, &dev_attr_remove_port);
3496 static int __devinit ehea_probe_adapter(struct platform_device *dev,
3497 const struct of_device_id *id)
3499 struct ehea_adapter *adapter;
3500 const u64 *adapter_handle;
3501 int ret;
3503 if (!dev || !dev->dev.of_node) {
3504 pr_err("Invalid ibmebus device probed\n");
3505 return -EINVAL;
3508 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3509 if (!adapter) {
3510 ret = -ENOMEM;
3511 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3512 goto out;
3515 list_add(&adapter->list, &adapter_list);
3517 adapter->ofdev = dev;
3519 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3520 NULL);
3521 if (adapter_handle)
3522 adapter->handle = *adapter_handle;
3524 if (!adapter->handle) {
3525 dev_err(&dev->dev, "failed getting handle for adapter"
3526 " '%s'\n", dev->dev.of_node->full_name);
3527 ret = -ENODEV;
3528 goto out_free_ad;
3531 adapter->pd = EHEA_PD_ID;
3533 dev_set_drvdata(&dev->dev, adapter);
3536 /* initialize adapter and ports */
3537 /* get adapter properties */
3538 ret = ehea_sense_adapter_attr(adapter);
3539 if (ret) {
3540 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3541 goto out_free_ad;
3544 adapter->neq = ehea_create_eq(adapter,
3545 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3546 if (!adapter->neq) {
3547 ret = -EIO;
3548 dev_err(&dev->dev, "NEQ creation failed\n");
3549 goto out_free_ad;
3552 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3553 (unsigned long)adapter);
3555 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3556 ehea_interrupt_neq, IRQF_DISABLED,
3557 "ehea_neq", adapter);
3558 if (ret) {
3559 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3560 goto out_kill_eq;
3563 ret = ehea_create_device_sysfs(dev);
3564 if (ret)
3565 goto out_free_irq;
3567 ret = ehea_setup_ports(adapter);
3568 if (ret) {
3569 dev_err(&dev->dev, "setup_ports failed\n");
3570 goto out_rem_dev_sysfs;
3573 ret = 0;
3574 goto out;
3576 out_rem_dev_sysfs:
3577 ehea_remove_device_sysfs(dev);
3579 out_free_irq:
3580 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3582 out_kill_eq:
3583 ehea_destroy_eq(adapter->neq);
3585 out_free_ad:
3586 list_del(&adapter->list);
3587 kfree(adapter);
3589 out:
3590 ehea_update_firmware_handles();
3592 return ret;
3595 static int __devexit ehea_remove(struct platform_device *dev)
3597 struct ehea_adapter *adapter = dev_get_drvdata(&dev->dev);
3598 int i;
3600 for (i = 0; i < EHEA_MAX_PORTS; i++)
3601 if (adapter->port[i]) {
3602 ehea_shutdown_single_port(adapter->port[i]);
3603 adapter->port[i] = NULL;
3606 ehea_remove_device_sysfs(dev);
3608 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3609 tasklet_kill(&adapter->neq_tasklet);
3611 ehea_destroy_eq(adapter->neq);
3612 ehea_remove_adapter_mr(adapter);
3613 list_del(&adapter->list);
3614 kfree(adapter);
3616 ehea_update_firmware_handles();
3618 return 0;
3621 void ehea_crash_handler(void)
3623 int i;
3625 if (ehea_fw_handles.arr)
3626 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3627 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3628 ehea_fw_handles.arr[i].fwh,
3629 FORCE_FREE);
3631 if (ehea_bcmc_regs.arr)
3632 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3633 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3634 ehea_bcmc_regs.arr[i].port_id,
3635 ehea_bcmc_regs.arr[i].reg_type,
3636 ehea_bcmc_regs.arr[i].macaddr,
3637 0, H_DEREG_BCMC);
3640 static int ehea_mem_notifier(struct notifier_block *nb,
3641 unsigned long action, void *data)
3643 int ret = NOTIFY_BAD;
3644 struct memory_notify *arg = data;
3646 mutex_lock(&dlpar_mem_lock);
3648 switch (action) {
3649 case MEM_CANCEL_OFFLINE:
3650 pr_info("memory offlining canceled");
3651 /* Readd canceled memory block */
3652 case MEM_ONLINE:
3653 pr_info("memory is going online");
3654 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3655 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3656 goto out_unlock;
3657 ehea_rereg_mrs();
3658 break;
3659 case MEM_GOING_OFFLINE:
3660 pr_info("memory is going offline");
3661 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3662 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3663 goto out_unlock;
3664 ehea_rereg_mrs();
3665 break;
3666 default:
3667 break;
3670 ehea_update_firmware_handles();
3671 ret = NOTIFY_OK;
3673 out_unlock:
3674 mutex_unlock(&dlpar_mem_lock);
3675 return ret;
3678 static struct notifier_block ehea_mem_nb = {
3679 .notifier_call = ehea_mem_notifier,
3682 static int ehea_reboot_notifier(struct notifier_block *nb,
3683 unsigned long action, void *unused)
3685 if (action == SYS_RESTART) {
3686 pr_info("Reboot: freeing all eHEA resources\n");
3687 ibmebus_unregister_driver(&ehea_driver);
3689 return NOTIFY_DONE;
3692 static struct notifier_block ehea_reboot_nb = {
3693 .notifier_call = ehea_reboot_notifier,
3696 static int check_module_parm(void)
3698 int ret = 0;
3700 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3701 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3702 pr_info("Bad parameter: rq1_entries\n");
3703 ret = -EINVAL;
3705 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3706 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3707 pr_info("Bad parameter: rq2_entries\n");
3708 ret = -EINVAL;
3710 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3711 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3712 pr_info("Bad parameter: rq3_entries\n");
3713 ret = -EINVAL;
3715 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3716 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3717 pr_info("Bad parameter: sq_entries\n");
3718 ret = -EINVAL;
3721 return ret;
3724 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3725 char *buf)
3727 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3730 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3731 ehea_show_capabilities, NULL);
3733 int __init ehea_module_init(void)
3735 int ret;
3737 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3739 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3740 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3742 mutex_init(&ehea_fw_handles.lock);
3743 spin_lock_init(&ehea_bcmc_regs.lock);
3745 ret = check_module_parm();
3746 if (ret)
3747 goto out;
3749 ret = ehea_create_busmap();
3750 if (ret)
3751 goto out;
3753 ret = register_reboot_notifier(&ehea_reboot_nb);
3754 if (ret)
3755 pr_info("failed registering reboot notifier\n");
3757 ret = register_memory_notifier(&ehea_mem_nb);
3758 if (ret)
3759 pr_info("failed registering memory remove notifier\n");
3761 ret = crash_shutdown_register(ehea_crash_handler);
3762 if (ret)
3763 pr_info("failed registering crash handler\n");
3765 ret = ibmebus_register_driver(&ehea_driver);
3766 if (ret) {
3767 pr_err("failed registering eHEA device driver on ebus\n");
3768 goto out2;
3771 ret = driver_create_file(&ehea_driver.driver,
3772 &driver_attr_capabilities);
3773 if (ret) {
3774 pr_err("failed to register capabilities attribute, ret=%d\n",
3775 ret);
3776 goto out3;
3779 return ret;
3781 out3:
3782 ibmebus_unregister_driver(&ehea_driver);
3783 out2:
3784 unregister_memory_notifier(&ehea_mem_nb);
3785 unregister_reboot_notifier(&ehea_reboot_nb);
3786 crash_shutdown_unregister(ehea_crash_handler);
3787 out:
3788 return ret;
3791 static void __exit ehea_module_exit(void)
3793 int ret;
3795 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3796 ibmebus_unregister_driver(&ehea_driver);
3797 unregister_reboot_notifier(&ehea_reboot_nb);
3798 ret = crash_shutdown_unregister(ehea_crash_handler);
3799 if (ret)
3800 pr_info("failed unregistering crash handler\n");
3801 unregister_memory_notifier(&ehea_mem_nb);
3802 kfree(ehea_fw_handles.arr);
3803 kfree(ehea_bcmc_regs.arr);
3804 ehea_destroy_busmap();
3807 module_init(ehea_module_init);
3808 module_exit(ehea_module_exit);