cmd64x: don't clear the other channel's interrupt
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / net / ehea / ehea_main.c
blobf0319f1e8e052e99adfde7ca658cfaf7074b1ac1
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 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/tcp.h>
32 #include <linux/udp.h>
33 #include <linux/if.h>
34 #include <linux/list.h>
35 #include <linux/if_ether.h>
36 #include <linux/notifier.h>
37 #include <linux/reboot.h>
39 #include <net/ip.h>
41 #include "ehea.h"
42 #include "ehea_qmr.h"
43 #include "ehea_phyp.h"
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
48 MODULE_DESCRIPTION("IBM eServer HEA Driver");
49 MODULE_VERSION(DRV_VERSION);
52 static int msg_level = -1;
53 static int rq1_entries = EHEA_DEF_ENTRIES_RQ1;
54 static int rq2_entries = EHEA_DEF_ENTRIES_RQ2;
55 static int rq3_entries = EHEA_DEF_ENTRIES_RQ3;
56 static int sq_entries = EHEA_DEF_ENTRIES_SQ;
57 static int use_mcs = 0;
58 static int use_lro = 0;
59 static int lro_max_aggr = EHEA_LRO_MAX_AGGR;
60 static int num_tx_qps = EHEA_NUM_TX_QP;
61 static int prop_carrier_state = 0;
63 module_param(msg_level, int, 0);
64 module_param(rq1_entries, int, 0);
65 module_param(rq2_entries, int, 0);
66 module_param(rq3_entries, int, 0);
67 module_param(sq_entries, int, 0);
68 module_param(prop_carrier_state, int, 0);
69 module_param(use_mcs, int, 0);
70 module_param(use_lro, int, 0);
71 module_param(lro_max_aggr, int, 0);
72 module_param(num_tx_qps, int, 0);
74 MODULE_PARM_DESC(num_tx_qps, "Number of TX-QPS");
75 MODULE_PARM_DESC(msg_level, "msg_level");
76 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
77 "port to stack. 1:yes, 0:no. Default = 0 ");
78 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
79 "[2^x - 1], x = [6..14]. Default = "
80 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
81 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
82 "[2^x - 1], x = [6..14]. Default = "
83 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
84 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
85 "[2^x - 1], x = [6..14]. Default = "
86 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
87 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue "
88 "[2^x - 1], x = [6..14]. Default = "
89 __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
90 MODULE_PARM_DESC(use_mcs, " 0:NAPI, 1:Multiple receive queues, Default = 0 ");
92 MODULE_PARM_DESC(lro_max_aggr, " LRO: Max packets to be aggregated. Default = "
93 __MODULE_STRING(EHEA_LRO_MAX_AGGR));
94 MODULE_PARM_DESC(use_lro, " Large Receive Offload, 1: enable, 0: disable, "
95 "Default = 0");
97 static int port_name_cnt = 0;
98 static LIST_HEAD(adapter_list);
99 u64 ehea_driver_flags = 0;
100 struct work_struct ehea_rereg_mr_task;
102 struct semaphore dlpar_mem_lock;
104 static int __devinit ehea_probe_adapter(struct of_device *dev,
105 const struct of_device_id *id);
107 static int __devexit ehea_remove(struct of_device *dev);
109 static struct of_device_id ehea_device_table[] = {
111 .name = "lhea",
112 .compatible = "IBM,lhea",
117 static struct of_platform_driver ehea_driver = {
118 .name = "ehea",
119 .match_table = ehea_device_table,
120 .probe = ehea_probe_adapter,
121 .remove = ehea_remove,
124 void ehea_dump(void *adr, int len, char *msg) {
125 int x;
126 unsigned char *deb = adr;
127 for (x = 0; x < len; x += 16) {
128 printk(DRV_NAME " %s adr=%p ofs=%04x %016lx %016lx\n", msg,
129 deb, x, *((u64*)&deb[0]), *((u64*)&deb[8]));
130 deb += 16;
134 static struct net_device_stats *ehea_get_stats(struct net_device *dev)
136 struct ehea_port *port = netdev_priv(dev);
137 struct net_device_stats *stats = &port->stats;
138 struct hcp_ehea_port_cb2 *cb2;
139 u64 hret, rx_packets;
140 int i;
142 memset(stats, 0, sizeof(*stats));
144 cb2 = kzalloc(PAGE_SIZE, GFP_KERNEL);
145 if (!cb2) {
146 ehea_error("no mem for cb2");
147 goto out;
150 hret = ehea_h_query_ehea_port(port->adapter->handle,
151 port->logical_port_id,
152 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
153 if (hret != H_SUCCESS) {
154 ehea_error("query_ehea_port failed");
155 goto out_herr;
158 if (netif_msg_hw(port))
159 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
161 rx_packets = 0;
162 for (i = 0; i < port->num_def_qps; i++)
163 rx_packets += port->port_res[i].rx_packets;
165 stats->tx_packets = cb2->txucp + cb2->txmcp + cb2->txbcp;
166 stats->multicast = cb2->rxmcp;
167 stats->rx_errors = cb2->rxuerr;
168 stats->rx_bytes = cb2->rxo;
169 stats->tx_bytes = cb2->txo;
170 stats->rx_packets = rx_packets;
172 out_herr:
173 kfree(cb2);
174 out:
175 return stats;
178 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
180 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
181 struct net_device *dev = pr->port->netdev;
182 int max_index_mask = pr->rq1_skba.len - 1;
183 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
184 int adder = 0;
185 int i;
187 pr->rq1_skba.os_skbs = 0;
189 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
190 pr->rq1_skba.index = index;
191 pr->rq1_skba.os_skbs = fill_wqes;
192 return;
195 for (i = 0; i < fill_wqes; i++) {
196 if (!skb_arr_rq1[index]) {
197 skb_arr_rq1[index] = netdev_alloc_skb(dev,
198 EHEA_L_PKT_SIZE);
199 if (!skb_arr_rq1[index]) {
200 pr->rq1_skba.os_skbs = fill_wqes - i;
201 ehea_error("%s: no mem for skb/%d wqes filled",
202 dev->name, i);
203 break;
206 index--;
207 index &= max_index_mask;
208 adder++;
211 if (adder == 0)
212 return;
214 /* Ring doorbell */
215 ehea_update_rq1a(pr->qp, adder);
218 static int ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
220 int ret = 0;
221 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
222 struct net_device *dev = pr->port->netdev;
223 int i;
225 for (i = 0; i < pr->rq1_skba.len; i++) {
226 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
227 if (!skb_arr_rq1[i]) {
228 ehea_error("%s: no mem for skb/%d wqes filled",
229 dev->name, i);
230 ret = -ENOMEM;
231 goto out;
234 /* Ring doorbell */
235 ehea_update_rq1a(pr->qp, nr_rq1a);
236 out:
237 return ret;
240 static int ehea_refill_rq_def(struct ehea_port_res *pr,
241 struct ehea_q_skb_arr *q_skba, int rq_nr,
242 int num_wqes, int wqe_type, int packet_size)
244 struct net_device *dev = pr->port->netdev;
245 struct ehea_qp *qp = pr->qp;
246 struct sk_buff **skb_arr = q_skba->arr;
247 struct ehea_rwqe *rwqe;
248 int i, index, max_index_mask, fill_wqes;
249 int adder = 0;
250 int ret = 0;
252 fill_wqes = q_skba->os_skbs + num_wqes;
253 q_skba->os_skbs = 0;
255 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
256 q_skba->os_skbs = fill_wqes;
257 return ret;
260 index = q_skba->index;
261 max_index_mask = q_skba->len - 1;
262 for (i = 0; i < fill_wqes; i++) {
263 u64 tmp_addr;
264 struct sk_buff *skb = netdev_alloc_skb(dev, packet_size);
265 if (!skb) {
266 ehea_error("%s: no mem for skb/%d wqes filled",
267 pr->port->netdev->name, i);
268 q_skba->os_skbs = fill_wqes - i;
269 ret = -ENOMEM;
270 break;
272 skb_reserve(skb, NET_IP_ALIGN);
274 skb_arr[index] = skb;
275 tmp_addr = ehea_map_vaddr(skb->data);
276 if (tmp_addr == -1) {
277 dev_kfree_skb(skb);
278 q_skba->os_skbs = fill_wqes - i;
279 ret = 0;
280 break;
283 rwqe = ehea_get_next_rwqe(qp, rq_nr);
284 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
285 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
286 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
287 rwqe->sg_list[0].vaddr = tmp_addr;
288 rwqe->sg_list[0].len = packet_size;
289 rwqe->data_segments = 1;
291 index++;
292 index &= max_index_mask;
293 adder++;
296 q_skba->index = index;
297 if (adder == 0)
298 goto out;
300 /* Ring doorbell */
301 iosync();
302 if (rq_nr == 2)
303 ehea_update_rq2a(pr->qp, adder);
304 else
305 ehea_update_rq3a(pr->qp, adder);
306 out:
307 return ret;
311 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
313 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
314 nr_of_wqes, EHEA_RWQE2_TYPE,
315 EHEA_RQ2_PKT_SIZE + NET_IP_ALIGN);
319 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
321 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
322 nr_of_wqes, EHEA_RWQE3_TYPE,
323 EHEA_MAX_PACKET_SIZE + NET_IP_ALIGN);
326 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
328 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
329 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
330 return 0;
331 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
332 (cqe->header_length == 0))
333 return 0;
334 return -EINVAL;
337 static inline void ehea_fill_skb(struct net_device *dev,
338 struct sk_buff *skb, struct ehea_cqe *cqe)
340 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
342 skb_put(skb, length);
343 skb->ip_summed = CHECKSUM_UNNECESSARY;
344 skb->protocol = eth_type_trans(skb, dev);
347 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
348 int arr_len,
349 struct ehea_cqe *cqe)
351 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
352 struct sk_buff *skb;
353 void *pref;
354 int x;
356 x = skb_index + 1;
357 x &= (arr_len - 1);
359 pref = skb_array[x];
360 prefetchw(pref);
361 prefetchw(pref + EHEA_CACHE_LINE);
363 pref = (skb_array[x]->data);
364 prefetch(pref);
365 prefetch(pref + EHEA_CACHE_LINE);
366 prefetch(pref + EHEA_CACHE_LINE * 2);
367 prefetch(pref + EHEA_CACHE_LINE * 3);
368 skb = skb_array[skb_index];
369 skb_array[skb_index] = NULL;
370 return skb;
373 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
374 int arr_len, int wqe_index)
376 struct sk_buff *skb;
377 void *pref;
378 int x;
380 x = wqe_index + 1;
381 x &= (arr_len - 1);
383 pref = skb_array[x];
384 prefetchw(pref);
385 prefetchw(pref + EHEA_CACHE_LINE);
387 pref = (skb_array[x]->data);
388 prefetchw(pref);
389 prefetchw(pref + EHEA_CACHE_LINE);
391 skb = skb_array[wqe_index];
392 skb_array[wqe_index] = NULL;
393 return skb;
396 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
397 struct ehea_cqe *cqe, int *processed_rq2,
398 int *processed_rq3)
400 struct sk_buff *skb;
402 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
403 pr->p_stats.err_tcp_cksum++;
404 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
405 pr->p_stats.err_ip_cksum++;
406 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
407 pr->p_stats.err_frame_crc++;
409 if (netif_msg_rx_err(pr->port)) {
410 ehea_error("CQE Error for QP %d", pr->qp->init_attr.qp_nr);
411 ehea_dump(cqe, sizeof(*cqe), "CQE");
414 if (rq == 2) {
415 *processed_rq2 += 1;
416 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
417 dev_kfree_skb(skb);
418 } else if (rq == 3) {
419 *processed_rq3 += 1;
420 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
421 dev_kfree_skb(skb);
424 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
425 ehea_error("Critical receive error. Resetting port.");
426 schedule_work(&pr->port->reset_task);
427 return 1;
430 return 0;
433 static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
434 void **tcph, u64 *hdr_flags, void *priv)
436 struct ehea_cqe *cqe = priv;
437 unsigned int ip_len;
438 struct iphdr *iph;
440 /* non tcp/udp packets */
441 if (!cqe->header_length)
442 return -1;
444 /* non tcp packet */
445 skb_reset_network_header(skb);
446 iph = ip_hdr(skb);
447 if (iph->protocol != IPPROTO_TCP)
448 return -1;
450 ip_len = ip_hdrlen(skb);
451 skb_set_transport_header(skb, ip_len);
452 *tcph = tcp_hdr(skb);
454 /* check if ip header and tcp header are complete */
455 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
456 return -1;
458 *hdr_flags = LRO_IPV4 | LRO_TCP;
459 *iphdr = iph;
461 return 0;
464 static void ehea_proc_skb(struct ehea_port_res *pr, struct ehea_cqe *cqe,
465 struct sk_buff *skb)
467 int vlan_extracted = (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
468 && pr->port->vgrp;
470 if (use_lro) {
471 if (vlan_extracted)
472 lro_vlan_hwaccel_receive_skb(&pr->lro_mgr, skb,
473 pr->port->vgrp,
474 cqe->vlan_tag,
475 cqe);
476 else
477 lro_receive_skb(&pr->lro_mgr, skb, cqe);
478 } else {
479 if (vlan_extracted)
480 vlan_hwaccel_receive_skb(skb, pr->port->vgrp,
481 cqe->vlan_tag);
482 else
483 netif_receive_skb(skb);
487 static int ehea_proc_rwqes(struct net_device *dev,
488 struct ehea_port_res *pr,
489 int budget)
491 struct ehea_port *port = pr->port;
492 struct ehea_qp *qp = pr->qp;
493 struct ehea_cqe *cqe;
494 struct sk_buff *skb;
495 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
496 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
497 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
498 int skb_arr_rq1_len = pr->rq1_skba.len;
499 int skb_arr_rq2_len = pr->rq2_skba.len;
500 int skb_arr_rq3_len = pr->rq3_skba.len;
501 int processed, processed_rq1, processed_rq2, processed_rq3;
502 int wqe_index, last_wqe_index, rq, port_reset;
504 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
505 last_wqe_index = 0;
507 cqe = ehea_poll_rq1(qp, &wqe_index);
508 while ((processed < budget) && cqe) {
509 ehea_inc_rq1(qp);
510 processed_rq1++;
511 processed++;
512 if (netif_msg_rx_status(port))
513 ehea_dump(cqe, sizeof(*cqe), "CQE");
515 last_wqe_index = wqe_index;
516 rmb();
517 if (!ehea_check_cqe(cqe, &rq)) {
518 if (rq == 1) { /* LL RQ1 */
519 skb = get_skb_by_index_ll(skb_arr_rq1,
520 skb_arr_rq1_len,
521 wqe_index);
522 if (unlikely(!skb)) {
523 if (netif_msg_rx_err(port))
524 ehea_error("LL rq1: skb=NULL");
526 skb = netdev_alloc_skb(dev,
527 EHEA_L_PKT_SIZE);
528 if (!skb)
529 break;
531 skb_copy_to_linear_data(skb, ((char*)cqe) + 64,
532 cqe->num_bytes_transfered - 4);
533 ehea_fill_skb(dev, skb, cqe);
534 } else if (rq == 2) { /* RQ2 */
535 skb = get_skb_by_index(skb_arr_rq2,
536 skb_arr_rq2_len, cqe);
537 if (unlikely(!skb)) {
538 if (netif_msg_rx_err(port))
539 ehea_error("rq2: skb=NULL");
540 break;
542 ehea_fill_skb(dev, skb, cqe);
543 processed_rq2++;
544 } else { /* RQ3 */
545 skb = get_skb_by_index(skb_arr_rq3,
546 skb_arr_rq3_len, cqe);
547 if (unlikely(!skb)) {
548 if (netif_msg_rx_err(port))
549 ehea_error("rq3: skb=NULL");
550 break;
552 ehea_fill_skb(dev, skb, cqe);
553 processed_rq3++;
556 ehea_proc_skb(pr, cqe, skb);
557 dev->last_rx = jiffies;
558 } else {
559 pr->p_stats.poll_receive_errors++;
560 port_reset = ehea_treat_poll_error(pr, rq, cqe,
561 &processed_rq2,
562 &processed_rq3);
563 if (port_reset)
564 break;
566 cqe = ehea_poll_rq1(qp, &wqe_index);
568 if (use_lro)
569 lro_flush_all(&pr->lro_mgr);
571 pr->rx_packets += processed;
573 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
574 ehea_refill_rq2(pr, processed_rq2);
575 ehea_refill_rq3(pr, processed_rq3);
577 return processed;
580 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
582 struct sk_buff *skb;
583 struct ehea_cq *send_cq = pr->send_cq;
584 struct ehea_cqe *cqe;
585 int quota = my_quota;
586 int cqe_counter = 0;
587 int swqe_av = 0;
588 int index;
589 unsigned long flags;
591 cqe = ehea_poll_cq(send_cq);
592 while(cqe && (quota > 0)) {
593 ehea_inc_cq(send_cq);
595 cqe_counter++;
596 rmb();
597 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
598 ehea_error("Send Completion Error: Resetting port");
599 if (netif_msg_tx_err(pr->port))
600 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
601 schedule_work(&pr->port->reset_task);
602 break;
605 if (netif_msg_tx_done(pr->port))
606 ehea_dump(cqe, sizeof(*cqe), "CQE");
608 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
609 == EHEA_SWQE2_TYPE)) {
611 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
612 skb = pr->sq_skba.arr[index];
613 dev_kfree_skb(skb);
614 pr->sq_skba.arr[index] = NULL;
617 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
618 quota--;
620 cqe = ehea_poll_cq(send_cq);
623 ehea_update_feca(send_cq, cqe_counter);
624 atomic_add(swqe_av, &pr->swqe_avail);
626 spin_lock_irqsave(&pr->netif_queue, flags);
628 if (pr->queue_stopped && (atomic_read(&pr->swqe_avail)
629 >= pr->swqe_refill_th)) {
630 netif_wake_queue(pr->port->netdev);
631 pr->queue_stopped = 0;
633 spin_unlock_irqrestore(&pr->netif_queue, flags);
635 return cqe;
638 #define EHEA_NAPI_POLL_NUM_BEFORE_IRQ 16
639 #define EHEA_POLL_MAX_CQES 65535
641 static int ehea_poll(struct napi_struct *napi, int budget)
643 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res, napi);
644 struct net_device *dev = pr->port->netdev;
645 struct ehea_cqe *cqe;
646 struct ehea_cqe *cqe_skb = NULL;
647 int force_irq, wqe_index;
648 int rx = 0;
650 force_irq = (pr->poll_counter > EHEA_NAPI_POLL_NUM_BEFORE_IRQ);
651 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
653 if (!force_irq)
654 rx += ehea_proc_rwqes(dev, pr, budget - rx);
656 while ((rx != budget) || force_irq) {
657 pr->poll_counter = 0;
658 force_irq = 0;
659 netif_rx_complete(dev, napi);
660 ehea_reset_cq_ep(pr->recv_cq);
661 ehea_reset_cq_ep(pr->send_cq);
662 ehea_reset_cq_n1(pr->recv_cq);
663 ehea_reset_cq_n1(pr->send_cq);
664 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
665 cqe_skb = ehea_poll_cq(pr->send_cq);
667 if (!cqe && !cqe_skb)
668 return rx;
670 if (!netif_rx_reschedule(dev, napi))
671 return rx;
673 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
674 rx += ehea_proc_rwqes(dev, pr, budget - rx);
677 pr->poll_counter++;
678 return rx;
681 #ifdef CONFIG_NET_POLL_CONTROLLER
682 static void ehea_netpoll(struct net_device *dev)
684 struct ehea_port *port = netdev_priv(dev);
685 int i;
687 for (i = 0; i < port->num_def_qps; i++)
688 netif_rx_schedule(dev, &port->port_res[i].napi);
690 #endif
692 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
694 struct ehea_port_res *pr = param;
696 netif_rx_schedule(pr->port->netdev, &pr->napi);
698 return IRQ_HANDLED;
701 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
703 struct ehea_port *port = param;
704 struct ehea_eqe *eqe;
705 struct ehea_qp *qp;
706 u32 qp_token;
708 eqe = ehea_poll_eq(port->qp_eq);
710 while (eqe) {
711 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
712 ehea_error("QP aff_err: entry=0x%lx, token=0x%x",
713 eqe->entry, qp_token);
715 qp = port->port_res[qp_token].qp;
716 ehea_error_data(port->adapter, qp->fw_handle);
717 eqe = ehea_poll_eq(port->qp_eq);
720 schedule_work(&port->reset_task);
722 return IRQ_HANDLED;
725 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
726 int logical_port)
728 int i;
730 for (i = 0; i < EHEA_MAX_PORTS; i++)
731 if (adapter->port[i])
732 if (adapter->port[i]->logical_port_id == logical_port)
733 return adapter->port[i];
734 return NULL;
737 int ehea_sense_port_attr(struct ehea_port *port)
739 int ret;
740 u64 hret;
741 struct hcp_ehea_port_cb0 *cb0;
743 cb0 = kzalloc(PAGE_SIZE, GFP_ATOMIC); /* May be called via */
744 if (!cb0) { /* ehea_neq_tasklet() */
745 ehea_error("no mem for cb0");
746 ret = -ENOMEM;
747 goto out;
750 hret = ehea_h_query_ehea_port(port->adapter->handle,
751 port->logical_port_id, H_PORT_CB0,
752 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
753 cb0);
754 if (hret != H_SUCCESS) {
755 ret = -EIO;
756 goto out_free;
759 /* MAC address */
760 port->mac_addr = cb0->port_mac_addr << 16;
762 if (!is_valid_ether_addr((u8*)&port->mac_addr)) {
763 ret = -EADDRNOTAVAIL;
764 goto out_free;
767 /* Port speed */
768 switch (cb0->port_speed) {
769 case H_SPEED_10M_H:
770 port->port_speed = EHEA_SPEED_10M;
771 port->full_duplex = 0;
772 break;
773 case H_SPEED_10M_F:
774 port->port_speed = EHEA_SPEED_10M;
775 port->full_duplex = 1;
776 break;
777 case H_SPEED_100M_H:
778 port->port_speed = EHEA_SPEED_100M;
779 port->full_duplex = 0;
780 break;
781 case H_SPEED_100M_F:
782 port->port_speed = EHEA_SPEED_100M;
783 port->full_duplex = 1;
784 break;
785 case H_SPEED_1G_F:
786 port->port_speed = EHEA_SPEED_1G;
787 port->full_duplex = 1;
788 break;
789 case H_SPEED_10G_F:
790 port->port_speed = EHEA_SPEED_10G;
791 port->full_duplex = 1;
792 break;
793 default:
794 port->port_speed = 0;
795 port->full_duplex = 0;
796 break;
799 port->autoneg = 1;
800 port->num_mcs = cb0->num_default_qps;
802 /* Number of default QPs */
803 if (use_mcs)
804 port->num_def_qps = cb0->num_default_qps;
805 else
806 port->num_def_qps = 1;
808 if (!port->num_def_qps) {
809 ret = -EINVAL;
810 goto out_free;
813 port->num_tx_qps = num_tx_qps;
815 if (port->num_def_qps >= port->num_tx_qps)
816 port->num_add_tx_qps = 0;
817 else
818 port->num_add_tx_qps = port->num_tx_qps - port->num_def_qps;
820 ret = 0;
821 out_free:
822 if (ret || netif_msg_probe(port))
823 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
824 kfree(cb0);
825 out:
826 return ret;
829 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
831 struct hcp_ehea_port_cb4 *cb4;
832 u64 hret;
833 int ret = 0;
835 cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
836 if (!cb4) {
837 ehea_error("no mem for cb4");
838 ret = -ENOMEM;
839 goto out;
842 cb4->port_speed = port_speed;
844 netif_carrier_off(port->netdev);
846 hret = ehea_h_modify_ehea_port(port->adapter->handle,
847 port->logical_port_id,
848 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
849 if (hret == H_SUCCESS) {
850 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
852 hret = ehea_h_query_ehea_port(port->adapter->handle,
853 port->logical_port_id,
854 H_PORT_CB4, H_PORT_CB4_SPEED,
855 cb4);
856 if (hret == H_SUCCESS) {
857 switch (cb4->port_speed) {
858 case H_SPEED_10M_H:
859 port->port_speed = EHEA_SPEED_10M;
860 port->full_duplex = 0;
861 break;
862 case H_SPEED_10M_F:
863 port->port_speed = EHEA_SPEED_10M;
864 port->full_duplex = 1;
865 break;
866 case H_SPEED_100M_H:
867 port->port_speed = EHEA_SPEED_100M;
868 port->full_duplex = 0;
869 break;
870 case H_SPEED_100M_F:
871 port->port_speed = EHEA_SPEED_100M;
872 port->full_duplex = 1;
873 break;
874 case H_SPEED_1G_F:
875 port->port_speed = EHEA_SPEED_1G;
876 port->full_duplex = 1;
877 break;
878 case H_SPEED_10G_F:
879 port->port_speed = EHEA_SPEED_10G;
880 port->full_duplex = 1;
881 break;
882 default:
883 port->port_speed = 0;
884 port->full_duplex = 0;
885 break;
887 } else {
888 ehea_error("Failed sensing port speed");
889 ret = -EIO;
891 } else {
892 if (hret == H_AUTHORITY) {
893 ehea_info("Hypervisor denied setting port speed");
894 ret = -EPERM;
895 } else {
896 ret = -EIO;
897 ehea_error("Failed setting port speed");
900 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
901 netif_carrier_on(port->netdev);
903 kfree(cb4);
904 out:
905 return ret;
908 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
910 int ret;
911 u8 ec;
912 u8 portnum;
913 struct ehea_port *port;
915 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
916 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
917 port = ehea_get_port(adapter, portnum);
919 switch (ec) {
920 case EHEA_EC_PORTSTATE_CHG: /* port state change */
922 if (!port) {
923 ehea_error("unknown portnum %x", portnum);
924 break;
927 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
928 if (!netif_carrier_ok(port->netdev)) {
929 ret = ehea_sense_port_attr(port);
930 if (ret) {
931 ehea_error("failed resensing port "
932 "attributes");
933 break;
936 if (netif_msg_link(port))
937 ehea_info("%s: Logical port up: %dMbps "
938 "%s Duplex",
939 port->netdev->name,
940 port->port_speed,
941 port->full_duplex ==
942 1 ? "Full" : "Half");
944 netif_carrier_on(port->netdev);
945 netif_wake_queue(port->netdev);
947 } else
948 if (netif_carrier_ok(port->netdev)) {
949 if (netif_msg_link(port))
950 ehea_info("%s: Logical port down",
951 port->netdev->name);
952 netif_carrier_off(port->netdev);
953 netif_stop_queue(port->netdev);
956 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
957 port->phy_link = EHEA_PHY_LINK_UP;
958 if (netif_msg_link(port))
959 ehea_info("%s: Physical port up",
960 port->netdev->name);
961 if (prop_carrier_state)
962 netif_carrier_on(port->netdev);
963 } else {
964 port->phy_link = EHEA_PHY_LINK_DOWN;
965 if (netif_msg_link(port))
966 ehea_info("%s: Physical port down",
967 port->netdev->name);
968 if (prop_carrier_state)
969 netif_carrier_off(port->netdev);
972 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
973 ehea_info("External switch port is primary port");
974 else
975 ehea_info("External switch port is backup port");
977 break;
978 case EHEA_EC_ADAPTER_MALFUNC:
979 ehea_error("Adapter malfunction");
980 break;
981 case EHEA_EC_PORT_MALFUNC:
982 ehea_info("Port malfunction: Device: %s", port->netdev->name);
983 netif_carrier_off(port->netdev);
984 netif_stop_queue(port->netdev);
985 break;
986 default:
987 ehea_error("unknown event code %x, eqe=0x%lX", ec, eqe);
988 break;
992 static void ehea_neq_tasklet(unsigned long data)
994 struct ehea_adapter *adapter = (struct ehea_adapter*)data;
995 struct ehea_eqe *eqe;
996 u64 event_mask;
998 eqe = ehea_poll_eq(adapter->neq);
999 ehea_debug("eqe=%p", eqe);
1001 while (eqe) {
1002 ehea_debug("*eqe=%lx", eqe->entry);
1003 ehea_parse_eqe(adapter, eqe->entry);
1004 eqe = ehea_poll_eq(adapter->neq);
1005 ehea_debug("next eqe=%p", eqe);
1008 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1009 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1010 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1012 ehea_h_reset_events(adapter->handle,
1013 adapter->neq->fw_handle, event_mask);
1016 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1018 struct ehea_adapter *adapter = param;
1019 tasklet_hi_schedule(&adapter->neq_tasklet);
1020 return IRQ_HANDLED;
1024 static int ehea_fill_port_res(struct ehea_port_res *pr)
1026 int ret;
1027 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1029 ret = ehea_init_fill_rq1(pr, init_attr->act_nr_rwqes_rq1
1030 - init_attr->act_nr_rwqes_rq2
1031 - init_attr->act_nr_rwqes_rq3 - 1);
1033 ret |= ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1035 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1037 return ret;
1040 static int ehea_reg_interrupts(struct net_device *dev)
1042 struct ehea_port *port = netdev_priv(dev);
1043 struct ehea_port_res *pr;
1044 int i, ret;
1047 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1048 dev->name);
1050 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1051 ehea_qp_aff_irq_handler,
1052 IRQF_DISABLED, port->int_aff_name, port);
1053 if (ret) {
1054 ehea_error("failed registering irq for qp_aff_irq_handler:"
1055 "ist=%X", port->qp_eq->attr.ist1);
1056 goto out_free_qpeq;
1059 if (netif_msg_ifup(port))
1060 ehea_info("irq_handle 0x%X for function qp_aff_irq_handler "
1061 "registered", port->qp_eq->attr.ist1);
1064 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1065 pr = &port->port_res[i];
1066 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1067 "%s-queue%d", dev->name, i);
1068 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1069 ehea_recv_irq_handler,
1070 IRQF_DISABLED, pr->int_send_name,
1071 pr);
1072 if (ret) {
1073 ehea_error("failed registering irq for ehea_queue "
1074 "port_res_nr:%d, ist=%X", i,
1075 pr->eq->attr.ist1);
1076 goto out_free_req;
1078 if (netif_msg_ifup(port))
1079 ehea_info("irq_handle 0x%X for function ehea_queue_int "
1080 "%d registered", pr->eq->attr.ist1, i);
1082 out:
1083 return ret;
1086 out_free_req:
1087 while (--i >= 0) {
1088 u32 ist = port->port_res[i].eq->attr.ist1;
1089 ibmebus_free_irq(ist, &port->port_res[i]);
1092 out_free_qpeq:
1093 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1094 i = port->num_def_qps;
1096 goto out;
1100 static void ehea_free_interrupts(struct net_device *dev)
1102 struct ehea_port *port = netdev_priv(dev);
1103 struct ehea_port_res *pr;
1104 int i;
1106 /* send */
1108 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1109 pr = &port->port_res[i];
1110 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1111 if (netif_msg_intr(port))
1112 ehea_info("free send irq for res %d with handle 0x%X",
1113 i, pr->eq->attr.ist1);
1116 /* associated events */
1117 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1118 if (netif_msg_intr(port))
1119 ehea_info("associated event interrupt for handle 0x%X freed",
1120 port->qp_eq->attr.ist1);
1123 static int ehea_configure_port(struct ehea_port *port)
1125 int ret, i;
1126 u64 hret, mask;
1127 struct hcp_ehea_port_cb0 *cb0;
1129 ret = -ENOMEM;
1130 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1131 if (!cb0)
1132 goto out;
1134 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1135 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1136 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1137 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1138 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1139 PXLY_RC_VLAN_FILTER)
1140 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1142 for (i = 0; i < port->num_mcs; i++)
1143 if (use_mcs)
1144 cb0->default_qpn_arr[i] =
1145 port->port_res[i].qp->init_attr.qp_nr;
1146 else
1147 cb0->default_qpn_arr[i] =
1148 port->port_res[0].qp->init_attr.qp_nr;
1150 if (netif_msg_ifup(port))
1151 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1153 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1154 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1156 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1157 port->logical_port_id,
1158 H_PORT_CB0, mask, cb0);
1159 ret = -EIO;
1160 if (hret != H_SUCCESS)
1161 goto out_free;
1163 ret = 0;
1165 out_free:
1166 kfree(cb0);
1167 out:
1168 return ret;
1171 int ehea_gen_smrs(struct ehea_port_res *pr)
1173 int ret;
1174 struct ehea_adapter *adapter = pr->port->adapter;
1176 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1177 if (ret)
1178 goto out;
1180 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1181 if (ret)
1182 goto out_free;
1184 return 0;
1186 out_free:
1187 ehea_rem_mr(&pr->send_mr);
1188 out:
1189 ehea_error("Generating SMRS failed\n");
1190 return -EIO;
1193 int ehea_rem_smrs(struct ehea_port_res *pr)
1195 if ((ehea_rem_mr(&pr->send_mr))
1196 || (ehea_rem_mr(&pr->recv_mr)))
1197 return -EIO;
1198 else
1199 return 0;
1202 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1204 int arr_size = sizeof(void*) * max_q_entries;
1206 q_skba->arr = vmalloc(arr_size);
1207 if (!q_skba->arr)
1208 return -ENOMEM;
1210 memset(q_skba->arr, 0, arr_size);
1212 q_skba->len = max_q_entries;
1213 q_skba->index = 0;
1214 q_skba->os_skbs = 0;
1216 return 0;
1219 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1220 struct port_res_cfg *pr_cfg, int queue_token)
1222 struct ehea_adapter *adapter = port->adapter;
1223 enum ehea_eq_type eq_type = EHEA_EQ;
1224 struct ehea_qp_init_attr *init_attr = NULL;
1225 int ret = -EIO;
1227 memset(pr, 0, sizeof(struct ehea_port_res));
1229 pr->port = port;
1230 spin_lock_init(&pr->xmit_lock);
1231 spin_lock_init(&pr->netif_queue);
1233 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1234 if (!pr->eq) {
1235 ehea_error("create_eq failed (eq)");
1236 goto out_free;
1239 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1240 pr->eq->fw_handle,
1241 port->logical_port_id);
1242 if (!pr->recv_cq) {
1243 ehea_error("create_cq failed (cq_recv)");
1244 goto out_free;
1247 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1248 pr->eq->fw_handle,
1249 port->logical_port_id);
1250 if (!pr->send_cq) {
1251 ehea_error("create_cq failed (cq_send)");
1252 goto out_free;
1255 if (netif_msg_ifup(port))
1256 ehea_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d",
1257 pr->send_cq->attr.act_nr_of_cqes,
1258 pr->recv_cq->attr.act_nr_of_cqes);
1260 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1261 if (!init_attr) {
1262 ret = -ENOMEM;
1263 ehea_error("no mem for ehea_qp_init_attr");
1264 goto out_free;
1267 init_attr->low_lat_rq1 = 1;
1268 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1269 init_attr->rq_count = 3;
1270 init_attr->qp_token = queue_token;
1271 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1272 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1273 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1274 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1275 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1276 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1277 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1278 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1279 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1280 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1281 init_attr->port_nr = port->logical_port_id;
1282 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1283 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1284 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1286 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1287 if (!pr->qp) {
1288 ehea_error("create_qp failed");
1289 ret = -EIO;
1290 goto out_free;
1293 if (netif_msg_ifup(port))
1294 ehea_info("QP: qp_nr=%d\n act_nr_snd_wqe=%d\n nr_rwqe_rq1=%d\n "
1295 "nr_rwqe_rq2=%d\n nr_rwqe_rq3=%d", init_attr->qp_nr,
1296 init_attr->act_nr_send_wqes,
1297 init_attr->act_nr_rwqes_rq1,
1298 init_attr->act_nr_rwqes_rq2,
1299 init_attr->act_nr_rwqes_rq3);
1301 ret = ehea_init_q_skba(&pr->sq_skba, init_attr->act_nr_send_wqes + 1);
1302 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1303 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1304 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1305 if (ret)
1306 goto out_free;
1308 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1309 if (ehea_gen_smrs(pr) != 0) {
1310 ret = -EIO;
1311 goto out_free;
1314 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1316 kfree(init_attr);
1318 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1320 pr->lro_mgr.max_aggr = pr->port->lro_max_aggr;
1321 pr->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1322 pr->lro_mgr.lro_arr = pr->lro_desc;
1323 pr->lro_mgr.get_skb_header = get_skb_hdr;
1324 pr->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1325 pr->lro_mgr.dev = port->netdev;
1326 pr->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1327 pr->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1329 ret = 0;
1330 goto out;
1332 out_free:
1333 kfree(init_attr);
1334 vfree(pr->sq_skba.arr);
1335 vfree(pr->rq1_skba.arr);
1336 vfree(pr->rq2_skba.arr);
1337 vfree(pr->rq3_skba.arr);
1338 ehea_destroy_qp(pr->qp);
1339 ehea_destroy_cq(pr->send_cq);
1340 ehea_destroy_cq(pr->recv_cq);
1341 ehea_destroy_eq(pr->eq);
1342 out:
1343 return ret;
1346 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1348 int ret, i;
1350 ret = ehea_destroy_qp(pr->qp);
1352 if (!ret) {
1353 ehea_destroy_cq(pr->send_cq);
1354 ehea_destroy_cq(pr->recv_cq);
1355 ehea_destroy_eq(pr->eq);
1357 for (i = 0; i < pr->rq1_skba.len; i++)
1358 if (pr->rq1_skba.arr[i])
1359 dev_kfree_skb(pr->rq1_skba.arr[i]);
1361 for (i = 0; i < pr->rq2_skba.len; i++)
1362 if (pr->rq2_skba.arr[i])
1363 dev_kfree_skb(pr->rq2_skba.arr[i]);
1365 for (i = 0; i < pr->rq3_skba.len; i++)
1366 if (pr->rq3_skba.arr[i])
1367 dev_kfree_skb(pr->rq3_skba.arr[i]);
1369 for (i = 0; i < pr->sq_skba.len; i++)
1370 if (pr->sq_skba.arr[i])
1371 dev_kfree_skb(pr->sq_skba.arr[i]);
1373 vfree(pr->rq1_skba.arr);
1374 vfree(pr->rq2_skba.arr);
1375 vfree(pr->rq3_skba.arr);
1376 vfree(pr->sq_skba.arr);
1377 ret = ehea_rem_smrs(pr);
1379 return ret;
1383 * The write_* functions store information in swqe which is used by
1384 * the hardware to calculate the ip/tcp/udp checksum
1387 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1388 const struct sk_buff *skb)
1390 swqe->ip_start = skb_network_offset(skb);
1391 swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1394 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1395 const struct sk_buff *skb)
1397 swqe->tcp_offset =
1398 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1400 swqe->tcp_end = (u16)skb->len - 1;
1403 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1404 const struct sk_buff *skb)
1406 swqe->tcp_offset =
1407 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1409 swqe->tcp_end = (u16)skb->len - 1;
1413 static void write_swqe2_TSO(struct sk_buff *skb,
1414 struct ehea_swqe *swqe, u32 lkey)
1416 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1417 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1418 int skb_data_size = skb->len - skb->data_len;
1419 int headersize;
1421 /* Packet is TCP with TSO enabled */
1422 swqe->tx_control |= EHEA_SWQE_TSO;
1423 swqe->mss = skb_shinfo(skb)->gso_size;
1424 /* copy only eth/ip/tcp headers to immediate data and
1425 * the rest of skb->data to sg1entry
1427 headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1429 skb_data_size = skb->len - skb->data_len;
1431 if (skb_data_size >= headersize) {
1432 /* copy immediate data */
1433 skb_copy_from_linear_data(skb, imm_data, headersize);
1434 swqe->immediate_data_length = headersize;
1436 if (skb_data_size > headersize) {
1437 /* set sg1entry data */
1438 sg1entry->l_key = lkey;
1439 sg1entry->len = skb_data_size - headersize;
1440 sg1entry->vaddr =
1441 ehea_map_vaddr(skb->data + headersize);
1442 swqe->descriptors++;
1444 } else
1445 ehea_error("cannot handle fragmented headers");
1448 static void write_swqe2_nonTSO(struct sk_buff *skb,
1449 struct ehea_swqe *swqe, u32 lkey)
1451 int skb_data_size = skb->len - skb->data_len;
1452 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1453 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1455 /* Packet is any nonTSO type
1457 * Copy as much as possible skb->data to immediate data and
1458 * the rest to sg1entry
1460 if (skb_data_size >= SWQE2_MAX_IMM) {
1461 /* copy immediate data */
1462 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1464 swqe->immediate_data_length = SWQE2_MAX_IMM;
1466 if (skb_data_size > SWQE2_MAX_IMM) {
1467 /* copy sg1entry data */
1468 sg1entry->l_key = lkey;
1469 sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1470 sg1entry->vaddr =
1471 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1472 swqe->descriptors++;
1474 } else {
1475 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1476 swqe->immediate_data_length = skb_data_size;
1480 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1481 struct ehea_swqe *swqe, u32 lkey)
1483 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1484 skb_frag_t *frag;
1485 int nfrags, sg1entry_contains_frag_data, i;
1487 nfrags = skb_shinfo(skb)->nr_frags;
1488 sg1entry = &swqe->u.immdata_desc.sg_entry;
1489 sg_list = (struct ehea_vsgentry*)&swqe->u.immdata_desc.sg_list;
1490 swqe->descriptors = 0;
1491 sg1entry_contains_frag_data = 0;
1493 if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1494 write_swqe2_TSO(skb, swqe, lkey);
1495 else
1496 write_swqe2_nonTSO(skb, swqe, lkey);
1498 /* write descriptors */
1499 if (nfrags > 0) {
1500 if (swqe->descriptors == 0) {
1501 /* sg1entry not yet used */
1502 frag = &skb_shinfo(skb)->frags[0];
1504 /* copy sg1entry data */
1505 sg1entry->l_key = lkey;
1506 sg1entry->len = frag->size;
1507 sg1entry->vaddr =
1508 ehea_map_vaddr(page_address(frag->page)
1509 + frag->page_offset);
1510 swqe->descriptors++;
1511 sg1entry_contains_frag_data = 1;
1514 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1516 frag = &skb_shinfo(skb)->frags[i];
1517 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1519 sgentry->l_key = lkey;
1520 sgentry->len = frag->size;
1521 sgentry->vaddr =
1522 ehea_map_vaddr(page_address(frag->page)
1523 + frag->page_offset);
1524 swqe->descriptors++;
1529 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1531 int ret = 0;
1532 u64 hret;
1533 u8 reg_type;
1535 /* De/Register untagged packets */
1536 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1537 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1538 port->logical_port_id,
1539 reg_type, port->mac_addr, 0, hcallid);
1540 if (hret != H_SUCCESS) {
1541 ehea_error("%sregistering bc address failed (tagged)",
1542 hcallid == H_REG_BCMC ? "" : "de");
1543 ret = -EIO;
1544 goto out_herr;
1547 /* De/Register VLAN packets */
1548 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1549 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1550 port->logical_port_id,
1551 reg_type, port->mac_addr, 0, hcallid);
1552 if (hret != H_SUCCESS) {
1553 ehea_error("%sregistering bc address failed (vlan)",
1554 hcallid == H_REG_BCMC ? "" : "de");
1555 ret = -EIO;
1557 out_herr:
1558 return ret;
1561 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1563 struct ehea_port *port = netdev_priv(dev);
1564 struct sockaddr *mac_addr = sa;
1565 struct hcp_ehea_port_cb0 *cb0;
1566 int ret;
1567 u64 hret;
1569 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1570 ret = -EADDRNOTAVAIL;
1571 goto out;
1574 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1575 if (!cb0) {
1576 ehea_error("no mem for cb0");
1577 ret = -ENOMEM;
1578 goto out;
1581 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1583 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1585 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1586 port->logical_port_id, H_PORT_CB0,
1587 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1588 if (hret != H_SUCCESS) {
1589 ret = -EIO;
1590 goto out_free;
1593 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1595 /* Deregister old MAC in pHYP */
1596 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1597 if (ret)
1598 goto out_free;
1600 port->mac_addr = cb0->port_mac_addr << 16;
1602 /* Register new MAC in pHYP */
1603 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1604 if (ret)
1605 goto out_free;
1607 ret = 0;
1608 out_free:
1609 kfree(cb0);
1610 out:
1611 return ret;
1614 static void ehea_promiscuous_error(u64 hret, int enable)
1616 if (hret == H_AUTHORITY)
1617 ehea_info("Hypervisor denied %sabling promiscuous mode",
1618 enable == 1 ? "en" : "dis");
1619 else
1620 ehea_error("failed %sabling promiscuous mode",
1621 enable == 1 ? "en" : "dis");
1624 static void ehea_promiscuous(struct net_device *dev, int enable)
1626 struct ehea_port *port = netdev_priv(dev);
1627 struct hcp_ehea_port_cb7 *cb7;
1628 u64 hret;
1630 if ((enable && port->promisc) || (!enable && !port->promisc))
1631 return;
1633 cb7 = kzalloc(PAGE_SIZE, GFP_ATOMIC);
1634 if (!cb7) {
1635 ehea_error("no mem for cb7");
1636 goto out;
1639 /* Modify Pxs_DUCQPN in CB7 */
1640 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1642 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1643 port->logical_port_id,
1644 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1645 if (hret) {
1646 ehea_promiscuous_error(hret, enable);
1647 goto out;
1650 port->promisc = enable;
1651 out:
1652 kfree(cb7);
1653 return;
1656 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1657 u32 hcallid)
1659 u64 hret;
1660 u8 reg_type;
1662 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1663 | EHEA_BCMC_UNTAGGED;
1665 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1666 port->logical_port_id,
1667 reg_type, mc_mac_addr, 0, hcallid);
1668 if (hret)
1669 goto out;
1671 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1672 | EHEA_BCMC_VLANID_ALL;
1674 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1675 port->logical_port_id,
1676 reg_type, mc_mac_addr, 0, hcallid);
1677 out:
1678 return hret;
1681 static int ehea_drop_multicast_list(struct net_device *dev)
1683 struct ehea_port *port = netdev_priv(dev);
1684 struct ehea_mc_list *mc_entry = port->mc_list;
1685 struct list_head *pos;
1686 struct list_head *temp;
1687 int ret = 0;
1688 u64 hret;
1690 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1691 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1693 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1694 H_DEREG_BCMC);
1695 if (hret) {
1696 ehea_error("failed deregistering mcast MAC");
1697 ret = -EIO;
1700 list_del(pos);
1701 kfree(mc_entry);
1703 return ret;
1706 static void ehea_allmulti(struct net_device *dev, int enable)
1708 struct ehea_port *port = netdev_priv(dev);
1709 u64 hret;
1711 if (!port->allmulti) {
1712 if (enable) {
1713 /* Enable ALLMULTI */
1714 ehea_drop_multicast_list(dev);
1715 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1716 if (!hret)
1717 port->allmulti = 1;
1718 else
1719 ehea_error("failed enabling IFF_ALLMULTI");
1721 } else
1722 if (!enable) {
1723 /* Disable ALLMULTI */
1724 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1725 if (!hret)
1726 port->allmulti = 0;
1727 else
1728 ehea_error("failed disabling IFF_ALLMULTI");
1732 static void ehea_add_multicast_entry(struct ehea_port* port, u8* mc_mac_addr)
1734 struct ehea_mc_list *ehea_mcl_entry;
1735 u64 hret;
1737 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1738 if (!ehea_mcl_entry) {
1739 ehea_error("no mem for mcl_entry");
1740 return;
1743 INIT_LIST_HEAD(&ehea_mcl_entry->list);
1745 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1747 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1748 H_REG_BCMC);
1749 if (!hret)
1750 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1751 else {
1752 ehea_error("failed registering mcast MAC");
1753 kfree(ehea_mcl_entry);
1757 static void ehea_set_multicast_list(struct net_device *dev)
1759 struct ehea_port *port = netdev_priv(dev);
1760 struct dev_mc_list *k_mcl_entry;
1761 int ret, i;
1763 if (dev->flags & IFF_PROMISC) {
1764 ehea_promiscuous(dev, 1);
1765 return;
1767 ehea_promiscuous(dev, 0);
1769 if (dev->flags & IFF_ALLMULTI) {
1770 ehea_allmulti(dev, 1);
1771 return;
1773 ehea_allmulti(dev, 0);
1775 if (dev->mc_count) {
1776 ret = ehea_drop_multicast_list(dev);
1777 if (ret) {
1778 /* Dropping the current multicast list failed.
1779 * Enabling ALL_MULTI is the best we can do.
1781 ehea_allmulti(dev, 1);
1784 if (dev->mc_count > port->adapter->max_mc_mac) {
1785 ehea_info("Mcast registration limit reached (0x%lx). "
1786 "Use ALLMULTI!",
1787 port->adapter->max_mc_mac);
1788 goto out;
1791 for (i = 0, k_mcl_entry = dev->mc_list;
1792 i < dev->mc_count;
1793 i++, k_mcl_entry = k_mcl_entry->next) {
1794 ehea_add_multicast_entry(port, k_mcl_entry->dmi_addr);
1797 out:
1798 return;
1801 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1803 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1804 return -EINVAL;
1805 dev->mtu = new_mtu;
1806 return 0;
1809 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
1810 struct ehea_swqe *swqe, u32 lkey)
1812 if (skb->protocol == htons(ETH_P_IP)) {
1813 const struct iphdr *iph = ip_hdr(skb);
1815 /* IPv4 */
1816 swqe->tx_control |= EHEA_SWQE_CRC
1817 | EHEA_SWQE_IP_CHECKSUM
1818 | EHEA_SWQE_TCP_CHECKSUM
1819 | EHEA_SWQE_IMM_DATA_PRESENT
1820 | EHEA_SWQE_DESCRIPTORS_PRESENT;
1822 write_ip_start_end(swqe, skb);
1824 if (iph->protocol == IPPROTO_UDP) {
1825 if ((iph->frag_off & IP_MF)
1826 || (iph->frag_off & IP_OFFSET))
1827 /* IP fragment, so don't change cs */
1828 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
1829 else
1830 write_udp_offset_end(swqe, skb);
1831 } else if (iph->protocol == IPPROTO_TCP) {
1832 write_tcp_offset_end(swqe, skb);
1835 /* icmp (big data) and ip segmentation packets (all other ip
1836 packets) do not require any special handling */
1838 } else {
1839 /* Other Ethernet Protocol */
1840 swqe->tx_control |= EHEA_SWQE_CRC
1841 | EHEA_SWQE_IMM_DATA_PRESENT
1842 | EHEA_SWQE_DESCRIPTORS_PRESENT;
1845 write_swqe2_data(skb, dev, swqe, lkey);
1848 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
1849 struct ehea_swqe *swqe)
1851 int nfrags = skb_shinfo(skb)->nr_frags;
1852 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
1853 skb_frag_t *frag;
1854 int i;
1856 if (skb->protocol == htons(ETH_P_IP)) {
1857 const struct iphdr *iph = ip_hdr(skb);
1859 /* IPv4 */
1860 write_ip_start_end(swqe, skb);
1862 if (iph->protocol == IPPROTO_TCP) {
1863 swqe->tx_control |= EHEA_SWQE_CRC
1864 | EHEA_SWQE_IP_CHECKSUM
1865 | EHEA_SWQE_TCP_CHECKSUM
1866 | EHEA_SWQE_IMM_DATA_PRESENT;
1868 write_tcp_offset_end(swqe, skb);
1870 } else if (iph->protocol == IPPROTO_UDP) {
1871 if ((iph->frag_off & IP_MF)
1872 || (iph->frag_off & IP_OFFSET))
1873 /* IP fragment, so don't change cs */
1874 swqe->tx_control |= EHEA_SWQE_CRC
1875 | EHEA_SWQE_IMM_DATA_PRESENT;
1876 else {
1877 swqe->tx_control |= EHEA_SWQE_CRC
1878 | EHEA_SWQE_IP_CHECKSUM
1879 | EHEA_SWQE_TCP_CHECKSUM
1880 | EHEA_SWQE_IMM_DATA_PRESENT;
1882 write_udp_offset_end(swqe, skb);
1884 } else {
1885 /* icmp (big data) and
1886 ip segmentation packets (all other ip packets) */
1887 swqe->tx_control |= EHEA_SWQE_CRC
1888 | EHEA_SWQE_IP_CHECKSUM
1889 | EHEA_SWQE_IMM_DATA_PRESENT;
1891 } else {
1892 /* Other Ethernet Protocol */
1893 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
1895 /* copy (immediate) data */
1896 if (nfrags == 0) {
1897 /* data is in a single piece */
1898 skb_copy_from_linear_data(skb, imm_data, skb->len);
1899 } else {
1900 /* first copy data from the skb->data buffer ... */
1901 skb_copy_from_linear_data(skb, imm_data,
1902 skb->len - skb->data_len);
1903 imm_data += skb->len - skb->data_len;
1905 /* ... then copy data from the fragments */
1906 for (i = 0; i < nfrags; i++) {
1907 frag = &skb_shinfo(skb)->frags[i];
1908 memcpy(imm_data,
1909 page_address(frag->page) + frag->page_offset,
1910 frag->size);
1911 imm_data += frag->size;
1914 swqe->immediate_data_length = skb->len;
1915 dev_kfree_skb(skb);
1918 static inline int ehea_hash_skb(struct sk_buff *skb, int num_qps)
1920 struct tcphdr *tcp;
1921 u32 tmp;
1923 if ((skb->protocol == htons(ETH_P_IP)) &&
1924 (ip_hdr(skb)->protocol == IPPROTO_TCP)) {
1925 tcp = (struct tcphdr*)(skb_network_header(skb) + (ip_hdr(skb)->ihl * 4));
1926 tmp = (tcp->source + (tcp->dest << 16)) % 31;
1927 tmp += ip_hdr(skb)->daddr % 31;
1928 return tmp % num_qps;
1930 else
1931 return 0;
1934 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
1936 struct ehea_port *port = netdev_priv(dev);
1937 struct ehea_swqe *swqe;
1938 unsigned long flags;
1939 u32 lkey;
1940 int swqe_index;
1941 struct ehea_port_res *pr;
1943 pr = &port->port_res[ehea_hash_skb(skb, port->num_tx_qps)];
1945 if (!spin_trylock(&pr->xmit_lock))
1946 return NETDEV_TX_BUSY;
1948 if (pr->queue_stopped) {
1949 spin_unlock(&pr->xmit_lock);
1950 return NETDEV_TX_BUSY;
1953 swqe = ehea_get_swqe(pr->qp, &swqe_index);
1954 memset(swqe, 0, SWQE_HEADER_SIZE);
1955 atomic_dec(&pr->swqe_avail);
1957 if (skb->len <= SWQE3_MAX_IMM) {
1958 u32 sig_iv = port->sig_comp_iv;
1959 u32 swqe_num = pr->swqe_id_counter;
1960 ehea_xmit3(skb, dev, swqe);
1961 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
1962 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
1963 if (pr->swqe_ll_count >= (sig_iv - 1)) {
1964 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
1965 sig_iv);
1966 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1967 pr->swqe_ll_count = 0;
1968 } else
1969 pr->swqe_ll_count += 1;
1970 } else {
1971 swqe->wr_id =
1972 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
1973 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
1974 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
1975 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
1976 pr->sq_skba.arr[pr->sq_skba.index] = skb;
1978 pr->sq_skba.index++;
1979 pr->sq_skba.index &= (pr->sq_skba.len - 1);
1981 lkey = pr->send_mr.lkey;
1982 ehea_xmit2(skb, dev, swqe, lkey);
1983 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1985 pr->swqe_id_counter += 1;
1987 if (port->vgrp && vlan_tx_tag_present(skb)) {
1988 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
1989 swqe->vlan_tag = vlan_tx_tag_get(skb);
1992 if (netif_msg_tx_queued(port)) {
1993 ehea_info("post swqe on QP %d", pr->qp->init_attr.qp_nr);
1994 ehea_dump(swqe, 512, "swqe");
1997 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
1998 netif_stop_queue(dev);
1999 swqe->tx_control |= EHEA_SWQE_PURGE;
2002 ehea_post_swqe(pr->qp, swqe);
2004 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2005 spin_lock_irqsave(&pr->netif_queue, flags);
2006 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2007 pr->p_stats.queue_stopped++;
2008 netif_stop_queue(dev);
2009 pr->queue_stopped = 1;
2011 spin_unlock_irqrestore(&pr->netif_queue, flags);
2013 dev->trans_start = jiffies;
2014 spin_unlock(&pr->xmit_lock);
2016 return NETDEV_TX_OK;
2019 static void ehea_vlan_rx_register(struct net_device *dev,
2020 struct vlan_group *grp)
2022 struct ehea_port *port = netdev_priv(dev);
2023 struct ehea_adapter *adapter = port->adapter;
2024 struct hcp_ehea_port_cb1 *cb1;
2025 u64 hret;
2027 port->vgrp = grp;
2029 cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2030 if (!cb1) {
2031 ehea_error("no mem for cb1");
2032 goto out;
2035 memset(cb1->vlan_filter, 0, sizeof(cb1->vlan_filter));
2037 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2038 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2039 if (hret != H_SUCCESS)
2040 ehea_error("modify_ehea_port failed");
2042 kfree(cb1);
2043 out:
2044 return;
2047 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
2049 struct ehea_port *port = netdev_priv(dev);
2050 struct ehea_adapter *adapter = port->adapter;
2051 struct hcp_ehea_port_cb1 *cb1;
2052 int index;
2053 u64 hret;
2055 cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2056 if (!cb1) {
2057 ehea_error("no mem for cb1");
2058 goto out;
2061 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2062 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2063 if (hret != H_SUCCESS) {
2064 ehea_error("query_ehea_port failed");
2065 goto out;
2068 index = (vid / 64);
2069 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2071 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2072 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2073 if (hret != H_SUCCESS)
2074 ehea_error("modify_ehea_port failed");
2075 out:
2076 kfree(cb1);
2077 return;
2080 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2082 struct ehea_port *port = netdev_priv(dev);
2083 struct ehea_adapter *adapter = port->adapter;
2084 struct hcp_ehea_port_cb1 *cb1;
2085 int index;
2086 u64 hret;
2088 vlan_group_set_device(port->vgrp, vid, NULL);
2090 cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2091 if (!cb1) {
2092 ehea_error("no mem for cb1");
2093 goto out;
2096 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2097 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2098 if (hret != H_SUCCESS) {
2099 ehea_error("query_ehea_port failed");
2100 goto out;
2103 index = (vid / 64);
2104 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2106 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2107 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2108 if (hret != H_SUCCESS)
2109 ehea_error("modify_ehea_port failed");
2110 out:
2111 kfree(cb1);
2112 return;
2115 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2117 int ret = -EIO;
2118 u64 hret;
2119 u16 dummy16 = 0;
2120 u64 dummy64 = 0;
2121 struct hcp_modify_qp_cb0* cb0;
2123 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2124 if (!cb0) {
2125 ret = -ENOMEM;
2126 goto out;
2129 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2130 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2131 if (hret != H_SUCCESS) {
2132 ehea_error("query_ehea_qp failed (1)");
2133 goto out;
2136 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2137 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2138 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2139 &dummy64, &dummy64, &dummy16, &dummy16);
2140 if (hret != H_SUCCESS) {
2141 ehea_error("modify_ehea_qp failed (1)");
2142 goto out;
2145 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2146 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2147 if (hret != H_SUCCESS) {
2148 ehea_error("query_ehea_qp failed (2)");
2149 goto out;
2152 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2153 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2154 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2155 &dummy64, &dummy64, &dummy16, &dummy16);
2156 if (hret != H_SUCCESS) {
2157 ehea_error("modify_ehea_qp failed (2)");
2158 goto out;
2161 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2162 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2163 if (hret != H_SUCCESS) {
2164 ehea_error("query_ehea_qp failed (3)");
2165 goto out;
2168 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2169 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2170 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2171 &dummy64, &dummy64, &dummy16, &dummy16);
2172 if (hret != H_SUCCESS) {
2173 ehea_error("modify_ehea_qp failed (3)");
2174 goto out;
2177 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2178 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2179 if (hret != H_SUCCESS) {
2180 ehea_error("query_ehea_qp failed (4)");
2181 goto out;
2184 ret = 0;
2185 out:
2186 kfree(cb0);
2187 return ret;
2190 static int ehea_port_res_setup(struct ehea_port *port, int def_qps,
2191 int add_tx_qps)
2193 int ret, i;
2194 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2195 enum ehea_eq_type eq_type = EHEA_EQ;
2197 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2198 EHEA_MAX_ENTRIES_EQ, 1);
2199 if (!port->qp_eq) {
2200 ret = -EINVAL;
2201 ehea_error("ehea_create_eq failed (qp_eq)");
2202 goto out_kill_eq;
2205 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2206 pr_cfg.max_entries_scq = sq_entries * 2;
2207 pr_cfg.max_entries_sq = sq_entries;
2208 pr_cfg.max_entries_rq1 = rq1_entries;
2209 pr_cfg.max_entries_rq2 = rq2_entries;
2210 pr_cfg.max_entries_rq3 = rq3_entries;
2212 pr_cfg_small_rx.max_entries_rcq = 1;
2213 pr_cfg_small_rx.max_entries_scq = sq_entries;
2214 pr_cfg_small_rx.max_entries_sq = sq_entries;
2215 pr_cfg_small_rx.max_entries_rq1 = 1;
2216 pr_cfg_small_rx.max_entries_rq2 = 1;
2217 pr_cfg_small_rx.max_entries_rq3 = 1;
2219 for (i = 0; i < def_qps; i++) {
2220 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2221 if (ret)
2222 goto out_clean_pr;
2224 for (i = def_qps; i < def_qps + add_tx_qps; i++) {
2225 ret = ehea_init_port_res(port, &port->port_res[i],
2226 &pr_cfg_small_rx, i);
2227 if (ret)
2228 goto out_clean_pr;
2231 return 0;
2233 out_clean_pr:
2234 while (--i >= 0)
2235 ehea_clean_portres(port, &port->port_res[i]);
2237 out_kill_eq:
2238 ehea_destroy_eq(port->qp_eq);
2239 return ret;
2242 static int ehea_clean_all_portres(struct ehea_port *port)
2244 int ret = 0;
2245 int i;
2247 for(i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2248 ret |= ehea_clean_portres(port, &port->port_res[i]);
2250 ret |= ehea_destroy_eq(port->qp_eq);
2252 return ret;
2255 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2257 if (adapter->active_ports)
2258 return;
2260 ehea_rem_mr(&adapter->mr);
2263 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2265 if (adapter->active_ports)
2266 return 0;
2268 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2271 static int ehea_up(struct net_device *dev)
2273 int ret, i;
2274 struct ehea_port *port = netdev_priv(dev);
2276 if (port->state == EHEA_PORT_UP)
2277 return 0;
2279 ret = ehea_port_res_setup(port, port->num_def_qps,
2280 port->num_add_tx_qps);
2281 if (ret) {
2282 ehea_error("port_res_failed");
2283 goto out;
2286 /* Set default QP for this port */
2287 ret = ehea_configure_port(port);
2288 if (ret) {
2289 ehea_error("ehea_configure_port failed. ret:%d", ret);
2290 goto out_clean_pr;
2293 ret = ehea_reg_interrupts(dev);
2294 if (ret) {
2295 ehea_error("reg_interrupts failed. ret:%d", ret);
2296 goto out_clean_pr;
2299 for(i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2300 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2301 if (ret) {
2302 ehea_error("activate_qp failed");
2303 goto out_free_irqs;
2307 for(i = 0; i < port->num_def_qps; i++) {
2308 ret = ehea_fill_port_res(&port->port_res[i]);
2309 if (ret) {
2310 ehea_error("out_free_irqs");
2311 goto out_free_irqs;
2315 ret = 0;
2316 port->state = EHEA_PORT_UP;
2317 goto out;
2319 out_free_irqs:
2320 ehea_free_interrupts(dev);
2322 out_clean_pr:
2323 ehea_clean_all_portres(port);
2324 out:
2325 if (ret)
2326 ehea_info("Failed starting %s. ret=%i", dev->name, ret);
2328 return ret;
2331 static void port_napi_disable(struct ehea_port *port)
2333 int i;
2335 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2336 napi_disable(&port->port_res[i].napi);
2339 static void port_napi_enable(struct ehea_port *port)
2341 int i;
2343 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2344 napi_enable(&port->port_res[i].napi);
2347 static int ehea_open(struct net_device *dev)
2349 int ret;
2350 struct ehea_port *port = netdev_priv(dev);
2352 down(&port->port_lock);
2354 if (netif_msg_ifup(port))
2355 ehea_info("enabling port %s", dev->name);
2357 ret = ehea_up(dev);
2358 if (!ret) {
2359 port_napi_enable(port);
2360 netif_start_queue(dev);
2363 up(&port->port_lock);
2365 return ret;
2368 static int ehea_down(struct net_device *dev)
2370 int ret;
2371 struct ehea_port *port = netdev_priv(dev);
2373 if (port->state == EHEA_PORT_DOWN)
2374 return 0;
2376 ehea_drop_multicast_list(dev);
2377 ehea_free_interrupts(dev);
2379 port->state = EHEA_PORT_DOWN;
2381 ret = ehea_clean_all_portres(port);
2382 if (ret)
2383 ehea_info("Failed freeing resources for %s. ret=%i",
2384 dev->name, ret);
2386 return ret;
2389 static int ehea_stop(struct net_device *dev)
2391 int ret;
2392 struct ehea_port *port = netdev_priv(dev);
2394 if (netif_msg_ifdown(port))
2395 ehea_info("disabling port %s", dev->name);
2397 flush_scheduled_work();
2398 down(&port->port_lock);
2399 netif_stop_queue(dev);
2400 port_napi_disable(port);
2401 ret = ehea_down(dev);
2402 up(&port->port_lock);
2403 return ret;
2406 void ehea_purge_sq(struct ehea_qp *orig_qp)
2408 struct ehea_qp qp = *orig_qp;
2409 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2410 struct ehea_swqe *swqe;
2411 int wqe_index;
2412 int i;
2414 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2415 swqe = ehea_get_swqe(&qp, &wqe_index);
2416 swqe->tx_control |= EHEA_SWQE_PURGE;
2420 int ehea_stop_qps(struct net_device *dev)
2422 struct ehea_port *port = netdev_priv(dev);
2423 struct ehea_adapter *adapter = port->adapter;
2424 struct hcp_modify_qp_cb0* cb0;
2425 int ret = -EIO;
2426 int dret;
2427 int i;
2428 u64 hret;
2429 u64 dummy64 = 0;
2430 u16 dummy16 = 0;
2432 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2433 if (!cb0) {
2434 ret = -ENOMEM;
2435 goto out;
2438 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2439 struct ehea_port_res *pr = &port->port_res[i];
2440 struct ehea_qp *qp = pr->qp;
2442 /* Purge send queue */
2443 ehea_purge_sq(qp);
2445 /* Disable queue pair */
2446 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2447 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2448 cb0);
2449 if (hret != H_SUCCESS) {
2450 ehea_error("query_ehea_qp failed (1)");
2451 goto out;
2454 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2455 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2457 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2458 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2459 1), cb0, &dummy64,
2460 &dummy64, &dummy16, &dummy16);
2461 if (hret != H_SUCCESS) {
2462 ehea_error("modify_ehea_qp failed (1)");
2463 goto out;
2466 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2467 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2468 cb0);
2469 if (hret != H_SUCCESS) {
2470 ehea_error("query_ehea_qp failed (2)");
2471 goto out;
2474 /* deregister shared memory regions */
2475 dret = ehea_rem_smrs(pr);
2476 if (dret) {
2477 ehea_error("unreg shared memory region failed");
2478 goto out;
2482 ret = 0;
2483 out:
2484 kfree(cb0);
2486 return ret;
2489 void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res * pr)
2491 struct ehea_qp qp = *orig_qp;
2492 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2493 struct ehea_rwqe *rwqe;
2494 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2495 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2496 struct sk_buff *skb;
2497 u32 lkey = pr->recv_mr.lkey;
2500 int i;
2501 int index;
2503 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2504 rwqe = ehea_get_next_rwqe(&qp, 2);
2505 rwqe->sg_list[0].l_key = lkey;
2506 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2507 skb = skba_rq2[index];
2508 if (skb)
2509 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2512 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2513 rwqe = ehea_get_next_rwqe(&qp, 3);
2514 rwqe->sg_list[0].l_key = lkey;
2515 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2516 skb = skba_rq3[index];
2517 if (skb)
2518 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2522 int ehea_restart_qps(struct net_device *dev)
2524 struct ehea_port *port = netdev_priv(dev);
2525 struct ehea_adapter *adapter = port->adapter;
2526 int ret = 0;
2527 int i;
2529 struct hcp_modify_qp_cb0* cb0;
2530 u64 hret;
2531 u64 dummy64 = 0;
2532 u16 dummy16 = 0;
2534 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2535 if (!cb0) {
2536 ret = -ENOMEM;
2537 goto out;
2540 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2541 struct ehea_port_res *pr = &port->port_res[i];
2542 struct ehea_qp *qp = pr->qp;
2544 ret = ehea_gen_smrs(pr);
2545 if (ret) {
2546 ehea_error("creation of shared memory regions failed");
2547 goto out;
2550 ehea_update_rqs(qp, pr);
2552 /* Enable queue pair */
2553 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2554 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2555 cb0);
2556 if (hret != H_SUCCESS) {
2557 ehea_error("query_ehea_qp failed (1)");
2558 goto out;
2561 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2562 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2564 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2565 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2566 1), cb0, &dummy64,
2567 &dummy64, &dummy16, &dummy16);
2568 if (hret != H_SUCCESS) {
2569 ehea_error("modify_ehea_qp failed (1)");
2570 goto out;
2573 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2574 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2575 cb0);
2576 if (hret != H_SUCCESS) {
2577 ehea_error("query_ehea_qp failed (2)");
2578 goto out;
2581 /* refill entire queue */
2582 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2583 ehea_refill_rq2(pr, 0);
2584 ehea_refill_rq3(pr, 0);
2586 out:
2587 kfree(cb0);
2589 return ret;
2592 static void ehea_reset_port(struct work_struct *work)
2594 int ret;
2595 struct ehea_port *port =
2596 container_of(work, struct ehea_port, reset_task);
2597 struct net_device *dev = port->netdev;
2599 port->resets++;
2600 down(&port->port_lock);
2601 netif_stop_queue(dev);
2603 port_napi_disable(port);
2605 ehea_down(dev);
2607 ret = ehea_up(dev);
2608 if (ret)
2609 goto out;
2611 ehea_set_multicast_list(dev);
2613 if (netif_msg_timer(port))
2614 ehea_info("Device %s resetted successfully", dev->name);
2616 port_napi_enable(port);
2618 netif_wake_queue(dev);
2619 out:
2620 up(&port->port_lock);
2621 return;
2624 static void ehea_rereg_mrs(struct work_struct *work)
2626 int ret, i;
2627 struct ehea_adapter *adapter;
2629 down(&dlpar_mem_lock);
2630 ehea_info("LPAR memory enlarged - re-initializing driver");
2632 list_for_each_entry(adapter, &adapter_list, list)
2633 if (adapter->active_ports) {
2634 /* Shutdown all ports */
2635 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2636 struct ehea_port *port = adapter->port[i];
2638 if (port) {
2639 struct net_device *dev = port->netdev;
2641 if (dev->flags & IFF_UP) {
2642 down(&port->port_lock);
2643 netif_stop_queue(dev);
2644 ret = ehea_stop_qps(dev);
2645 if (ret) {
2646 up(&port->port_lock);
2647 goto out;
2649 port_napi_disable(port);
2650 up(&port->port_lock);
2655 /* Unregister old memory region */
2656 ret = ehea_rem_mr(&adapter->mr);
2657 if (ret) {
2658 ehea_error("unregister MR failed - driver"
2659 " inoperable!");
2660 goto out;
2664 ehea_destroy_busmap();
2665 ret = ehea_create_busmap();
2666 if (ret) {
2667 ehea_error("creating ehea busmap failed");
2668 goto out;
2671 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2673 list_for_each_entry(adapter, &adapter_list, list)
2674 if (adapter->active_ports) {
2675 /* Register new memory region */
2676 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2677 if (ret) {
2678 ehea_error("register MR failed - driver"
2679 " inoperable!");
2680 goto out;
2683 /* Restart all ports */
2684 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2685 struct ehea_port *port = adapter->port[i];
2687 if (port) {
2688 struct net_device *dev = port->netdev;
2690 if (dev->flags & IFF_UP) {
2691 down(&port->port_lock);
2692 port_napi_enable(port);
2693 ret = ehea_restart_qps(dev);
2694 if (!ret)
2695 netif_wake_queue(dev);
2696 up(&port->port_lock);
2701 up(&dlpar_mem_lock);
2702 ehea_info("re-initializing driver complete");
2703 out:
2704 return;
2707 static void ehea_tx_watchdog(struct net_device *dev)
2709 struct ehea_port *port = netdev_priv(dev);
2711 if (netif_carrier_ok(dev) &&
2712 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2713 schedule_work(&port->reset_task);
2716 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2718 struct hcp_query_ehea *cb;
2719 u64 hret;
2720 int ret;
2722 cb = kzalloc(PAGE_SIZE, GFP_KERNEL);
2723 if (!cb) {
2724 ret = -ENOMEM;
2725 goto out;
2728 hret = ehea_h_query_ehea(adapter->handle, cb);
2730 if (hret != H_SUCCESS) {
2731 ret = -EIO;
2732 goto out_herr;
2735 adapter->max_mc_mac = cb->max_mc_mac - 1;
2736 ret = 0;
2738 out_herr:
2739 kfree(cb);
2740 out:
2741 return ret;
2744 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2746 struct hcp_ehea_port_cb4 *cb4;
2747 u64 hret;
2748 int ret = 0;
2750 *jumbo = 0;
2752 /* (Try to) enable *jumbo frames */
2753 cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2754 if (!cb4) {
2755 ehea_error("no mem for cb4");
2756 ret = -ENOMEM;
2757 goto out;
2758 } else {
2759 hret = ehea_h_query_ehea_port(port->adapter->handle,
2760 port->logical_port_id,
2761 H_PORT_CB4,
2762 H_PORT_CB4_JUMBO, cb4);
2763 if (hret == H_SUCCESS) {
2764 if (cb4->jumbo_frame)
2765 *jumbo = 1;
2766 else {
2767 cb4->jumbo_frame = 1;
2768 hret = ehea_h_modify_ehea_port(port->adapter->
2769 handle,
2770 port->
2771 logical_port_id,
2772 H_PORT_CB4,
2773 H_PORT_CB4_JUMBO,
2774 cb4);
2775 if (hret == H_SUCCESS)
2776 *jumbo = 1;
2778 } else
2779 ret = -EINVAL;
2781 kfree(cb4);
2783 out:
2784 return ret;
2787 static ssize_t ehea_show_port_id(struct device *dev,
2788 struct device_attribute *attr, char *buf)
2790 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2791 return sprintf(buf, "%d", port->logical_port_id);
2794 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2795 NULL);
2797 static void __devinit logical_port_release(struct device *dev)
2799 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2800 of_node_put(port->ofdev.node);
2803 static int ehea_driver_sysfs_add(struct device *dev,
2804 struct device_driver *driver)
2806 int ret;
2808 ret = sysfs_create_link(&driver->kobj, &dev->kobj,
2809 kobject_name(&dev->kobj));
2810 if (ret == 0) {
2811 ret = sysfs_create_link(&dev->kobj, &driver->kobj,
2812 "driver");
2813 if (ret)
2814 sysfs_remove_link(&driver->kobj,
2815 kobject_name(&dev->kobj));
2817 return ret;
2820 static void ehea_driver_sysfs_remove(struct device *dev,
2821 struct device_driver *driver)
2823 struct device_driver *drv = driver;
2825 if (drv) {
2826 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
2827 sysfs_remove_link(&dev->kobj, "driver");
2831 static struct device *ehea_register_port(struct ehea_port *port,
2832 struct device_node *dn)
2834 int ret;
2836 port->ofdev.node = of_node_get(dn);
2837 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
2838 port->ofdev.dev.bus = &ibmebus_bus_type;
2840 sprintf(port->ofdev.dev.bus_id, "port%d", port_name_cnt++);
2841 port->ofdev.dev.release = logical_port_release;
2843 ret = of_device_register(&port->ofdev);
2844 if (ret) {
2845 ehea_error("failed to register device. ret=%d", ret);
2846 goto out;
2849 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2850 if (ret) {
2851 ehea_error("failed to register attributes, ret=%d", ret);
2852 goto out_unreg_of_dev;
2855 ret = ehea_driver_sysfs_add(&port->ofdev.dev, &ehea_driver.driver);
2856 if (ret) {
2857 ehea_error("failed to register sysfs driver link");
2858 goto out_rem_dev_file;
2861 return &port->ofdev.dev;
2863 out_rem_dev_file:
2864 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2865 out_unreg_of_dev:
2866 of_device_unregister(&port->ofdev);
2867 out:
2868 return NULL;
2871 static void ehea_unregister_port(struct ehea_port *port)
2873 ehea_driver_sysfs_remove(&port->ofdev.dev, &ehea_driver.driver);
2874 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2875 of_device_unregister(&port->ofdev);
2878 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2879 u32 logical_port_id,
2880 struct device_node *dn)
2882 int ret;
2883 struct net_device *dev;
2884 struct ehea_port *port;
2885 struct device *port_dev;
2886 int jumbo;
2888 /* allocate memory for the port structures */
2889 dev = alloc_etherdev(sizeof(struct ehea_port));
2891 if (!dev) {
2892 ehea_error("no mem for net_device");
2893 ret = -ENOMEM;
2894 goto out_err;
2897 port = netdev_priv(dev);
2899 sema_init(&port->port_lock, 1);
2900 port->state = EHEA_PORT_DOWN;
2901 port->sig_comp_iv = sq_entries / 10;
2903 port->adapter = adapter;
2904 port->netdev = dev;
2905 port->logical_port_id = logical_port_id;
2907 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2909 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2910 if (!port->mc_list) {
2911 ret = -ENOMEM;
2912 goto out_free_ethdev;
2915 INIT_LIST_HEAD(&port->mc_list->list);
2917 ret = ehea_sense_port_attr(port);
2918 if (ret)
2919 goto out_free_mc_list;
2921 port_dev = ehea_register_port(port, dn);
2922 if (!port_dev)
2923 goto out_free_mc_list;
2925 SET_NETDEV_DEV(dev, port_dev);
2927 /* initialize net_device structure */
2928 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
2930 dev->open = ehea_open;
2931 #ifdef CONFIG_NET_POLL_CONTROLLER
2932 dev->poll_controller = ehea_netpoll;
2933 #endif
2934 dev->stop = ehea_stop;
2935 dev->hard_start_xmit = ehea_start_xmit;
2936 dev->get_stats = ehea_get_stats;
2937 dev->set_multicast_list = ehea_set_multicast_list;
2938 dev->set_mac_address = ehea_set_mac_addr;
2939 dev->change_mtu = ehea_change_mtu;
2940 dev->vlan_rx_register = ehea_vlan_rx_register;
2941 dev->vlan_rx_add_vid = ehea_vlan_rx_add_vid;
2942 dev->vlan_rx_kill_vid = ehea_vlan_rx_kill_vid;
2943 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
2944 | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX
2945 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
2946 | NETIF_F_LLTX;
2947 dev->tx_timeout = &ehea_tx_watchdog;
2948 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
2950 INIT_WORK(&port->reset_task, ehea_reset_port);
2952 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2953 if (ret) {
2954 ret = -EIO;
2955 goto out_unreg_port;
2958 ehea_set_ethtool_ops(dev);
2960 ret = register_netdev(dev);
2961 if (ret) {
2962 ehea_error("register_netdev failed. ret=%d", ret);
2963 goto out_dereg_bc;
2966 port->lro_max_aggr = lro_max_aggr;
2968 ret = ehea_get_jumboframe_status(port, &jumbo);
2969 if (ret)
2970 ehea_error("failed determining jumbo frame status for %s",
2971 port->netdev->name);
2973 ehea_info("%s: Jumbo frames are %sabled", dev->name,
2974 jumbo == 1 ? "en" : "dis");
2976 adapter->active_ports++;
2978 return port;
2980 out_dereg_bc:
2981 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2983 out_unreg_port:
2984 ehea_unregister_port(port);
2986 out_free_mc_list:
2987 kfree(port->mc_list);
2989 out_free_ethdev:
2990 free_netdev(dev);
2992 out_err:
2993 ehea_error("setting up logical port with id=%d failed, ret=%d",
2994 logical_port_id, ret);
2995 return NULL;
2998 static void ehea_shutdown_single_port(struct ehea_port *port)
3000 unregister_netdev(port->netdev);
3001 ehea_unregister_port(port);
3002 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
3003 kfree(port->mc_list);
3004 free_netdev(port->netdev);
3005 port->adapter->active_ports--;
3008 static int ehea_setup_ports(struct ehea_adapter *adapter)
3010 struct device_node *lhea_dn;
3011 struct device_node *eth_dn = NULL;
3013 const u32 *dn_log_port_id;
3014 int i = 0;
3016 lhea_dn = adapter->ofdev->node;
3017 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3019 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3020 NULL);
3021 if (!dn_log_port_id) {
3022 ehea_error("bad device node: eth_dn name=%s",
3023 eth_dn->full_name);
3024 continue;
3027 if (ehea_add_adapter_mr(adapter)) {
3028 ehea_error("creating MR failed");
3029 of_node_put(eth_dn);
3030 return -EIO;
3033 adapter->port[i] = ehea_setup_single_port(adapter,
3034 *dn_log_port_id,
3035 eth_dn);
3036 if (adapter->port[i])
3037 ehea_info("%s -> logical port id #%d",
3038 adapter->port[i]->netdev->name,
3039 *dn_log_port_id);
3040 else
3041 ehea_remove_adapter_mr(adapter);
3043 i++;
3046 return 0;
3049 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3050 u32 logical_port_id)
3052 struct device_node *lhea_dn;
3053 struct device_node *eth_dn = NULL;
3054 const u32 *dn_log_port_id;
3056 lhea_dn = adapter->ofdev->node;
3057 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3059 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3060 NULL);
3061 if (dn_log_port_id)
3062 if (*dn_log_port_id == logical_port_id)
3063 return eth_dn;
3066 return NULL;
3069 static ssize_t ehea_probe_port(struct device *dev,
3070 struct device_attribute *attr,
3071 const char *buf, size_t count)
3073 struct ehea_adapter *adapter = dev->driver_data;
3074 struct ehea_port *port;
3075 struct device_node *eth_dn = NULL;
3076 int i;
3078 u32 logical_port_id;
3080 sscanf(buf, "%d", &logical_port_id);
3082 port = ehea_get_port(adapter, logical_port_id);
3084 if (port) {
3085 ehea_info("adding port with logical port id=%d failed. port "
3086 "already configured as %s.", logical_port_id,
3087 port->netdev->name);
3088 return -EINVAL;
3091 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3093 if (!eth_dn) {
3094 ehea_info("no logical port with id %d found", logical_port_id);
3095 return -EINVAL;
3098 if (ehea_add_adapter_mr(adapter)) {
3099 ehea_error("creating MR failed");
3100 return -EIO;
3103 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3105 of_node_put(eth_dn);
3107 if (port) {
3108 for (i=0; i < EHEA_MAX_PORTS; i++)
3109 if (!adapter->port[i]) {
3110 adapter->port[i] = port;
3111 break;
3114 ehea_info("added %s (logical port id=%d)", port->netdev->name,
3115 logical_port_id);
3116 } else {
3117 ehea_remove_adapter_mr(adapter);
3118 return -EIO;
3121 return (ssize_t) count;
3124 static ssize_t ehea_remove_port(struct device *dev,
3125 struct device_attribute *attr,
3126 const char *buf, size_t count)
3128 struct ehea_adapter *adapter = dev->driver_data;
3129 struct ehea_port *port;
3130 int i;
3131 u32 logical_port_id;
3133 sscanf(buf, "%d", &logical_port_id);
3135 port = ehea_get_port(adapter, logical_port_id);
3137 if (port) {
3138 ehea_info("removed %s (logical port id=%d)", port->netdev->name,
3139 logical_port_id);
3141 ehea_shutdown_single_port(port);
3143 for (i=0; i < EHEA_MAX_PORTS; i++)
3144 if (adapter->port[i] == port) {
3145 adapter->port[i] = NULL;
3146 break;
3148 } else {
3149 ehea_error("removing port with logical port id=%d failed. port "
3150 "not configured.", logical_port_id);
3151 return -EINVAL;
3154 ehea_remove_adapter_mr(adapter);
3156 return (ssize_t) count;
3159 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3160 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3162 int ehea_create_device_sysfs(struct of_device *dev)
3164 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3165 if (ret)
3166 goto out;
3168 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3169 out:
3170 return ret;
3173 void ehea_remove_device_sysfs(struct of_device *dev)
3175 device_remove_file(&dev->dev, &dev_attr_probe_port);
3176 device_remove_file(&dev->dev, &dev_attr_remove_port);
3179 static int __devinit ehea_probe_adapter(struct of_device *dev,
3180 const struct of_device_id *id)
3182 struct ehea_adapter *adapter;
3183 const u64 *adapter_handle;
3184 int ret;
3186 if (!dev || !dev->node) {
3187 ehea_error("Invalid ibmebus device probed");
3188 return -EINVAL;
3191 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3192 if (!adapter) {
3193 ret = -ENOMEM;
3194 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3195 goto out;
3198 list_add(&adapter->list, &adapter_list);
3200 adapter->ofdev = dev;
3202 adapter_handle = of_get_property(dev->node, "ibm,hea-handle",
3203 NULL);
3204 if (adapter_handle)
3205 adapter->handle = *adapter_handle;
3207 if (!adapter->handle) {
3208 dev_err(&dev->dev, "failed getting handle for adapter"
3209 " '%s'\n", dev->node->full_name);
3210 ret = -ENODEV;
3211 goto out_free_ad;
3214 adapter->pd = EHEA_PD_ID;
3216 dev->dev.driver_data = adapter;
3219 /* initialize adapter and ports */
3220 /* get adapter properties */
3221 ret = ehea_sense_adapter_attr(adapter);
3222 if (ret) {
3223 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3224 goto out_free_ad;
3227 adapter->neq = ehea_create_eq(adapter,
3228 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3229 if (!adapter->neq) {
3230 ret = -EIO;
3231 dev_err(&dev->dev, "NEQ creation failed\n");
3232 goto out_free_ad;
3235 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3236 (unsigned long)adapter);
3238 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3239 ehea_interrupt_neq, IRQF_DISABLED,
3240 "ehea_neq", adapter);
3241 if (ret) {
3242 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3243 goto out_kill_eq;
3246 ret = ehea_create_device_sysfs(dev);
3247 if (ret)
3248 goto out_free_irq;
3250 ret = ehea_setup_ports(adapter);
3251 if (ret) {
3252 dev_err(&dev->dev, "setup_ports failed\n");
3253 goto out_rem_dev_sysfs;
3256 ret = 0;
3257 goto out;
3259 out_rem_dev_sysfs:
3260 ehea_remove_device_sysfs(dev);
3262 out_free_irq:
3263 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3265 out_kill_eq:
3266 ehea_destroy_eq(adapter->neq);
3268 out_free_ad:
3269 kfree(adapter);
3270 out:
3271 return ret;
3274 static int __devexit ehea_remove(struct of_device *dev)
3276 struct ehea_adapter *adapter = dev->dev.driver_data;
3277 int i;
3279 for (i = 0; i < EHEA_MAX_PORTS; i++)
3280 if (adapter->port[i]) {
3281 ehea_shutdown_single_port(adapter->port[i]);
3282 adapter->port[i] = NULL;
3285 ehea_remove_device_sysfs(dev);
3287 flush_scheduled_work();
3289 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3290 tasklet_kill(&adapter->neq_tasklet);
3292 ehea_destroy_eq(adapter->neq);
3293 ehea_remove_adapter_mr(adapter);
3294 list_del(&adapter->list);
3296 kfree(adapter);
3298 return 0;
3301 static int ehea_reboot_notifier(struct notifier_block *nb,
3302 unsigned long action, void *unused)
3304 if (action == SYS_RESTART) {
3305 ehea_info("Reboot: freeing all eHEA resources");
3306 ibmebus_unregister_driver(&ehea_driver);
3308 return NOTIFY_DONE;
3311 static struct notifier_block ehea_reboot_nb = {
3312 .notifier_call = ehea_reboot_notifier,
3315 static int check_module_parm(void)
3317 int ret = 0;
3319 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3320 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3321 ehea_info("Bad parameter: rq1_entries");
3322 ret = -EINVAL;
3324 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3325 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3326 ehea_info("Bad parameter: rq2_entries");
3327 ret = -EINVAL;
3329 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3330 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3331 ehea_info("Bad parameter: rq3_entries");
3332 ret = -EINVAL;
3334 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3335 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3336 ehea_info("Bad parameter: sq_entries");
3337 ret = -EINVAL;
3340 return ret;
3343 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3344 char *buf)
3346 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3349 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3350 ehea_show_capabilities, NULL);
3352 int __init ehea_module_init(void)
3354 int ret;
3356 printk(KERN_INFO "IBM eHEA ethernet device driver (Release %s)\n",
3357 DRV_VERSION);
3360 INIT_WORK(&ehea_rereg_mr_task, ehea_rereg_mrs);
3361 sema_init(&dlpar_mem_lock, 1);
3363 ret = check_module_parm();
3364 if (ret)
3365 goto out;
3367 ret = ehea_create_busmap();
3368 if (ret)
3369 goto out;
3371 register_reboot_notifier(&ehea_reboot_nb);
3373 ret = ibmebus_register_driver(&ehea_driver);
3374 if (ret) {
3375 ehea_error("failed registering eHEA device driver on ebus");
3376 goto out;
3379 ret = driver_create_file(&ehea_driver.driver,
3380 &driver_attr_capabilities);
3381 if (ret) {
3382 ehea_error("failed to register capabilities attribute, ret=%d",
3383 ret);
3384 unregister_reboot_notifier(&ehea_reboot_nb);
3385 ibmebus_unregister_driver(&ehea_driver);
3386 goto out;
3389 out:
3390 return ret;
3393 static void __exit ehea_module_exit(void)
3395 flush_scheduled_work();
3396 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3397 ibmebus_unregister_driver(&ehea_driver);
3398 unregister_reboot_notifier(&ehea_reboot_nb);
3399 ehea_destroy_busmap();
3402 module_init(ehea_module_init);
3403 module_exit(ehea_module_exit);