pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / ehea / ehea_main.c
blob2ba57e6ace4dfe2af3d8d3bb135e075f94ce89a9
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 <net/ip.h>
38 #include "ehea.h"
39 #include "ehea_qmr.h"
40 #include "ehea_phyp.h"
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
45 MODULE_DESCRIPTION("IBM eServer HEA Driver");
46 MODULE_VERSION(DRV_VERSION);
49 static int msg_level = -1;
50 static int rq1_entries = EHEA_DEF_ENTRIES_RQ1;
51 static int rq2_entries = EHEA_DEF_ENTRIES_RQ2;
52 static int rq3_entries = EHEA_DEF_ENTRIES_RQ3;
53 static int sq_entries = EHEA_DEF_ENTRIES_SQ;
54 static int use_mcs = 0;
55 static int use_lro = 0;
56 static int lro_max_aggr = EHEA_LRO_MAX_AGGR;
57 static int num_tx_qps = EHEA_NUM_TX_QP;
58 static int prop_carrier_state = 0;
60 module_param(msg_level, int, 0);
61 module_param(rq1_entries, int, 0);
62 module_param(rq2_entries, int, 0);
63 module_param(rq3_entries, int, 0);
64 module_param(sq_entries, int, 0);
65 module_param(prop_carrier_state, int, 0);
66 module_param(use_mcs, int, 0);
67 module_param(use_lro, int, 0);
68 module_param(lro_max_aggr, int, 0);
69 module_param(num_tx_qps, int, 0);
71 MODULE_PARM_DESC(num_tx_qps, "Number of TX-QPS");
72 MODULE_PARM_DESC(msg_level, "msg_level");
73 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
74 "port to stack. 1:yes, 0:no. Default = 0 ");
75 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
76 "[2^x - 1], x = [6..14]. Default = "
77 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
78 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
79 "[2^x - 1], x = [6..14]. Default = "
80 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
81 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
82 "[2^x - 1], x = [6..14]. Default = "
83 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
84 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue "
85 "[2^x - 1], x = [6..14]. Default = "
86 __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
87 MODULE_PARM_DESC(use_mcs, " 0:NAPI, 1:Multiple receive queues, Default = 0 ");
89 MODULE_PARM_DESC(lro_max_aggr, " LRO: Max packets to be aggregated. Default = "
90 __MODULE_STRING(EHEA_LRO_MAX_AGGR));
91 MODULE_PARM_DESC(use_lro, " Large Receive Offload, 1: enable, 0: disable, "
92 "Default = 0");
94 static int port_name_cnt = 0;
95 static LIST_HEAD(adapter_list);
96 u64 ehea_driver_flags = 0;
97 struct work_struct ehea_rereg_mr_task;
99 struct semaphore dlpar_mem_lock;
101 static int __devinit ehea_probe_adapter(struct ibmebus_dev *dev,
102 const struct of_device_id *id);
104 static int __devexit ehea_remove(struct ibmebus_dev *dev);
106 static struct of_device_id ehea_device_table[] = {
108 .name = "lhea",
109 .compatible = "IBM,lhea",
114 static struct ibmebus_driver ehea_driver = {
115 .name = "ehea",
116 .id_table = ehea_device_table,
117 .probe = ehea_probe_adapter,
118 .remove = ehea_remove,
121 void ehea_dump(void *adr, int len, char *msg) {
122 int x;
123 unsigned char *deb = adr;
124 for (x = 0; x < len; x += 16) {
125 printk(DRV_NAME " %s adr=%p ofs=%04x %016lx %016lx\n", msg,
126 deb, x, *((u64*)&deb[0]), *((u64*)&deb[8]));
127 deb += 16;
131 static struct net_device_stats *ehea_get_stats(struct net_device *dev)
133 struct ehea_port *port = netdev_priv(dev);
134 struct net_device_stats *stats = &port->stats;
135 struct hcp_ehea_port_cb2 *cb2;
136 u64 hret, rx_packets;
137 int i;
139 memset(stats, 0, sizeof(*stats));
141 cb2 = kzalloc(PAGE_SIZE, GFP_KERNEL);
142 if (!cb2) {
143 ehea_error("no mem for cb2");
144 goto out;
147 hret = ehea_h_query_ehea_port(port->adapter->handle,
148 port->logical_port_id,
149 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
150 if (hret != H_SUCCESS) {
151 ehea_error("query_ehea_port failed");
152 goto out_herr;
155 if (netif_msg_hw(port))
156 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
158 rx_packets = 0;
159 for (i = 0; i < port->num_def_qps; i++)
160 rx_packets += port->port_res[i].rx_packets;
162 stats->tx_packets = cb2->txucp + cb2->txmcp + cb2->txbcp;
163 stats->multicast = cb2->rxmcp;
164 stats->rx_errors = cb2->rxuerr;
165 stats->rx_bytes = cb2->rxo;
166 stats->tx_bytes = cb2->txo;
167 stats->rx_packets = rx_packets;
169 out_herr:
170 kfree(cb2);
171 out:
172 return stats;
175 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
177 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
178 struct net_device *dev = pr->port->netdev;
179 int max_index_mask = pr->rq1_skba.len - 1;
180 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
181 int adder = 0;
182 int i;
184 pr->rq1_skba.os_skbs = 0;
186 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
187 pr->rq1_skba.index = index;
188 pr->rq1_skba.os_skbs = fill_wqes;
189 return;
192 for (i = 0; i < fill_wqes; i++) {
193 if (!skb_arr_rq1[index]) {
194 skb_arr_rq1[index] = netdev_alloc_skb(dev,
195 EHEA_L_PKT_SIZE);
196 if (!skb_arr_rq1[index]) {
197 pr->rq1_skba.os_skbs = fill_wqes - i;
198 ehea_error("%s: no mem for skb/%d wqes filled",
199 dev->name, i);
200 break;
203 index--;
204 index &= max_index_mask;
205 adder++;
208 if (adder == 0)
209 return;
211 /* Ring doorbell */
212 ehea_update_rq1a(pr->qp, adder);
215 static int ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
217 int ret = 0;
218 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
219 struct net_device *dev = pr->port->netdev;
220 int i;
222 for (i = 0; i < pr->rq1_skba.len; i++) {
223 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
224 if (!skb_arr_rq1[i]) {
225 ehea_error("%s: no mem for skb/%d wqes filled",
226 dev->name, i);
227 ret = -ENOMEM;
228 goto out;
231 /* Ring doorbell */
232 ehea_update_rq1a(pr->qp, nr_rq1a);
233 out:
234 return ret;
237 static int ehea_refill_rq_def(struct ehea_port_res *pr,
238 struct ehea_q_skb_arr *q_skba, int rq_nr,
239 int num_wqes, int wqe_type, int packet_size)
241 struct net_device *dev = pr->port->netdev;
242 struct ehea_qp *qp = pr->qp;
243 struct sk_buff **skb_arr = q_skba->arr;
244 struct ehea_rwqe *rwqe;
245 int i, index, max_index_mask, fill_wqes;
246 int adder = 0;
247 int ret = 0;
249 fill_wqes = q_skba->os_skbs + num_wqes;
250 q_skba->os_skbs = 0;
252 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
253 q_skba->os_skbs = fill_wqes;
254 return ret;
257 index = q_skba->index;
258 max_index_mask = q_skba->len - 1;
259 for (i = 0; i < fill_wqes; i++) {
260 u64 tmp_addr;
261 struct sk_buff *skb = netdev_alloc_skb(dev, packet_size);
262 if (!skb) {
263 ehea_error("%s: no mem for skb/%d wqes filled",
264 pr->port->netdev->name, i);
265 q_skba->os_skbs = fill_wqes - i;
266 ret = -ENOMEM;
267 break;
269 skb_reserve(skb, NET_IP_ALIGN);
271 skb_arr[index] = skb;
272 tmp_addr = ehea_map_vaddr(skb->data);
273 if (tmp_addr == -1) {
274 dev_kfree_skb(skb);
275 q_skba->os_skbs = fill_wqes - i;
276 ret = 0;
277 break;
280 rwqe = ehea_get_next_rwqe(qp, rq_nr);
281 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
282 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
283 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
284 rwqe->sg_list[0].vaddr = tmp_addr;
285 rwqe->sg_list[0].len = packet_size;
286 rwqe->data_segments = 1;
288 index++;
289 index &= max_index_mask;
290 adder++;
293 q_skba->index = index;
294 if (adder == 0)
295 goto out;
297 /* Ring doorbell */
298 iosync();
299 if (rq_nr == 2)
300 ehea_update_rq2a(pr->qp, adder);
301 else
302 ehea_update_rq3a(pr->qp, adder);
303 out:
304 return ret;
308 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
310 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
311 nr_of_wqes, EHEA_RWQE2_TYPE,
312 EHEA_RQ2_PKT_SIZE + NET_IP_ALIGN);
316 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
318 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
319 nr_of_wqes, EHEA_RWQE3_TYPE,
320 EHEA_MAX_PACKET_SIZE + NET_IP_ALIGN);
323 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
325 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
326 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
327 return 0;
328 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
329 (cqe->header_length == 0))
330 return 0;
331 return -EINVAL;
334 static inline void ehea_fill_skb(struct net_device *dev,
335 struct sk_buff *skb, struct ehea_cqe *cqe)
337 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
339 skb_put(skb, length);
340 skb->ip_summed = CHECKSUM_UNNECESSARY;
341 skb->protocol = eth_type_trans(skb, dev);
344 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
345 int arr_len,
346 struct ehea_cqe *cqe)
348 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
349 struct sk_buff *skb;
350 void *pref;
351 int x;
353 x = skb_index + 1;
354 x &= (arr_len - 1);
356 pref = skb_array[x];
357 prefetchw(pref);
358 prefetchw(pref + EHEA_CACHE_LINE);
360 pref = (skb_array[x]->data);
361 prefetch(pref);
362 prefetch(pref + EHEA_CACHE_LINE);
363 prefetch(pref + EHEA_CACHE_LINE * 2);
364 prefetch(pref + EHEA_CACHE_LINE * 3);
365 skb = skb_array[skb_index];
366 skb_array[skb_index] = NULL;
367 return skb;
370 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
371 int arr_len, int wqe_index)
373 struct sk_buff *skb;
374 void *pref;
375 int x;
377 x = wqe_index + 1;
378 x &= (arr_len - 1);
380 pref = skb_array[x];
381 prefetchw(pref);
382 prefetchw(pref + EHEA_CACHE_LINE);
384 pref = (skb_array[x]->data);
385 prefetchw(pref);
386 prefetchw(pref + EHEA_CACHE_LINE);
388 skb = skb_array[wqe_index];
389 skb_array[wqe_index] = NULL;
390 return skb;
393 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
394 struct ehea_cqe *cqe, int *processed_rq2,
395 int *processed_rq3)
397 struct sk_buff *skb;
399 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
400 pr->p_stats.err_tcp_cksum++;
401 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
402 pr->p_stats.err_ip_cksum++;
403 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
404 pr->p_stats.err_frame_crc++;
406 if (netif_msg_rx_err(pr->port)) {
407 ehea_error("CQE Error for QP %d", pr->qp->init_attr.qp_nr);
408 ehea_dump(cqe, sizeof(*cqe), "CQE");
411 if (rq == 2) {
412 *processed_rq2 += 1;
413 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
414 dev_kfree_skb(skb);
415 } else if (rq == 3) {
416 *processed_rq3 += 1;
417 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
418 dev_kfree_skb(skb);
421 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
422 ehea_error("Critical receive error. Resetting port.");
423 schedule_work(&pr->port->reset_task);
424 return 1;
427 return 0;
430 static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
431 void **tcph, u64 *hdr_flags, void *priv)
433 struct ehea_cqe *cqe = priv;
434 unsigned int ip_len;
435 struct iphdr *iph;
437 /* non tcp/udp packets */
438 if (!cqe->header_length)
439 return -1;
441 /* non tcp packet */
442 skb_reset_network_header(skb);
443 iph = ip_hdr(skb);
444 if (iph->protocol != IPPROTO_TCP)
445 return -1;
447 ip_len = ip_hdrlen(skb);
448 skb_set_transport_header(skb, ip_len);
449 *tcph = tcp_hdr(skb);
451 /* check if ip header and tcp header are complete */
452 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
453 return -1;
455 *hdr_flags = LRO_IPV4 | LRO_TCP;
456 *iphdr = iph;
458 return 0;
461 static void ehea_proc_skb(struct ehea_port_res *pr, struct ehea_cqe *cqe,
462 struct sk_buff *skb)
464 int vlan_extracted = (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
465 && pr->port->vgrp;
467 if (use_lro) {
468 if (vlan_extracted)
469 lro_vlan_hwaccel_receive_skb(&pr->lro_mgr, skb,
470 pr->port->vgrp,
471 cqe->vlan_tag,
472 cqe);
473 else
474 lro_receive_skb(&pr->lro_mgr, skb, cqe);
475 } else {
476 if (vlan_extracted)
477 vlan_hwaccel_receive_skb(skb, pr->port->vgrp,
478 cqe->vlan_tag);
479 else
480 netif_receive_skb(skb);
484 static int ehea_proc_rwqes(struct net_device *dev,
485 struct ehea_port_res *pr,
486 int budget)
488 struct ehea_port *port = pr->port;
489 struct ehea_qp *qp = pr->qp;
490 struct ehea_cqe *cqe;
491 struct sk_buff *skb;
492 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
493 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
494 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
495 int skb_arr_rq1_len = pr->rq1_skba.len;
496 int skb_arr_rq2_len = pr->rq2_skba.len;
497 int skb_arr_rq3_len = pr->rq3_skba.len;
498 int processed, processed_rq1, processed_rq2, processed_rq3;
499 int wqe_index, last_wqe_index, rq, port_reset;
501 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
502 last_wqe_index = 0;
504 cqe = ehea_poll_rq1(qp, &wqe_index);
505 while ((processed < budget) && cqe) {
506 ehea_inc_rq1(qp);
507 processed_rq1++;
508 processed++;
509 if (netif_msg_rx_status(port))
510 ehea_dump(cqe, sizeof(*cqe), "CQE");
512 last_wqe_index = wqe_index;
513 rmb();
514 if (!ehea_check_cqe(cqe, &rq)) {
515 if (rq == 1) { /* LL RQ1 */
516 skb = get_skb_by_index_ll(skb_arr_rq1,
517 skb_arr_rq1_len,
518 wqe_index);
519 if (unlikely(!skb)) {
520 if (netif_msg_rx_err(port))
521 ehea_error("LL rq1: skb=NULL");
523 skb = netdev_alloc_skb(dev,
524 EHEA_L_PKT_SIZE);
525 if (!skb)
526 break;
528 skb_copy_to_linear_data(skb, ((char*)cqe) + 64,
529 cqe->num_bytes_transfered - 4);
530 ehea_fill_skb(dev, skb, cqe);
531 } else if (rq == 2) { /* RQ2 */
532 skb = get_skb_by_index(skb_arr_rq2,
533 skb_arr_rq2_len, cqe);
534 if (unlikely(!skb)) {
535 if (netif_msg_rx_err(port))
536 ehea_error("rq2: skb=NULL");
537 break;
539 ehea_fill_skb(dev, skb, cqe);
540 processed_rq2++;
541 } else { /* RQ3 */
542 skb = get_skb_by_index(skb_arr_rq3,
543 skb_arr_rq3_len, cqe);
544 if (unlikely(!skb)) {
545 if (netif_msg_rx_err(port))
546 ehea_error("rq3: skb=NULL");
547 break;
549 ehea_fill_skb(dev, skb, cqe);
550 processed_rq3++;
553 ehea_proc_skb(pr, cqe, skb);
554 dev->last_rx = jiffies;
555 } else {
556 pr->p_stats.poll_receive_errors++;
557 port_reset = ehea_treat_poll_error(pr, rq, cqe,
558 &processed_rq2,
559 &processed_rq3);
560 if (port_reset)
561 break;
563 cqe = ehea_poll_rq1(qp, &wqe_index);
565 if (use_lro)
566 lro_flush_all(&pr->lro_mgr);
568 pr->rx_packets += processed;
570 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
571 ehea_refill_rq2(pr, processed_rq2);
572 ehea_refill_rq3(pr, processed_rq3);
574 return processed;
577 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
579 struct sk_buff *skb;
580 struct ehea_cq *send_cq = pr->send_cq;
581 struct ehea_cqe *cqe;
582 int quota = my_quota;
583 int cqe_counter = 0;
584 int swqe_av = 0;
585 int index;
586 unsigned long flags;
588 cqe = ehea_poll_cq(send_cq);
589 while(cqe && (quota > 0)) {
590 ehea_inc_cq(send_cq);
592 cqe_counter++;
593 rmb();
594 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
595 ehea_error("Send Completion Error: Resetting port");
596 if (netif_msg_tx_err(pr->port))
597 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
598 schedule_work(&pr->port->reset_task);
599 break;
602 if (netif_msg_tx_done(pr->port))
603 ehea_dump(cqe, sizeof(*cqe), "CQE");
605 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
606 == EHEA_SWQE2_TYPE)) {
608 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
609 skb = pr->sq_skba.arr[index];
610 dev_kfree_skb(skb);
611 pr->sq_skba.arr[index] = NULL;
614 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
615 quota--;
617 cqe = ehea_poll_cq(send_cq);
620 ehea_update_feca(send_cq, cqe_counter);
621 atomic_add(swqe_av, &pr->swqe_avail);
623 spin_lock_irqsave(&pr->netif_queue, flags);
625 if (pr->queue_stopped && (atomic_read(&pr->swqe_avail)
626 >= pr->swqe_refill_th)) {
627 netif_wake_queue(pr->port->netdev);
628 pr->queue_stopped = 0;
630 spin_unlock_irqrestore(&pr->netif_queue, flags);
632 return cqe;
635 #define EHEA_NAPI_POLL_NUM_BEFORE_IRQ 16
636 #define EHEA_POLL_MAX_CQES 65535
638 static int ehea_poll(struct napi_struct *napi, int budget)
640 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res, napi);
641 struct net_device *dev = pr->port->netdev;
642 struct ehea_cqe *cqe;
643 struct ehea_cqe *cqe_skb = NULL;
644 int force_irq, wqe_index;
645 int rx = 0;
647 force_irq = (pr->poll_counter > EHEA_NAPI_POLL_NUM_BEFORE_IRQ);
648 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
650 if (!force_irq)
651 rx += ehea_proc_rwqes(dev, pr, budget - rx);
653 while ((rx != budget) || force_irq) {
654 pr->poll_counter = 0;
655 force_irq = 0;
656 netif_rx_complete(dev, napi);
657 ehea_reset_cq_ep(pr->recv_cq);
658 ehea_reset_cq_ep(pr->send_cq);
659 ehea_reset_cq_n1(pr->recv_cq);
660 ehea_reset_cq_n1(pr->send_cq);
661 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
662 cqe_skb = ehea_poll_cq(pr->send_cq);
664 if (!cqe && !cqe_skb)
665 return rx;
667 if (!netif_rx_reschedule(dev, napi))
668 return rx;
670 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
671 rx += ehea_proc_rwqes(dev, pr, budget - rx);
674 pr->poll_counter++;
675 return rx;
678 #ifdef CONFIG_NET_POLL_CONTROLLER
679 static void ehea_netpoll(struct net_device *dev)
681 struct ehea_port *port = netdev_priv(dev);
682 int i;
684 for (i = 0; i < port->num_def_qps; i++)
685 netif_rx_schedule(dev, &port->port_res[i].napi);
687 #endif
689 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
691 struct ehea_port_res *pr = param;
693 netif_rx_schedule(pr->port->netdev, &pr->napi);
695 return IRQ_HANDLED;
698 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
700 struct ehea_port *port = param;
701 struct ehea_eqe *eqe;
702 struct ehea_qp *qp;
703 u32 qp_token;
705 eqe = ehea_poll_eq(port->qp_eq);
707 while (eqe) {
708 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
709 ehea_error("QP aff_err: entry=0x%lx, token=0x%x",
710 eqe->entry, qp_token);
712 qp = port->port_res[qp_token].qp;
713 ehea_error_data(port->adapter, qp->fw_handle);
714 eqe = ehea_poll_eq(port->qp_eq);
717 schedule_work(&port->reset_task);
719 return IRQ_HANDLED;
722 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
723 int logical_port)
725 int i;
727 for (i = 0; i < EHEA_MAX_PORTS; i++)
728 if (adapter->port[i])
729 if (adapter->port[i]->logical_port_id == logical_port)
730 return adapter->port[i];
731 return NULL;
734 int ehea_sense_port_attr(struct ehea_port *port)
736 int ret;
737 u64 hret;
738 struct hcp_ehea_port_cb0 *cb0;
740 cb0 = kzalloc(PAGE_SIZE, GFP_ATOMIC); /* May be called via */
741 if (!cb0) { /* ehea_neq_tasklet() */
742 ehea_error("no mem for cb0");
743 ret = -ENOMEM;
744 goto out;
747 hret = ehea_h_query_ehea_port(port->adapter->handle,
748 port->logical_port_id, H_PORT_CB0,
749 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
750 cb0);
751 if (hret != H_SUCCESS) {
752 ret = -EIO;
753 goto out_free;
756 /* MAC address */
757 port->mac_addr = cb0->port_mac_addr << 16;
759 if (!is_valid_ether_addr((u8*)&port->mac_addr)) {
760 ret = -EADDRNOTAVAIL;
761 goto out_free;
764 /* Port speed */
765 switch (cb0->port_speed) {
766 case H_SPEED_10M_H:
767 port->port_speed = EHEA_SPEED_10M;
768 port->full_duplex = 0;
769 break;
770 case H_SPEED_10M_F:
771 port->port_speed = EHEA_SPEED_10M;
772 port->full_duplex = 1;
773 break;
774 case H_SPEED_100M_H:
775 port->port_speed = EHEA_SPEED_100M;
776 port->full_duplex = 0;
777 break;
778 case H_SPEED_100M_F:
779 port->port_speed = EHEA_SPEED_100M;
780 port->full_duplex = 1;
781 break;
782 case H_SPEED_1G_F:
783 port->port_speed = EHEA_SPEED_1G;
784 port->full_duplex = 1;
785 break;
786 case H_SPEED_10G_F:
787 port->port_speed = EHEA_SPEED_10G;
788 port->full_duplex = 1;
789 break;
790 default:
791 port->port_speed = 0;
792 port->full_duplex = 0;
793 break;
796 port->autoneg = 1;
797 port->num_mcs = cb0->num_default_qps;
799 /* Number of default QPs */
800 if (use_mcs)
801 port->num_def_qps = cb0->num_default_qps;
802 else
803 port->num_def_qps = 1;
805 if (!port->num_def_qps) {
806 ret = -EINVAL;
807 goto out_free;
810 port->num_tx_qps = num_tx_qps;
812 if (port->num_def_qps >= port->num_tx_qps)
813 port->num_add_tx_qps = 0;
814 else
815 port->num_add_tx_qps = port->num_tx_qps - port->num_def_qps;
817 ret = 0;
818 out_free:
819 if (ret || netif_msg_probe(port))
820 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
821 kfree(cb0);
822 out:
823 return ret;
826 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
828 struct hcp_ehea_port_cb4 *cb4;
829 u64 hret;
830 int ret = 0;
832 cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
833 if (!cb4) {
834 ehea_error("no mem for cb4");
835 ret = -ENOMEM;
836 goto out;
839 cb4->port_speed = port_speed;
841 netif_carrier_off(port->netdev);
843 hret = ehea_h_modify_ehea_port(port->adapter->handle,
844 port->logical_port_id,
845 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
846 if (hret == H_SUCCESS) {
847 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
849 hret = ehea_h_query_ehea_port(port->adapter->handle,
850 port->logical_port_id,
851 H_PORT_CB4, H_PORT_CB4_SPEED,
852 cb4);
853 if (hret == H_SUCCESS) {
854 switch (cb4->port_speed) {
855 case H_SPEED_10M_H:
856 port->port_speed = EHEA_SPEED_10M;
857 port->full_duplex = 0;
858 break;
859 case H_SPEED_10M_F:
860 port->port_speed = EHEA_SPEED_10M;
861 port->full_duplex = 1;
862 break;
863 case H_SPEED_100M_H:
864 port->port_speed = EHEA_SPEED_100M;
865 port->full_duplex = 0;
866 break;
867 case H_SPEED_100M_F:
868 port->port_speed = EHEA_SPEED_100M;
869 port->full_duplex = 1;
870 break;
871 case H_SPEED_1G_F:
872 port->port_speed = EHEA_SPEED_1G;
873 port->full_duplex = 1;
874 break;
875 case H_SPEED_10G_F:
876 port->port_speed = EHEA_SPEED_10G;
877 port->full_duplex = 1;
878 break;
879 default:
880 port->port_speed = 0;
881 port->full_duplex = 0;
882 break;
884 } else {
885 ehea_error("Failed sensing port speed");
886 ret = -EIO;
888 } else {
889 if (hret == H_AUTHORITY) {
890 ehea_info("Hypervisor denied setting port speed");
891 ret = -EPERM;
892 } else {
893 ret = -EIO;
894 ehea_error("Failed setting port speed");
897 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
898 netif_carrier_on(port->netdev);
900 kfree(cb4);
901 out:
902 return ret;
905 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
907 int ret;
908 u8 ec;
909 u8 portnum;
910 struct ehea_port *port;
912 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
913 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
914 port = ehea_get_port(adapter, portnum);
916 switch (ec) {
917 case EHEA_EC_PORTSTATE_CHG: /* port state change */
919 if (!port) {
920 ehea_error("unknown portnum %x", portnum);
921 break;
924 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
925 if (!netif_carrier_ok(port->netdev)) {
926 ret = ehea_sense_port_attr(port);
927 if (ret) {
928 ehea_error("failed resensing port "
929 "attributes");
930 break;
933 if (netif_msg_link(port))
934 ehea_info("%s: Logical port up: %dMbps "
935 "%s Duplex",
936 port->netdev->name,
937 port->port_speed,
938 port->full_duplex ==
939 1 ? "Full" : "Half");
941 netif_carrier_on(port->netdev);
942 netif_wake_queue(port->netdev);
944 } else
945 if (netif_carrier_ok(port->netdev)) {
946 if (netif_msg_link(port))
947 ehea_info("%s: Logical port down",
948 port->netdev->name);
949 netif_carrier_off(port->netdev);
950 netif_stop_queue(port->netdev);
953 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
954 port->phy_link = EHEA_PHY_LINK_UP;
955 if (netif_msg_link(port))
956 ehea_info("%s: Physical port up",
957 port->netdev->name);
958 if (prop_carrier_state)
959 netif_carrier_on(port->netdev);
960 } else {
961 port->phy_link = EHEA_PHY_LINK_DOWN;
962 if (netif_msg_link(port))
963 ehea_info("%s: Physical port down",
964 port->netdev->name);
965 if (prop_carrier_state)
966 netif_carrier_off(port->netdev);
969 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
970 ehea_info("External switch port is primary port");
971 else
972 ehea_info("External switch port is backup port");
974 break;
975 case EHEA_EC_ADAPTER_MALFUNC:
976 ehea_error("Adapter malfunction");
977 break;
978 case EHEA_EC_PORT_MALFUNC:
979 ehea_info("Port malfunction: Device: %s", port->netdev->name);
980 netif_carrier_off(port->netdev);
981 netif_stop_queue(port->netdev);
982 break;
983 default:
984 ehea_error("unknown event code %x, eqe=0x%lX", ec, eqe);
985 break;
989 static void ehea_neq_tasklet(unsigned long data)
991 struct ehea_adapter *adapter = (struct ehea_adapter*)data;
992 struct ehea_eqe *eqe;
993 u64 event_mask;
995 eqe = ehea_poll_eq(adapter->neq);
996 ehea_debug("eqe=%p", eqe);
998 while (eqe) {
999 ehea_debug("*eqe=%lx", eqe->entry);
1000 ehea_parse_eqe(adapter, eqe->entry);
1001 eqe = ehea_poll_eq(adapter->neq);
1002 ehea_debug("next eqe=%p", eqe);
1005 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1006 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1007 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1009 ehea_h_reset_events(adapter->handle,
1010 adapter->neq->fw_handle, event_mask);
1013 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1015 struct ehea_adapter *adapter = param;
1016 tasklet_hi_schedule(&adapter->neq_tasklet);
1017 return IRQ_HANDLED;
1021 static int ehea_fill_port_res(struct ehea_port_res *pr)
1023 int ret;
1024 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1026 ret = ehea_init_fill_rq1(pr, init_attr->act_nr_rwqes_rq1
1027 - init_attr->act_nr_rwqes_rq2
1028 - init_attr->act_nr_rwqes_rq3 - 1);
1030 ret |= ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1032 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1034 return ret;
1037 static int ehea_reg_interrupts(struct net_device *dev)
1039 struct ehea_port *port = netdev_priv(dev);
1040 struct ehea_port_res *pr;
1041 int i, ret;
1044 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1045 dev->name);
1047 ret = ibmebus_request_irq(NULL, port->qp_eq->attr.ist1,
1048 ehea_qp_aff_irq_handler,
1049 IRQF_DISABLED, port->int_aff_name, port);
1050 if (ret) {
1051 ehea_error("failed registering irq for qp_aff_irq_handler:"
1052 "ist=%X", port->qp_eq->attr.ist1);
1053 goto out_free_qpeq;
1056 if (netif_msg_ifup(port))
1057 ehea_info("irq_handle 0x%X for function qp_aff_irq_handler "
1058 "registered", port->qp_eq->attr.ist1);
1061 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1062 pr = &port->port_res[i];
1063 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1064 "%s-queue%d", dev->name, i);
1065 ret = ibmebus_request_irq(NULL, pr->eq->attr.ist1,
1066 ehea_recv_irq_handler,
1067 IRQF_DISABLED, pr->int_send_name,
1068 pr);
1069 if (ret) {
1070 ehea_error("failed registering irq for ehea_queue "
1071 "port_res_nr:%d, ist=%X", i,
1072 pr->eq->attr.ist1);
1073 goto out_free_req;
1075 if (netif_msg_ifup(port))
1076 ehea_info("irq_handle 0x%X for function ehea_queue_int "
1077 "%d registered", pr->eq->attr.ist1, i);
1079 out:
1080 return ret;
1083 out_free_req:
1084 while (--i >= 0) {
1085 u32 ist = port->port_res[i].eq->attr.ist1;
1086 ibmebus_free_irq(NULL, ist, &port->port_res[i]);
1089 out_free_qpeq:
1090 ibmebus_free_irq(NULL, port->qp_eq->attr.ist1, port);
1091 i = port->num_def_qps;
1093 goto out;
1097 static void ehea_free_interrupts(struct net_device *dev)
1099 struct ehea_port *port = netdev_priv(dev);
1100 struct ehea_port_res *pr;
1101 int i;
1103 /* send */
1105 for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1106 pr = &port->port_res[i];
1107 ibmebus_free_irq(NULL, pr->eq->attr.ist1, pr);
1108 if (netif_msg_intr(port))
1109 ehea_info("free send irq for res %d with handle 0x%X",
1110 i, pr->eq->attr.ist1);
1113 /* associated events */
1114 ibmebus_free_irq(NULL, port->qp_eq->attr.ist1, port);
1115 if (netif_msg_intr(port))
1116 ehea_info("associated event interrupt for handle 0x%X freed",
1117 port->qp_eq->attr.ist1);
1120 static int ehea_configure_port(struct ehea_port *port)
1122 int ret, i;
1123 u64 hret, mask;
1124 struct hcp_ehea_port_cb0 *cb0;
1126 ret = -ENOMEM;
1127 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1128 if (!cb0)
1129 goto out;
1131 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1132 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1133 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1134 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1135 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1136 PXLY_RC_VLAN_FILTER)
1137 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1139 for (i = 0; i < port->num_mcs; i++)
1140 if (use_mcs)
1141 cb0->default_qpn_arr[i] =
1142 port->port_res[i].qp->init_attr.qp_nr;
1143 else
1144 cb0->default_qpn_arr[i] =
1145 port->port_res[0].qp->init_attr.qp_nr;
1147 if (netif_msg_ifup(port))
1148 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1150 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1151 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1153 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1154 port->logical_port_id,
1155 H_PORT_CB0, mask, cb0);
1156 ret = -EIO;
1157 if (hret != H_SUCCESS)
1158 goto out_free;
1160 ret = 0;
1162 out_free:
1163 kfree(cb0);
1164 out:
1165 return ret;
1168 int ehea_gen_smrs(struct ehea_port_res *pr)
1170 int ret;
1171 struct ehea_adapter *adapter = pr->port->adapter;
1173 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1174 if (ret)
1175 goto out;
1177 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1178 if (ret)
1179 goto out_free;
1181 return 0;
1183 out_free:
1184 ehea_rem_mr(&pr->send_mr);
1185 out:
1186 ehea_error("Generating SMRS failed\n");
1187 return -EIO;
1190 int ehea_rem_smrs(struct ehea_port_res *pr)
1192 if ((ehea_rem_mr(&pr->send_mr))
1193 || (ehea_rem_mr(&pr->recv_mr)))
1194 return -EIO;
1195 else
1196 return 0;
1199 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1201 int arr_size = sizeof(void*) * max_q_entries;
1203 q_skba->arr = vmalloc(arr_size);
1204 if (!q_skba->arr)
1205 return -ENOMEM;
1207 memset(q_skba->arr, 0, arr_size);
1209 q_skba->len = max_q_entries;
1210 q_skba->index = 0;
1211 q_skba->os_skbs = 0;
1213 return 0;
1216 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1217 struct port_res_cfg *pr_cfg, int queue_token)
1219 struct ehea_adapter *adapter = port->adapter;
1220 enum ehea_eq_type eq_type = EHEA_EQ;
1221 struct ehea_qp_init_attr *init_attr = NULL;
1222 int ret = -EIO;
1224 memset(pr, 0, sizeof(struct ehea_port_res));
1226 pr->port = port;
1227 spin_lock_init(&pr->xmit_lock);
1228 spin_lock_init(&pr->netif_queue);
1230 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1231 if (!pr->eq) {
1232 ehea_error("create_eq failed (eq)");
1233 goto out_free;
1236 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1237 pr->eq->fw_handle,
1238 port->logical_port_id);
1239 if (!pr->recv_cq) {
1240 ehea_error("create_cq failed (cq_recv)");
1241 goto out_free;
1244 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1245 pr->eq->fw_handle,
1246 port->logical_port_id);
1247 if (!pr->send_cq) {
1248 ehea_error("create_cq failed (cq_send)");
1249 goto out_free;
1252 if (netif_msg_ifup(port))
1253 ehea_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d",
1254 pr->send_cq->attr.act_nr_of_cqes,
1255 pr->recv_cq->attr.act_nr_of_cqes);
1257 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1258 if (!init_attr) {
1259 ret = -ENOMEM;
1260 ehea_error("no mem for ehea_qp_init_attr");
1261 goto out_free;
1264 init_attr->low_lat_rq1 = 1;
1265 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1266 init_attr->rq_count = 3;
1267 init_attr->qp_token = queue_token;
1268 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1269 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1270 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1271 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1272 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1273 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1274 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1275 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1276 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1277 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1278 init_attr->port_nr = port->logical_port_id;
1279 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1280 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1281 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1283 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1284 if (!pr->qp) {
1285 ehea_error("create_qp failed");
1286 ret = -EIO;
1287 goto out_free;
1290 if (netif_msg_ifup(port))
1291 ehea_info("QP: qp_nr=%d\n act_nr_snd_wqe=%d\n nr_rwqe_rq1=%d\n "
1292 "nr_rwqe_rq2=%d\n nr_rwqe_rq3=%d", init_attr->qp_nr,
1293 init_attr->act_nr_send_wqes,
1294 init_attr->act_nr_rwqes_rq1,
1295 init_attr->act_nr_rwqes_rq2,
1296 init_attr->act_nr_rwqes_rq3);
1298 ret = ehea_init_q_skba(&pr->sq_skba, init_attr->act_nr_send_wqes + 1);
1299 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1300 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1301 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1302 if (ret)
1303 goto out_free;
1305 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1306 if (ehea_gen_smrs(pr) != 0) {
1307 ret = -EIO;
1308 goto out_free;
1311 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1313 kfree(init_attr);
1315 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1317 pr->lro_mgr.max_aggr = pr->port->lro_max_aggr;
1318 pr->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1319 pr->lro_mgr.lro_arr = pr->lro_desc;
1320 pr->lro_mgr.get_skb_header = get_skb_hdr;
1321 pr->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1322 pr->lro_mgr.dev = port->netdev;
1323 pr->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1324 pr->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1326 ret = 0;
1327 goto out;
1329 out_free:
1330 kfree(init_attr);
1331 vfree(pr->sq_skba.arr);
1332 vfree(pr->rq1_skba.arr);
1333 vfree(pr->rq2_skba.arr);
1334 vfree(pr->rq3_skba.arr);
1335 ehea_destroy_qp(pr->qp);
1336 ehea_destroy_cq(pr->send_cq);
1337 ehea_destroy_cq(pr->recv_cq);
1338 ehea_destroy_eq(pr->eq);
1339 out:
1340 return ret;
1343 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1345 int ret, i;
1347 ret = ehea_destroy_qp(pr->qp);
1349 if (!ret) {
1350 ehea_destroy_cq(pr->send_cq);
1351 ehea_destroy_cq(pr->recv_cq);
1352 ehea_destroy_eq(pr->eq);
1354 for (i = 0; i < pr->rq1_skba.len; i++)
1355 if (pr->rq1_skba.arr[i])
1356 dev_kfree_skb(pr->rq1_skba.arr[i]);
1358 for (i = 0; i < pr->rq2_skba.len; i++)
1359 if (pr->rq2_skba.arr[i])
1360 dev_kfree_skb(pr->rq2_skba.arr[i]);
1362 for (i = 0; i < pr->rq3_skba.len; i++)
1363 if (pr->rq3_skba.arr[i])
1364 dev_kfree_skb(pr->rq3_skba.arr[i]);
1366 for (i = 0; i < pr->sq_skba.len; i++)
1367 if (pr->sq_skba.arr[i])
1368 dev_kfree_skb(pr->sq_skba.arr[i]);
1370 vfree(pr->rq1_skba.arr);
1371 vfree(pr->rq2_skba.arr);
1372 vfree(pr->rq3_skba.arr);
1373 vfree(pr->sq_skba.arr);
1374 ret = ehea_rem_smrs(pr);
1376 return ret;
1380 * The write_* functions store information in swqe which is used by
1381 * the hardware to calculate the ip/tcp/udp checksum
1384 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1385 const struct sk_buff *skb)
1387 swqe->ip_start = skb_network_offset(skb);
1388 swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1391 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1392 const struct sk_buff *skb)
1394 swqe->tcp_offset =
1395 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1397 swqe->tcp_end = (u16)skb->len - 1;
1400 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1401 const struct sk_buff *skb)
1403 swqe->tcp_offset =
1404 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1406 swqe->tcp_end = (u16)skb->len - 1;
1410 static void write_swqe2_TSO(struct sk_buff *skb,
1411 struct ehea_swqe *swqe, u32 lkey)
1413 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1414 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1415 int skb_data_size = skb->len - skb->data_len;
1416 int headersize;
1418 /* Packet is TCP with TSO enabled */
1419 swqe->tx_control |= EHEA_SWQE_TSO;
1420 swqe->mss = skb_shinfo(skb)->gso_size;
1421 /* copy only eth/ip/tcp headers to immediate data and
1422 * the rest of skb->data to sg1entry
1424 headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1426 skb_data_size = skb->len - skb->data_len;
1428 if (skb_data_size >= headersize) {
1429 /* copy immediate data */
1430 skb_copy_from_linear_data(skb, imm_data, headersize);
1431 swqe->immediate_data_length = headersize;
1433 if (skb_data_size > headersize) {
1434 /* set sg1entry data */
1435 sg1entry->l_key = lkey;
1436 sg1entry->len = skb_data_size - headersize;
1437 sg1entry->vaddr =
1438 ehea_map_vaddr(skb->data + headersize);
1439 swqe->descriptors++;
1441 } else
1442 ehea_error("cannot handle fragmented headers");
1445 static void write_swqe2_nonTSO(struct sk_buff *skb,
1446 struct ehea_swqe *swqe, u32 lkey)
1448 int skb_data_size = skb->len - skb->data_len;
1449 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1450 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1452 /* Packet is any nonTSO type
1454 * Copy as much as possible skb->data to immediate data and
1455 * the rest to sg1entry
1457 if (skb_data_size >= SWQE2_MAX_IMM) {
1458 /* copy immediate data */
1459 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1461 swqe->immediate_data_length = SWQE2_MAX_IMM;
1463 if (skb_data_size > SWQE2_MAX_IMM) {
1464 /* copy sg1entry data */
1465 sg1entry->l_key = lkey;
1466 sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1467 sg1entry->vaddr =
1468 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1469 swqe->descriptors++;
1471 } else {
1472 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1473 swqe->immediate_data_length = skb_data_size;
1477 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1478 struct ehea_swqe *swqe, u32 lkey)
1480 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1481 skb_frag_t *frag;
1482 int nfrags, sg1entry_contains_frag_data, i;
1484 nfrags = skb_shinfo(skb)->nr_frags;
1485 sg1entry = &swqe->u.immdata_desc.sg_entry;
1486 sg_list = (struct ehea_vsgentry*)&swqe->u.immdata_desc.sg_list;
1487 swqe->descriptors = 0;
1488 sg1entry_contains_frag_data = 0;
1490 if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1491 write_swqe2_TSO(skb, swqe, lkey);
1492 else
1493 write_swqe2_nonTSO(skb, swqe, lkey);
1495 /* write descriptors */
1496 if (nfrags > 0) {
1497 if (swqe->descriptors == 0) {
1498 /* sg1entry not yet used */
1499 frag = &skb_shinfo(skb)->frags[0];
1501 /* copy sg1entry data */
1502 sg1entry->l_key = lkey;
1503 sg1entry->len = frag->size;
1504 sg1entry->vaddr =
1505 ehea_map_vaddr(page_address(frag->page)
1506 + frag->page_offset);
1507 swqe->descriptors++;
1508 sg1entry_contains_frag_data = 1;
1511 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1513 frag = &skb_shinfo(skb)->frags[i];
1514 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1516 sgentry->l_key = lkey;
1517 sgentry->len = frag->size;
1518 sgentry->vaddr =
1519 ehea_map_vaddr(page_address(frag->page)
1520 + frag->page_offset);
1521 swqe->descriptors++;
1526 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1528 int ret = 0;
1529 u64 hret;
1530 u8 reg_type;
1532 /* De/Register untagged packets */
1533 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1534 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1535 port->logical_port_id,
1536 reg_type, port->mac_addr, 0, hcallid);
1537 if (hret != H_SUCCESS) {
1538 ehea_error("%sregistering bc address failed (tagged)",
1539 hcallid == H_REG_BCMC ? "" : "de");
1540 ret = -EIO;
1541 goto out_herr;
1544 /* De/Register VLAN packets */
1545 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1546 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1547 port->logical_port_id,
1548 reg_type, port->mac_addr, 0, hcallid);
1549 if (hret != H_SUCCESS) {
1550 ehea_error("%sregistering bc address failed (vlan)",
1551 hcallid == H_REG_BCMC ? "" : "de");
1552 ret = -EIO;
1554 out_herr:
1555 return ret;
1558 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1560 struct ehea_port *port = netdev_priv(dev);
1561 struct sockaddr *mac_addr = sa;
1562 struct hcp_ehea_port_cb0 *cb0;
1563 int ret;
1564 u64 hret;
1566 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1567 ret = -EADDRNOTAVAIL;
1568 goto out;
1571 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1572 if (!cb0) {
1573 ehea_error("no mem for cb0");
1574 ret = -ENOMEM;
1575 goto out;
1578 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1580 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1582 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1583 port->logical_port_id, H_PORT_CB0,
1584 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1585 if (hret != H_SUCCESS) {
1586 ret = -EIO;
1587 goto out_free;
1590 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1592 /* Deregister old MAC in pHYP */
1593 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1594 if (ret)
1595 goto out_free;
1597 port->mac_addr = cb0->port_mac_addr << 16;
1599 /* Register new MAC in pHYP */
1600 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1601 if (ret)
1602 goto out_free;
1604 ret = 0;
1605 out_free:
1606 kfree(cb0);
1607 out:
1608 return ret;
1611 static void ehea_promiscuous_error(u64 hret, int enable)
1613 if (hret == H_AUTHORITY)
1614 ehea_info("Hypervisor denied %sabling promiscuous mode",
1615 enable == 1 ? "en" : "dis");
1616 else
1617 ehea_error("failed %sabling promiscuous mode",
1618 enable == 1 ? "en" : "dis");
1621 static void ehea_promiscuous(struct net_device *dev, int enable)
1623 struct ehea_port *port = netdev_priv(dev);
1624 struct hcp_ehea_port_cb7 *cb7;
1625 u64 hret;
1627 if ((enable && port->promisc) || (!enable && !port->promisc))
1628 return;
1630 cb7 = kzalloc(PAGE_SIZE, GFP_ATOMIC);
1631 if (!cb7) {
1632 ehea_error("no mem for cb7");
1633 goto out;
1636 /* Modify Pxs_DUCQPN in CB7 */
1637 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1639 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1640 port->logical_port_id,
1641 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1642 if (hret) {
1643 ehea_promiscuous_error(hret, enable);
1644 goto out;
1647 port->promisc = enable;
1648 out:
1649 kfree(cb7);
1650 return;
1653 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1654 u32 hcallid)
1656 u64 hret;
1657 u8 reg_type;
1659 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1660 | EHEA_BCMC_UNTAGGED;
1662 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1663 port->logical_port_id,
1664 reg_type, mc_mac_addr, 0, hcallid);
1665 if (hret)
1666 goto out;
1668 reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1669 | EHEA_BCMC_VLANID_ALL;
1671 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1672 port->logical_port_id,
1673 reg_type, mc_mac_addr, 0, hcallid);
1674 out:
1675 return hret;
1678 static int ehea_drop_multicast_list(struct net_device *dev)
1680 struct ehea_port *port = netdev_priv(dev);
1681 struct ehea_mc_list *mc_entry = port->mc_list;
1682 struct list_head *pos;
1683 struct list_head *temp;
1684 int ret = 0;
1685 u64 hret;
1687 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1688 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1690 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1691 H_DEREG_BCMC);
1692 if (hret) {
1693 ehea_error("failed deregistering mcast MAC");
1694 ret = -EIO;
1697 list_del(pos);
1698 kfree(mc_entry);
1700 return ret;
1703 static void ehea_allmulti(struct net_device *dev, int enable)
1705 struct ehea_port *port = netdev_priv(dev);
1706 u64 hret;
1708 if (!port->allmulti) {
1709 if (enable) {
1710 /* Enable ALLMULTI */
1711 ehea_drop_multicast_list(dev);
1712 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1713 if (!hret)
1714 port->allmulti = 1;
1715 else
1716 ehea_error("failed enabling IFF_ALLMULTI");
1718 } else
1719 if (!enable) {
1720 /* Disable ALLMULTI */
1721 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1722 if (!hret)
1723 port->allmulti = 0;
1724 else
1725 ehea_error("failed disabling IFF_ALLMULTI");
1729 static void ehea_add_multicast_entry(struct ehea_port* port, u8* mc_mac_addr)
1731 struct ehea_mc_list *ehea_mcl_entry;
1732 u64 hret;
1734 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1735 if (!ehea_mcl_entry) {
1736 ehea_error("no mem for mcl_entry");
1737 return;
1740 INIT_LIST_HEAD(&ehea_mcl_entry->list);
1742 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1744 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1745 H_REG_BCMC);
1746 if (!hret)
1747 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1748 else {
1749 ehea_error("failed registering mcast MAC");
1750 kfree(ehea_mcl_entry);
1754 static void ehea_set_multicast_list(struct net_device *dev)
1756 struct ehea_port *port = netdev_priv(dev);
1757 struct dev_mc_list *k_mcl_entry;
1758 int ret, i;
1760 if (dev->flags & IFF_PROMISC) {
1761 ehea_promiscuous(dev, 1);
1762 return;
1764 ehea_promiscuous(dev, 0);
1766 if (dev->flags & IFF_ALLMULTI) {
1767 ehea_allmulti(dev, 1);
1768 return;
1770 ehea_allmulti(dev, 0);
1772 if (dev->mc_count) {
1773 ret = ehea_drop_multicast_list(dev);
1774 if (ret) {
1775 /* Dropping the current multicast list failed.
1776 * Enabling ALL_MULTI is the best we can do.
1778 ehea_allmulti(dev, 1);
1781 if (dev->mc_count > port->adapter->max_mc_mac) {
1782 ehea_info("Mcast registration limit reached (0x%lx). "
1783 "Use ALLMULTI!",
1784 port->adapter->max_mc_mac);
1785 goto out;
1788 for (i = 0, k_mcl_entry = dev->mc_list;
1789 i < dev->mc_count;
1790 i++, k_mcl_entry = k_mcl_entry->next) {
1791 ehea_add_multicast_entry(port, k_mcl_entry->dmi_addr);
1794 out:
1795 return;
1798 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1800 if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1801 return -EINVAL;
1802 dev->mtu = new_mtu;
1803 return 0;
1806 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
1807 struct ehea_swqe *swqe, u32 lkey)
1809 if (skb->protocol == htons(ETH_P_IP)) {
1810 const struct iphdr *iph = ip_hdr(skb);
1812 /* IPv4 */
1813 swqe->tx_control |= EHEA_SWQE_CRC
1814 | EHEA_SWQE_IP_CHECKSUM
1815 | EHEA_SWQE_TCP_CHECKSUM
1816 | EHEA_SWQE_IMM_DATA_PRESENT
1817 | EHEA_SWQE_DESCRIPTORS_PRESENT;
1819 write_ip_start_end(swqe, skb);
1821 if (iph->protocol == IPPROTO_UDP) {
1822 if ((iph->frag_off & IP_MF)
1823 || (iph->frag_off & IP_OFFSET))
1824 /* IP fragment, so don't change cs */
1825 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
1826 else
1827 write_udp_offset_end(swqe, skb);
1828 } else if (iph->protocol == IPPROTO_TCP) {
1829 write_tcp_offset_end(swqe, skb);
1832 /* icmp (big data) and ip segmentation packets (all other ip
1833 packets) do not require any special handling */
1835 } else {
1836 /* Other Ethernet Protocol */
1837 swqe->tx_control |= EHEA_SWQE_CRC
1838 | EHEA_SWQE_IMM_DATA_PRESENT
1839 | EHEA_SWQE_DESCRIPTORS_PRESENT;
1842 write_swqe2_data(skb, dev, swqe, lkey);
1845 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
1846 struct ehea_swqe *swqe)
1848 int nfrags = skb_shinfo(skb)->nr_frags;
1849 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
1850 skb_frag_t *frag;
1851 int i;
1853 if (skb->protocol == htons(ETH_P_IP)) {
1854 const struct iphdr *iph = ip_hdr(skb);
1856 /* IPv4 */
1857 write_ip_start_end(swqe, skb);
1859 if (iph->protocol == IPPROTO_TCP) {
1860 swqe->tx_control |= EHEA_SWQE_CRC
1861 | EHEA_SWQE_IP_CHECKSUM
1862 | EHEA_SWQE_TCP_CHECKSUM
1863 | EHEA_SWQE_IMM_DATA_PRESENT;
1865 write_tcp_offset_end(swqe, skb);
1867 } else if (iph->protocol == IPPROTO_UDP) {
1868 if ((iph->frag_off & IP_MF)
1869 || (iph->frag_off & IP_OFFSET))
1870 /* IP fragment, so don't change cs */
1871 swqe->tx_control |= EHEA_SWQE_CRC
1872 | EHEA_SWQE_IMM_DATA_PRESENT;
1873 else {
1874 swqe->tx_control |= EHEA_SWQE_CRC
1875 | EHEA_SWQE_IP_CHECKSUM
1876 | EHEA_SWQE_TCP_CHECKSUM
1877 | EHEA_SWQE_IMM_DATA_PRESENT;
1879 write_udp_offset_end(swqe, skb);
1881 } else {
1882 /* icmp (big data) and
1883 ip segmentation packets (all other ip packets) */
1884 swqe->tx_control |= EHEA_SWQE_CRC
1885 | EHEA_SWQE_IP_CHECKSUM
1886 | EHEA_SWQE_IMM_DATA_PRESENT;
1888 } else {
1889 /* Other Ethernet Protocol */
1890 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
1892 /* copy (immediate) data */
1893 if (nfrags == 0) {
1894 /* data is in a single piece */
1895 skb_copy_from_linear_data(skb, imm_data, skb->len);
1896 } else {
1897 /* first copy data from the skb->data buffer ... */
1898 skb_copy_from_linear_data(skb, imm_data,
1899 skb->len - skb->data_len);
1900 imm_data += skb->len - skb->data_len;
1902 /* ... then copy data from the fragments */
1903 for (i = 0; i < nfrags; i++) {
1904 frag = &skb_shinfo(skb)->frags[i];
1905 memcpy(imm_data,
1906 page_address(frag->page) + frag->page_offset,
1907 frag->size);
1908 imm_data += frag->size;
1911 swqe->immediate_data_length = skb->len;
1912 dev_kfree_skb(skb);
1915 static inline int ehea_hash_skb(struct sk_buff *skb, int num_qps)
1917 struct tcphdr *tcp;
1918 u32 tmp;
1920 if ((skb->protocol == htons(ETH_P_IP)) &&
1921 (ip_hdr(skb)->protocol == IPPROTO_TCP)) {
1922 tcp = (struct tcphdr*)(skb_network_header(skb) + (ip_hdr(skb)->ihl * 4));
1923 tmp = (tcp->source + (tcp->dest << 16)) % 31;
1924 tmp += ip_hdr(skb)->daddr % 31;
1925 return tmp % num_qps;
1927 else
1928 return 0;
1931 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
1933 struct ehea_port *port = netdev_priv(dev);
1934 struct ehea_swqe *swqe;
1935 unsigned long flags;
1936 u32 lkey;
1937 int swqe_index;
1938 struct ehea_port_res *pr;
1940 pr = &port->port_res[ehea_hash_skb(skb, port->num_tx_qps)];
1942 if (!spin_trylock(&pr->xmit_lock))
1943 return NETDEV_TX_BUSY;
1945 if (pr->queue_stopped) {
1946 spin_unlock(&pr->xmit_lock);
1947 return NETDEV_TX_BUSY;
1950 swqe = ehea_get_swqe(pr->qp, &swqe_index);
1951 memset(swqe, 0, SWQE_HEADER_SIZE);
1952 atomic_dec(&pr->swqe_avail);
1954 if (skb->len <= SWQE3_MAX_IMM) {
1955 u32 sig_iv = port->sig_comp_iv;
1956 u32 swqe_num = pr->swqe_id_counter;
1957 ehea_xmit3(skb, dev, swqe);
1958 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
1959 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
1960 if (pr->swqe_ll_count >= (sig_iv - 1)) {
1961 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
1962 sig_iv);
1963 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1964 pr->swqe_ll_count = 0;
1965 } else
1966 pr->swqe_ll_count += 1;
1967 } else {
1968 swqe->wr_id =
1969 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
1970 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
1971 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
1972 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
1973 pr->sq_skba.arr[pr->sq_skba.index] = skb;
1975 pr->sq_skba.index++;
1976 pr->sq_skba.index &= (pr->sq_skba.len - 1);
1978 lkey = pr->send_mr.lkey;
1979 ehea_xmit2(skb, dev, swqe, lkey);
1980 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1982 pr->swqe_id_counter += 1;
1984 if (port->vgrp && vlan_tx_tag_present(skb)) {
1985 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
1986 swqe->vlan_tag = vlan_tx_tag_get(skb);
1989 if (netif_msg_tx_queued(port)) {
1990 ehea_info("post swqe on QP %d", pr->qp->init_attr.qp_nr);
1991 ehea_dump(swqe, 512, "swqe");
1994 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
1995 netif_stop_queue(dev);
1996 swqe->tx_control |= EHEA_SWQE_PURGE;
1999 ehea_post_swqe(pr->qp, swqe);
2001 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2002 spin_lock_irqsave(&pr->netif_queue, flags);
2003 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2004 pr->p_stats.queue_stopped++;
2005 netif_stop_queue(dev);
2006 pr->queue_stopped = 1;
2008 spin_unlock_irqrestore(&pr->netif_queue, flags);
2010 dev->trans_start = jiffies;
2011 spin_unlock(&pr->xmit_lock);
2013 return NETDEV_TX_OK;
2016 static void ehea_vlan_rx_register(struct net_device *dev,
2017 struct vlan_group *grp)
2019 struct ehea_port *port = netdev_priv(dev);
2020 struct ehea_adapter *adapter = port->adapter;
2021 struct hcp_ehea_port_cb1 *cb1;
2022 u64 hret;
2024 port->vgrp = grp;
2026 cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2027 if (!cb1) {
2028 ehea_error("no mem for cb1");
2029 goto out;
2032 memset(cb1->vlan_filter, 0, sizeof(cb1->vlan_filter));
2034 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2035 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2036 if (hret != H_SUCCESS)
2037 ehea_error("modify_ehea_port failed");
2039 kfree(cb1);
2040 out:
2041 return;
2044 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
2046 struct ehea_port *port = netdev_priv(dev);
2047 struct ehea_adapter *adapter = port->adapter;
2048 struct hcp_ehea_port_cb1 *cb1;
2049 int index;
2050 u64 hret;
2052 cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2053 if (!cb1) {
2054 ehea_error("no mem for cb1");
2055 goto out;
2058 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2059 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2060 if (hret != H_SUCCESS) {
2061 ehea_error("query_ehea_port failed");
2062 goto out;
2065 index = (vid / 64);
2066 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2068 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2069 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2070 if (hret != H_SUCCESS)
2071 ehea_error("modify_ehea_port failed");
2072 out:
2073 kfree(cb1);
2074 return;
2077 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2079 struct ehea_port *port = netdev_priv(dev);
2080 struct ehea_adapter *adapter = port->adapter;
2081 struct hcp_ehea_port_cb1 *cb1;
2082 int index;
2083 u64 hret;
2085 vlan_group_set_device(port->vgrp, vid, NULL);
2087 cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2088 if (!cb1) {
2089 ehea_error("no mem for cb1");
2090 goto out;
2093 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2094 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2095 if (hret != H_SUCCESS) {
2096 ehea_error("query_ehea_port failed");
2097 goto out;
2100 index = (vid / 64);
2101 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2103 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2104 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2105 if (hret != H_SUCCESS)
2106 ehea_error("modify_ehea_port failed");
2107 out:
2108 kfree(cb1);
2109 return;
2112 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2114 int ret = -EIO;
2115 u64 hret;
2116 u16 dummy16 = 0;
2117 u64 dummy64 = 0;
2118 struct hcp_modify_qp_cb0* cb0;
2120 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2121 if (!cb0) {
2122 ret = -ENOMEM;
2123 goto out;
2126 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2127 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2128 if (hret != H_SUCCESS) {
2129 ehea_error("query_ehea_qp failed (1)");
2130 goto out;
2133 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2134 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2135 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2136 &dummy64, &dummy64, &dummy16, &dummy16);
2137 if (hret != H_SUCCESS) {
2138 ehea_error("modify_ehea_qp failed (1)");
2139 goto out;
2142 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2143 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2144 if (hret != H_SUCCESS) {
2145 ehea_error("query_ehea_qp failed (2)");
2146 goto out;
2149 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2150 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2151 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2152 &dummy64, &dummy64, &dummy16, &dummy16);
2153 if (hret != H_SUCCESS) {
2154 ehea_error("modify_ehea_qp failed (2)");
2155 goto out;
2158 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2159 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2160 if (hret != H_SUCCESS) {
2161 ehea_error("query_ehea_qp failed (3)");
2162 goto out;
2165 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2166 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2167 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2168 &dummy64, &dummy64, &dummy16, &dummy16);
2169 if (hret != H_SUCCESS) {
2170 ehea_error("modify_ehea_qp failed (3)");
2171 goto out;
2174 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2175 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2176 if (hret != H_SUCCESS) {
2177 ehea_error("query_ehea_qp failed (4)");
2178 goto out;
2181 ret = 0;
2182 out:
2183 kfree(cb0);
2184 return ret;
2187 static int ehea_port_res_setup(struct ehea_port *port, int def_qps,
2188 int add_tx_qps)
2190 int ret, i;
2191 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2192 enum ehea_eq_type eq_type = EHEA_EQ;
2194 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2195 EHEA_MAX_ENTRIES_EQ, 1);
2196 if (!port->qp_eq) {
2197 ret = -EINVAL;
2198 ehea_error("ehea_create_eq failed (qp_eq)");
2199 goto out_kill_eq;
2202 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2203 pr_cfg.max_entries_scq = sq_entries * 2;
2204 pr_cfg.max_entries_sq = sq_entries;
2205 pr_cfg.max_entries_rq1 = rq1_entries;
2206 pr_cfg.max_entries_rq2 = rq2_entries;
2207 pr_cfg.max_entries_rq3 = rq3_entries;
2209 pr_cfg_small_rx.max_entries_rcq = 1;
2210 pr_cfg_small_rx.max_entries_scq = sq_entries;
2211 pr_cfg_small_rx.max_entries_sq = sq_entries;
2212 pr_cfg_small_rx.max_entries_rq1 = 1;
2213 pr_cfg_small_rx.max_entries_rq2 = 1;
2214 pr_cfg_small_rx.max_entries_rq3 = 1;
2216 for (i = 0; i < def_qps; i++) {
2217 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2218 if (ret)
2219 goto out_clean_pr;
2221 for (i = def_qps; i < def_qps + add_tx_qps; i++) {
2222 ret = ehea_init_port_res(port, &port->port_res[i],
2223 &pr_cfg_small_rx, i);
2224 if (ret)
2225 goto out_clean_pr;
2228 return 0;
2230 out_clean_pr:
2231 while (--i >= 0)
2232 ehea_clean_portres(port, &port->port_res[i]);
2234 out_kill_eq:
2235 ehea_destroy_eq(port->qp_eq);
2236 return ret;
2239 static int ehea_clean_all_portres(struct ehea_port *port)
2241 int ret = 0;
2242 int i;
2244 for(i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2245 ret |= ehea_clean_portres(port, &port->port_res[i]);
2247 ret |= ehea_destroy_eq(port->qp_eq);
2249 return ret;
2252 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2254 if (adapter->active_ports)
2255 return;
2257 ehea_rem_mr(&adapter->mr);
2260 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2262 if (adapter->active_ports)
2263 return 0;
2265 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2268 static int ehea_up(struct net_device *dev)
2270 int ret, i;
2271 struct ehea_port *port = netdev_priv(dev);
2273 if (port->state == EHEA_PORT_UP)
2274 return 0;
2276 ret = ehea_port_res_setup(port, port->num_def_qps,
2277 port->num_add_tx_qps);
2278 if (ret) {
2279 ehea_error("port_res_failed");
2280 goto out;
2283 /* Set default QP for this port */
2284 ret = ehea_configure_port(port);
2285 if (ret) {
2286 ehea_error("ehea_configure_port failed. ret:%d", ret);
2287 goto out_clean_pr;
2290 ret = ehea_reg_interrupts(dev);
2291 if (ret) {
2292 ehea_error("reg_interrupts failed. ret:%d", ret);
2293 goto out_clean_pr;
2296 for(i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2297 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2298 if (ret) {
2299 ehea_error("activate_qp failed");
2300 goto out_free_irqs;
2304 for(i = 0; i < port->num_def_qps; i++) {
2305 ret = ehea_fill_port_res(&port->port_res[i]);
2306 if (ret) {
2307 ehea_error("out_free_irqs");
2308 goto out_free_irqs;
2312 ret = 0;
2313 port->state = EHEA_PORT_UP;
2314 goto out;
2316 out_free_irqs:
2317 ehea_free_interrupts(dev);
2319 out_clean_pr:
2320 ehea_clean_all_portres(port);
2321 out:
2322 if (ret)
2323 ehea_info("Failed starting %s. ret=%i", dev->name, ret);
2325 return ret;
2328 static void port_napi_disable(struct ehea_port *port)
2330 int i;
2332 for (i = 0; i < port->num_def_qps; i++)
2333 napi_disable(&port->port_res[i].napi);
2336 static void port_napi_enable(struct ehea_port *port)
2338 int i;
2340 for (i = 0; i < port->num_def_qps; i++)
2341 napi_enable(&port->port_res[i].napi);
2344 static int ehea_open(struct net_device *dev)
2346 int ret;
2347 struct ehea_port *port = netdev_priv(dev);
2349 down(&port->port_lock);
2351 if (netif_msg_ifup(port))
2352 ehea_info("enabling port %s", dev->name);
2354 ret = ehea_up(dev);
2355 if (!ret) {
2356 port_napi_enable(port);
2357 netif_start_queue(dev);
2360 up(&port->port_lock);
2362 return ret;
2365 static int ehea_down(struct net_device *dev)
2367 int ret;
2368 struct ehea_port *port = netdev_priv(dev);
2370 if (port->state == EHEA_PORT_DOWN)
2371 return 0;
2373 ehea_drop_multicast_list(dev);
2374 ehea_free_interrupts(dev);
2376 port_napi_disable(port);
2378 port->state = EHEA_PORT_DOWN;
2380 ret = ehea_clean_all_portres(port);
2381 if (ret)
2382 ehea_info("Failed freeing resources for %s. ret=%i",
2383 dev->name, ret);
2385 return ret;
2388 static int ehea_stop(struct net_device *dev)
2390 int ret;
2391 struct ehea_port *port = netdev_priv(dev);
2393 if (netif_msg_ifdown(port))
2394 ehea_info("disabling port %s", dev->name);
2396 flush_scheduled_work();
2397 down(&port->port_lock);
2398 netif_stop_queue(dev);
2399 ret = ehea_down(dev);
2400 up(&port->port_lock);
2401 return ret;
2404 void ehea_purge_sq(struct ehea_qp *orig_qp)
2406 struct ehea_qp qp = *orig_qp;
2407 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2408 struct ehea_swqe *swqe;
2409 int wqe_index;
2410 int i;
2412 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2413 swqe = ehea_get_swqe(&qp, &wqe_index);
2414 swqe->tx_control |= EHEA_SWQE_PURGE;
2418 int ehea_stop_qps(struct net_device *dev)
2420 struct ehea_port *port = netdev_priv(dev);
2421 struct ehea_adapter *adapter = port->adapter;
2422 struct hcp_modify_qp_cb0* cb0;
2423 int ret = -EIO;
2424 int dret;
2425 int i;
2426 u64 hret;
2427 u64 dummy64 = 0;
2428 u16 dummy16 = 0;
2430 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2431 if (!cb0) {
2432 ret = -ENOMEM;
2433 goto out;
2436 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2437 struct ehea_port_res *pr = &port->port_res[i];
2438 struct ehea_qp *qp = pr->qp;
2440 /* Purge send queue */
2441 ehea_purge_sq(qp);
2443 /* Disable queue pair */
2444 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2445 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2446 cb0);
2447 if (hret != H_SUCCESS) {
2448 ehea_error("query_ehea_qp failed (1)");
2449 goto out;
2452 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2453 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2455 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2456 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2457 1), cb0, &dummy64,
2458 &dummy64, &dummy16, &dummy16);
2459 if (hret != H_SUCCESS) {
2460 ehea_error("modify_ehea_qp failed (1)");
2461 goto out;
2464 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2465 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2466 cb0);
2467 if (hret != H_SUCCESS) {
2468 ehea_error("query_ehea_qp failed (2)");
2469 goto out;
2472 /* deregister shared memory regions */
2473 dret = ehea_rem_smrs(pr);
2474 if (dret) {
2475 ehea_error("unreg shared memory region failed");
2476 goto out;
2480 ret = 0;
2481 out:
2482 kfree(cb0);
2484 return ret;
2487 void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res * pr)
2489 struct ehea_qp qp = *orig_qp;
2490 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2491 struct ehea_rwqe *rwqe;
2492 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2493 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2494 struct sk_buff *skb;
2495 u32 lkey = pr->recv_mr.lkey;
2498 int i;
2499 int index;
2501 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2502 rwqe = ehea_get_next_rwqe(&qp, 2);
2503 rwqe->sg_list[0].l_key = lkey;
2504 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2505 skb = skba_rq2[index];
2506 if (skb)
2507 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2510 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2511 rwqe = ehea_get_next_rwqe(&qp, 3);
2512 rwqe->sg_list[0].l_key = lkey;
2513 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2514 skb = skba_rq3[index];
2515 if (skb)
2516 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2520 int ehea_restart_qps(struct net_device *dev)
2522 struct ehea_port *port = netdev_priv(dev);
2523 struct ehea_adapter *adapter = port->adapter;
2524 int ret = 0;
2525 int i;
2527 struct hcp_modify_qp_cb0* cb0;
2528 u64 hret;
2529 u64 dummy64 = 0;
2530 u16 dummy16 = 0;
2532 cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2533 if (!cb0) {
2534 ret = -ENOMEM;
2535 goto out;
2538 for (i = 0; i < (port->num_def_qps + port->num_add_tx_qps); i++) {
2539 struct ehea_port_res *pr = &port->port_res[i];
2540 struct ehea_qp *qp = pr->qp;
2542 ret = ehea_gen_smrs(pr);
2543 if (ret) {
2544 ehea_error("creation of shared memory regions failed");
2545 goto out;
2548 ehea_update_rqs(qp, pr);
2550 /* Enable queue pair */
2551 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2552 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2553 cb0);
2554 if (hret != H_SUCCESS) {
2555 ehea_error("query_ehea_qp failed (1)");
2556 goto out;
2559 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2560 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2562 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2563 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2564 1), cb0, &dummy64,
2565 &dummy64, &dummy16, &dummy16);
2566 if (hret != H_SUCCESS) {
2567 ehea_error("modify_ehea_qp failed (1)");
2568 goto out;
2571 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2572 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2573 cb0);
2574 if (hret != H_SUCCESS) {
2575 ehea_error("query_ehea_qp failed (2)");
2576 goto out;
2579 /* refill entire queue */
2580 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2581 ehea_refill_rq2(pr, 0);
2582 ehea_refill_rq3(pr, 0);
2584 out:
2585 kfree(cb0);
2587 return ret;
2590 static void ehea_reset_port(struct work_struct *work)
2592 int ret;
2593 struct ehea_port *port =
2594 container_of(work, struct ehea_port, reset_task);
2595 struct net_device *dev = port->netdev;
2597 port->resets++;
2598 down(&port->port_lock);
2599 netif_stop_queue(dev);
2601 port_napi_disable(port);
2603 ehea_down(dev);
2605 ret = ehea_up(dev);
2606 if (ret)
2607 goto out;
2609 ehea_set_multicast_list(dev);
2611 if (netif_msg_timer(port))
2612 ehea_info("Device %s resetted successfully", dev->name);
2614 port_napi_enable(port);
2616 netif_wake_queue(dev);
2617 out:
2618 up(&port->port_lock);
2619 return;
2622 static void ehea_rereg_mrs(struct work_struct *work)
2624 int ret, i;
2625 struct ehea_adapter *adapter;
2627 down(&dlpar_mem_lock);
2628 ehea_info("LPAR memory enlarged - re-initializing driver");
2630 list_for_each_entry(adapter, &adapter_list, list)
2631 if (adapter->active_ports) {
2632 /* Shutdown all ports */
2633 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2634 struct ehea_port *port = adapter->port[i];
2636 if (port) {
2637 struct net_device *dev = port->netdev;
2639 if (dev->flags & IFF_UP) {
2640 down(&port->port_lock);
2641 netif_stop_queue(dev);
2642 ret = ehea_stop_qps(dev);
2643 if (ret) {
2644 up(&port->port_lock);
2645 goto out;
2647 port_napi_disable(port);
2648 up(&port->port_lock);
2653 /* Unregister old memory region */
2654 ret = ehea_rem_mr(&adapter->mr);
2655 if (ret) {
2656 ehea_error("unregister MR failed - driver"
2657 " inoperable!");
2658 goto out;
2662 ehea_destroy_busmap();
2663 ret = ehea_create_busmap();
2664 if (ret) {
2665 ehea_error("creating ehea busmap failed");
2666 goto out;
2669 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2671 list_for_each_entry(adapter, &adapter_list, list)
2672 if (adapter->active_ports) {
2673 /* Register new memory region */
2674 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2675 if (ret) {
2676 ehea_error("register MR failed - driver"
2677 " inoperable!");
2678 goto out;
2681 /* Restart all ports */
2682 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2683 struct ehea_port *port = adapter->port[i];
2685 if (port) {
2686 struct net_device *dev = port->netdev;
2688 if (dev->flags & IFF_UP) {
2689 down(&port->port_lock);
2690 port_napi_enable(port);
2691 ret = ehea_restart_qps(dev);
2692 if (!ret)
2693 netif_wake_queue(dev);
2694 up(&port->port_lock);
2699 up(&dlpar_mem_lock);
2700 ehea_info("re-initializing driver complete");
2701 out:
2702 return;
2705 static void ehea_tx_watchdog(struct net_device *dev)
2707 struct ehea_port *port = netdev_priv(dev);
2709 if (netif_carrier_ok(dev) &&
2710 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2711 schedule_work(&port->reset_task);
2714 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2716 struct hcp_query_ehea *cb;
2717 u64 hret;
2718 int ret;
2720 cb = kzalloc(PAGE_SIZE, GFP_KERNEL);
2721 if (!cb) {
2722 ret = -ENOMEM;
2723 goto out;
2726 hret = ehea_h_query_ehea(adapter->handle, cb);
2728 if (hret != H_SUCCESS) {
2729 ret = -EIO;
2730 goto out_herr;
2733 adapter->max_mc_mac = cb->max_mc_mac - 1;
2734 ret = 0;
2736 out_herr:
2737 kfree(cb);
2738 out:
2739 return ret;
2742 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2744 struct hcp_ehea_port_cb4 *cb4;
2745 u64 hret;
2746 int ret = 0;
2748 *jumbo = 0;
2750 /* (Try to) enable *jumbo frames */
2751 cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2752 if (!cb4) {
2753 ehea_error("no mem for cb4");
2754 ret = -ENOMEM;
2755 goto out;
2756 } else {
2757 hret = ehea_h_query_ehea_port(port->adapter->handle,
2758 port->logical_port_id,
2759 H_PORT_CB4,
2760 H_PORT_CB4_JUMBO, cb4);
2761 if (hret == H_SUCCESS) {
2762 if (cb4->jumbo_frame)
2763 *jumbo = 1;
2764 else {
2765 cb4->jumbo_frame = 1;
2766 hret = ehea_h_modify_ehea_port(port->adapter->
2767 handle,
2768 port->
2769 logical_port_id,
2770 H_PORT_CB4,
2771 H_PORT_CB4_JUMBO,
2772 cb4);
2773 if (hret == H_SUCCESS)
2774 *jumbo = 1;
2776 } else
2777 ret = -EINVAL;
2779 kfree(cb4);
2781 out:
2782 return ret;
2785 static ssize_t ehea_show_port_id(struct device *dev,
2786 struct device_attribute *attr, char *buf)
2788 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2789 return sprintf(buf, "%d", port->logical_port_id);
2792 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2793 NULL);
2795 static void __devinit logical_port_release(struct device *dev)
2797 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2798 of_node_put(port->ofdev.node);
2801 static int ehea_driver_sysfs_add(struct device *dev,
2802 struct device_driver *driver)
2804 int ret;
2806 ret = sysfs_create_link(&driver->kobj, &dev->kobj,
2807 kobject_name(&dev->kobj));
2808 if (ret == 0) {
2809 ret = sysfs_create_link(&dev->kobj, &driver->kobj,
2810 "driver");
2811 if (ret)
2812 sysfs_remove_link(&driver->kobj,
2813 kobject_name(&dev->kobj));
2815 return ret;
2818 static void ehea_driver_sysfs_remove(struct device *dev,
2819 struct device_driver *driver)
2821 struct device_driver *drv = driver;
2823 if (drv) {
2824 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
2825 sysfs_remove_link(&dev->kobj, "driver");
2829 static struct device *ehea_register_port(struct ehea_port *port,
2830 struct device_node *dn)
2832 int ret;
2834 port->ofdev.node = of_node_get(dn);
2835 port->ofdev.dev.parent = &port->adapter->ebus_dev->ofdev.dev;
2836 port->ofdev.dev.bus = &ibmebus_bus_type;
2838 sprintf(port->ofdev.dev.bus_id, "port%d", port_name_cnt++);
2839 port->ofdev.dev.release = logical_port_release;
2841 ret = of_device_register(&port->ofdev);
2842 if (ret) {
2843 ehea_error("failed to register device. ret=%d", ret);
2844 goto out;
2847 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2848 if (ret) {
2849 ehea_error("failed to register attributes, ret=%d", ret);
2850 goto out_unreg_of_dev;
2853 ret = ehea_driver_sysfs_add(&port->ofdev.dev, &ehea_driver.driver);
2854 if (ret) {
2855 ehea_error("failed to register sysfs driver link");
2856 goto out_rem_dev_file;
2859 return &port->ofdev.dev;
2861 out_rem_dev_file:
2862 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2863 out_unreg_of_dev:
2864 of_device_unregister(&port->ofdev);
2865 out:
2866 return NULL;
2869 static void ehea_unregister_port(struct ehea_port *port)
2871 ehea_driver_sysfs_remove(&port->ofdev.dev, &ehea_driver.driver);
2872 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2873 of_device_unregister(&port->ofdev);
2876 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2877 u32 logical_port_id,
2878 struct device_node *dn)
2880 int ret;
2881 struct net_device *dev;
2882 struct ehea_port *port;
2883 struct device *port_dev;
2884 int jumbo;
2886 /* allocate memory for the port structures */
2887 dev = alloc_etherdev(sizeof(struct ehea_port));
2889 if (!dev) {
2890 ehea_error("no mem for net_device");
2891 ret = -ENOMEM;
2892 goto out_err;
2895 port = netdev_priv(dev);
2897 sema_init(&port->port_lock, 1);
2898 port->state = EHEA_PORT_DOWN;
2899 port->sig_comp_iv = sq_entries / 10;
2901 port->adapter = adapter;
2902 port->netdev = dev;
2903 port->logical_port_id = logical_port_id;
2905 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2907 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2908 if (!port->mc_list) {
2909 ret = -ENOMEM;
2910 goto out_free_ethdev;
2913 INIT_LIST_HEAD(&port->mc_list->list);
2915 ret = ehea_sense_port_attr(port);
2916 if (ret)
2917 goto out_free_mc_list;
2919 port_dev = ehea_register_port(port, dn);
2920 if (!port_dev)
2921 goto out_free_mc_list;
2923 SET_NETDEV_DEV(dev, port_dev);
2925 /* initialize net_device structure */
2926 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
2928 dev->open = ehea_open;
2929 #ifdef CONFIG_NET_POLL_CONTROLLER
2930 dev->poll_controller = ehea_netpoll;
2931 #endif
2932 dev->stop = ehea_stop;
2933 dev->hard_start_xmit = ehea_start_xmit;
2934 dev->get_stats = ehea_get_stats;
2935 dev->set_multicast_list = ehea_set_multicast_list;
2936 dev->set_mac_address = ehea_set_mac_addr;
2937 dev->change_mtu = ehea_change_mtu;
2938 dev->vlan_rx_register = ehea_vlan_rx_register;
2939 dev->vlan_rx_add_vid = ehea_vlan_rx_add_vid;
2940 dev->vlan_rx_kill_vid = ehea_vlan_rx_kill_vid;
2941 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
2942 | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX
2943 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
2944 | NETIF_F_LLTX;
2945 dev->tx_timeout = &ehea_tx_watchdog;
2946 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
2948 INIT_WORK(&port->reset_task, ehea_reset_port);
2950 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2951 if (ret) {
2952 ret = -EIO;
2953 goto out_unreg_port;
2956 ehea_set_ethtool_ops(dev);
2958 ret = register_netdev(dev);
2959 if (ret) {
2960 ehea_error("register_netdev failed. ret=%d", ret);
2961 goto out_dereg_bc;
2964 port->lro_max_aggr = lro_max_aggr;
2966 ret = ehea_get_jumboframe_status(port, &jumbo);
2967 if (ret)
2968 ehea_error("failed determining jumbo frame status for %s",
2969 port->netdev->name);
2971 ehea_info("%s: Jumbo frames are %sabled", dev->name,
2972 jumbo == 1 ? "en" : "dis");
2974 adapter->active_ports++;
2976 return port;
2978 out_dereg_bc:
2979 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2981 out_unreg_port:
2982 ehea_unregister_port(port);
2984 out_free_mc_list:
2985 kfree(port->mc_list);
2987 out_free_ethdev:
2988 free_netdev(dev);
2990 out_err:
2991 ehea_error("setting up logical port with id=%d failed, ret=%d",
2992 logical_port_id, ret);
2993 return NULL;
2996 static void ehea_shutdown_single_port(struct ehea_port *port)
2998 unregister_netdev(port->netdev);
2999 ehea_unregister_port(port);
3000 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
3001 kfree(port->mc_list);
3002 free_netdev(port->netdev);
3003 port->adapter->active_ports--;
3006 static int ehea_setup_ports(struct ehea_adapter *adapter)
3008 struct device_node *lhea_dn;
3009 struct device_node *eth_dn = NULL;
3011 const u32 *dn_log_port_id;
3012 int i = 0;
3014 lhea_dn = adapter->ebus_dev->ofdev.node;
3015 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3017 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3018 NULL);
3019 if (!dn_log_port_id) {
3020 ehea_error("bad device node: eth_dn name=%s",
3021 eth_dn->full_name);
3022 continue;
3025 if (ehea_add_adapter_mr(adapter)) {
3026 ehea_error("creating MR failed");
3027 of_node_put(eth_dn);
3028 return -EIO;
3031 adapter->port[i] = ehea_setup_single_port(adapter,
3032 *dn_log_port_id,
3033 eth_dn);
3034 if (adapter->port[i])
3035 ehea_info("%s -> logical port id #%d",
3036 adapter->port[i]->netdev->name,
3037 *dn_log_port_id);
3038 else
3039 ehea_remove_adapter_mr(adapter);
3041 i++;
3044 return 0;
3047 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3048 u32 logical_port_id)
3050 struct device_node *lhea_dn;
3051 struct device_node *eth_dn = NULL;
3052 const u32 *dn_log_port_id;
3054 lhea_dn = adapter->ebus_dev->ofdev.node;
3055 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3057 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3058 NULL);
3059 if (dn_log_port_id)
3060 if (*dn_log_port_id == logical_port_id)
3061 return eth_dn;
3064 return NULL;
3067 static ssize_t ehea_probe_port(struct device *dev,
3068 struct device_attribute *attr,
3069 const char *buf, size_t count)
3071 struct ehea_adapter *adapter = dev->driver_data;
3072 struct ehea_port *port;
3073 struct device_node *eth_dn = NULL;
3074 int i;
3076 u32 logical_port_id;
3078 sscanf(buf, "%d", &logical_port_id);
3080 port = ehea_get_port(adapter, logical_port_id);
3082 if (port) {
3083 ehea_info("adding port with logical port id=%d failed. port "
3084 "already configured as %s.", logical_port_id,
3085 port->netdev->name);
3086 return -EINVAL;
3089 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3091 if (!eth_dn) {
3092 ehea_info("no logical port with id %d found", logical_port_id);
3093 return -EINVAL;
3096 if (ehea_add_adapter_mr(adapter)) {
3097 ehea_error("creating MR failed");
3098 return -EIO;
3101 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3103 of_node_put(eth_dn);
3105 if (port) {
3106 for (i=0; i < EHEA_MAX_PORTS; i++)
3107 if (!adapter->port[i]) {
3108 adapter->port[i] = port;
3109 break;
3112 ehea_info("added %s (logical port id=%d)", port->netdev->name,
3113 logical_port_id);
3114 } else {
3115 ehea_remove_adapter_mr(adapter);
3116 return -EIO;
3119 return (ssize_t) count;
3122 static ssize_t ehea_remove_port(struct device *dev,
3123 struct device_attribute *attr,
3124 const char *buf, size_t count)
3126 struct ehea_adapter *adapter = dev->driver_data;
3127 struct ehea_port *port;
3128 int i;
3129 u32 logical_port_id;
3131 sscanf(buf, "%d", &logical_port_id);
3133 port = ehea_get_port(adapter, logical_port_id);
3135 if (port) {
3136 ehea_info("removed %s (logical port id=%d)", port->netdev->name,
3137 logical_port_id);
3139 ehea_shutdown_single_port(port);
3141 for (i=0; i < EHEA_MAX_PORTS; i++)
3142 if (adapter->port[i] == port) {
3143 adapter->port[i] = NULL;
3144 break;
3146 } else {
3147 ehea_error("removing port with logical port id=%d failed. port "
3148 "not configured.", logical_port_id);
3149 return -EINVAL;
3152 ehea_remove_adapter_mr(adapter);
3154 return (ssize_t) count;
3157 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
3158 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
3160 int ehea_create_device_sysfs(struct ibmebus_dev *dev)
3162 int ret = device_create_file(&dev->ofdev.dev, &dev_attr_probe_port);
3163 if (ret)
3164 goto out;
3166 ret = device_create_file(&dev->ofdev.dev, &dev_attr_remove_port);
3167 out:
3168 return ret;
3171 void ehea_remove_device_sysfs(struct ibmebus_dev *dev)
3173 device_remove_file(&dev->ofdev.dev, &dev_attr_probe_port);
3174 device_remove_file(&dev->ofdev.dev, &dev_attr_remove_port);
3177 static int __devinit ehea_probe_adapter(struct ibmebus_dev *dev,
3178 const struct of_device_id *id)
3180 struct ehea_adapter *adapter;
3181 const u64 *adapter_handle;
3182 int ret;
3184 if (!dev || !dev->ofdev.node) {
3185 ehea_error("Invalid ibmebus device probed");
3186 return -EINVAL;
3189 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3190 if (!adapter) {
3191 ret = -ENOMEM;
3192 dev_err(&dev->ofdev.dev, "no mem for ehea_adapter\n");
3193 goto out;
3196 list_add(&adapter->list, &adapter_list);
3198 adapter->ebus_dev = dev;
3200 adapter_handle = of_get_property(dev->ofdev.node, "ibm,hea-handle",
3201 NULL);
3202 if (adapter_handle)
3203 adapter->handle = *adapter_handle;
3205 if (!adapter->handle) {
3206 dev_err(&dev->ofdev.dev, "failed getting handle for adapter"
3207 " '%s'\n", dev->ofdev.node->full_name);
3208 ret = -ENODEV;
3209 goto out_free_ad;
3212 adapter->pd = EHEA_PD_ID;
3214 dev->ofdev.dev.driver_data = adapter;
3217 /* initialize adapter and ports */
3218 /* get adapter properties */
3219 ret = ehea_sense_adapter_attr(adapter);
3220 if (ret) {
3221 dev_err(&dev->ofdev.dev, "sense_adapter_attr failed: %d", ret);
3222 goto out_free_ad;
3225 adapter->neq = ehea_create_eq(adapter,
3226 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3227 if (!adapter->neq) {
3228 ret = -EIO;
3229 dev_err(&dev->ofdev.dev, "NEQ creation failed");
3230 goto out_free_ad;
3233 tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3234 (unsigned long)adapter);
3236 ret = ibmebus_request_irq(NULL, adapter->neq->attr.ist1,
3237 ehea_interrupt_neq, IRQF_DISABLED,
3238 "ehea_neq", adapter);
3239 if (ret) {
3240 dev_err(&dev->ofdev.dev, "requesting NEQ IRQ failed");
3241 goto out_kill_eq;
3244 ret = ehea_create_device_sysfs(dev);
3245 if (ret)
3246 goto out_free_irq;
3248 ret = ehea_setup_ports(adapter);
3249 if (ret) {
3250 dev_err(&dev->ofdev.dev, "setup_ports failed");
3251 goto out_rem_dev_sysfs;
3254 ret = 0;
3255 goto out;
3257 out_rem_dev_sysfs:
3258 ehea_remove_device_sysfs(dev);
3260 out_free_irq:
3261 ibmebus_free_irq(NULL, adapter->neq->attr.ist1, adapter);
3263 out_kill_eq:
3264 ehea_destroy_eq(adapter->neq);
3266 out_free_ad:
3267 kfree(adapter);
3268 out:
3269 return ret;
3272 static int __devexit ehea_remove(struct ibmebus_dev *dev)
3274 struct ehea_adapter *adapter = dev->ofdev.dev.driver_data;
3275 int i;
3277 for (i = 0; i < EHEA_MAX_PORTS; i++)
3278 if (adapter->port[i]) {
3279 ehea_shutdown_single_port(adapter->port[i]);
3280 adapter->port[i] = NULL;
3283 ehea_remove_device_sysfs(dev);
3285 flush_scheduled_work();
3287 ibmebus_free_irq(NULL, adapter->neq->attr.ist1, adapter);
3288 tasklet_kill(&adapter->neq_tasklet);
3290 ehea_destroy_eq(adapter->neq);
3291 ehea_remove_adapter_mr(adapter);
3292 list_del(&adapter->list);
3294 kfree(adapter);
3296 return 0;
3299 static int check_module_parm(void)
3301 int ret = 0;
3303 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3304 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3305 ehea_info("Bad parameter: rq1_entries");
3306 ret = -EINVAL;
3308 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3309 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3310 ehea_info("Bad parameter: rq2_entries");
3311 ret = -EINVAL;
3313 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3314 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3315 ehea_info("Bad parameter: rq3_entries");
3316 ret = -EINVAL;
3318 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3319 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3320 ehea_info("Bad parameter: sq_entries");
3321 ret = -EINVAL;
3324 return ret;
3327 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3328 char *buf)
3330 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3333 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3334 ehea_show_capabilities, NULL);
3336 int __init ehea_module_init(void)
3338 int ret;
3340 printk(KERN_INFO "IBM eHEA ethernet device driver (Release %s)\n",
3341 DRV_VERSION);
3344 INIT_WORK(&ehea_rereg_mr_task, ehea_rereg_mrs);
3345 sema_init(&dlpar_mem_lock, 1);
3347 ret = check_module_parm();
3348 if (ret)
3349 goto out;
3351 ret = ehea_create_busmap();
3352 if (ret)
3353 goto out;
3355 ret = ibmebus_register_driver(&ehea_driver);
3356 if (ret) {
3357 ehea_error("failed registering eHEA device driver on ebus");
3358 goto out;
3361 ret = driver_create_file(&ehea_driver.driver,
3362 &driver_attr_capabilities);
3363 if (ret) {
3364 ehea_error("failed to register capabilities attribute, ret=%d",
3365 ret);
3366 ibmebus_unregister_driver(&ehea_driver);
3367 goto out;
3370 out:
3371 return ret;
3374 static void __exit ehea_module_exit(void)
3376 flush_scheduled_work();
3377 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3378 ibmebus_unregister_driver(&ehea_driver);
3379 ehea_destroy_busmap();
3382 module_init(ehea_module_init);
3383 module_exit(ehea_module_exit);