ehea: Remove num_tx_qps module option
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / ethernet / ibm / ehea / ehea_main.c
blob6ded42a965ac3ec9d2006b1551dc3dd51202a5aa
1 /*
2 * linux/drivers/net/ehea/ehea_main.c
4 * eHEA ethernet device driver for IBM eServer System p
6 * (C) Copyright IBM Corp. 2006
8 * Authors:
9 * Christoph Raisch <raisch@de.ibm.com>
10 * Jan-Bernd Themann <themann@de.ibm.com>
11 * Thomas Klein <tklein@de.ibm.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/in.h>
32 #include <linux/ip.h>
33 #include <linux/tcp.h>
34 #include <linux/udp.h>
35 #include <linux/if.h>
36 #include <linux/list.h>
37 #include <linux/slab.h>
38 #include <linux/if_ether.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/memory.h>
42 #include <asm/kexec.h>
43 #include <linux/mutex.h>
44 #include <linux/prefetch.h>
46 #include <net/ip.h>
48 #include "ehea.h"
49 #include "ehea_qmr.h"
50 #include "ehea_phyp.h"
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
55 MODULE_DESCRIPTION("IBM eServer HEA Driver");
56 MODULE_VERSION(DRV_VERSION);
59 static int msg_level = -1;
60 static int rq1_entries = EHEA_DEF_ENTRIES_RQ1;
61 static int rq2_entries = EHEA_DEF_ENTRIES_RQ2;
62 static int rq3_entries = EHEA_DEF_ENTRIES_RQ3;
63 static int sq_entries = EHEA_DEF_ENTRIES_SQ;
64 static int use_mcs = 1;
65 static int use_lro;
66 static int lro_max_aggr = EHEA_LRO_MAX_AGGR;
67 static int prop_carrier_state;
69 module_param(msg_level, int, 0);
70 module_param(rq1_entries, int, 0);
71 module_param(rq2_entries, int, 0);
72 module_param(rq3_entries, int, 0);
73 module_param(sq_entries, int, 0);
74 module_param(prop_carrier_state, int, 0);
75 module_param(use_mcs, int, 0);
76 module_param(use_lro, int, 0);
77 module_param(lro_max_aggr, int, 0);
79 MODULE_PARM_DESC(msg_level, "msg_level");
80 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
81 "port to stack. 1:yes, 0:no. Default = 0 ");
82 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
83 "[2^x - 1], x = [6..14]. Default = "
84 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
85 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
86 "[2^x - 1], x = [6..14]. Default = "
87 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
88 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
89 "[2^x - 1], x = [6..14]. Default = "
90 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
91 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue "
92 "[2^x - 1], x = [6..14]. Default = "
93 __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
94 MODULE_PARM_DESC(use_mcs, " Multiple receive queues, 1: enable, 0: disable, "
95 "Default = 1");
97 MODULE_PARM_DESC(lro_max_aggr, " LRO: Max packets to be aggregated. Default = "
98 __MODULE_STRING(EHEA_LRO_MAX_AGGR));
99 MODULE_PARM_DESC(use_lro, " Large Receive Offload, 1: enable, 0: disable, "
100 "Default = 0");
102 static int port_name_cnt;
103 static LIST_HEAD(adapter_list);
104 static unsigned long ehea_driver_flags;
105 static DEFINE_MUTEX(dlpar_mem_lock);
106 struct ehea_fw_handle_array ehea_fw_handles;
107 struct ehea_bcmc_reg_array ehea_bcmc_regs;
110 static int __devinit ehea_probe_adapter(struct platform_device *dev,
111 const struct of_device_id *id);
113 static int __devexit ehea_remove(struct platform_device *dev);
115 static struct of_device_id ehea_device_table[] = {
117 .name = "lhea",
118 .compatible = "IBM,lhea",
122 MODULE_DEVICE_TABLE(of, ehea_device_table);
124 static struct of_platform_driver ehea_driver = {
125 .driver = {
126 .name = "ehea",
127 .owner = THIS_MODULE,
128 .of_match_table = ehea_device_table,
130 .probe = ehea_probe_adapter,
131 .remove = ehea_remove,
134 void ehea_dump(void *adr, int len, char *msg)
136 int x;
137 unsigned char *deb = adr;
138 for (x = 0; x < len; x += 16) {
139 pr_info("%s adr=%p ofs=%04x %016llx %016llx\n",
140 msg, deb, x, *((u64 *)&deb[0]), *((u64 *)&deb[8]));
141 deb += 16;
145 void ehea_schedule_port_reset(struct ehea_port *port)
147 if (!test_bit(__EHEA_DISABLE_PORT_RESET, &port->flags))
148 schedule_work(&port->reset_task);
151 static void ehea_update_firmware_handles(void)
153 struct ehea_fw_handle_entry *arr = NULL;
154 struct ehea_adapter *adapter;
155 int num_adapters = 0;
156 int num_ports = 0;
157 int num_portres = 0;
158 int i = 0;
159 int num_fw_handles, k, l;
161 /* Determine number of handles */
162 mutex_lock(&ehea_fw_handles.lock);
164 list_for_each_entry(adapter, &adapter_list, list) {
165 num_adapters++;
167 for (k = 0; k < EHEA_MAX_PORTS; k++) {
168 struct ehea_port *port = adapter->port[k];
170 if (!port || (port->state != EHEA_PORT_UP))
171 continue;
173 num_ports++;
174 num_portres += port->num_def_qps;
178 num_fw_handles = num_adapters * EHEA_NUM_ADAPTER_FW_HANDLES +
179 num_ports * EHEA_NUM_PORT_FW_HANDLES +
180 num_portres * EHEA_NUM_PORTRES_FW_HANDLES;
182 if (num_fw_handles) {
183 arr = kcalloc(num_fw_handles, sizeof(*arr), GFP_KERNEL);
184 if (!arr)
185 goto out; /* Keep the existing array */
186 } else
187 goto out_update;
189 list_for_each_entry(adapter, &adapter_list, list) {
190 if (num_adapters == 0)
191 break;
193 for (k = 0; k < EHEA_MAX_PORTS; k++) {
194 struct ehea_port *port = adapter->port[k];
196 if (!port || (port->state != EHEA_PORT_UP) ||
197 (num_ports == 0))
198 continue;
200 for (l = 0; l < port->num_def_qps; l++) {
201 struct ehea_port_res *pr = &port->port_res[l];
203 arr[i].adh = adapter->handle;
204 arr[i++].fwh = pr->qp->fw_handle;
205 arr[i].adh = adapter->handle;
206 arr[i++].fwh = pr->send_cq->fw_handle;
207 arr[i].adh = adapter->handle;
208 arr[i++].fwh = pr->recv_cq->fw_handle;
209 arr[i].adh = adapter->handle;
210 arr[i++].fwh = pr->eq->fw_handle;
211 arr[i].adh = adapter->handle;
212 arr[i++].fwh = pr->send_mr.handle;
213 arr[i].adh = adapter->handle;
214 arr[i++].fwh = pr->recv_mr.handle;
216 arr[i].adh = adapter->handle;
217 arr[i++].fwh = port->qp_eq->fw_handle;
218 num_ports--;
221 arr[i].adh = adapter->handle;
222 arr[i++].fwh = adapter->neq->fw_handle;
224 if (adapter->mr.handle) {
225 arr[i].adh = adapter->handle;
226 arr[i++].fwh = adapter->mr.handle;
228 num_adapters--;
231 out_update:
232 kfree(ehea_fw_handles.arr);
233 ehea_fw_handles.arr = arr;
234 ehea_fw_handles.num_entries = i;
235 out:
236 mutex_unlock(&ehea_fw_handles.lock);
239 static void ehea_update_bcmc_registrations(void)
241 unsigned long flags;
242 struct ehea_bcmc_reg_entry *arr = NULL;
243 struct ehea_adapter *adapter;
244 struct ehea_mc_list *mc_entry;
245 int num_registrations = 0;
246 int i = 0;
247 int k;
249 spin_lock_irqsave(&ehea_bcmc_regs.lock, flags);
251 /* Determine number of registrations */
252 list_for_each_entry(adapter, &adapter_list, list)
253 for (k = 0; k < EHEA_MAX_PORTS; k++) {
254 struct ehea_port *port = adapter->port[k];
256 if (!port || (port->state != EHEA_PORT_UP))
257 continue;
259 num_registrations += 2; /* Broadcast registrations */
261 list_for_each_entry(mc_entry, &port->mc_list->list,list)
262 num_registrations += 2;
265 if (num_registrations) {
266 arr = kcalloc(num_registrations, sizeof(*arr), GFP_ATOMIC);
267 if (!arr)
268 goto out; /* Keep the existing array */
269 } else
270 goto out_update;
272 list_for_each_entry(adapter, &adapter_list, list) {
273 for (k = 0; k < EHEA_MAX_PORTS; k++) {
274 struct ehea_port *port = adapter->port[k];
276 if (!port || (port->state != EHEA_PORT_UP))
277 continue;
279 if (num_registrations == 0)
280 goto out_update;
282 arr[i].adh = adapter->handle;
283 arr[i].port_id = port->logical_port_id;
284 arr[i].reg_type = EHEA_BCMC_BROADCAST |
285 EHEA_BCMC_UNTAGGED;
286 arr[i++].macaddr = port->mac_addr;
288 arr[i].adh = adapter->handle;
289 arr[i].port_id = port->logical_port_id;
290 arr[i].reg_type = EHEA_BCMC_BROADCAST |
291 EHEA_BCMC_VLANID_ALL;
292 arr[i++].macaddr = port->mac_addr;
293 num_registrations -= 2;
295 list_for_each_entry(mc_entry,
296 &port->mc_list->list, list) {
297 if (num_registrations == 0)
298 goto out_update;
300 arr[i].adh = adapter->handle;
301 arr[i].port_id = port->logical_port_id;
302 arr[i].reg_type = EHEA_BCMC_SCOPE_ALL |
303 EHEA_BCMC_MULTICAST |
304 EHEA_BCMC_UNTAGGED;
305 arr[i++].macaddr = mc_entry->macaddr;
307 arr[i].adh = adapter->handle;
308 arr[i].port_id = port->logical_port_id;
309 arr[i].reg_type = EHEA_BCMC_SCOPE_ALL |
310 EHEA_BCMC_MULTICAST |
311 EHEA_BCMC_VLANID_ALL;
312 arr[i++].macaddr = mc_entry->macaddr;
313 num_registrations -= 2;
318 out_update:
319 kfree(ehea_bcmc_regs.arr);
320 ehea_bcmc_regs.arr = arr;
321 ehea_bcmc_regs.num_entries = i;
322 out:
323 spin_unlock_irqrestore(&ehea_bcmc_regs.lock, flags);
326 static struct net_device_stats *ehea_get_stats(struct net_device *dev)
328 struct ehea_port *port = netdev_priv(dev);
329 struct net_device_stats *stats = &port->stats;
330 u64 rx_packets = 0, tx_packets = 0, rx_bytes = 0, tx_bytes = 0;
331 int i;
333 for (i = 0; i < port->num_def_qps; i++) {
334 rx_packets += port->port_res[i].rx_packets;
335 rx_bytes += port->port_res[i].rx_bytes;
338 for (i = 0; i < port->num_def_qps; i++) {
339 tx_packets += port->port_res[i].tx_packets;
340 tx_bytes += port->port_res[i].tx_bytes;
343 stats->tx_packets = tx_packets;
344 stats->rx_bytes = rx_bytes;
345 stats->tx_bytes = tx_bytes;
346 stats->rx_packets = rx_packets;
348 return &port->stats;
351 static void ehea_update_stats(struct work_struct *work)
353 struct ehea_port *port =
354 container_of(work, struct ehea_port, stats_work.work);
355 struct net_device *dev = port->netdev;
356 struct net_device_stats *stats = &port->stats;
357 struct hcp_ehea_port_cb2 *cb2;
358 u64 hret;
360 cb2 = (void *)get_zeroed_page(GFP_KERNEL);
361 if (!cb2) {
362 netdev_err(dev, "No mem for cb2. Some interface statistics were not updated\n");
363 goto resched;
366 hret = ehea_h_query_ehea_port(port->adapter->handle,
367 port->logical_port_id,
368 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
369 if (hret != H_SUCCESS) {
370 netdev_err(dev, "query_ehea_port failed\n");
371 goto out_herr;
374 if (netif_msg_hw(port))
375 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
377 stats->multicast = cb2->rxmcp;
378 stats->rx_errors = cb2->rxuerr;
380 out_herr:
381 free_page((unsigned long)cb2);
382 resched:
383 schedule_delayed_work(&port->stats_work, msecs_to_jiffies(1000));
386 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
388 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
389 struct net_device *dev = pr->port->netdev;
390 int max_index_mask = pr->rq1_skba.len - 1;
391 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
392 int adder = 0;
393 int i;
395 pr->rq1_skba.os_skbs = 0;
397 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
398 if (nr_of_wqes > 0)
399 pr->rq1_skba.index = index;
400 pr->rq1_skba.os_skbs = fill_wqes;
401 return;
404 for (i = 0; i < fill_wqes; i++) {
405 if (!skb_arr_rq1[index]) {
406 skb_arr_rq1[index] = netdev_alloc_skb(dev,
407 EHEA_L_PKT_SIZE);
408 if (!skb_arr_rq1[index]) {
409 netdev_info(dev, "Unable to allocate enough skb in the array\n");
410 pr->rq1_skba.os_skbs = fill_wqes - i;
411 break;
414 index--;
415 index &= max_index_mask;
416 adder++;
419 if (adder == 0)
420 return;
422 /* Ring doorbell */
423 ehea_update_rq1a(pr->qp, adder);
426 static void ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
428 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
429 struct net_device *dev = pr->port->netdev;
430 int i;
432 if (nr_rq1a > pr->rq1_skba.len) {
433 netdev_err(dev, "NR_RQ1A bigger than skb array len\n");
434 return;
437 for (i = 0; i < nr_rq1a; i++) {
438 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
439 if (!skb_arr_rq1[i]) {
440 netdev_info(dev, "Not enough memory to allocate skb array\n");
441 break;
444 /* Ring doorbell */
445 ehea_update_rq1a(pr->qp, i - 1);
448 static int ehea_refill_rq_def(struct ehea_port_res *pr,
449 struct ehea_q_skb_arr *q_skba, int rq_nr,
450 int num_wqes, int wqe_type, int packet_size)
452 struct net_device *dev = pr->port->netdev;
453 struct ehea_qp *qp = pr->qp;
454 struct sk_buff **skb_arr = q_skba->arr;
455 struct ehea_rwqe *rwqe;
456 int i, index, max_index_mask, fill_wqes;
457 int adder = 0;
458 int ret = 0;
460 fill_wqes = q_skba->os_skbs + num_wqes;
461 q_skba->os_skbs = 0;
463 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
464 q_skba->os_skbs = fill_wqes;
465 return ret;
468 index = q_skba->index;
469 max_index_mask = q_skba->len - 1;
470 for (i = 0; i < fill_wqes; i++) {
471 u64 tmp_addr;
472 struct sk_buff *skb;
474 skb = netdev_alloc_skb_ip_align(dev, packet_size);
475 if (!skb) {
476 q_skba->os_skbs = fill_wqes - i;
477 if (q_skba->os_skbs == q_skba->len - 2) {
478 netdev_info(pr->port->netdev,
479 "rq%i ran dry - no mem for skb\n",
480 rq_nr);
481 ret = -ENOMEM;
483 break;
486 skb_arr[index] = skb;
487 tmp_addr = ehea_map_vaddr(skb->data);
488 if (tmp_addr == -1) {
489 dev_kfree_skb(skb);
490 q_skba->os_skbs = fill_wqes - i;
491 ret = 0;
492 break;
495 rwqe = ehea_get_next_rwqe(qp, rq_nr);
496 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
497 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
498 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
499 rwqe->sg_list[0].vaddr = tmp_addr;
500 rwqe->sg_list[0].len = packet_size;
501 rwqe->data_segments = 1;
503 index++;
504 index &= max_index_mask;
505 adder++;
508 q_skba->index = index;
509 if (adder == 0)
510 goto out;
512 /* Ring doorbell */
513 iosync();
514 if (rq_nr == 2)
515 ehea_update_rq2a(pr->qp, adder);
516 else
517 ehea_update_rq3a(pr->qp, adder);
518 out:
519 return ret;
523 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
525 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
526 nr_of_wqes, EHEA_RWQE2_TYPE,
527 EHEA_RQ2_PKT_SIZE);
531 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
533 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
534 nr_of_wqes, EHEA_RWQE3_TYPE,
535 EHEA_MAX_PACKET_SIZE);
538 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
540 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
541 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
542 return 0;
543 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
544 (cqe->header_length == 0))
545 return 0;
546 return -EINVAL;
549 static inline void ehea_fill_skb(struct net_device *dev,
550 struct sk_buff *skb, struct ehea_cqe *cqe,
551 struct ehea_port_res *pr)
553 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
555 skb_put(skb, length);
556 skb->protocol = eth_type_trans(skb, dev);
558 /* The packet was not an IPV4 packet so a complemented checksum was
559 calculated. The value is found in the Internet Checksum field. */
560 if (cqe->status & EHEA_CQE_BLIND_CKSUM) {
561 skb->ip_summed = CHECKSUM_COMPLETE;
562 skb->csum = csum_unfold(~cqe->inet_checksum_value);
563 } else
564 skb->ip_summed = CHECKSUM_UNNECESSARY;
566 skb_record_rx_queue(skb, pr - &pr->port->port_res[0]);
569 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
570 int arr_len,
571 struct ehea_cqe *cqe)
573 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
574 struct sk_buff *skb;
575 void *pref;
576 int x;
578 x = skb_index + 1;
579 x &= (arr_len - 1);
581 pref = skb_array[x];
582 if (pref) {
583 prefetchw(pref);
584 prefetchw(pref + EHEA_CACHE_LINE);
586 pref = (skb_array[x]->data);
587 prefetch(pref);
588 prefetch(pref + EHEA_CACHE_LINE);
589 prefetch(pref + EHEA_CACHE_LINE * 2);
590 prefetch(pref + EHEA_CACHE_LINE * 3);
593 skb = skb_array[skb_index];
594 skb_array[skb_index] = NULL;
595 return skb;
598 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
599 int arr_len, int wqe_index)
601 struct sk_buff *skb;
602 void *pref;
603 int x;
605 x = wqe_index + 1;
606 x &= (arr_len - 1);
608 pref = skb_array[x];
609 if (pref) {
610 prefetchw(pref);
611 prefetchw(pref + EHEA_CACHE_LINE);
613 pref = (skb_array[x]->data);
614 prefetchw(pref);
615 prefetchw(pref + EHEA_CACHE_LINE);
618 skb = skb_array[wqe_index];
619 skb_array[wqe_index] = NULL;
620 return skb;
623 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
624 struct ehea_cqe *cqe, int *processed_rq2,
625 int *processed_rq3)
627 struct sk_buff *skb;
629 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
630 pr->p_stats.err_tcp_cksum++;
631 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
632 pr->p_stats.err_ip_cksum++;
633 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
634 pr->p_stats.err_frame_crc++;
636 if (rq == 2) {
637 *processed_rq2 += 1;
638 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
639 dev_kfree_skb(skb);
640 } else if (rq == 3) {
641 *processed_rq3 += 1;
642 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
643 dev_kfree_skb(skb);
646 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
647 if (netif_msg_rx_err(pr->port)) {
648 pr_err("Critical receive error for QP %d. Resetting port.\n",
649 pr->qp->init_attr.qp_nr);
650 ehea_dump(cqe, sizeof(*cqe), "CQE");
652 ehea_schedule_port_reset(pr->port);
653 return 1;
656 return 0;
659 static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
660 void **tcph, u64 *hdr_flags, void *priv)
662 struct ehea_cqe *cqe = priv;
663 unsigned int ip_len;
664 struct iphdr *iph;
666 /* non tcp/udp packets */
667 if (!cqe->header_length)
668 return -1;
670 /* non tcp packet */
671 skb_reset_network_header(skb);
672 iph = ip_hdr(skb);
673 if (iph->protocol != IPPROTO_TCP)
674 return -1;
676 ip_len = ip_hdrlen(skb);
677 skb_set_transport_header(skb, ip_len);
678 *tcph = tcp_hdr(skb);
680 /* check if ip header and tcp header are complete */
681 if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
682 return -1;
684 *hdr_flags = LRO_IPV4 | LRO_TCP;
685 *iphdr = iph;
687 return 0;
690 static void ehea_proc_skb(struct ehea_port_res *pr, struct ehea_cqe *cqe,
691 struct sk_buff *skb)
693 if (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
694 __vlan_hwaccel_put_tag(skb, cqe->vlan_tag);
696 if (skb->dev->features & NETIF_F_LRO)
697 lro_receive_skb(&pr->lro_mgr, skb, cqe);
698 else
699 netif_receive_skb(skb);
702 static int ehea_proc_rwqes(struct net_device *dev,
703 struct ehea_port_res *pr,
704 int budget)
706 struct ehea_port *port = pr->port;
707 struct ehea_qp *qp = pr->qp;
708 struct ehea_cqe *cqe;
709 struct sk_buff *skb;
710 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
711 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
712 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
713 int skb_arr_rq1_len = pr->rq1_skba.len;
714 int skb_arr_rq2_len = pr->rq2_skba.len;
715 int skb_arr_rq3_len = pr->rq3_skba.len;
716 int processed, processed_rq1, processed_rq2, processed_rq3;
717 u64 processed_bytes = 0;
718 int wqe_index, last_wqe_index, rq, port_reset;
720 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
721 last_wqe_index = 0;
723 cqe = ehea_poll_rq1(qp, &wqe_index);
724 while ((processed < budget) && cqe) {
725 ehea_inc_rq1(qp);
726 processed_rq1++;
727 processed++;
728 if (netif_msg_rx_status(port))
729 ehea_dump(cqe, sizeof(*cqe), "CQE");
731 last_wqe_index = wqe_index;
732 rmb();
733 if (!ehea_check_cqe(cqe, &rq)) {
734 if (rq == 1) {
735 /* LL RQ1 */
736 skb = get_skb_by_index_ll(skb_arr_rq1,
737 skb_arr_rq1_len,
738 wqe_index);
739 if (unlikely(!skb)) {
740 netif_info(port, rx_err, dev,
741 "LL rq1: skb=NULL\n");
743 skb = netdev_alloc_skb(dev,
744 EHEA_L_PKT_SIZE);
745 if (!skb) {
746 netdev_err(dev, "Not enough memory to allocate skb\n");
747 break;
750 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
751 cqe->num_bytes_transfered - 4);
752 ehea_fill_skb(dev, skb, cqe, pr);
753 } else if (rq == 2) {
754 /* RQ2 */
755 skb = get_skb_by_index(skb_arr_rq2,
756 skb_arr_rq2_len, cqe);
757 if (unlikely(!skb)) {
758 netif_err(port, rx_err, dev,
759 "rq2: skb=NULL\n");
760 break;
762 ehea_fill_skb(dev, skb, cqe, pr);
763 processed_rq2++;
764 } else {
765 /* RQ3 */
766 skb = get_skb_by_index(skb_arr_rq3,
767 skb_arr_rq3_len, cqe);
768 if (unlikely(!skb)) {
769 netif_err(port, rx_err, dev,
770 "rq3: skb=NULL\n");
771 break;
773 ehea_fill_skb(dev, skb, cqe, pr);
774 processed_rq3++;
777 processed_bytes += skb->len;
778 ehea_proc_skb(pr, cqe, skb);
779 } else {
780 pr->p_stats.poll_receive_errors++;
781 port_reset = ehea_treat_poll_error(pr, rq, cqe,
782 &processed_rq2,
783 &processed_rq3);
784 if (port_reset)
785 break;
787 cqe = ehea_poll_rq1(qp, &wqe_index);
789 if (dev->features & NETIF_F_LRO)
790 lro_flush_all(&pr->lro_mgr);
792 pr->rx_packets += processed;
793 pr->rx_bytes += processed_bytes;
795 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
796 ehea_refill_rq2(pr, processed_rq2);
797 ehea_refill_rq3(pr, processed_rq3);
799 return processed;
802 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
804 static void reset_sq_restart_flag(struct ehea_port *port)
806 int i;
808 for (i = 0; i < port->num_def_qps; i++) {
809 struct ehea_port_res *pr = &port->port_res[i];
810 pr->sq_restart_flag = 0;
812 wake_up(&port->restart_wq);
815 static void check_sqs(struct ehea_port *port)
817 struct ehea_swqe *swqe;
818 int swqe_index;
819 int i, k;
821 for (i = 0; i < port->num_def_qps; i++) {
822 struct ehea_port_res *pr = &port->port_res[i];
823 int ret;
824 k = 0;
825 swqe = ehea_get_swqe(pr->qp, &swqe_index);
826 memset(swqe, 0, SWQE_HEADER_SIZE);
827 atomic_dec(&pr->swqe_avail);
829 swqe->tx_control |= EHEA_SWQE_PURGE;
830 swqe->wr_id = SWQE_RESTART_CHECK;
831 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
832 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
833 swqe->immediate_data_length = 80;
835 ehea_post_swqe(pr->qp, swqe);
837 ret = wait_event_timeout(port->restart_wq,
838 pr->sq_restart_flag == 0,
839 msecs_to_jiffies(100));
841 if (!ret) {
842 pr_err("HW/SW queues out of sync\n");
843 ehea_schedule_port_reset(pr->port);
844 return;
850 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
852 struct sk_buff *skb;
853 struct ehea_cq *send_cq = pr->send_cq;
854 struct ehea_cqe *cqe;
855 int quota = my_quota;
856 int cqe_counter = 0;
857 int swqe_av = 0;
858 int index;
859 struct netdev_queue *txq = netdev_get_tx_queue(pr->port->netdev,
860 pr - &pr->port->port_res[0]);
862 cqe = ehea_poll_cq(send_cq);
863 while (cqe && (quota > 0)) {
864 ehea_inc_cq(send_cq);
866 cqe_counter++;
867 rmb();
869 if (cqe->wr_id == SWQE_RESTART_CHECK) {
870 pr->sq_restart_flag = 1;
871 swqe_av++;
872 break;
875 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
876 pr_err("Bad send completion status=0x%04X\n",
877 cqe->status);
879 if (netif_msg_tx_err(pr->port))
880 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
882 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
883 pr_err("Resetting port\n");
884 ehea_schedule_port_reset(pr->port);
885 break;
889 if (netif_msg_tx_done(pr->port))
890 ehea_dump(cqe, sizeof(*cqe), "CQE");
892 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
893 == EHEA_SWQE2_TYPE)) {
895 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
896 skb = pr->sq_skba.arr[index];
897 dev_kfree_skb(skb);
898 pr->sq_skba.arr[index] = NULL;
901 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
902 quota--;
904 cqe = ehea_poll_cq(send_cq);
907 ehea_update_feca(send_cq, cqe_counter);
908 atomic_add(swqe_av, &pr->swqe_avail);
910 if (unlikely(netif_tx_queue_stopped(txq) &&
911 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))) {
912 __netif_tx_lock(txq, smp_processor_id());
913 if (netif_tx_queue_stopped(txq) &&
914 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))
915 netif_tx_wake_queue(txq);
916 __netif_tx_unlock(txq);
919 wake_up(&pr->port->swqe_avail_wq);
921 return cqe;
924 #define EHEA_POLL_MAX_CQES 65535
926 static int ehea_poll(struct napi_struct *napi, int budget)
928 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
929 napi);
930 struct net_device *dev = pr->port->netdev;
931 struct ehea_cqe *cqe;
932 struct ehea_cqe *cqe_skb = NULL;
933 int wqe_index;
934 int rx = 0;
936 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
937 rx += ehea_proc_rwqes(dev, pr, budget - rx);
939 while (rx != budget) {
940 napi_complete(napi);
941 ehea_reset_cq_ep(pr->recv_cq);
942 ehea_reset_cq_ep(pr->send_cq);
943 ehea_reset_cq_n1(pr->recv_cq);
944 ehea_reset_cq_n1(pr->send_cq);
945 rmb();
946 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
947 cqe_skb = ehea_poll_cq(pr->send_cq);
949 if (!cqe && !cqe_skb)
950 return rx;
952 if (!napi_reschedule(napi))
953 return rx;
955 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
956 rx += ehea_proc_rwqes(dev, pr, budget - rx);
959 return rx;
962 #ifdef CONFIG_NET_POLL_CONTROLLER
963 static void ehea_netpoll(struct net_device *dev)
965 struct ehea_port *port = netdev_priv(dev);
966 int i;
968 for (i = 0; i < port->num_def_qps; i++)
969 napi_schedule(&port->port_res[i].napi);
971 #endif
973 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
975 struct ehea_port_res *pr = param;
977 napi_schedule(&pr->napi);
979 return IRQ_HANDLED;
982 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
984 struct ehea_port *port = param;
985 struct ehea_eqe *eqe;
986 struct ehea_qp *qp;
987 u32 qp_token;
988 u64 resource_type, aer, aerr;
989 int reset_port = 0;
991 eqe = ehea_poll_eq(port->qp_eq);
993 while (eqe) {
994 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
995 pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
996 eqe->entry, qp_token);
998 qp = port->port_res[qp_token].qp;
1000 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
1001 &aer, &aerr);
1003 if (resource_type == EHEA_AER_RESTYPE_QP) {
1004 if ((aer & EHEA_AER_RESET_MASK) ||
1005 (aerr & EHEA_AERR_RESET_MASK))
1006 reset_port = 1;
1007 } else
1008 reset_port = 1; /* Reset in case of CQ or EQ error */
1010 eqe = ehea_poll_eq(port->qp_eq);
1013 if (reset_port) {
1014 pr_err("Resetting port\n");
1015 ehea_schedule_port_reset(port);
1018 return IRQ_HANDLED;
1021 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
1022 int logical_port)
1024 int i;
1026 for (i = 0; i < EHEA_MAX_PORTS; i++)
1027 if (adapter->port[i])
1028 if (adapter->port[i]->logical_port_id == logical_port)
1029 return adapter->port[i];
1030 return NULL;
1033 int ehea_sense_port_attr(struct ehea_port *port)
1035 int ret;
1036 u64 hret;
1037 struct hcp_ehea_port_cb0 *cb0;
1039 /* may be called via ehea_neq_tasklet() */
1040 cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
1041 if (!cb0) {
1042 pr_err("no mem for cb0\n");
1043 ret = -ENOMEM;
1044 goto out;
1047 hret = ehea_h_query_ehea_port(port->adapter->handle,
1048 port->logical_port_id, H_PORT_CB0,
1049 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
1050 cb0);
1051 if (hret != H_SUCCESS) {
1052 ret = -EIO;
1053 goto out_free;
1056 /* MAC address */
1057 port->mac_addr = cb0->port_mac_addr << 16;
1059 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1060 ret = -EADDRNOTAVAIL;
1061 goto out_free;
1064 /* Port speed */
1065 switch (cb0->port_speed) {
1066 case H_SPEED_10M_H:
1067 port->port_speed = EHEA_SPEED_10M;
1068 port->full_duplex = 0;
1069 break;
1070 case H_SPEED_10M_F:
1071 port->port_speed = EHEA_SPEED_10M;
1072 port->full_duplex = 1;
1073 break;
1074 case H_SPEED_100M_H:
1075 port->port_speed = EHEA_SPEED_100M;
1076 port->full_duplex = 0;
1077 break;
1078 case H_SPEED_100M_F:
1079 port->port_speed = EHEA_SPEED_100M;
1080 port->full_duplex = 1;
1081 break;
1082 case H_SPEED_1G_F:
1083 port->port_speed = EHEA_SPEED_1G;
1084 port->full_duplex = 1;
1085 break;
1086 case H_SPEED_10G_F:
1087 port->port_speed = EHEA_SPEED_10G;
1088 port->full_duplex = 1;
1089 break;
1090 default:
1091 port->port_speed = 0;
1092 port->full_duplex = 0;
1093 break;
1096 port->autoneg = 1;
1097 port->num_mcs = cb0->num_default_qps;
1099 /* Number of default QPs */
1100 if (use_mcs)
1101 port->num_def_qps = cb0->num_default_qps;
1102 else
1103 port->num_def_qps = 1;
1105 if (!port->num_def_qps) {
1106 ret = -EINVAL;
1107 goto out_free;
1110 ret = 0;
1111 out_free:
1112 if (ret || netif_msg_probe(port))
1113 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1114 free_page((unsigned long)cb0);
1115 out:
1116 return ret;
1119 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1121 struct hcp_ehea_port_cb4 *cb4;
1122 u64 hret;
1123 int ret = 0;
1125 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1126 if (!cb4) {
1127 pr_err("no mem for cb4\n");
1128 ret = -ENOMEM;
1129 goto out;
1132 cb4->port_speed = port_speed;
1134 netif_carrier_off(port->netdev);
1136 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1137 port->logical_port_id,
1138 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1139 if (hret == H_SUCCESS) {
1140 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1142 hret = ehea_h_query_ehea_port(port->adapter->handle,
1143 port->logical_port_id,
1144 H_PORT_CB4, H_PORT_CB4_SPEED,
1145 cb4);
1146 if (hret == H_SUCCESS) {
1147 switch (cb4->port_speed) {
1148 case H_SPEED_10M_H:
1149 port->port_speed = EHEA_SPEED_10M;
1150 port->full_duplex = 0;
1151 break;
1152 case H_SPEED_10M_F:
1153 port->port_speed = EHEA_SPEED_10M;
1154 port->full_duplex = 1;
1155 break;
1156 case H_SPEED_100M_H:
1157 port->port_speed = EHEA_SPEED_100M;
1158 port->full_duplex = 0;
1159 break;
1160 case H_SPEED_100M_F:
1161 port->port_speed = EHEA_SPEED_100M;
1162 port->full_duplex = 1;
1163 break;
1164 case H_SPEED_1G_F:
1165 port->port_speed = EHEA_SPEED_1G;
1166 port->full_duplex = 1;
1167 break;
1168 case H_SPEED_10G_F:
1169 port->port_speed = EHEA_SPEED_10G;
1170 port->full_duplex = 1;
1171 break;
1172 default:
1173 port->port_speed = 0;
1174 port->full_duplex = 0;
1175 break;
1177 } else {
1178 pr_err("Failed sensing port speed\n");
1179 ret = -EIO;
1181 } else {
1182 if (hret == H_AUTHORITY) {
1183 pr_info("Hypervisor denied setting port speed\n");
1184 ret = -EPERM;
1185 } else {
1186 ret = -EIO;
1187 pr_err("Failed setting port speed\n");
1190 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1191 netif_carrier_on(port->netdev);
1193 free_page((unsigned long)cb4);
1194 out:
1195 return ret;
1198 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1200 int ret;
1201 u8 ec;
1202 u8 portnum;
1203 struct ehea_port *port;
1204 struct net_device *dev;
1206 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1207 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1208 port = ehea_get_port(adapter, portnum);
1209 dev = port->netdev;
1211 switch (ec) {
1212 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1214 if (!port) {
1215 netdev_err(dev, "unknown portnum %x\n", portnum);
1216 break;
1219 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1220 if (!netif_carrier_ok(dev)) {
1221 ret = ehea_sense_port_attr(port);
1222 if (ret) {
1223 netdev_err(dev, "failed resensing port attributes\n");
1224 break;
1227 netif_info(port, link, dev,
1228 "Logical port up: %dMbps %s Duplex\n",
1229 port->port_speed,
1230 port->full_duplex == 1 ?
1231 "Full" : "Half");
1233 netif_carrier_on(dev);
1234 netif_wake_queue(dev);
1236 } else
1237 if (netif_carrier_ok(dev)) {
1238 netif_info(port, link, dev,
1239 "Logical port down\n");
1240 netif_carrier_off(dev);
1241 netif_tx_disable(dev);
1244 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1245 port->phy_link = EHEA_PHY_LINK_UP;
1246 netif_info(port, link, dev,
1247 "Physical port up\n");
1248 if (prop_carrier_state)
1249 netif_carrier_on(dev);
1250 } else {
1251 port->phy_link = EHEA_PHY_LINK_DOWN;
1252 netif_info(port, link, dev,
1253 "Physical port down\n");
1254 if (prop_carrier_state)
1255 netif_carrier_off(dev);
1258 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1259 netdev_info(dev,
1260 "External switch port is primary port\n");
1261 else
1262 netdev_info(dev,
1263 "External switch port is backup port\n");
1265 break;
1266 case EHEA_EC_ADAPTER_MALFUNC:
1267 netdev_err(dev, "Adapter malfunction\n");
1268 break;
1269 case EHEA_EC_PORT_MALFUNC:
1270 netdev_info(dev, "Port malfunction\n");
1271 netif_carrier_off(dev);
1272 netif_tx_disable(dev);
1273 break;
1274 default:
1275 netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1276 break;
1280 static void ehea_neq_tasklet(unsigned long data)
1282 struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1283 struct ehea_eqe *eqe;
1284 u64 event_mask;
1286 eqe = ehea_poll_eq(adapter->neq);
1287 pr_debug("eqe=%p\n", eqe);
1289 while (eqe) {
1290 pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1291 ehea_parse_eqe(adapter, eqe->entry);
1292 eqe = ehea_poll_eq(adapter->neq);
1293 pr_debug("next eqe=%p\n", eqe);
1296 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1297 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1298 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1300 ehea_h_reset_events(adapter->handle,
1301 adapter->neq->fw_handle, event_mask);
1304 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1306 struct ehea_adapter *adapter = param;
1307 tasklet_hi_schedule(&adapter->neq_tasklet);
1308 return IRQ_HANDLED;
1312 static int ehea_fill_port_res(struct ehea_port_res *pr)
1314 int ret;
1315 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1317 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1319 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1321 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1323 return ret;
1326 static int ehea_reg_interrupts(struct net_device *dev)
1328 struct ehea_port *port = netdev_priv(dev);
1329 struct ehea_port_res *pr;
1330 int i, ret;
1333 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1334 dev->name);
1336 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1337 ehea_qp_aff_irq_handler,
1338 IRQF_DISABLED, port->int_aff_name, port);
1339 if (ret) {
1340 netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1341 port->qp_eq->attr.ist1);
1342 goto out_free_qpeq;
1345 netif_info(port, ifup, dev,
1346 "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1347 port->qp_eq->attr.ist1);
1350 for (i = 0; i < port->num_def_qps; i++) {
1351 pr = &port->port_res[i];
1352 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1353 "%s-queue%d", dev->name, i);
1354 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1355 ehea_recv_irq_handler,
1356 IRQF_DISABLED, pr->int_send_name,
1357 pr);
1358 if (ret) {
1359 netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1360 i, pr->eq->attr.ist1);
1361 goto out_free_req;
1363 netif_info(port, ifup, dev,
1364 "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1365 pr->eq->attr.ist1, i);
1367 out:
1368 return ret;
1371 out_free_req:
1372 while (--i >= 0) {
1373 u32 ist = port->port_res[i].eq->attr.ist1;
1374 ibmebus_free_irq(ist, &port->port_res[i]);
1377 out_free_qpeq:
1378 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1379 i = port->num_def_qps;
1381 goto out;
1385 static void ehea_free_interrupts(struct net_device *dev)
1387 struct ehea_port *port = netdev_priv(dev);
1388 struct ehea_port_res *pr;
1389 int i;
1391 /* send */
1393 for (i = 0; i < port->num_def_qps; i++) {
1394 pr = &port->port_res[i];
1395 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1396 netif_info(port, intr, dev,
1397 "free send irq for res %d with handle 0x%X\n",
1398 i, pr->eq->attr.ist1);
1401 /* associated events */
1402 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1403 netif_info(port, intr, dev,
1404 "associated event interrupt for handle 0x%X freed\n",
1405 port->qp_eq->attr.ist1);
1408 static int ehea_configure_port(struct ehea_port *port)
1410 int ret, i;
1411 u64 hret, mask;
1412 struct hcp_ehea_port_cb0 *cb0;
1414 ret = -ENOMEM;
1415 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1416 if (!cb0)
1417 goto out;
1419 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1420 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1421 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1422 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1423 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1424 PXLY_RC_VLAN_FILTER)
1425 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1427 for (i = 0; i < port->num_mcs; i++)
1428 if (use_mcs)
1429 cb0->default_qpn_arr[i] =
1430 port->port_res[i].qp->init_attr.qp_nr;
1431 else
1432 cb0->default_qpn_arr[i] =
1433 port->port_res[0].qp->init_attr.qp_nr;
1435 if (netif_msg_ifup(port))
1436 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1438 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1439 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1441 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1442 port->logical_port_id,
1443 H_PORT_CB0, mask, cb0);
1444 ret = -EIO;
1445 if (hret != H_SUCCESS)
1446 goto out_free;
1448 ret = 0;
1450 out_free:
1451 free_page((unsigned long)cb0);
1452 out:
1453 return ret;
1456 int ehea_gen_smrs(struct ehea_port_res *pr)
1458 int ret;
1459 struct ehea_adapter *adapter = pr->port->adapter;
1461 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1462 if (ret)
1463 goto out;
1465 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1466 if (ret)
1467 goto out_free;
1469 return 0;
1471 out_free:
1472 ehea_rem_mr(&pr->send_mr);
1473 out:
1474 pr_err("Generating SMRS failed\n");
1475 return -EIO;
1478 int ehea_rem_smrs(struct ehea_port_res *pr)
1480 if ((ehea_rem_mr(&pr->send_mr)) ||
1481 (ehea_rem_mr(&pr->recv_mr)))
1482 return -EIO;
1483 else
1484 return 0;
1487 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1489 int arr_size = sizeof(void *) * max_q_entries;
1491 q_skba->arr = vzalloc(arr_size);
1492 if (!q_skba->arr)
1493 return -ENOMEM;
1495 q_skba->len = max_q_entries;
1496 q_skba->index = 0;
1497 q_skba->os_skbs = 0;
1499 return 0;
1502 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1503 struct port_res_cfg *pr_cfg, int queue_token)
1505 struct ehea_adapter *adapter = port->adapter;
1506 enum ehea_eq_type eq_type = EHEA_EQ;
1507 struct ehea_qp_init_attr *init_attr = NULL;
1508 int ret = -EIO;
1509 u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1511 tx_bytes = pr->tx_bytes;
1512 tx_packets = pr->tx_packets;
1513 rx_bytes = pr->rx_bytes;
1514 rx_packets = pr->rx_packets;
1516 memset(pr, 0, sizeof(struct ehea_port_res));
1518 pr->tx_bytes = rx_bytes;
1519 pr->tx_packets = tx_packets;
1520 pr->rx_bytes = rx_bytes;
1521 pr->rx_packets = rx_packets;
1523 pr->port = port;
1525 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1526 if (!pr->eq) {
1527 pr_err("create_eq failed (eq)\n");
1528 goto out_free;
1531 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1532 pr->eq->fw_handle,
1533 port->logical_port_id);
1534 if (!pr->recv_cq) {
1535 pr_err("create_cq failed (cq_recv)\n");
1536 goto out_free;
1539 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1540 pr->eq->fw_handle,
1541 port->logical_port_id);
1542 if (!pr->send_cq) {
1543 pr_err("create_cq failed (cq_send)\n");
1544 goto out_free;
1547 if (netif_msg_ifup(port))
1548 pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1549 pr->send_cq->attr.act_nr_of_cqes,
1550 pr->recv_cq->attr.act_nr_of_cqes);
1552 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1553 if (!init_attr) {
1554 ret = -ENOMEM;
1555 pr_err("no mem for ehea_qp_init_attr\n");
1556 goto out_free;
1559 init_attr->low_lat_rq1 = 1;
1560 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1561 init_attr->rq_count = 3;
1562 init_attr->qp_token = queue_token;
1563 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1564 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1565 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1566 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1567 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1568 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1569 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1570 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1571 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1572 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1573 init_attr->port_nr = port->logical_port_id;
1574 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1575 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1576 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1578 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1579 if (!pr->qp) {
1580 pr_err("create_qp failed\n");
1581 ret = -EIO;
1582 goto out_free;
1585 if (netif_msg_ifup(port))
1586 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",
1587 init_attr->qp_nr,
1588 init_attr->act_nr_send_wqes,
1589 init_attr->act_nr_rwqes_rq1,
1590 init_attr->act_nr_rwqes_rq2,
1591 init_attr->act_nr_rwqes_rq3);
1593 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1595 ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1596 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1597 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1598 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1599 if (ret)
1600 goto out_free;
1602 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1603 if (ehea_gen_smrs(pr) != 0) {
1604 ret = -EIO;
1605 goto out_free;
1608 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1610 kfree(init_attr);
1612 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1614 pr->lro_mgr.max_aggr = pr->port->lro_max_aggr;
1615 pr->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1616 pr->lro_mgr.lro_arr = pr->lro_desc;
1617 pr->lro_mgr.get_skb_header = get_skb_hdr;
1618 pr->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1619 pr->lro_mgr.dev = port->netdev;
1620 pr->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1621 pr->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1623 ret = 0;
1624 goto out;
1626 out_free:
1627 kfree(init_attr);
1628 vfree(pr->sq_skba.arr);
1629 vfree(pr->rq1_skba.arr);
1630 vfree(pr->rq2_skba.arr);
1631 vfree(pr->rq3_skba.arr);
1632 ehea_destroy_qp(pr->qp);
1633 ehea_destroy_cq(pr->send_cq);
1634 ehea_destroy_cq(pr->recv_cq);
1635 ehea_destroy_eq(pr->eq);
1636 out:
1637 return ret;
1640 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1642 int ret, i;
1644 if (pr->qp)
1645 netif_napi_del(&pr->napi);
1647 ret = ehea_destroy_qp(pr->qp);
1649 if (!ret) {
1650 ehea_destroy_cq(pr->send_cq);
1651 ehea_destroy_cq(pr->recv_cq);
1652 ehea_destroy_eq(pr->eq);
1654 for (i = 0; i < pr->rq1_skba.len; i++)
1655 if (pr->rq1_skba.arr[i])
1656 dev_kfree_skb(pr->rq1_skba.arr[i]);
1658 for (i = 0; i < pr->rq2_skba.len; i++)
1659 if (pr->rq2_skba.arr[i])
1660 dev_kfree_skb(pr->rq2_skba.arr[i]);
1662 for (i = 0; i < pr->rq3_skba.len; i++)
1663 if (pr->rq3_skba.arr[i])
1664 dev_kfree_skb(pr->rq3_skba.arr[i]);
1666 for (i = 0; i < pr->sq_skba.len; i++)
1667 if (pr->sq_skba.arr[i])
1668 dev_kfree_skb(pr->sq_skba.arr[i]);
1670 vfree(pr->rq1_skba.arr);
1671 vfree(pr->rq2_skba.arr);
1672 vfree(pr->rq3_skba.arr);
1673 vfree(pr->sq_skba.arr);
1674 ret = ehea_rem_smrs(pr);
1676 return ret;
1680 * The write_* functions store information in swqe which is used by
1681 * the hardware to calculate the ip/tcp/udp checksum
1684 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1685 const struct sk_buff *skb)
1687 swqe->ip_start = skb_network_offset(skb);
1688 swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1691 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1692 const struct sk_buff *skb)
1694 swqe->tcp_offset =
1695 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1697 swqe->tcp_end = (u16)skb->len - 1;
1700 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1701 const struct sk_buff *skb)
1703 swqe->tcp_offset =
1704 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1706 swqe->tcp_end = (u16)skb->len - 1;
1710 static void write_swqe2_TSO(struct sk_buff *skb,
1711 struct ehea_swqe *swqe, u32 lkey)
1713 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1714 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1715 int skb_data_size = skb_headlen(skb);
1716 int headersize;
1718 /* Packet is TCP with TSO enabled */
1719 swqe->tx_control |= EHEA_SWQE_TSO;
1720 swqe->mss = skb_shinfo(skb)->gso_size;
1721 /* copy only eth/ip/tcp headers to immediate data and
1722 * the rest of skb->data to sg1entry
1724 headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1726 skb_data_size = skb_headlen(skb);
1728 if (skb_data_size >= headersize) {
1729 /* copy immediate data */
1730 skb_copy_from_linear_data(skb, imm_data, headersize);
1731 swqe->immediate_data_length = headersize;
1733 if (skb_data_size > headersize) {
1734 /* set sg1entry data */
1735 sg1entry->l_key = lkey;
1736 sg1entry->len = skb_data_size - headersize;
1737 sg1entry->vaddr =
1738 ehea_map_vaddr(skb->data + headersize);
1739 swqe->descriptors++;
1741 } else
1742 pr_err("cannot handle fragmented headers\n");
1745 static void write_swqe2_nonTSO(struct sk_buff *skb,
1746 struct ehea_swqe *swqe, u32 lkey)
1748 int skb_data_size = skb_headlen(skb);
1749 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1750 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1752 /* Packet is any nonTSO type
1754 * Copy as much as possible skb->data to immediate data and
1755 * the rest to sg1entry
1757 if (skb_data_size >= SWQE2_MAX_IMM) {
1758 /* copy immediate data */
1759 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1761 swqe->immediate_data_length = SWQE2_MAX_IMM;
1763 if (skb_data_size > SWQE2_MAX_IMM) {
1764 /* copy sg1entry data */
1765 sg1entry->l_key = lkey;
1766 sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1767 sg1entry->vaddr =
1768 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1769 swqe->descriptors++;
1771 } else {
1772 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1773 swqe->immediate_data_length = skb_data_size;
1777 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1778 struct ehea_swqe *swqe, u32 lkey)
1780 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1781 skb_frag_t *frag;
1782 int nfrags, sg1entry_contains_frag_data, i;
1784 nfrags = skb_shinfo(skb)->nr_frags;
1785 sg1entry = &swqe->u.immdata_desc.sg_entry;
1786 sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1787 swqe->descriptors = 0;
1788 sg1entry_contains_frag_data = 0;
1790 if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1791 write_swqe2_TSO(skb, swqe, lkey);
1792 else
1793 write_swqe2_nonTSO(skb, swqe, lkey);
1795 /* write descriptors */
1796 if (nfrags > 0) {
1797 if (swqe->descriptors == 0) {
1798 /* sg1entry not yet used */
1799 frag = &skb_shinfo(skb)->frags[0];
1801 /* copy sg1entry data */
1802 sg1entry->l_key = lkey;
1803 sg1entry->len = frag->size;
1804 sg1entry->vaddr =
1805 ehea_map_vaddr(skb_frag_address(frag));
1806 swqe->descriptors++;
1807 sg1entry_contains_frag_data = 1;
1810 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1812 frag = &skb_shinfo(skb)->frags[i];
1813 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1815 sgentry->l_key = lkey;
1816 sgentry->len = frag->size;
1817 sgentry->vaddr = ehea_map_vaddr(skb_frag_address(frag));
1818 swqe->descriptors++;
1823 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1825 int ret = 0;
1826 u64 hret;
1827 u8 reg_type;
1829 /* De/Register untagged packets */
1830 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1831 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1832 port->logical_port_id,
1833 reg_type, port->mac_addr, 0, hcallid);
1834 if (hret != H_SUCCESS) {
1835 pr_err("%sregistering bc address failed (tagged)\n",
1836 hcallid == H_REG_BCMC ? "" : "de");
1837 ret = -EIO;
1838 goto out_herr;
1841 /* De/Register VLAN packets */
1842 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1843 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1844 port->logical_port_id,
1845 reg_type, port->mac_addr, 0, hcallid);
1846 if (hret != H_SUCCESS) {
1847 pr_err("%sregistering bc address failed (vlan)\n",
1848 hcallid == H_REG_BCMC ? "" : "de");
1849 ret = -EIO;
1851 out_herr:
1852 return ret;
1855 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1857 struct ehea_port *port = netdev_priv(dev);
1858 struct sockaddr *mac_addr = sa;
1859 struct hcp_ehea_port_cb0 *cb0;
1860 int ret;
1861 u64 hret;
1863 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1864 ret = -EADDRNOTAVAIL;
1865 goto out;
1868 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1869 if (!cb0) {
1870 pr_err("no mem for cb0\n");
1871 ret = -ENOMEM;
1872 goto out;
1875 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1877 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1879 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1880 port->logical_port_id, H_PORT_CB0,
1881 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1882 if (hret != H_SUCCESS) {
1883 ret = -EIO;
1884 goto out_free;
1887 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1889 /* Deregister old MAC in pHYP */
1890 if (port->state == EHEA_PORT_UP) {
1891 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1892 if (ret)
1893 goto out_upregs;
1896 port->mac_addr = cb0->port_mac_addr << 16;
1898 /* Register new MAC in pHYP */
1899 if (port->state == EHEA_PORT_UP) {
1900 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1901 if (ret)
1902 goto out_upregs;
1905 ret = 0;
1907 out_upregs:
1908 ehea_update_bcmc_registrations();
1909 out_free:
1910 free_page((unsigned long)cb0);
1911 out:
1912 return ret;
1915 static void ehea_promiscuous_error(u64 hret, int enable)
1917 if (hret == H_AUTHORITY)
1918 pr_info("Hypervisor denied %sabling promiscuous mode\n",
1919 enable == 1 ? "en" : "dis");
1920 else
1921 pr_err("failed %sabling promiscuous mode\n",
1922 enable == 1 ? "en" : "dis");
1925 static void ehea_promiscuous(struct net_device *dev, int enable)
1927 struct ehea_port *port = netdev_priv(dev);
1928 struct hcp_ehea_port_cb7 *cb7;
1929 u64 hret;
1931 if (enable == port->promisc)
1932 return;
1934 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1935 if (!cb7) {
1936 pr_err("no mem for cb7\n");
1937 goto out;
1940 /* Modify Pxs_DUCQPN in CB7 */
1941 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1943 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1944 port->logical_port_id,
1945 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1946 if (hret) {
1947 ehea_promiscuous_error(hret, enable);
1948 goto out;
1951 port->promisc = enable;
1952 out:
1953 free_page((unsigned long)cb7);
1956 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1957 u32 hcallid)
1959 u64 hret;
1960 u8 reg_type;
1962 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1963 | EHEA_BCMC_UNTAGGED;
1965 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1966 port->logical_port_id,
1967 reg_type, mc_mac_addr, 0, hcallid);
1968 if (hret)
1969 goto out;
1971 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1972 | EHEA_BCMC_VLANID_ALL;
1974 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1975 port->logical_port_id,
1976 reg_type, mc_mac_addr, 0, hcallid);
1977 out:
1978 return hret;
1981 static int ehea_drop_multicast_list(struct net_device *dev)
1983 struct ehea_port *port = netdev_priv(dev);
1984 struct ehea_mc_list *mc_entry = port->mc_list;
1985 struct list_head *pos;
1986 struct list_head *temp;
1987 int ret = 0;
1988 u64 hret;
1990 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1991 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1993 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1994 H_DEREG_BCMC);
1995 if (hret) {
1996 pr_err("failed deregistering mcast MAC\n");
1997 ret = -EIO;
2000 list_del(pos);
2001 kfree(mc_entry);
2003 return ret;
2006 static void ehea_allmulti(struct net_device *dev, int enable)
2008 struct ehea_port *port = netdev_priv(dev);
2009 u64 hret;
2011 if (!port->allmulti) {
2012 if (enable) {
2013 /* Enable ALLMULTI */
2014 ehea_drop_multicast_list(dev);
2015 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
2016 if (!hret)
2017 port->allmulti = 1;
2018 else
2019 netdev_err(dev,
2020 "failed enabling IFF_ALLMULTI\n");
2022 } else
2023 if (!enable) {
2024 /* Disable ALLMULTI */
2025 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
2026 if (!hret)
2027 port->allmulti = 0;
2028 else
2029 netdev_err(dev,
2030 "failed disabling IFF_ALLMULTI\n");
2034 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
2036 struct ehea_mc_list *ehea_mcl_entry;
2037 u64 hret;
2039 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
2040 if (!ehea_mcl_entry) {
2041 pr_err("no mem for mcl_entry\n");
2042 return;
2045 INIT_LIST_HEAD(&ehea_mcl_entry->list);
2047 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
2049 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
2050 H_REG_BCMC);
2051 if (!hret)
2052 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
2053 else {
2054 pr_err("failed registering mcast MAC\n");
2055 kfree(ehea_mcl_entry);
2059 static void ehea_set_multicast_list(struct net_device *dev)
2061 struct ehea_port *port = netdev_priv(dev);
2062 struct netdev_hw_addr *ha;
2063 int ret;
2065 if (port->promisc) {
2066 ehea_promiscuous(dev, 1);
2067 return;
2069 ehea_promiscuous(dev, 0);
2071 if (dev->flags & IFF_ALLMULTI) {
2072 ehea_allmulti(dev, 1);
2073 goto out;
2075 ehea_allmulti(dev, 0);
2077 if (!netdev_mc_empty(dev)) {
2078 ret = ehea_drop_multicast_list(dev);
2079 if (ret) {
2080 /* Dropping the current multicast list failed.
2081 * Enabling ALL_MULTI is the best we can do.
2083 ehea_allmulti(dev, 1);
2086 if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
2087 pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
2088 port->adapter->max_mc_mac);
2089 goto out;
2092 netdev_for_each_mc_addr(ha, dev)
2093 ehea_add_multicast_entry(port, ha->addr);
2096 out:
2097 ehea_update_bcmc_registrations();
2100 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
2102 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
2103 return -EINVAL;
2104 dev->mtu = new_mtu;
2105 return 0;
2108 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2109 struct ehea_swqe *swqe, u32 lkey)
2111 if (skb->protocol == htons(ETH_P_IP)) {
2112 const struct iphdr *iph = ip_hdr(skb);
2114 /* IPv4 */
2115 swqe->tx_control |= EHEA_SWQE_CRC
2116 | EHEA_SWQE_IP_CHECKSUM
2117 | EHEA_SWQE_TCP_CHECKSUM
2118 | EHEA_SWQE_IMM_DATA_PRESENT
2119 | EHEA_SWQE_DESCRIPTORS_PRESENT;
2121 write_ip_start_end(swqe, skb);
2123 if (iph->protocol == IPPROTO_UDP) {
2124 if ((iph->frag_off & IP_MF) ||
2125 (iph->frag_off & IP_OFFSET))
2126 /* IP fragment, so don't change cs */
2127 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
2128 else
2129 write_udp_offset_end(swqe, skb);
2130 } else if (iph->protocol == IPPROTO_TCP) {
2131 write_tcp_offset_end(swqe, skb);
2134 /* icmp (big data) and ip segmentation packets (all other ip
2135 packets) do not require any special handling */
2137 } else {
2138 /* Other Ethernet Protocol */
2139 swqe->tx_control |= EHEA_SWQE_CRC
2140 | EHEA_SWQE_IMM_DATA_PRESENT
2141 | EHEA_SWQE_DESCRIPTORS_PRESENT;
2144 write_swqe2_data(skb, dev, swqe, lkey);
2147 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2148 struct ehea_swqe *swqe)
2150 int nfrags = skb_shinfo(skb)->nr_frags;
2151 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2152 skb_frag_t *frag;
2153 int i;
2155 if (skb->protocol == htons(ETH_P_IP)) {
2156 const struct iphdr *iph = ip_hdr(skb);
2158 /* IPv4 */
2159 write_ip_start_end(swqe, skb);
2161 if (iph->protocol == IPPROTO_TCP) {
2162 swqe->tx_control |= EHEA_SWQE_CRC
2163 | EHEA_SWQE_IP_CHECKSUM
2164 | EHEA_SWQE_TCP_CHECKSUM
2165 | EHEA_SWQE_IMM_DATA_PRESENT;
2167 write_tcp_offset_end(swqe, skb);
2169 } else if (iph->protocol == IPPROTO_UDP) {
2170 if ((iph->frag_off & IP_MF) ||
2171 (iph->frag_off & IP_OFFSET))
2172 /* IP fragment, so don't change cs */
2173 swqe->tx_control |= EHEA_SWQE_CRC
2174 | EHEA_SWQE_IMM_DATA_PRESENT;
2175 else {
2176 swqe->tx_control |= EHEA_SWQE_CRC
2177 | EHEA_SWQE_IP_CHECKSUM
2178 | EHEA_SWQE_TCP_CHECKSUM
2179 | EHEA_SWQE_IMM_DATA_PRESENT;
2181 write_udp_offset_end(swqe, skb);
2183 } else {
2184 /* icmp (big data) and
2185 ip segmentation packets (all other ip packets) */
2186 swqe->tx_control |= EHEA_SWQE_CRC
2187 | EHEA_SWQE_IP_CHECKSUM
2188 | EHEA_SWQE_IMM_DATA_PRESENT;
2190 } else {
2191 /* Other Ethernet Protocol */
2192 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
2194 /* copy (immediate) data */
2195 if (nfrags == 0) {
2196 /* data is in a single piece */
2197 skb_copy_from_linear_data(skb, imm_data, skb->len);
2198 } else {
2199 /* first copy data from the skb->data buffer ... */
2200 skb_copy_from_linear_data(skb, imm_data,
2201 skb_headlen(skb));
2202 imm_data += skb_headlen(skb);
2204 /* ... then copy data from the fragments */
2205 for (i = 0; i < nfrags; i++) {
2206 frag = &skb_shinfo(skb)->frags[i];
2207 memcpy(imm_data, skb_frag_address(frag), frag->size);
2208 imm_data += frag->size;
2211 swqe->immediate_data_length = skb->len;
2212 dev_kfree_skb(skb);
2215 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2217 struct ehea_port *port = netdev_priv(dev);
2218 struct ehea_swqe *swqe;
2219 u32 lkey;
2220 int swqe_index;
2221 struct ehea_port_res *pr;
2222 struct netdev_queue *txq;
2224 pr = &port->port_res[skb_get_queue_mapping(skb)];
2225 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2227 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2228 memset(swqe, 0, SWQE_HEADER_SIZE);
2229 atomic_dec(&pr->swqe_avail);
2231 if (vlan_tx_tag_present(skb)) {
2232 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2233 swqe->vlan_tag = vlan_tx_tag_get(skb);
2236 pr->tx_packets++;
2237 pr->tx_bytes += skb->len;
2239 if (skb->len <= SWQE3_MAX_IMM) {
2240 u32 sig_iv = port->sig_comp_iv;
2241 u32 swqe_num = pr->swqe_id_counter;
2242 ehea_xmit3(skb, dev, swqe);
2243 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2244 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2245 if (pr->swqe_ll_count >= (sig_iv - 1)) {
2246 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2247 sig_iv);
2248 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2249 pr->swqe_ll_count = 0;
2250 } else
2251 pr->swqe_ll_count += 1;
2252 } else {
2253 swqe->wr_id =
2254 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2255 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2256 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2257 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2258 pr->sq_skba.arr[pr->sq_skba.index] = skb;
2260 pr->sq_skba.index++;
2261 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2263 lkey = pr->send_mr.lkey;
2264 ehea_xmit2(skb, dev, swqe, lkey);
2265 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2267 pr->swqe_id_counter += 1;
2269 netif_info(port, tx_queued, dev,
2270 "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2271 if (netif_msg_tx_queued(port))
2272 ehea_dump(swqe, 512, "swqe");
2274 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2275 netif_tx_stop_queue(txq);
2276 swqe->tx_control |= EHEA_SWQE_PURGE;
2279 ehea_post_swqe(pr->qp, swqe);
2281 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2282 pr->p_stats.queue_stopped++;
2283 netif_tx_stop_queue(txq);
2286 return NETDEV_TX_OK;
2289 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
2291 struct ehea_port *port = netdev_priv(dev);
2292 struct ehea_adapter *adapter = port->adapter;
2293 struct hcp_ehea_port_cb1 *cb1;
2294 int index;
2295 u64 hret;
2297 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2298 if (!cb1) {
2299 pr_err("no mem for cb1\n");
2300 goto out;
2303 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2304 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2305 if (hret != H_SUCCESS) {
2306 pr_err("query_ehea_port failed\n");
2307 goto out;
2310 index = (vid / 64);
2311 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2313 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2314 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2315 if (hret != H_SUCCESS)
2316 pr_err("modify_ehea_port failed\n");
2317 out:
2318 free_page((unsigned long)cb1);
2319 return;
2322 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2324 struct ehea_port *port = netdev_priv(dev);
2325 struct ehea_adapter *adapter = port->adapter;
2326 struct hcp_ehea_port_cb1 *cb1;
2327 int index;
2328 u64 hret;
2330 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2331 if (!cb1) {
2332 pr_err("no mem for cb1\n");
2333 goto out;
2336 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2337 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2338 if (hret != H_SUCCESS) {
2339 pr_err("query_ehea_port failed\n");
2340 goto out;
2343 index = (vid / 64);
2344 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2346 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2347 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2348 if (hret != H_SUCCESS)
2349 pr_err("modify_ehea_port failed\n");
2350 out:
2351 free_page((unsigned long)cb1);
2354 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2356 int ret = -EIO;
2357 u64 hret;
2358 u16 dummy16 = 0;
2359 u64 dummy64 = 0;
2360 struct hcp_modify_qp_cb0 *cb0;
2362 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2363 if (!cb0) {
2364 ret = -ENOMEM;
2365 goto out;
2368 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2369 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2370 if (hret != H_SUCCESS) {
2371 pr_err("query_ehea_qp failed (1)\n");
2372 goto out;
2375 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2376 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2377 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2378 &dummy64, &dummy64, &dummy16, &dummy16);
2379 if (hret != H_SUCCESS) {
2380 pr_err("modify_ehea_qp failed (1)\n");
2381 goto out;
2384 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2385 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2386 if (hret != H_SUCCESS) {
2387 pr_err("query_ehea_qp failed (2)\n");
2388 goto out;
2391 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2392 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2393 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2394 &dummy64, &dummy64, &dummy16, &dummy16);
2395 if (hret != H_SUCCESS) {
2396 pr_err("modify_ehea_qp failed (2)\n");
2397 goto out;
2400 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2401 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2402 if (hret != H_SUCCESS) {
2403 pr_err("query_ehea_qp failed (3)\n");
2404 goto out;
2407 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2408 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2409 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2410 &dummy64, &dummy64, &dummy16, &dummy16);
2411 if (hret != H_SUCCESS) {
2412 pr_err("modify_ehea_qp failed (3)\n");
2413 goto out;
2416 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2417 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2418 if (hret != H_SUCCESS) {
2419 pr_err("query_ehea_qp failed (4)\n");
2420 goto out;
2423 ret = 0;
2424 out:
2425 free_page((unsigned long)cb0);
2426 return ret;
2429 static int ehea_port_res_setup(struct ehea_port *port, int def_qps)
2431 int ret, i;
2432 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2433 enum ehea_eq_type eq_type = EHEA_EQ;
2435 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2436 EHEA_MAX_ENTRIES_EQ, 1);
2437 if (!port->qp_eq) {
2438 ret = -EINVAL;
2439 pr_err("ehea_create_eq failed (qp_eq)\n");
2440 goto out_kill_eq;
2443 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2444 pr_cfg.max_entries_scq = sq_entries * 2;
2445 pr_cfg.max_entries_sq = sq_entries;
2446 pr_cfg.max_entries_rq1 = rq1_entries;
2447 pr_cfg.max_entries_rq2 = rq2_entries;
2448 pr_cfg.max_entries_rq3 = rq3_entries;
2450 pr_cfg_small_rx.max_entries_rcq = 1;
2451 pr_cfg_small_rx.max_entries_scq = sq_entries;
2452 pr_cfg_small_rx.max_entries_sq = sq_entries;
2453 pr_cfg_small_rx.max_entries_rq1 = 1;
2454 pr_cfg_small_rx.max_entries_rq2 = 1;
2455 pr_cfg_small_rx.max_entries_rq3 = 1;
2457 for (i = 0; i < def_qps; i++) {
2458 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2459 if (ret)
2460 goto out_clean_pr;
2462 for (i = def_qps; i < def_qps; i++) {
2463 ret = ehea_init_port_res(port, &port->port_res[i],
2464 &pr_cfg_small_rx, i);
2465 if (ret)
2466 goto out_clean_pr;
2469 return 0;
2471 out_clean_pr:
2472 while (--i >= 0)
2473 ehea_clean_portres(port, &port->port_res[i]);
2475 out_kill_eq:
2476 ehea_destroy_eq(port->qp_eq);
2477 return ret;
2480 static int ehea_clean_all_portres(struct ehea_port *port)
2482 int ret = 0;
2483 int i;
2485 for (i = 0; i < port->num_def_qps; i++)
2486 ret |= ehea_clean_portres(port, &port->port_res[i]);
2488 ret |= ehea_destroy_eq(port->qp_eq);
2490 return ret;
2493 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2495 if (adapter->active_ports)
2496 return;
2498 ehea_rem_mr(&adapter->mr);
2501 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2503 if (adapter->active_ports)
2504 return 0;
2506 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2509 static int ehea_up(struct net_device *dev)
2511 int ret, i;
2512 struct ehea_port *port = netdev_priv(dev);
2514 if (port->state == EHEA_PORT_UP)
2515 return 0;
2517 ret = ehea_port_res_setup(port, port->num_def_qps);
2518 if (ret) {
2519 netdev_err(dev, "port_res_failed\n");
2520 goto out;
2523 /* Set default QP for this port */
2524 ret = ehea_configure_port(port);
2525 if (ret) {
2526 netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2527 goto out_clean_pr;
2530 ret = ehea_reg_interrupts(dev);
2531 if (ret) {
2532 netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2533 goto out_clean_pr;
2536 for (i = 0; i < port->num_def_qps; i++) {
2537 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2538 if (ret) {
2539 netdev_err(dev, "activate_qp failed\n");
2540 goto out_free_irqs;
2544 for (i = 0; i < port->num_def_qps; i++) {
2545 ret = ehea_fill_port_res(&port->port_res[i]);
2546 if (ret) {
2547 netdev_err(dev, "out_free_irqs\n");
2548 goto out_free_irqs;
2552 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2553 if (ret) {
2554 ret = -EIO;
2555 goto out_free_irqs;
2558 port->state = EHEA_PORT_UP;
2560 ret = 0;
2561 goto out;
2563 out_free_irqs:
2564 ehea_free_interrupts(dev);
2566 out_clean_pr:
2567 ehea_clean_all_portres(port);
2568 out:
2569 if (ret)
2570 netdev_info(dev, "Failed starting. ret=%i\n", ret);
2572 ehea_update_bcmc_registrations();
2573 ehea_update_firmware_handles();
2575 return ret;
2578 static void port_napi_disable(struct ehea_port *port)
2580 int i;
2582 for (i = 0; i < port->num_def_qps; i++)
2583 napi_disable(&port->port_res[i].napi);
2586 static void port_napi_enable(struct ehea_port *port)
2588 int i;
2590 for (i = 0; i < port->num_def_qps; i++)
2591 napi_enable(&port->port_res[i].napi);
2594 static int ehea_open(struct net_device *dev)
2596 int ret;
2597 struct ehea_port *port = netdev_priv(dev);
2599 mutex_lock(&port->port_lock);
2601 netif_info(port, ifup, dev, "enabling port\n");
2603 ret = ehea_up(dev);
2604 if (!ret) {
2605 port_napi_enable(port);
2606 netif_tx_start_all_queues(dev);
2609 mutex_unlock(&port->port_lock);
2610 schedule_delayed_work(&port->stats_work, msecs_to_jiffies(1000));
2612 return ret;
2615 static int ehea_down(struct net_device *dev)
2617 int ret;
2618 struct ehea_port *port = netdev_priv(dev);
2620 if (port->state == EHEA_PORT_DOWN)
2621 return 0;
2623 ehea_drop_multicast_list(dev);
2624 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2626 ehea_free_interrupts(dev);
2628 port->state = EHEA_PORT_DOWN;
2630 ehea_update_bcmc_registrations();
2632 ret = ehea_clean_all_portres(port);
2633 if (ret)
2634 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2636 ehea_update_firmware_handles();
2638 return ret;
2641 static int ehea_stop(struct net_device *dev)
2643 int ret;
2644 struct ehea_port *port = netdev_priv(dev);
2646 netif_info(port, ifdown, dev, "disabling port\n");
2648 set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2649 cancel_work_sync(&port->reset_task);
2650 cancel_delayed_work_sync(&port->stats_work);
2651 mutex_lock(&port->port_lock);
2652 netif_tx_stop_all_queues(dev);
2653 port_napi_disable(port);
2654 ret = ehea_down(dev);
2655 mutex_unlock(&port->port_lock);
2656 clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2657 return ret;
2660 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2662 struct ehea_qp qp = *orig_qp;
2663 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2664 struct ehea_swqe *swqe;
2665 int wqe_index;
2666 int i;
2668 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2669 swqe = ehea_get_swqe(&qp, &wqe_index);
2670 swqe->tx_control |= EHEA_SWQE_PURGE;
2674 static void ehea_flush_sq(struct ehea_port *port)
2676 int i;
2678 for (i = 0; i < port->num_def_qps; i++) {
2679 struct ehea_port_res *pr = &port->port_res[i];
2680 int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2681 int ret;
2683 ret = wait_event_timeout(port->swqe_avail_wq,
2684 atomic_read(&pr->swqe_avail) >= swqe_max,
2685 msecs_to_jiffies(100));
2687 if (!ret) {
2688 pr_err("WARNING: sq not flushed completely\n");
2689 break;
2694 int ehea_stop_qps(struct net_device *dev)
2696 struct ehea_port *port = netdev_priv(dev);
2697 struct ehea_adapter *adapter = port->adapter;
2698 struct hcp_modify_qp_cb0 *cb0;
2699 int ret = -EIO;
2700 int dret;
2701 int i;
2702 u64 hret;
2703 u64 dummy64 = 0;
2704 u16 dummy16 = 0;
2706 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2707 if (!cb0) {
2708 ret = -ENOMEM;
2709 goto out;
2712 for (i = 0; i < (port->num_def_qps); i++) {
2713 struct ehea_port_res *pr = &port->port_res[i];
2714 struct ehea_qp *qp = pr->qp;
2716 /* Purge send queue */
2717 ehea_purge_sq(qp);
2719 /* Disable queue pair */
2720 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2721 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2722 cb0);
2723 if (hret != H_SUCCESS) {
2724 pr_err("query_ehea_qp failed (1)\n");
2725 goto out;
2728 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2729 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2731 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2732 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2733 1), cb0, &dummy64,
2734 &dummy64, &dummy16, &dummy16);
2735 if (hret != H_SUCCESS) {
2736 pr_err("modify_ehea_qp failed (1)\n");
2737 goto out;
2740 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2741 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2742 cb0);
2743 if (hret != H_SUCCESS) {
2744 pr_err("query_ehea_qp failed (2)\n");
2745 goto out;
2748 /* deregister shared memory regions */
2749 dret = ehea_rem_smrs(pr);
2750 if (dret) {
2751 pr_err("unreg shared memory region failed\n");
2752 goto out;
2756 ret = 0;
2757 out:
2758 free_page((unsigned long)cb0);
2760 return ret;
2763 void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2765 struct ehea_qp qp = *orig_qp;
2766 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2767 struct ehea_rwqe *rwqe;
2768 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2769 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2770 struct sk_buff *skb;
2771 u32 lkey = pr->recv_mr.lkey;
2774 int i;
2775 int index;
2777 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2778 rwqe = ehea_get_next_rwqe(&qp, 2);
2779 rwqe->sg_list[0].l_key = lkey;
2780 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2781 skb = skba_rq2[index];
2782 if (skb)
2783 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2786 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2787 rwqe = ehea_get_next_rwqe(&qp, 3);
2788 rwqe->sg_list[0].l_key = lkey;
2789 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2790 skb = skba_rq3[index];
2791 if (skb)
2792 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2796 int ehea_restart_qps(struct net_device *dev)
2798 struct ehea_port *port = netdev_priv(dev);
2799 struct ehea_adapter *adapter = port->adapter;
2800 int ret = 0;
2801 int i;
2803 struct hcp_modify_qp_cb0 *cb0;
2804 u64 hret;
2805 u64 dummy64 = 0;
2806 u16 dummy16 = 0;
2808 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2809 if (!cb0) {
2810 ret = -ENOMEM;
2811 goto out;
2814 for (i = 0; i < (port->num_def_qps); i++) {
2815 struct ehea_port_res *pr = &port->port_res[i];
2816 struct ehea_qp *qp = pr->qp;
2818 ret = ehea_gen_smrs(pr);
2819 if (ret) {
2820 netdev_err(dev, "creation of shared memory regions failed\n");
2821 goto out;
2824 ehea_update_rqs(qp, pr);
2826 /* Enable queue pair */
2827 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2828 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2829 cb0);
2830 if (hret != H_SUCCESS) {
2831 netdev_err(dev, "query_ehea_qp failed (1)\n");
2832 goto out;
2835 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2836 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2838 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2839 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2840 1), cb0, &dummy64,
2841 &dummy64, &dummy16, &dummy16);
2842 if (hret != H_SUCCESS) {
2843 netdev_err(dev, "modify_ehea_qp failed (1)\n");
2844 goto out;
2847 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2848 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2849 cb0);
2850 if (hret != H_SUCCESS) {
2851 netdev_err(dev, "query_ehea_qp failed (2)\n");
2852 goto out;
2855 /* refill entire queue */
2856 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2857 ehea_refill_rq2(pr, 0);
2858 ehea_refill_rq3(pr, 0);
2860 out:
2861 free_page((unsigned long)cb0);
2863 return ret;
2866 static void ehea_reset_port(struct work_struct *work)
2868 int ret;
2869 struct ehea_port *port =
2870 container_of(work, struct ehea_port, reset_task);
2871 struct net_device *dev = port->netdev;
2873 mutex_lock(&dlpar_mem_lock);
2874 port->resets++;
2875 mutex_lock(&port->port_lock);
2876 netif_tx_disable(dev);
2878 port_napi_disable(port);
2880 ehea_down(dev);
2882 ret = ehea_up(dev);
2883 if (ret)
2884 goto out;
2886 ehea_set_multicast_list(dev);
2888 netif_info(port, timer, dev, "reset successful\n");
2890 port_napi_enable(port);
2892 netif_tx_wake_all_queues(dev);
2893 out:
2894 mutex_unlock(&port->port_lock);
2895 mutex_unlock(&dlpar_mem_lock);
2898 static void ehea_rereg_mrs(void)
2900 int ret, i;
2901 struct ehea_adapter *adapter;
2903 pr_info("LPAR memory changed - re-initializing driver\n");
2905 list_for_each_entry(adapter, &adapter_list, list)
2906 if (adapter->active_ports) {
2907 /* Shutdown all ports */
2908 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2909 struct ehea_port *port = adapter->port[i];
2910 struct net_device *dev;
2912 if (!port)
2913 continue;
2915 dev = port->netdev;
2917 if (dev->flags & IFF_UP) {
2918 mutex_lock(&port->port_lock);
2919 netif_tx_disable(dev);
2920 ehea_flush_sq(port);
2921 ret = ehea_stop_qps(dev);
2922 if (ret) {
2923 mutex_unlock(&port->port_lock);
2924 goto out;
2926 port_napi_disable(port);
2927 mutex_unlock(&port->port_lock);
2929 reset_sq_restart_flag(port);
2932 /* Unregister old memory region */
2933 ret = ehea_rem_mr(&adapter->mr);
2934 if (ret) {
2935 pr_err("unregister MR failed - driver inoperable!\n");
2936 goto out;
2940 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2942 list_for_each_entry(adapter, &adapter_list, list)
2943 if (adapter->active_ports) {
2944 /* Register new memory region */
2945 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2946 if (ret) {
2947 pr_err("register MR failed - driver inoperable!\n");
2948 goto out;
2951 /* Restart all ports */
2952 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2953 struct ehea_port *port = adapter->port[i];
2955 if (port) {
2956 struct net_device *dev = port->netdev;
2958 if (dev->flags & IFF_UP) {
2959 mutex_lock(&port->port_lock);
2960 ret = ehea_restart_qps(dev);
2961 if (!ret) {
2962 check_sqs(port);
2963 port_napi_enable(port);
2964 netif_tx_wake_all_queues(dev);
2965 } else {
2966 netdev_err(dev, "Unable to restart QPS\n");
2968 mutex_unlock(&port->port_lock);
2973 pr_info("re-initializing driver complete\n");
2974 out:
2975 return;
2978 static void ehea_tx_watchdog(struct net_device *dev)
2980 struct ehea_port *port = netdev_priv(dev);
2982 if (netif_carrier_ok(dev) &&
2983 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2984 ehea_schedule_port_reset(port);
2987 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2989 struct hcp_query_ehea *cb;
2990 u64 hret;
2991 int ret;
2993 cb = (void *)get_zeroed_page(GFP_KERNEL);
2994 if (!cb) {
2995 ret = -ENOMEM;
2996 goto out;
2999 hret = ehea_h_query_ehea(adapter->handle, cb);
3001 if (hret != H_SUCCESS) {
3002 ret = -EIO;
3003 goto out_herr;
3006 adapter->max_mc_mac = cb->max_mc_mac - 1;
3007 ret = 0;
3009 out_herr:
3010 free_page((unsigned long)cb);
3011 out:
3012 return ret;
3015 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
3017 struct hcp_ehea_port_cb4 *cb4;
3018 u64 hret;
3019 int ret = 0;
3021 *jumbo = 0;
3023 /* (Try to) enable *jumbo frames */
3024 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
3025 if (!cb4) {
3026 pr_err("no mem for cb4\n");
3027 ret = -ENOMEM;
3028 goto out;
3029 } else {
3030 hret = ehea_h_query_ehea_port(port->adapter->handle,
3031 port->logical_port_id,
3032 H_PORT_CB4,
3033 H_PORT_CB4_JUMBO, cb4);
3034 if (hret == H_SUCCESS) {
3035 if (cb4->jumbo_frame)
3036 *jumbo = 1;
3037 else {
3038 cb4->jumbo_frame = 1;
3039 hret = ehea_h_modify_ehea_port(port->adapter->
3040 handle,
3041 port->
3042 logical_port_id,
3043 H_PORT_CB4,
3044 H_PORT_CB4_JUMBO,
3045 cb4);
3046 if (hret == H_SUCCESS)
3047 *jumbo = 1;
3049 } else
3050 ret = -EINVAL;
3052 free_page((unsigned long)cb4);
3054 out:
3055 return ret;
3058 static ssize_t ehea_show_port_id(struct device *dev,
3059 struct device_attribute *attr, char *buf)
3061 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
3062 return sprintf(buf, "%d", port->logical_port_id);
3065 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
3066 NULL);
3068 static void __devinit logical_port_release(struct device *dev)
3070 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
3071 of_node_put(port->ofdev.dev.of_node);
3074 static struct device *ehea_register_port(struct ehea_port *port,
3075 struct device_node *dn)
3077 int ret;
3079 port->ofdev.dev.of_node = of_node_get(dn);
3080 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
3081 port->ofdev.dev.bus = &ibmebus_bus_type;
3083 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
3084 port->ofdev.dev.release = logical_port_release;
3086 ret = of_device_register(&port->ofdev);
3087 if (ret) {
3088 pr_err("failed to register device. ret=%d\n", ret);
3089 goto out;
3092 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
3093 if (ret) {
3094 pr_err("failed to register attributes, ret=%d\n", ret);
3095 goto out_unreg_of_dev;
3098 return &port->ofdev.dev;
3100 out_unreg_of_dev:
3101 of_device_unregister(&port->ofdev);
3102 out:
3103 return NULL;
3106 static void ehea_unregister_port(struct ehea_port *port)
3108 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
3109 of_device_unregister(&port->ofdev);
3112 static const struct net_device_ops ehea_netdev_ops = {
3113 .ndo_open = ehea_open,
3114 .ndo_stop = ehea_stop,
3115 .ndo_start_xmit = ehea_start_xmit,
3116 #ifdef CONFIG_NET_POLL_CONTROLLER
3117 .ndo_poll_controller = ehea_netpoll,
3118 #endif
3119 .ndo_get_stats = ehea_get_stats,
3120 .ndo_set_mac_address = ehea_set_mac_addr,
3121 .ndo_validate_addr = eth_validate_addr,
3122 .ndo_set_rx_mode = ehea_set_multicast_list,
3123 .ndo_change_mtu = ehea_change_mtu,
3124 .ndo_vlan_rx_add_vid = ehea_vlan_rx_add_vid,
3125 .ndo_vlan_rx_kill_vid = ehea_vlan_rx_kill_vid,
3126 .ndo_tx_timeout = ehea_tx_watchdog,
3129 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
3130 u32 logical_port_id,
3131 struct device_node *dn)
3133 int ret;
3134 struct net_device *dev;
3135 struct ehea_port *port;
3136 struct device *port_dev;
3137 int jumbo;
3139 /* allocate memory for the port structures */
3140 dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
3142 if (!dev) {
3143 pr_err("no mem for net_device\n");
3144 ret = -ENOMEM;
3145 goto out_err;
3148 port = netdev_priv(dev);
3150 mutex_init(&port->port_lock);
3151 port->state = EHEA_PORT_DOWN;
3152 port->sig_comp_iv = sq_entries / 10;
3154 port->adapter = adapter;
3155 port->netdev = dev;
3156 port->logical_port_id = logical_port_id;
3158 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
3160 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
3161 if (!port->mc_list) {
3162 ret = -ENOMEM;
3163 goto out_free_ethdev;
3166 INIT_LIST_HEAD(&port->mc_list->list);
3168 ret = ehea_sense_port_attr(port);
3169 if (ret)
3170 goto out_free_mc_list;
3172 netif_set_real_num_rx_queues(dev, port->num_def_qps);
3173 netif_set_real_num_tx_queues(dev, port->num_def_qps);
3175 port_dev = ehea_register_port(port, dn);
3176 if (!port_dev)
3177 goto out_free_mc_list;
3179 SET_NETDEV_DEV(dev, port_dev);
3181 /* initialize net_device structure */
3182 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3184 dev->netdev_ops = &ehea_netdev_ops;
3185 ehea_set_ethtool_ops(dev);
3187 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
3188 | NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_TX | NETIF_F_LRO;
3189 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
3190 | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_TX
3191 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
3192 | NETIF_F_RXCSUM;
3193 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3195 if (use_lro)
3196 dev->features |= NETIF_F_LRO;
3198 INIT_WORK(&port->reset_task, ehea_reset_port);
3199 INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3201 init_waitqueue_head(&port->swqe_avail_wq);
3202 init_waitqueue_head(&port->restart_wq);
3204 memset(&port->stats, 0, sizeof(struct net_device_stats));
3205 ret = register_netdev(dev);
3206 if (ret) {
3207 pr_err("register_netdev failed. ret=%d\n", ret);
3208 goto out_unreg_port;
3211 port->lro_max_aggr = lro_max_aggr;
3213 ret = ehea_get_jumboframe_status(port, &jumbo);
3214 if (ret)
3215 netdev_err(dev, "failed determining jumbo frame status\n");
3217 netdev_info(dev, "Jumbo frames are %sabled\n",
3218 jumbo == 1 ? "en" : "dis");
3220 adapter->active_ports++;
3222 return port;
3224 out_unreg_port:
3225 ehea_unregister_port(port);
3227 out_free_mc_list:
3228 kfree(port->mc_list);
3230 out_free_ethdev:
3231 free_netdev(dev);
3233 out_err:
3234 pr_err("setting up logical port with id=%d failed, ret=%d\n",
3235 logical_port_id, ret);
3236 return NULL;
3239 static void ehea_shutdown_single_port(struct ehea_port *port)
3241 struct ehea_adapter *adapter = port->adapter;
3243 cancel_work_sync(&port->reset_task);
3244 cancel_delayed_work_sync(&port->stats_work);
3245 unregister_netdev(port->netdev);
3246 ehea_unregister_port(port);
3247 kfree(port->mc_list);
3248 free_netdev(port->netdev);
3249 adapter->active_ports--;
3252 static int ehea_setup_ports(struct ehea_adapter *adapter)
3254 struct device_node *lhea_dn;
3255 struct device_node *eth_dn = NULL;
3257 const u32 *dn_log_port_id;
3258 int i = 0;
3260 lhea_dn = adapter->ofdev->dev.of_node;
3261 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3263 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3264 NULL);
3265 if (!dn_log_port_id) {
3266 pr_err("bad device node: eth_dn name=%s\n",
3267 eth_dn->full_name);
3268 continue;
3271 if (ehea_add_adapter_mr(adapter)) {
3272 pr_err("creating MR failed\n");
3273 of_node_put(eth_dn);
3274 return -EIO;
3277 adapter->port[i] = ehea_setup_single_port(adapter,
3278 *dn_log_port_id,
3279 eth_dn);
3280 if (adapter->port[i])
3281 netdev_info(adapter->port[i]->netdev,
3282 "logical port id #%d\n", *dn_log_port_id);
3283 else
3284 ehea_remove_adapter_mr(adapter);
3286 i++;
3288 return 0;
3291 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3292 u32 logical_port_id)
3294 struct device_node *lhea_dn;
3295 struct device_node *eth_dn = NULL;
3296 const u32 *dn_log_port_id;
3298 lhea_dn = adapter->ofdev->dev.of_node;
3299 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3301 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3302 NULL);
3303 if (dn_log_port_id)
3304 if (*dn_log_port_id == logical_port_id)
3305 return eth_dn;
3308 return NULL;
3311 static ssize_t ehea_probe_port(struct device *dev,
3312 struct device_attribute *attr,
3313 const char *buf, size_t count)
3315 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3316 struct ehea_port *port;
3317 struct device_node *eth_dn = NULL;
3318 int i;
3320 u32 logical_port_id;
3322 sscanf(buf, "%d", &logical_port_id);
3324 port = ehea_get_port(adapter, logical_port_id);
3326 if (port) {
3327 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3328 logical_port_id);
3329 return -EINVAL;
3332 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3334 if (!eth_dn) {
3335 pr_info("no logical port with id %d found\n", logical_port_id);
3336 return -EINVAL;
3339 if (ehea_add_adapter_mr(adapter)) {
3340 pr_err("creating MR failed\n");
3341 return -EIO;
3344 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3346 of_node_put(eth_dn);
3348 if (port) {
3349 for (i = 0; i < EHEA_MAX_PORTS; i++)
3350 if (!adapter->port[i]) {
3351 adapter->port[i] = port;
3352 break;
3355 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3356 logical_port_id);
3357 } else {
3358 ehea_remove_adapter_mr(adapter);
3359 return -EIO;
3362 return (ssize_t) count;
3365 static ssize_t ehea_remove_port(struct device *dev,
3366 struct device_attribute *attr,
3367 const char *buf, size_t count)
3369 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3370 struct ehea_port *port;
3371 int i;
3372 u32 logical_port_id;
3374 sscanf(buf, "%d", &logical_port_id);
3376 port = ehea_get_port(adapter, logical_port_id);
3378 if (port) {
3379 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3380 logical_port_id);
3382 ehea_shutdown_single_port(port);
3384 for (i = 0; i < EHEA_MAX_PORTS; i++)
3385 if (adapter->port[i] == port) {
3386 adapter->port[i] = NULL;
3387 break;
3389 } else {
3390 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3391 logical_port_id);
3392 return -EINVAL;
3395 ehea_remove_adapter_mr(adapter);
3397 return (ssize_t) count;
3400 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3401 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3403 int ehea_create_device_sysfs(struct platform_device *dev)
3405 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3406 if (ret)
3407 goto out;
3409 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3410 out:
3411 return ret;
3414 void ehea_remove_device_sysfs(struct platform_device *dev)
3416 device_remove_file(&dev->dev, &dev_attr_probe_port);
3417 device_remove_file(&dev->dev, &dev_attr_remove_port);
3420 static int __devinit ehea_probe_adapter(struct platform_device *dev,
3421 const struct of_device_id *id)
3423 struct ehea_adapter *adapter;
3424 const u64 *adapter_handle;
3425 int ret;
3427 if (!dev || !dev->dev.of_node) {
3428 pr_err("Invalid ibmebus device probed\n");
3429 return -EINVAL;
3432 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3433 if (!adapter) {
3434 ret = -ENOMEM;
3435 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3436 goto out;
3439 list_add(&adapter->list, &adapter_list);
3441 adapter->ofdev = dev;
3443 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3444 NULL);
3445 if (adapter_handle)
3446 adapter->handle = *adapter_handle;
3448 if (!adapter->handle) {
3449 dev_err(&dev->dev, "failed getting handle for adapter"
3450 " '%s'\n", dev->dev.of_node->full_name);
3451 ret = -ENODEV;
3452 goto out_free_ad;
3455 adapter->pd = EHEA_PD_ID;
3457 dev_set_drvdata(&dev->dev, adapter);
3460 /* initialize adapter and ports */
3461 /* get adapter properties */
3462 ret = ehea_sense_adapter_attr(adapter);
3463 if (ret) {
3464 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3465 goto out_free_ad;
3468 adapter->neq = ehea_create_eq(adapter,
3469 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3470 if (!adapter->neq) {
3471 ret = -EIO;
3472 dev_err(&dev->dev, "NEQ creation failed\n");
3473 goto out_free_ad;
3476 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3477 (unsigned long)adapter);
3479 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3480 ehea_interrupt_neq, IRQF_DISABLED,
3481 "ehea_neq", adapter);
3482 if (ret) {
3483 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3484 goto out_kill_eq;
3487 ret = ehea_create_device_sysfs(dev);
3488 if (ret)
3489 goto out_free_irq;
3491 ret = ehea_setup_ports(adapter);
3492 if (ret) {
3493 dev_err(&dev->dev, "setup_ports failed\n");
3494 goto out_rem_dev_sysfs;
3497 ret = 0;
3498 goto out;
3500 out_rem_dev_sysfs:
3501 ehea_remove_device_sysfs(dev);
3503 out_free_irq:
3504 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3506 out_kill_eq:
3507 ehea_destroy_eq(adapter->neq);
3509 out_free_ad:
3510 list_del(&adapter->list);
3511 kfree(adapter);
3513 out:
3514 ehea_update_firmware_handles();
3516 return ret;
3519 static int __devexit ehea_remove(struct platform_device *dev)
3521 struct ehea_adapter *adapter = dev_get_drvdata(&dev->dev);
3522 int i;
3524 for (i = 0; i < EHEA_MAX_PORTS; i++)
3525 if (adapter->port[i]) {
3526 ehea_shutdown_single_port(adapter->port[i]);
3527 adapter->port[i] = NULL;
3530 ehea_remove_device_sysfs(dev);
3532 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3533 tasklet_kill(&adapter->neq_tasklet);
3535 ehea_destroy_eq(adapter->neq);
3536 ehea_remove_adapter_mr(adapter);
3537 list_del(&adapter->list);
3538 kfree(adapter);
3540 ehea_update_firmware_handles();
3542 return 0;
3545 void ehea_crash_handler(void)
3547 int i;
3549 if (ehea_fw_handles.arr)
3550 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3551 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3552 ehea_fw_handles.arr[i].fwh,
3553 FORCE_FREE);
3555 if (ehea_bcmc_regs.arr)
3556 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3557 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3558 ehea_bcmc_regs.arr[i].port_id,
3559 ehea_bcmc_regs.arr[i].reg_type,
3560 ehea_bcmc_regs.arr[i].macaddr,
3561 0, H_DEREG_BCMC);
3564 static int ehea_mem_notifier(struct notifier_block *nb,
3565 unsigned long action, void *data)
3567 int ret = NOTIFY_BAD;
3568 struct memory_notify *arg = data;
3570 mutex_lock(&dlpar_mem_lock);
3572 switch (action) {
3573 case MEM_CANCEL_OFFLINE:
3574 pr_info("memory offlining canceled");
3575 /* Readd canceled memory block */
3576 case MEM_ONLINE:
3577 pr_info("memory is going online");
3578 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3579 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3580 goto out_unlock;
3581 ehea_rereg_mrs();
3582 break;
3583 case MEM_GOING_OFFLINE:
3584 pr_info("memory is going offline");
3585 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3586 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3587 goto out_unlock;
3588 ehea_rereg_mrs();
3589 break;
3590 default:
3591 break;
3594 ehea_update_firmware_handles();
3595 ret = NOTIFY_OK;
3597 out_unlock:
3598 mutex_unlock(&dlpar_mem_lock);
3599 return ret;
3602 static struct notifier_block ehea_mem_nb = {
3603 .notifier_call = ehea_mem_notifier,
3606 static int ehea_reboot_notifier(struct notifier_block *nb,
3607 unsigned long action, void *unused)
3609 if (action == SYS_RESTART) {
3610 pr_info("Reboot: freeing all eHEA resources\n");
3611 ibmebus_unregister_driver(&ehea_driver);
3613 return NOTIFY_DONE;
3616 static struct notifier_block ehea_reboot_nb = {
3617 .notifier_call = ehea_reboot_notifier,
3620 static int check_module_parm(void)
3622 int ret = 0;
3624 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3625 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3626 pr_info("Bad parameter: rq1_entries\n");
3627 ret = -EINVAL;
3629 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3630 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3631 pr_info("Bad parameter: rq2_entries\n");
3632 ret = -EINVAL;
3634 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3635 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3636 pr_info("Bad parameter: rq3_entries\n");
3637 ret = -EINVAL;
3639 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3640 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3641 pr_info("Bad parameter: sq_entries\n");
3642 ret = -EINVAL;
3645 return ret;
3648 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3649 char *buf)
3651 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3654 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3655 ehea_show_capabilities, NULL);
3657 int __init ehea_module_init(void)
3659 int ret;
3661 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3663 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3664 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3666 mutex_init(&ehea_fw_handles.lock);
3667 spin_lock_init(&ehea_bcmc_regs.lock);
3669 ret = check_module_parm();
3670 if (ret)
3671 goto out;
3673 ret = ehea_create_busmap();
3674 if (ret)
3675 goto out;
3677 ret = register_reboot_notifier(&ehea_reboot_nb);
3678 if (ret)
3679 pr_info("failed registering reboot notifier\n");
3681 ret = register_memory_notifier(&ehea_mem_nb);
3682 if (ret)
3683 pr_info("failed registering memory remove notifier\n");
3685 ret = crash_shutdown_register(ehea_crash_handler);
3686 if (ret)
3687 pr_info("failed registering crash handler\n");
3689 ret = ibmebus_register_driver(&ehea_driver);
3690 if (ret) {
3691 pr_err("failed registering eHEA device driver on ebus\n");
3692 goto out2;
3695 ret = driver_create_file(&ehea_driver.driver,
3696 &driver_attr_capabilities);
3697 if (ret) {
3698 pr_err("failed to register capabilities attribute, ret=%d\n",
3699 ret);
3700 goto out3;
3703 return ret;
3705 out3:
3706 ibmebus_unregister_driver(&ehea_driver);
3707 out2:
3708 unregister_memory_notifier(&ehea_mem_nb);
3709 unregister_reboot_notifier(&ehea_reboot_nb);
3710 crash_shutdown_unregister(ehea_crash_handler);
3711 out:
3712 return ret;
3715 static void __exit ehea_module_exit(void)
3717 int ret;
3719 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3720 ibmebus_unregister_driver(&ehea_driver);
3721 unregister_reboot_notifier(&ehea_reboot_nb);
3722 ret = crash_shutdown_unregister(ehea_crash_handler);
3723 if (ret)
3724 pr_info("failed unregistering crash handler\n");
3725 unregister_memory_notifier(&ehea_mem_nb);
3726 kfree(ehea_fw_handles.arr);
3727 kfree(ehea_bcmc_regs.arr);
3728 ehea_destroy_busmap();
3731 module_init(ehea_module_init);
3732 module_exit(ehea_module_exit);