Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / net / ehea / ehea_main.c
1 /*
2  *  linux/drivers/net/ehea/ehea_main.c
3  *
4  *  eHEA ethernet device driver for IBM eServer System p
5  *
6  *  (C) Copyright IBM Corp. 2006
7  *
8  *  Authors:
9  *       Christoph Raisch <raisch@de.ibm.com>
10  *       Jan-Bernd Themann <themann@de.ibm.com>
11  *       Thomas Klein <tklein@de.ibm.com>
12  *
13  *
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.
18  *
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.
23  *
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.
27  */
28
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>
37
38 #include "ehea.h"
39 #include "ehea_qmr.h"
40 #include "ehea_phyp.h"
41
42
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);
47
48
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 num_tx_qps = EHEA_NUM_TX_QP;
56 static int prop_carrier_state = 0;
57
58 module_param(msg_level, int, 0);
59 module_param(rq1_entries, int, 0);
60 module_param(rq2_entries, int, 0);
61 module_param(rq3_entries, int, 0);
62 module_param(sq_entries, int, 0);
63 module_param(prop_carrier_state, int, 0);
64 module_param(use_mcs, int, 0);
65 module_param(num_tx_qps, int, 0);
66
67 MODULE_PARM_DESC(num_tx_qps, "Number of TX-QPS");
68 MODULE_PARM_DESC(msg_level, "msg_level");
69 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
70                  "port to stack. 1:yes, 0:no.  Default = 0 ");
71 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
72                  "[2^x - 1], x = [6..14]. Default = "
73                  __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
74 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
75                  "[2^x - 1], x = [6..14]. Default = "
76                  __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
77 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
78                  "[2^x - 1], x = [6..14]. Default = "
79                  __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
80 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue  "
81                  "[2^x - 1], x = [6..14]. Default = "
82                  __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
83 MODULE_PARM_DESC(use_mcs, " 0:NAPI, 1:Multiple receive queues, Default = 0 ");
84
85 static int port_name_cnt = 0;
86 static LIST_HEAD(adapter_list);
87 u64 ehea_driver_flags = 0;
88 struct workqueue_struct *ehea_driver_wq;
89 struct work_struct ehea_rereg_mr_task;
90
91
92 static int __devinit ehea_probe_adapter(struct ibmebus_dev *dev,
93                                         const struct of_device_id *id);
94
95 static int __devexit ehea_remove(struct ibmebus_dev *dev);
96
97 static struct of_device_id ehea_device_table[] = {
98         {
99                 .name = "lhea",
100                 .compatible = "IBM,lhea",
101         },
102         {},
103 };
104
105 static struct ibmebus_driver ehea_driver = {
106         .name = "ehea",
107         .id_table = ehea_device_table,
108         .probe = ehea_probe_adapter,
109         .remove = ehea_remove,
110 };
111
112 void ehea_dump(void *adr, int len, char *msg) {
113         int x;
114         unsigned char *deb = adr;
115         for (x = 0; x < len; x += 16) {
116                 printk(DRV_NAME " %s adr=%p ofs=%04x %016lx %016lx\n", msg,
117                           deb, x, *((u64*)&deb[0]), *((u64*)&deb[8]));
118                 deb += 16;
119         }
120 }
121
122 static struct net_device_stats *ehea_get_stats(struct net_device *dev)
123 {
124         struct ehea_port *port = netdev_priv(dev);
125         struct net_device_stats *stats = &port->stats;
126         struct hcp_ehea_port_cb2 *cb2;
127         u64 hret, rx_packets;
128         int i;
129
130         memset(stats, 0, sizeof(*stats));
131
132         cb2 = kzalloc(PAGE_SIZE, GFP_KERNEL);
133         if (!cb2) {
134                 ehea_error("no mem for cb2");
135                 goto out;
136         }
137
138         hret = ehea_h_query_ehea_port(port->adapter->handle,
139                                       port->logical_port_id,
140                                       H_PORT_CB2, H_PORT_CB2_ALL, cb2);
141         if (hret != H_SUCCESS) {
142                 ehea_error("query_ehea_port failed");
143                 goto out_herr;
144         }
145
146         if (netif_msg_hw(port))
147                 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
148
149         rx_packets = 0;
150         for (i = 0; i < port->num_def_qps; i++)
151                 rx_packets += port->port_res[i].rx_packets;
152
153         stats->tx_packets = cb2->txucp + cb2->txmcp + cb2->txbcp;
154         stats->multicast = cb2->rxmcp;
155         stats->rx_errors = cb2->rxuerr;
156         stats->rx_bytes = cb2->rxo;
157         stats->tx_bytes = cb2->txo;
158         stats->rx_packets = rx_packets;
159
160 out_herr:
161         kfree(cb2);
162 out:
163         return stats;
164 }
165
166 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
167 {
168         struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
169         struct net_device *dev = pr->port->netdev;
170         int max_index_mask = pr->rq1_skba.len - 1;
171         int i;
172
173         if (!nr_of_wqes)
174                 return;
175
176         for (i = 0; i < nr_of_wqes; i++) {
177                 if (!skb_arr_rq1[index]) {
178                         skb_arr_rq1[index] = netdev_alloc_skb(dev,
179                                                               EHEA_L_PKT_SIZE);
180                         if (!skb_arr_rq1[index]) {
181                                 ehea_error("%s: no mem for skb/%d wqes filled",
182                                            dev->name, i);
183                                 break;
184                         }
185                 }
186                 index--;
187                 index &= max_index_mask;
188         }
189         /* Ring doorbell */
190         ehea_update_rq1a(pr->qp, i);
191 }
192
193 static int ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
194 {
195         int ret = 0;
196         struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
197         struct net_device *dev = pr->port->netdev;
198         int i;
199
200         for (i = 0; i < pr->rq1_skba.len; i++) {
201                 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
202                 if (!skb_arr_rq1[i]) {
203                         ehea_error("%s: no mem for skb/%d wqes filled",
204                                    dev->name, i);
205                         ret = -ENOMEM;
206                         goto out;
207                 }
208         }
209         /* Ring doorbell */
210         ehea_update_rq1a(pr->qp, nr_rq1a);
211 out:
212         return ret;
213 }
214
215 static int ehea_refill_rq_def(struct ehea_port_res *pr,
216                               struct ehea_q_skb_arr *q_skba, int rq_nr,
217                               int num_wqes, int wqe_type, int packet_size)
218 {
219         struct net_device *dev = pr->port->netdev;
220         struct ehea_qp *qp = pr->qp;
221         struct sk_buff **skb_arr = q_skba->arr;
222         struct ehea_rwqe *rwqe;
223         int i, index, max_index_mask, fill_wqes;
224         int ret = 0;
225
226         fill_wqes = q_skba->os_skbs + num_wqes;
227
228         if (!fill_wqes)
229                 return ret;
230
231         index = q_skba->index;
232         max_index_mask = q_skba->len - 1;
233         for (i = 0; i < fill_wqes; i++) {
234                 struct sk_buff *skb = netdev_alloc_skb(dev, packet_size);
235                 if (!skb) {
236                         ehea_error("%s: no mem for skb/%d wqes filled",
237                                    pr->port->netdev->name, i);
238                         q_skba->os_skbs = fill_wqes - i;
239                         ret = -ENOMEM;
240                         break;
241                 }
242                 skb_reserve(skb, NET_IP_ALIGN);
243
244                 skb_arr[index] = skb;
245
246                 rwqe = ehea_get_next_rwqe(qp, rq_nr);
247                 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
248                             | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
249                 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
250                 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
251                 rwqe->sg_list[0].len = packet_size;
252                 rwqe->data_segments = 1;
253
254                 index++;
255                 index &= max_index_mask;
256
257                 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags)))
258                         goto out;
259         }
260
261         q_skba->index = index;
262
263         /* Ring doorbell */
264         iosync();
265         if (rq_nr == 2)
266                 ehea_update_rq2a(pr->qp, i);
267         else
268                 ehea_update_rq3a(pr->qp, i);
269 out:
270         return ret;
271 }
272
273
274 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
275 {
276         return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
277                                   nr_of_wqes, EHEA_RWQE2_TYPE,
278                                   EHEA_RQ2_PKT_SIZE + NET_IP_ALIGN);
279 }
280
281
282 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
283 {
284         return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
285                                   nr_of_wqes, EHEA_RWQE3_TYPE,
286                                   EHEA_MAX_PACKET_SIZE + NET_IP_ALIGN);
287 }
288
289 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
290 {
291         *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
292         if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
293                 return 0;
294         if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
295             (cqe->header_length == 0))
296                 return 0;
297         return -EINVAL;
298 }
299
300 static inline void ehea_fill_skb(struct net_device *dev,
301                                  struct sk_buff *skb, struct ehea_cqe *cqe)
302 {
303         int length = cqe->num_bytes_transfered - 4;     /*remove CRC */
304
305         skb_put(skb, length);
306         skb->ip_summed = CHECKSUM_UNNECESSARY;
307         skb->protocol = eth_type_trans(skb, dev);
308 }
309
310 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
311                                                int arr_len,
312                                                struct ehea_cqe *cqe)
313 {
314         int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
315         struct sk_buff *skb;
316         void *pref;
317         int x;
318
319         x = skb_index + 1;
320         x &= (arr_len - 1);
321
322         pref = skb_array[x];
323         prefetchw(pref);
324         prefetchw(pref + EHEA_CACHE_LINE);
325
326         pref = (skb_array[x]->data);
327         prefetch(pref);
328         prefetch(pref + EHEA_CACHE_LINE);
329         prefetch(pref + EHEA_CACHE_LINE * 2);
330         prefetch(pref + EHEA_CACHE_LINE * 3);
331         skb = skb_array[skb_index];
332         skb_array[skb_index] = NULL;
333         return skb;
334 }
335
336 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
337                                                   int arr_len, int wqe_index)
338 {
339         struct sk_buff *skb;
340         void *pref;
341         int x;
342
343         x = wqe_index + 1;
344         x &= (arr_len - 1);
345
346         pref = skb_array[x];
347         prefetchw(pref);
348         prefetchw(pref + EHEA_CACHE_LINE);
349
350         pref = (skb_array[x]->data);
351         prefetchw(pref);
352         prefetchw(pref + EHEA_CACHE_LINE);
353
354         skb = skb_array[wqe_index];
355         skb_array[wqe_index] = NULL;
356         return skb;
357 }
358
359 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
360                                  struct ehea_cqe *cqe, int *processed_rq2,
361                                  int *processed_rq3)
362 {
363         struct sk_buff *skb;
364
365         if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
366                 pr->p_stats.err_tcp_cksum++;
367         if (cqe->status & EHEA_CQE_STAT_ERR_IP)
368                 pr->p_stats.err_ip_cksum++;
369         if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
370                 pr->p_stats.err_frame_crc++;
371
372         if (netif_msg_rx_err(pr->port)) {
373                 ehea_error("CQE Error for QP %d", pr->qp->init_attr.qp_nr);
374                 ehea_dump(cqe, sizeof(*cqe), "CQE");
375         }
376
377         if (rq == 2) {
378                 *processed_rq2 += 1;
379                 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
380                 dev_kfree_skb(skb);
381         } else if (rq == 3) {
382                 *processed_rq3 += 1;
383                 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
384                 dev_kfree_skb(skb);
385         }
386
387         if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
388                 ehea_error("Critical receive error. Resetting port.");
389                 queue_work(pr->port->adapter->ehea_wq, &pr->port->reset_task);
390                 return 1;
391         }
392
393         return 0;
394 }
395
396 static struct ehea_cqe *ehea_proc_rwqes(struct net_device *dev,
397                                         struct ehea_port_res *pr,
398                                         int *budget)
399 {
400         struct ehea_port *port = pr->port;
401         struct ehea_qp *qp = pr->qp;
402         struct ehea_cqe *cqe;
403         struct sk_buff *skb;
404         struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
405         struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
406         struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
407         int skb_arr_rq1_len = pr->rq1_skba.len;
408         int skb_arr_rq2_len = pr->rq2_skba.len;
409         int skb_arr_rq3_len = pr->rq3_skba.len;
410         int processed, processed_rq1, processed_rq2, processed_rq3;
411         int wqe_index, last_wqe_index, rq, my_quota, port_reset;
412
413         processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
414         last_wqe_index = 0;
415         my_quota = min(*budget, dev->quota);
416
417         cqe = ehea_poll_rq1(qp, &wqe_index);
418         while ((my_quota > 0) && cqe) {
419                 ehea_inc_rq1(qp);
420                 processed_rq1++;
421                 processed++;
422                 my_quota--;
423                 if (netif_msg_rx_status(port))
424                         ehea_dump(cqe, sizeof(*cqe), "CQE");
425
426                 last_wqe_index = wqe_index;
427                 rmb();
428                 if (!ehea_check_cqe(cqe, &rq)) {
429                         if (rq == 1) {  /* LL RQ1 */
430                                 skb = get_skb_by_index_ll(skb_arr_rq1,
431                                                           skb_arr_rq1_len,
432                                                           wqe_index);
433                                 if (unlikely(!skb)) {
434                                         if (netif_msg_rx_err(port))
435                                                 ehea_error("LL rq1: skb=NULL");
436
437                                         skb = netdev_alloc_skb(port->netdev,
438                                                                EHEA_L_PKT_SIZE);
439                                         if (!skb)
440                                                 break;
441                                 }
442                                 skb_copy_to_linear_data(skb, ((char*)cqe) + 64,
443                                                  cqe->num_bytes_transfered - 4);
444                                 ehea_fill_skb(port->netdev, skb, cqe);
445                         } else if (rq == 2) {  /* RQ2 */
446                                 skb = get_skb_by_index(skb_arr_rq2,
447                                                        skb_arr_rq2_len, cqe);
448                                 if (unlikely(!skb)) {
449                                         if (netif_msg_rx_err(port))
450                                                 ehea_error("rq2: skb=NULL");
451                                         break;
452                                 }
453                                 ehea_fill_skb(port->netdev, skb, cqe);
454                                 processed_rq2++;
455                         } else {  /* RQ3 */
456                                 skb = get_skb_by_index(skb_arr_rq3,
457                                                        skb_arr_rq3_len, cqe);
458                                 if (unlikely(!skb)) {
459                                         if (netif_msg_rx_err(port))
460                                                 ehea_error("rq3: skb=NULL");
461                                         break;
462                                 }
463                                 ehea_fill_skb(port->netdev, skb, cqe);
464                                 processed_rq3++;
465                         }
466
467                         if ((cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
468                             && port->vgrp)
469                                 vlan_hwaccel_receive_skb(skb, port->vgrp,
470                                                          cqe->vlan_tag);
471                         else
472                                 netif_receive_skb(skb);
473
474                         port->netdev->last_rx = jiffies;
475                 } else {
476                         pr->p_stats.poll_receive_errors++;
477                         port_reset = ehea_treat_poll_error(pr, rq, cqe,
478                                                            &processed_rq2,
479                                                            &processed_rq3);
480                         if (port_reset)
481                                 break;
482                 }
483                 cqe = ehea_poll_rq1(qp, &wqe_index);
484         }
485
486         pr->rx_packets += processed;
487         *budget -= processed;
488
489         ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
490         ehea_refill_rq2(pr, processed_rq2);
491         ehea_refill_rq3(pr, processed_rq3);
492
493         cqe = ehea_poll_rq1(qp, &wqe_index);
494         return cqe;
495 }
496
497 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
498 {
499         struct sk_buff *skb;
500         struct ehea_cq *send_cq = pr->send_cq;
501         struct ehea_cqe *cqe;
502         int quota = my_quota;
503         int cqe_counter = 0;
504         int swqe_av = 0;
505         int index;
506         unsigned long flags;
507
508         cqe = ehea_poll_cq(send_cq);
509         while(cqe && (quota > 0)) {
510                 ehea_inc_cq(send_cq);
511
512                 cqe_counter++;
513                 rmb();
514                 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
515                         ehea_error("Send Completion Error: Resetting port");
516                         if (netif_msg_tx_err(pr->port))
517                                 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
518                         queue_work(pr->port->adapter->ehea_wq,
519                                    &pr->port->reset_task);
520                         break;
521                 }
522
523                 if (netif_msg_tx_done(pr->port))
524                         ehea_dump(cqe, sizeof(*cqe), "CQE");
525
526                 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
527                            == EHEA_SWQE2_TYPE)) {
528
529                         index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
530                         skb = pr->sq_skba.arr[index];
531                         dev_kfree_skb(skb);
532                         pr->sq_skba.arr[index] = NULL;
533                 }
534
535                 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
536                 quota--;
537
538                 cqe = ehea_poll_cq(send_cq);
539         };
540
541         ehea_update_feca(send_cq, cqe_counter);
542         atomic_add(swqe_av, &pr->swqe_avail);
543
544         spin_lock_irqsave(&pr->netif_queue, flags);
545
546         if (pr->queue_stopped && (atomic_read(&pr->swqe_avail)
547                                   >= pr->swqe_refill_th)) {
548                 netif_wake_queue(pr->port->netdev);
549                 pr->queue_stopped = 0;
550         }
551         spin_unlock_irqrestore(&pr->netif_queue, flags);
552
553         return cqe;
554 }
555
556 #define EHEA_NAPI_POLL_NUM_BEFORE_IRQ 16
557
558 static int ehea_poll(struct net_device *dev, int *budget)
559 {
560         struct ehea_port_res *pr = dev->priv;
561         struct ehea_cqe *cqe;
562         struct ehea_cqe *cqe_skb = NULL;
563         int force_irq, wqe_index;
564
565         cqe = ehea_poll_rq1(pr->qp, &wqe_index);
566         cqe_skb = ehea_poll_cq(pr->send_cq);
567
568         force_irq = (pr->poll_counter > EHEA_NAPI_POLL_NUM_BEFORE_IRQ);
569
570         if ((!cqe && !cqe_skb) || force_irq) {
571                 pr->poll_counter = 0;
572                 netif_rx_complete(dev);
573                 ehea_reset_cq_ep(pr->recv_cq);
574                 ehea_reset_cq_ep(pr->send_cq);
575                 ehea_reset_cq_n1(pr->recv_cq);
576                 ehea_reset_cq_n1(pr->send_cq);
577                 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
578                 cqe_skb = ehea_poll_cq(pr->send_cq);
579
580                 if (!cqe && !cqe_skb)
581                         return 0;
582
583                 if (!netif_rx_reschedule(dev, dev->quota))
584                         return 0;
585         }
586
587         cqe = ehea_proc_rwqes(dev, pr, budget);
588         cqe_skb = ehea_proc_cqes(pr, 300);
589
590         if (cqe || cqe_skb)
591                 pr->poll_counter++;
592
593         return 1;
594 }
595
596 #ifdef CONFIG_NET_POLL_CONTROLLER
597 static void ehea_netpoll(struct net_device *dev)
598 {
599         struct ehea_port *port = netdev_priv(dev);
600
601         netif_rx_schedule(port->port_res[0].d_netdev);
602 }
603 #endif
604
605 static int ehea_poll_firstqueue(struct net_device *dev, int *budget)
606 {
607         struct ehea_port *port = netdev_priv(dev);
608         struct net_device *d_dev = port->port_res[0].d_netdev;
609
610         return ehea_poll(d_dev, budget);
611 }
612
613 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
614 {
615         struct ehea_port_res *pr = param;
616
617         netif_rx_schedule(pr->d_netdev);
618
619         return IRQ_HANDLED;
620 }
621
622 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
623 {
624         struct ehea_port *port = param;
625         struct ehea_eqe *eqe;
626         struct ehea_qp *qp;
627         u32 qp_token;
628
629         eqe = ehea_poll_eq(port->qp_eq);
630
631         while (eqe) {
632                 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
633                 ehea_error("QP aff_err: entry=0x%lx, token=0x%x",
634                            eqe->entry, qp_token);
635
636                 qp = port->port_res[qp_token].qp;
637                 ehea_error_data(port->adapter, qp->fw_handle);
638                 eqe = ehea_poll_eq(port->qp_eq);
639         }
640
641         queue_work(port->adapter->ehea_wq, &port->reset_task);
642
643         return IRQ_HANDLED;
644 }
645
646 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
647                                        int logical_port)
648 {
649         int i;
650
651         for (i = 0; i < EHEA_MAX_PORTS; i++)
652                 if (adapter->port[i])
653                         if (adapter->port[i]->logical_port_id == logical_port)
654                                 return adapter->port[i];
655         return NULL;
656 }
657
658 int ehea_sense_port_attr(struct ehea_port *port)
659 {
660         int ret;
661         u64 hret;
662         struct hcp_ehea_port_cb0 *cb0;
663
664         cb0 = kzalloc(PAGE_SIZE, GFP_ATOMIC);   /* May be called via */
665         if (!cb0) {                             /* ehea_neq_tasklet() */
666                 ehea_error("no mem for cb0");
667                 ret = -ENOMEM;
668                 goto out;
669         }
670
671         hret = ehea_h_query_ehea_port(port->adapter->handle,
672                                       port->logical_port_id, H_PORT_CB0,
673                                       EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
674                                       cb0);
675         if (hret != H_SUCCESS) {
676                 ret = -EIO;
677                 goto out_free;
678         }
679
680         /* MAC address */
681         port->mac_addr = cb0->port_mac_addr << 16;
682
683         if (!is_valid_ether_addr((u8*)&port->mac_addr)) {
684                 ret = -EADDRNOTAVAIL;
685                 goto out_free;
686         }
687
688         /* Port speed */
689         switch (cb0->port_speed) {
690         case H_SPEED_10M_H:
691                 port->port_speed = EHEA_SPEED_10M;
692                 port->full_duplex = 0;
693                 break;
694         case H_SPEED_10M_F:
695                 port->port_speed = EHEA_SPEED_10M;
696                 port->full_duplex = 1;
697                 break;
698         case H_SPEED_100M_H:
699                 port->port_speed = EHEA_SPEED_100M;
700                 port->full_duplex = 0;
701                 break;
702         case H_SPEED_100M_F:
703                 port->port_speed = EHEA_SPEED_100M;
704                 port->full_duplex = 1;
705                 break;
706         case H_SPEED_1G_F:
707                 port->port_speed = EHEA_SPEED_1G;
708                 port->full_duplex = 1;
709                 break;
710         case H_SPEED_10G_F:
711                 port->port_speed = EHEA_SPEED_10G;
712                 port->full_duplex = 1;
713                 break;
714         default:
715                 port->port_speed = 0;
716                 port->full_duplex = 0;
717                 break;
718         }
719
720         port->autoneg = 1;
721         port->num_mcs = cb0->num_default_qps;
722
723         /* Number of default QPs */
724         if (use_mcs)
725                 port->num_def_qps = cb0->num_default_qps;
726         else
727                 port->num_def_qps = 1;
728
729         if (!port->num_def_qps) {
730                 ret = -EINVAL;
731                 goto out_free;
732         }
733
734         port->num_tx_qps = num_tx_qps;
735
736         if (port->num_def_qps >= port->num_tx_qps)
737                 port->num_add_tx_qps = 0;
738         else
739                 port->num_add_tx_qps = port->num_tx_qps - port->num_def_qps;
740
741         ret = 0;
742 out_free:
743         if (ret || netif_msg_probe(port))
744                 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
745         kfree(cb0);
746 out:
747         return ret;
748 }
749
750 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
751 {
752         struct hcp_ehea_port_cb4 *cb4;
753         u64 hret;
754         int ret = 0;
755
756         cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
757         if (!cb4) {
758                 ehea_error("no mem for cb4");
759                 ret = -ENOMEM;
760                 goto out;
761         }
762
763         cb4->port_speed = port_speed;
764
765         netif_carrier_off(port->netdev);
766
767         hret = ehea_h_modify_ehea_port(port->adapter->handle,
768                                        port->logical_port_id,
769                                        H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
770         if (hret == H_SUCCESS) {
771                 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
772
773                 hret = ehea_h_query_ehea_port(port->adapter->handle,
774                                               port->logical_port_id,
775                                               H_PORT_CB4, H_PORT_CB4_SPEED,
776                                               cb4);
777                 if (hret == H_SUCCESS) {
778                         switch (cb4->port_speed) {
779                         case H_SPEED_10M_H:
780                                 port->port_speed = EHEA_SPEED_10M;
781                                 port->full_duplex = 0;
782                                 break;
783                         case H_SPEED_10M_F:
784                                 port->port_speed = EHEA_SPEED_10M;
785                                 port->full_duplex = 1;
786                                 break;
787                         case H_SPEED_100M_H:
788                                 port->port_speed = EHEA_SPEED_100M;
789                                 port->full_duplex = 0;
790                                 break;
791                         case H_SPEED_100M_F:
792                                 port->port_speed = EHEA_SPEED_100M;
793                                 port->full_duplex = 1;
794                                 break;
795                         case H_SPEED_1G_F:
796                                 port->port_speed = EHEA_SPEED_1G;
797                                 port->full_duplex = 1;
798                                 break;
799                         case H_SPEED_10G_F:
800                                 port->port_speed = EHEA_SPEED_10G;
801                                 port->full_duplex = 1;
802                                 break;
803                         default:
804                                 port->port_speed = 0;
805                                 port->full_duplex = 0;
806                                 break;
807                         }
808                 } else {
809                         ehea_error("Failed sensing port speed");
810                         ret = -EIO;
811                 }
812         } else {
813                 if (hret == H_AUTHORITY) {
814                         ehea_info("Hypervisor denied setting port speed");
815                         ret = -EPERM;
816                 } else {
817                         ret = -EIO;
818                         ehea_error("Failed setting port speed");
819                 }
820         }
821         if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
822                 netif_carrier_on(port->netdev);
823
824         kfree(cb4);
825 out:
826         return ret;
827 }
828
829 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
830 {
831         int ret;
832         u8 ec;
833         u8 portnum;
834         struct ehea_port *port;
835
836         ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
837         portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
838         port = ehea_get_port(adapter, portnum);
839
840         switch (ec) {
841         case EHEA_EC_PORTSTATE_CHG:     /* port state change */
842
843                 if (!port) {
844                         ehea_error("unknown portnum %x", portnum);
845                         break;
846                 }
847
848                 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
849                         if (!netif_carrier_ok(port->netdev)) {
850                                 ret = ehea_sense_port_attr(port);
851                                 if (ret) {
852                                         ehea_error("failed resensing port "
853                                                    "attributes");
854                                         break;
855                                 }
856
857                                 if (netif_msg_link(port))
858                                         ehea_info("%s: Logical port up: %dMbps "
859                                                   "%s Duplex",
860                                                   port->netdev->name,
861                                                   port->port_speed,
862                                                   port->full_duplex ==
863                                                   1 ? "Full" : "Half");
864
865                                 netif_carrier_on(port->netdev);
866                                 netif_wake_queue(port->netdev);
867                         }
868                 } else
869                         if (netif_carrier_ok(port->netdev)) {
870                                 if (netif_msg_link(port))
871                                         ehea_info("%s: Logical port down",
872                                                   port->netdev->name);
873                                 netif_carrier_off(port->netdev);
874                                 netif_stop_queue(port->netdev);
875                         }
876
877                 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
878                         port->phy_link = EHEA_PHY_LINK_UP;
879                         if (netif_msg_link(port))
880                                 ehea_info("%s: Physical port up",
881                                           port->netdev->name);
882                         if (prop_carrier_state)
883                                 netif_carrier_on(port->netdev);
884                 } else {
885                         port->phy_link = EHEA_PHY_LINK_DOWN;
886                         if (netif_msg_link(port))
887                                 ehea_info("%s: Physical port down",
888                                           port->netdev->name);
889                         if (prop_carrier_state)
890                                 netif_carrier_off(port->netdev);
891                 }
892
893                 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
894                         ehea_info("External switch port is primary port");
895                 else
896                         ehea_info("External switch port is backup port");
897
898                 break;
899         case EHEA_EC_ADAPTER_MALFUNC:
900                 ehea_error("Adapter malfunction");
901                 break;
902         case EHEA_EC_PORT_MALFUNC:
903                 ehea_info("Port malfunction: Device: %s", port->netdev->name);
904                 netif_carrier_off(port->netdev);
905                 netif_stop_queue(port->netdev);
906                 break;
907         default:
908                 ehea_error("unknown event code %x, eqe=0x%lX", ec, eqe);
909                 break;
910         }
911 }
912
913 static void ehea_neq_tasklet(unsigned long data)
914 {
915         struct ehea_adapter *adapter = (struct ehea_adapter*)data;
916         struct ehea_eqe *eqe;
917         u64 event_mask;
918
919         eqe = ehea_poll_eq(adapter->neq);
920         ehea_debug("eqe=%p", eqe);
921
922         while (eqe) {
923                 ehea_debug("*eqe=%lx", eqe->entry);
924                 ehea_parse_eqe(adapter, eqe->entry);
925                 eqe = ehea_poll_eq(adapter->neq);
926                 ehea_debug("next eqe=%p", eqe);
927         }
928
929         event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
930                    | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
931                    | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
932
933         ehea_h_reset_events(adapter->handle,
934                             adapter->neq->fw_handle, event_mask);
935 }
936
937 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
938 {
939         struct ehea_adapter *adapter = param;
940         tasklet_hi_schedule(&adapter->neq_tasklet);
941         return IRQ_HANDLED;
942 }
943
944
945 static int ehea_fill_port_res(struct ehea_port_res *pr)
946 {
947         int ret;
948         struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
949
950         ret = ehea_init_fill_rq1(pr, init_attr->act_nr_rwqes_rq1
951                                      - init_attr->act_nr_rwqes_rq2
952                                      - init_attr->act_nr_rwqes_rq3 - 1);
953
954         ret |= ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
955
956         ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
957
958         return ret;
959 }
960
961 static int ehea_reg_interrupts(struct net_device *dev)
962 {
963         struct ehea_port *port = netdev_priv(dev);
964         struct ehea_port_res *pr;
965         int i, ret;
966
967
968         snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
969                  dev->name);
970
971         ret = ibmebus_request_irq(NULL, port->qp_eq->attr.ist1,
972                                   ehea_qp_aff_irq_handler,
973                                   IRQF_DISABLED, port->int_aff_name, port);
974         if (ret) {
975                 ehea_error("failed registering irq for qp_aff_irq_handler:"
976                            "ist=%X", port->qp_eq->attr.ist1);
977                 goto out_free_qpeq;
978         }
979
980         if (netif_msg_ifup(port))
981                 ehea_info("irq_handle 0x%X for function qp_aff_irq_handler "
982                           "registered", port->qp_eq->attr.ist1);
983
984
985         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
986                 pr = &port->port_res[i];
987                 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
988                          "%s-queue%d", dev->name, i);
989                 ret = ibmebus_request_irq(NULL, pr->eq->attr.ist1,
990                                           ehea_recv_irq_handler,
991                                           IRQF_DISABLED, pr->int_send_name,
992                                           pr);
993                 if (ret) {
994                         ehea_error("failed registering irq for ehea_queue "
995                                    "port_res_nr:%d, ist=%X", i,
996                                    pr->eq->attr.ist1);
997                         goto out_free_req;
998                 }
999                 if (netif_msg_ifup(port))
1000                         ehea_info("irq_handle 0x%X for function ehea_queue_int "
1001                                   "%d registered", pr->eq->attr.ist1, i);
1002         }
1003 out:
1004         return ret;
1005
1006
1007 out_free_req:
1008         while (--i >= 0) {
1009                 u32 ist = port->port_res[i].eq->attr.ist1;
1010                 ibmebus_free_irq(NULL, ist, &port->port_res[i]);
1011         }
1012
1013 out_free_qpeq:
1014         ibmebus_free_irq(NULL, port->qp_eq->attr.ist1, port);
1015         i = port->num_def_qps;
1016
1017         goto out;
1018
1019 }
1020
1021 static void ehea_free_interrupts(struct net_device *dev)
1022 {
1023         struct ehea_port *port = netdev_priv(dev);
1024         struct ehea_port_res *pr;
1025         int i;
1026
1027         /* send */
1028
1029         for (i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
1030                 pr = &port->port_res[i];
1031                 ibmebus_free_irq(NULL, pr->eq->attr.ist1, pr);
1032                 if (netif_msg_intr(port))
1033                         ehea_info("free send irq for res %d with handle 0x%X",
1034                                   i, pr->eq->attr.ist1);
1035         }
1036
1037         /* associated events */
1038         ibmebus_free_irq(NULL, port->qp_eq->attr.ist1, port);
1039         if (netif_msg_intr(port))
1040                 ehea_info("associated event interrupt for handle 0x%X freed",
1041                           port->qp_eq->attr.ist1);
1042 }
1043
1044 static int ehea_configure_port(struct ehea_port *port)
1045 {
1046         int ret, i;
1047         u64 hret, mask;
1048         struct hcp_ehea_port_cb0 *cb0;
1049
1050         ret = -ENOMEM;
1051         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1052         if (!cb0)
1053                 goto out;
1054
1055         cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1056                      | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1057                      | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1058                      | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1059                      | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1060                                       PXLY_RC_VLAN_FILTER)
1061                      | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1062
1063         for (i = 0; i < port->num_mcs; i++)
1064                 if (use_mcs)
1065                         cb0->default_qpn_arr[i] =
1066                                 port->port_res[i].qp->init_attr.qp_nr;
1067                 else
1068                         cb0->default_qpn_arr[i] =
1069                                 port->port_res[0].qp->init_attr.qp_nr;
1070
1071         if (netif_msg_ifup(port))
1072                 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1073
1074         mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1075              | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1076
1077         hret = ehea_h_modify_ehea_port(port->adapter->handle,
1078                                        port->logical_port_id,
1079                                        H_PORT_CB0, mask, cb0);
1080         ret = -EIO;
1081         if (hret != H_SUCCESS)
1082                 goto out_free;
1083
1084         ret = 0;
1085
1086 out_free:
1087         kfree(cb0);
1088 out:
1089         return ret;
1090 }
1091
1092 int ehea_gen_smrs(struct ehea_port_res *pr)
1093 {
1094         int ret;
1095         struct ehea_adapter *adapter = pr->port->adapter;
1096
1097         ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1098         if (ret)
1099                 goto out;
1100
1101         ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1102         if (ret)
1103                 goto out_free;
1104
1105         return 0;
1106
1107 out_free:
1108         ehea_rem_mr(&pr->send_mr);
1109 out:
1110         ehea_error("Generating SMRS failed\n");
1111         return -EIO;
1112 }
1113
1114 int ehea_rem_smrs(struct ehea_port_res *pr)
1115 {
1116         if ((ehea_rem_mr(&pr->send_mr))
1117             || (ehea_rem_mr(&pr->recv_mr)))
1118                 return -EIO;
1119         else
1120                 return 0;
1121 }
1122
1123 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1124 {
1125         int arr_size = sizeof(void*) * max_q_entries;
1126
1127         q_skba->arr = vmalloc(arr_size);
1128         if (!q_skba->arr)
1129                 return -ENOMEM;
1130
1131         memset(q_skba->arr, 0, arr_size);
1132
1133         q_skba->len = max_q_entries;
1134         q_skba->index = 0;
1135         q_skba->os_skbs = 0;
1136
1137         return 0;
1138 }
1139
1140 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1141                               struct port_res_cfg *pr_cfg, int queue_token)
1142 {
1143         struct ehea_adapter *adapter = port->adapter;
1144         enum ehea_eq_type eq_type = EHEA_EQ;
1145         struct ehea_qp_init_attr *init_attr = NULL;
1146         int ret = -EIO;
1147
1148         memset(pr, 0, sizeof(struct ehea_port_res));
1149
1150         pr->port = port;
1151         spin_lock_init(&pr->xmit_lock);
1152         spin_lock_init(&pr->netif_queue);
1153
1154         pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1155         if (!pr->eq) {
1156                 ehea_error("create_eq failed (eq)");
1157                 goto out_free;
1158         }
1159
1160         pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1161                                      pr->eq->fw_handle,
1162                                      port->logical_port_id);
1163         if (!pr->recv_cq) {
1164                 ehea_error("create_cq failed (cq_recv)");
1165                 goto out_free;
1166         }
1167
1168         pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1169                                      pr->eq->fw_handle,
1170                                      port->logical_port_id);
1171         if (!pr->send_cq) {
1172                 ehea_error("create_cq failed (cq_send)");
1173                 goto out_free;
1174         }
1175
1176         if (netif_msg_ifup(port))
1177                 ehea_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d",
1178                           pr->send_cq->attr.act_nr_of_cqes,
1179                           pr->recv_cq->attr.act_nr_of_cqes);
1180
1181         init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1182         if (!init_attr) {
1183                 ret = -ENOMEM;
1184                 ehea_error("no mem for ehea_qp_init_attr");
1185                 goto out_free;
1186         }
1187
1188         init_attr->low_lat_rq1 = 1;
1189         init_attr->signalingtype = 1;   /* generate CQE if specified in WQE */
1190         init_attr->rq_count = 3;
1191         init_attr->qp_token = queue_token;
1192         init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1193         init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1194         init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1195         init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1196         init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1197         init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1198         init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1199         init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1200         init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1201         init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1202         init_attr->port_nr = port->logical_port_id;
1203         init_attr->send_cq_handle = pr->send_cq->fw_handle;
1204         init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1205         init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1206
1207         pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1208         if (!pr->qp) {
1209                 ehea_error("create_qp failed");
1210                 ret = -EIO;
1211                 goto out_free;
1212         }
1213
1214         if (netif_msg_ifup(port))
1215                 ehea_info("QP: qp_nr=%d\n act_nr_snd_wqe=%d\n nr_rwqe_rq1=%d\n "
1216                           "nr_rwqe_rq2=%d\n nr_rwqe_rq3=%d", init_attr->qp_nr,
1217                           init_attr->act_nr_send_wqes,
1218                           init_attr->act_nr_rwqes_rq1,
1219                           init_attr->act_nr_rwqes_rq2,
1220                           init_attr->act_nr_rwqes_rq3);
1221
1222         ret = ehea_init_q_skba(&pr->sq_skba, init_attr->act_nr_send_wqes + 1);
1223         ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1224         ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1225         ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1226         if (ret)
1227                 goto out_free;
1228
1229         pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1230         if (ehea_gen_smrs(pr) != 0) {
1231                 ret = -EIO;
1232                 goto out_free;
1233         }
1234
1235         atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1236
1237         kfree(init_attr);
1238
1239         pr->d_netdev = alloc_netdev(0, "", ether_setup);
1240         if (!pr->d_netdev)
1241                 goto out_free;
1242         pr->d_netdev->priv = pr;
1243         pr->d_netdev->weight = 64;
1244         pr->d_netdev->poll = ehea_poll;
1245         set_bit(__LINK_STATE_START, &pr->d_netdev->state);
1246         strcpy(pr->d_netdev->name, port->netdev->name);
1247
1248         ret = 0;
1249         goto out;
1250
1251 out_free:
1252         kfree(init_attr);
1253         vfree(pr->sq_skba.arr);
1254         vfree(pr->rq1_skba.arr);
1255         vfree(pr->rq2_skba.arr);
1256         vfree(pr->rq3_skba.arr);
1257         ehea_destroy_qp(pr->qp);
1258         ehea_destroy_cq(pr->send_cq);
1259         ehea_destroy_cq(pr->recv_cq);
1260         ehea_destroy_eq(pr->eq);
1261 out:
1262         return ret;
1263 }
1264
1265 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1266 {
1267         int ret, i;
1268
1269         free_netdev(pr->d_netdev);
1270
1271         ret = ehea_destroy_qp(pr->qp);
1272
1273         if (!ret) {
1274                 ehea_destroy_cq(pr->send_cq);
1275                 ehea_destroy_cq(pr->recv_cq);
1276                 ehea_destroy_eq(pr->eq);
1277
1278                 for (i = 0; i < pr->rq1_skba.len; i++)
1279                         if (pr->rq1_skba.arr[i])
1280                                 dev_kfree_skb(pr->rq1_skba.arr[i]);
1281
1282                 for (i = 0; i < pr->rq2_skba.len; i++)
1283                         if (pr->rq2_skba.arr[i])
1284                                 dev_kfree_skb(pr->rq2_skba.arr[i]);
1285
1286                 for (i = 0; i < pr->rq3_skba.len; i++)
1287                         if (pr->rq3_skba.arr[i])
1288                                 dev_kfree_skb(pr->rq3_skba.arr[i]);
1289
1290                 for (i = 0; i < pr->sq_skba.len; i++)
1291                         if (pr->sq_skba.arr[i])
1292                                 dev_kfree_skb(pr->sq_skba.arr[i]);
1293
1294                 vfree(pr->rq1_skba.arr);
1295                 vfree(pr->rq2_skba.arr);
1296                 vfree(pr->rq3_skba.arr);
1297                 vfree(pr->sq_skba.arr);
1298                 ret = ehea_rem_smrs(pr);
1299         }
1300         return ret;
1301 }
1302
1303 /*
1304  * The write_* functions store information in swqe which is used by
1305  * the hardware to calculate the ip/tcp/udp checksum
1306  */
1307
1308 static inline void write_ip_start_end(struct ehea_swqe *swqe,
1309                                       const struct sk_buff *skb)
1310 {
1311         swqe->ip_start = skb_network_offset(skb);
1312         swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
1313 }
1314
1315 static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
1316                                         const struct sk_buff *skb)
1317 {
1318         swqe->tcp_offset =
1319                 (u8)(swqe->ip_end + 1 + offsetof(struct tcphdr, check));
1320
1321         swqe->tcp_end = (u16)skb->len - 1;
1322 }
1323
1324 static inline void write_udp_offset_end(struct ehea_swqe *swqe,
1325                                         const struct sk_buff *skb)
1326 {
1327         swqe->tcp_offset =
1328                 (u8)(swqe->ip_end + 1 + offsetof(struct udphdr, check));
1329
1330         swqe->tcp_end = (u16)skb->len - 1;
1331 }
1332
1333
1334 static void write_swqe2_TSO(struct sk_buff *skb,
1335                             struct ehea_swqe *swqe, u32 lkey)
1336 {
1337         struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1338         u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1339         int skb_data_size = skb->len - skb->data_len;
1340         int headersize;
1341
1342         /* Packet is TCP with TSO enabled */
1343         swqe->tx_control |= EHEA_SWQE_TSO;
1344         swqe->mss = skb_shinfo(skb)->gso_size;
1345         /* copy only eth/ip/tcp headers to immediate data and
1346          * the rest of skb->data to sg1entry
1347          */
1348         headersize = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1349
1350         skb_data_size = skb->len - skb->data_len;
1351
1352         if (skb_data_size >= headersize) {
1353                 /* copy immediate data */
1354                 skb_copy_from_linear_data(skb, imm_data, headersize);
1355                 swqe->immediate_data_length = headersize;
1356
1357                 if (skb_data_size > headersize) {
1358                         /* set sg1entry data */
1359                         sg1entry->l_key = lkey;
1360                         sg1entry->len = skb_data_size - headersize;
1361                         sg1entry->vaddr =
1362                                 ehea_map_vaddr(skb->data + headersize);
1363                         swqe->descriptors++;
1364                 }
1365         } else
1366                 ehea_error("cannot handle fragmented headers");
1367 }
1368
1369 static void write_swqe2_nonTSO(struct sk_buff *skb,
1370                                struct ehea_swqe *swqe, u32 lkey)
1371 {
1372         int skb_data_size = skb->len - skb->data_len;
1373         u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1374         struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1375
1376         /* Packet is any nonTSO type
1377          *
1378          * Copy as much as possible skb->data to immediate data and
1379          * the rest to sg1entry
1380          */
1381         if (skb_data_size >= SWQE2_MAX_IMM) {
1382                 /* copy immediate data */
1383                 skb_copy_from_linear_data(skb, imm_data, SWQE2_MAX_IMM);
1384
1385                 swqe->immediate_data_length = SWQE2_MAX_IMM;
1386
1387                 if (skb_data_size > SWQE2_MAX_IMM) {
1388                         /* copy sg1entry data */
1389                         sg1entry->l_key = lkey;
1390                         sg1entry->len = skb_data_size - SWQE2_MAX_IMM;
1391                         sg1entry->vaddr =
1392                                 ehea_map_vaddr(skb->data + SWQE2_MAX_IMM);
1393                         swqe->descriptors++;
1394                 }
1395         } else {
1396                 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1397                 swqe->immediate_data_length = skb_data_size;
1398         }
1399 }
1400
1401 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1402                                     struct ehea_swqe *swqe, u32 lkey)
1403 {
1404         struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1405         skb_frag_t *frag;
1406         int nfrags, sg1entry_contains_frag_data, i;
1407
1408         nfrags = skb_shinfo(skb)->nr_frags;
1409         sg1entry = &swqe->u.immdata_desc.sg_entry;
1410         sg_list = (struct ehea_vsgentry*)&swqe->u.immdata_desc.sg_list;
1411         swqe->descriptors = 0;
1412         sg1entry_contains_frag_data = 0;
1413
1414         if ((dev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size)
1415                 write_swqe2_TSO(skb, swqe, lkey);
1416         else
1417                 write_swqe2_nonTSO(skb, swqe, lkey);
1418
1419         /* write descriptors */
1420         if (nfrags > 0) {
1421                 if (swqe->descriptors == 0) {
1422                         /* sg1entry not yet used */
1423                         frag = &skb_shinfo(skb)->frags[0];
1424
1425                         /* copy sg1entry data */
1426                         sg1entry->l_key = lkey;
1427                         sg1entry->len = frag->size;
1428                         sg1entry->vaddr =
1429                                 ehea_map_vaddr(page_address(frag->page)
1430                                                + frag->page_offset);
1431                         swqe->descriptors++;
1432                         sg1entry_contains_frag_data = 1;
1433                 }
1434
1435                 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1436
1437                         frag = &skb_shinfo(skb)->frags[i];
1438                         sgentry = &sg_list[i - sg1entry_contains_frag_data];
1439
1440                         sgentry->l_key = lkey;
1441                         sgentry->len = frag->size;
1442                         sgentry->vaddr =
1443                                 ehea_map_vaddr(page_address(frag->page)
1444                                                + frag->page_offset);
1445                         swqe->descriptors++;
1446                 }
1447         }
1448 }
1449
1450 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1451 {
1452         int ret = 0;
1453         u64 hret;
1454         u8 reg_type;
1455
1456         /* De/Register untagged packets */
1457         reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1458         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1459                                      port->logical_port_id,
1460                                      reg_type, port->mac_addr, 0, hcallid);
1461         if (hret != H_SUCCESS) {
1462                 ehea_error("%sregistering bc address failed (tagged)",
1463                            hcallid == H_REG_BCMC ? "" : "de");
1464                 ret = -EIO;
1465                 goto out_herr;
1466         }
1467
1468         /* De/Register VLAN packets */
1469         reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1470         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1471                                      port->logical_port_id,
1472                                      reg_type, port->mac_addr, 0, hcallid);
1473         if (hret != H_SUCCESS) {
1474                 ehea_error("%sregistering bc address failed (vlan)",
1475                            hcallid == H_REG_BCMC ? "" : "de");
1476                 ret = -EIO;
1477         }
1478 out_herr:
1479         return ret;
1480 }
1481
1482 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1483 {
1484         struct ehea_port *port = netdev_priv(dev);
1485         struct sockaddr *mac_addr = sa;
1486         struct hcp_ehea_port_cb0 *cb0;
1487         int ret;
1488         u64 hret;
1489
1490         if (!is_valid_ether_addr(mac_addr->sa_data)) {
1491                 ret = -EADDRNOTAVAIL;
1492                 goto out;
1493         }
1494
1495         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1496         if (!cb0) {
1497                 ehea_error("no mem for cb0");
1498                 ret = -ENOMEM;
1499                 goto out;
1500         }
1501
1502         memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1503
1504         cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1505
1506         hret = ehea_h_modify_ehea_port(port->adapter->handle,
1507                                        port->logical_port_id, H_PORT_CB0,
1508                                        EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1509         if (hret != H_SUCCESS) {
1510                 ret = -EIO;
1511                 goto out_free;
1512         }
1513
1514         memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1515
1516         /* Deregister old MAC in pHYP */
1517         ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1518         if (ret)
1519                 goto out_free;
1520
1521         port->mac_addr = cb0->port_mac_addr << 16;
1522
1523         /* Register new MAC in pHYP */
1524         ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1525         if (ret)
1526                 goto out_free;
1527
1528         ret = 0;
1529 out_free:
1530         kfree(cb0);
1531 out:
1532         return ret;
1533 }
1534
1535 static void ehea_promiscuous_error(u64 hret, int enable)
1536 {
1537         if (hret == H_AUTHORITY)
1538                 ehea_info("Hypervisor denied %sabling promiscuous mode",
1539                           enable == 1 ? "en" : "dis");
1540         else
1541                 ehea_error("failed %sabling promiscuous mode",
1542                            enable == 1 ? "en" : "dis");
1543 }
1544
1545 static void ehea_promiscuous(struct net_device *dev, int enable)
1546 {
1547         struct ehea_port *port = netdev_priv(dev);
1548         struct hcp_ehea_port_cb7 *cb7;
1549         u64 hret;
1550
1551         if ((enable && port->promisc) || (!enable && !port->promisc))
1552                 return;
1553
1554         cb7 = kzalloc(PAGE_SIZE, GFP_ATOMIC);
1555         if (!cb7) {
1556                 ehea_error("no mem for cb7");
1557                 goto out;
1558         }
1559
1560         /* Modify Pxs_DUCQPN in CB7 */
1561         cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1562
1563         hret = ehea_h_modify_ehea_port(port->adapter->handle,
1564                                        port->logical_port_id,
1565                                        H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1566         if (hret) {
1567                 ehea_promiscuous_error(hret, enable);
1568                 goto out;
1569         }
1570
1571         port->promisc = enable;
1572 out:
1573         kfree(cb7);
1574         return;
1575 }
1576
1577 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1578                                      u32 hcallid)
1579 {
1580         u64 hret;
1581         u8 reg_type;
1582
1583         reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1584                  | EHEA_BCMC_UNTAGGED;
1585
1586         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1587                                      port->logical_port_id,
1588                                      reg_type, mc_mac_addr, 0, hcallid);
1589         if (hret)
1590                 goto out;
1591
1592         reg_type = EHEA_BCMC_SCOPE_ALL | EHEA_BCMC_MULTICAST
1593                  | EHEA_BCMC_VLANID_ALL;
1594
1595         hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1596                                      port->logical_port_id,
1597                                      reg_type, mc_mac_addr, 0, hcallid);
1598 out:
1599         return hret;
1600 }
1601
1602 static int ehea_drop_multicast_list(struct net_device *dev)
1603 {
1604         struct ehea_port *port = netdev_priv(dev);
1605         struct ehea_mc_list *mc_entry = port->mc_list;
1606         struct list_head *pos;
1607         struct list_head *temp;
1608         int ret = 0;
1609         u64 hret;
1610
1611         list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1612                 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1613
1614                 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1615                                                  H_DEREG_BCMC);
1616                 if (hret) {
1617                         ehea_error("failed deregistering mcast MAC");
1618                         ret = -EIO;
1619                 }
1620
1621                 list_del(pos);
1622                 kfree(mc_entry);
1623         }
1624         return ret;
1625 }
1626
1627 static void ehea_allmulti(struct net_device *dev, int enable)
1628 {
1629         struct ehea_port *port = netdev_priv(dev);
1630         u64 hret;
1631
1632         if (!port->allmulti) {
1633                 if (enable) {
1634                         /* Enable ALLMULTI */
1635                         ehea_drop_multicast_list(dev);
1636                         hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1637                         if (!hret)
1638                                 port->allmulti = 1;
1639                         else
1640                                 ehea_error("failed enabling IFF_ALLMULTI");
1641                 }
1642         } else
1643                 if (!enable) {
1644                         /* Disable ALLMULTI */
1645                         hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1646                         if (!hret)
1647                                 port->allmulti = 0;
1648                         else
1649                                 ehea_error("failed disabling IFF_ALLMULTI");
1650                 }
1651 }
1652
1653 static void ehea_add_multicast_entry(struct ehea_port* port, u8* mc_mac_addr)
1654 {
1655         struct ehea_mc_list *ehea_mcl_entry;
1656         u64 hret;
1657
1658         ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1659         if (!ehea_mcl_entry) {
1660                 ehea_error("no mem for mcl_entry");
1661                 return;
1662         }
1663
1664         INIT_LIST_HEAD(&ehea_mcl_entry->list);
1665
1666         memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1667
1668         hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1669                                          H_REG_BCMC);
1670         if (!hret)
1671                 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1672         else {
1673                 ehea_error("failed registering mcast MAC");
1674                 kfree(ehea_mcl_entry);
1675         }
1676 }
1677
1678 static void ehea_set_multicast_list(struct net_device *dev)
1679 {
1680         struct ehea_port *port = netdev_priv(dev);
1681         struct dev_mc_list *k_mcl_entry;
1682         int ret, i;
1683
1684         if (dev->flags & IFF_PROMISC) {
1685                 ehea_promiscuous(dev, 1);
1686                 return;
1687         }
1688         ehea_promiscuous(dev, 0);
1689
1690         if (dev->flags & IFF_ALLMULTI) {
1691                 ehea_allmulti(dev, 1);
1692                 return;
1693         }
1694         ehea_allmulti(dev, 0);
1695
1696         if (dev->mc_count) {
1697                 ret = ehea_drop_multicast_list(dev);
1698                 if (ret) {
1699                         /* Dropping the current multicast list failed.
1700                          * Enabling ALL_MULTI is the best we can do.
1701                          */
1702                         ehea_allmulti(dev, 1);
1703                 }
1704
1705                 if (dev->mc_count > port->adapter->max_mc_mac) {
1706                         ehea_info("Mcast registration limit reached (0x%lx). "
1707                                   "Use ALLMULTI!",
1708                                   port->adapter->max_mc_mac);
1709                         goto out;
1710                 }
1711
1712                 for (i = 0, k_mcl_entry = dev->mc_list;
1713                      i < dev->mc_count;
1714                      i++, k_mcl_entry = k_mcl_entry->next) {
1715                         ehea_add_multicast_entry(port, k_mcl_entry->dmi_addr);
1716                 }
1717         }
1718 out:
1719         return;
1720 }
1721
1722 static int ehea_change_mtu(struct net_device *dev, int new_mtu)
1723 {
1724         if ((new_mtu < 68) || (new_mtu > EHEA_MAX_PACKET_SIZE))
1725                 return -EINVAL;
1726         dev->mtu = new_mtu;
1727         return 0;
1728 }
1729
1730 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
1731                        struct ehea_swqe *swqe, u32 lkey)
1732 {
1733         if (skb->protocol == htons(ETH_P_IP)) {
1734                 const struct iphdr *iph = ip_hdr(skb);
1735
1736                 /* IPv4 */
1737                 swqe->tx_control |= EHEA_SWQE_CRC
1738                                  | EHEA_SWQE_IP_CHECKSUM
1739                                  | EHEA_SWQE_TCP_CHECKSUM
1740                                  | EHEA_SWQE_IMM_DATA_PRESENT
1741                                  | EHEA_SWQE_DESCRIPTORS_PRESENT;
1742
1743                 write_ip_start_end(swqe, skb);
1744
1745                 if (iph->protocol == IPPROTO_UDP) {
1746                         if ((iph->frag_off & IP_MF)
1747                             || (iph->frag_off & IP_OFFSET))
1748                                 /* IP fragment, so don't change cs */
1749                                 swqe->tx_control &= ~EHEA_SWQE_TCP_CHECKSUM;
1750                         else
1751                                 write_udp_offset_end(swqe, skb);
1752                 } else if (iph->protocol == IPPROTO_TCP) {
1753                         write_tcp_offset_end(swqe, skb);
1754                 }
1755
1756                 /* icmp (big data) and ip segmentation packets (all other ip
1757                    packets) do not require any special handling */
1758
1759         } else {
1760                 /* Other Ethernet Protocol */
1761                 swqe->tx_control |= EHEA_SWQE_CRC
1762                                  | EHEA_SWQE_IMM_DATA_PRESENT
1763                                  | EHEA_SWQE_DESCRIPTORS_PRESENT;
1764         }
1765
1766         write_swqe2_data(skb, dev, swqe, lkey);
1767 }
1768
1769 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
1770                        struct ehea_swqe *swqe)
1771 {
1772         int nfrags = skb_shinfo(skb)->nr_frags;
1773         u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
1774         skb_frag_t *frag;
1775         int i;
1776
1777         if (skb->protocol == htons(ETH_P_IP)) {
1778                 const struct iphdr *iph = ip_hdr(skb);
1779
1780                 /* IPv4 */
1781                 write_ip_start_end(swqe, skb);
1782
1783                 if (iph->protocol == IPPROTO_TCP) {
1784                         swqe->tx_control |= EHEA_SWQE_CRC
1785                                          | EHEA_SWQE_IP_CHECKSUM
1786                                          | EHEA_SWQE_TCP_CHECKSUM
1787                                          | EHEA_SWQE_IMM_DATA_PRESENT;
1788
1789                         write_tcp_offset_end(swqe, skb);
1790
1791                 } else if (iph->protocol == IPPROTO_UDP) {
1792                         if ((iph->frag_off & IP_MF)
1793                             || (iph->frag_off & IP_OFFSET))
1794                                 /* IP fragment, so don't change cs */
1795                                 swqe->tx_control |= EHEA_SWQE_CRC
1796                                                  | EHEA_SWQE_IMM_DATA_PRESENT;
1797                         else {
1798                                 swqe->tx_control |= EHEA_SWQE_CRC
1799                                                  | EHEA_SWQE_IP_CHECKSUM
1800                                                  | EHEA_SWQE_TCP_CHECKSUM
1801                                                  | EHEA_SWQE_IMM_DATA_PRESENT;
1802
1803                                 write_udp_offset_end(swqe, skb);
1804                         }
1805                 } else {
1806                         /* icmp (big data) and
1807                            ip segmentation packets (all other ip packets) */
1808                         swqe->tx_control |= EHEA_SWQE_CRC
1809                                          | EHEA_SWQE_IP_CHECKSUM
1810                                          | EHEA_SWQE_IMM_DATA_PRESENT;
1811                 }
1812         } else {
1813                 /* Other Ethernet Protocol */
1814                 swqe->tx_control |= EHEA_SWQE_CRC | EHEA_SWQE_IMM_DATA_PRESENT;
1815         }
1816         /* copy (immediate) data */
1817         if (nfrags == 0) {
1818                 /* data is in a single piece */
1819                 skb_copy_from_linear_data(skb, imm_data, skb->len);
1820         } else {
1821                 /* first copy data from the skb->data buffer ... */
1822                 skb_copy_from_linear_data(skb, imm_data,
1823                                           skb->len - skb->data_len);
1824                 imm_data += skb->len - skb->data_len;
1825
1826                 /* ... then copy data from the fragments */
1827                 for (i = 0; i < nfrags; i++) {
1828                         frag = &skb_shinfo(skb)->frags[i];
1829                         memcpy(imm_data,
1830                                page_address(frag->page) + frag->page_offset,
1831                                frag->size);
1832                         imm_data += frag->size;
1833                 }
1834         }
1835         swqe->immediate_data_length = skb->len;
1836         dev_kfree_skb(skb);
1837 }
1838
1839 static inline int ehea_hash_skb(struct sk_buff *skb, int num_qps)
1840 {
1841         struct tcphdr *tcp;
1842         u32 tmp;
1843
1844         if ((skb->protocol == htons(ETH_P_IP)) &&
1845             (ip_hdr(skb)->protocol == IPPROTO_TCP)) {
1846                 tcp = (struct tcphdr*)(skb_network_header(skb) + (ip_hdr(skb)->ihl * 4));
1847                 tmp = (tcp->source + (tcp->dest << 16)) % 31;
1848                 tmp += ip_hdr(skb)->daddr % 31;
1849                 return tmp % num_qps;
1850         }
1851         else
1852                 return 0;
1853 }
1854
1855 static int ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
1856 {
1857         struct ehea_port *port = netdev_priv(dev);
1858         struct ehea_swqe *swqe;
1859         unsigned long flags;
1860         u32 lkey;
1861         int swqe_index;
1862         struct ehea_port_res *pr;
1863
1864         pr = &port->port_res[ehea_hash_skb(skb, port->num_tx_qps)];
1865
1866         if (!spin_trylock(&pr->xmit_lock))
1867                 return NETDEV_TX_BUSY;
1868
1869         if (pr->queue_stopped) {
1870                 spin_unlock(&pr->xmit_lock);
1871                 return NETDEV_TX_BUSY;
1872         }
1873
1874         swqe = ehea_get_swqe(pr->qp, &swqe_index);
1875         memset(swqe, 0, SWQE_HEADER_SIZE);
1876         atomic_dec(&pr->swqe_avail);
1877
1878         if (skb->len <= SWQE3_MAX_IMM) {
1879                 u32 sig_iv = port->sig_comp_iv;
1880                 u32 swqe_num = pr->swqe_id_counter;
1881                 ehea_xmit3(skb, dev, swqe);
1882                 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
1883                         | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
1884                 if (pr->swqe_ll_count >= (sig_iv - 1)) {
1885                         swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
1886                                                       sig_iv);
1887                         swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1888                         pr->swqe_ll_count = 0;
1889                 } else
1890                         pr->swqe_ll_count += 1;
1891         } else {
1892                 swqe->wr_id =
1893                         EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
1894                       | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
1895                       | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
1896                       | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
1897                 pr->sq_skba.arr[pr->sq_skba.index] = skb;
1898
1899                 pr->sq_skba.index++;
1900                 pr->sq_skba.index &= (pr->sq_skba.len - 1);
1901
1902                 lkey = pr->send_mr.lkey;
1903                 ehea_xmit2(skb, dev, swqe, lkey);
1904                 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
1905         }
1906         pr->swqe_id_counter += 1;
1907
1908         if (port->vgrp && vlan_tx_tag_present(skb)) {
1909                 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
1910                 swqe->vlan_tag = vlan_tx_tag_get(skb);
1911         }
1912
1913         if (netif_msg_tx_queued(port)) {
1914                 ehea_info("post swqe on QP %d", pr->qp->init_attr.qp_nr);
1915                 ehea_dump(swqe, 512, "swqe");
1916         }
1917
1918         if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags)))
1919                 goto out;
1920
1921         ehea_post_swqe(pr->qp, swqe);
1922         pr->tx_packets++;
1923
1924         if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
1925                 spin_lock_irqsave(&pr->netif_queue, flags);
1926                 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
1927                         pr->p_stats.queue_stopped++;
1928                         netif_stop_queue(dev);
1929                         pr->queue_stopped = 1;
1930                 }
1931                 spin_unlock_irqrestore(&pr->netif_queue, flags);
1932         }
1933         dev->trans_start = jiffies;
1934         spin_unlock(&pr->xmit_lock);
1935 out:
1936         return NETDEV_TX_OK;
1937 }
1938
1939 static void ehea_vlan_rx_register(struct net_device *dev,
1940                                   struct vlan_group *grp)
1941 {
1942         struct ehea_port *port = netdev_priv(dev);
1943         struct ehea_adapter *adapter = port->adapter;
1944         struct hcp_ehea_port_cb1 *cb1;
1945         u64 hret;
1946
1947         port->vgrp = grp;
1948
1949         cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1950         if (!cb1) {
1951                 ehea_error("no mem for cb1");
1952                 goto out;
1953         }
1954
1955         memset(cb1->vlan_filter, 0, sizeof(cb1->vlan_filter));
1956
1957         hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
1958                                        H_PORT_CB1, H_PORT_CB1_ALL, cb1);
1959         if (hret != H_SUCCESS)
1960                 ehea_error("modify_ehea_port failed");
1961
1962         kfree(cb1);
1963 out:
1964         return;
1965 }
1966
1967 static void ehea_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
1968 {
1969         struct ehea_port *port = netdev_priv(dev);
1970         struct ehea_adapter *adapter = port->adapter;
1971         struct hcp_ehea_port_cb1 *cb1;
1972         int index;
1973         u64 hret;
1974
1975         cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
1976         if (!cb1) {
1977                 ehea_error("no mem for cb1");
1978                 goto out;
1979         }
1980
1981         hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
1982                                       H_PORT_CB1, H_PORT_CB1_ALL, cb1);
1983         if (hret != H_SUCCESS) {
1984                 ehea_error("query_ehea_port failed");
1985                 goto out;
1986         }
1987
1988         index = (vid / 64);
1989         cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
1990
1991         hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
1992                                        H_PORT_CB1, H_PORT_CB1_ALL, cb1);
1993         if (hret != H_SUCCESS)
1994                 ehea_error("modify_ehea_port failed");
1995 out:
1996         kfree(cb1);
1997         return;
1998 }
1999
2000 static void ehea_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
2001 {
2002         struct ehea_port *port = netdev_priv(dev);
2003         struct ehea_adapter *adapter = port->adapter;
2004         struct hcp_ehea_port_cb1 *cb1;
2005         int index;
2006         u64 hret;
2007
2008         vlan_group_set_device(port->vgrp, vid, NULL);
2009
2010         cb1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2011         if (!cb1) {
2012                 ehea_error("no mem for cb1");
2013                 goto out;
2014         }
2015
2016         hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2017                                       H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2018         if (hret != H_SUCCESS) {
2019                 ehea_error("query_ehea_port failed");
2020                 goto out;
2021         }
2022
2023         index = (vid / 64);
2024         cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2025
2026         hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2027                                        H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2028         if (hret != H_SUCCESS)
2029                 ehea_error("modify_ehea_port failed");
2030 out:
2031         kfree(cb1);
2032         return;
2033 }
2034
2035 int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2036 {
2037         int ret = -EIO;
2038         u64 hret;
2039         u16 dummy16 = 0;
2040         u64 dummy64 = 0;
2041         struct hcp_modify_qp_cb0* cb0;
2042
2043         cb0 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2044         if (!cb0) {
2045                 ret = -ENOMEM;
2046                 goto out;
2047         }
2048
2049         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2050                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2051         if (hret != H_SUCCESS) {
2052                 ehea_error("query_ehea_qp failed (1)");
2053                 goto out;
2054         }
2055
2056         cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2057         hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2058                                      EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2059                                      &dummy64, &dummy64, &dummy16, &dummy16);
2060         if (hret != H_SUCCESS) {
2061                 ehea_error("modify_ehea_qp failed (1)");
2062                 goto out;
2063         }
2064
2065         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2066                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2067         if (hret != H_SUCCESS) {
2068                 ehea_error("query_ehea_qp failed (2)");
2069                 goto out;
2070         }
2071
2072         cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2073         hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2074                                      EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2075                                      &dummy64, &dummy64, &dummy16, &dummy16);
2076         if (hret != H_SUCCESS) {
2077                 ehea_error("modify_ehea_qp failed (2)");
2078                 goto out;
2079         }
2080
2081         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2082                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2083         if (hret != H_SUCCESS) {
2084                 ehea_error("query_ehea_qp failed (3)");
2085                 goto out;
2086         }
2087
2088         cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2089         hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2090                                      EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2091                                      &dummy64, &dummy64, &dummy16, &dummy16);
2092         if (hret != H_SUCCESS) {
2093                 ehea_error("modify_ehea_qp failed (3)");
2094                 goto out;
2095         }
2096
2097         hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2098                                     EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2099         if (hret != H_SUCCESS) {
2100                 ehea_error("query_ehea_qp failed (4)");
2101                 goto out;
2102         }
2103
2104         ret = 0;
2105 out:
2106         kfree(cb0);
2107         return ret;
2108 }
2109
2110 static int ehea_port_res_setup(struct ehea_port *port, int def_qps,
2111                                int add_tx_qps)
2112 {
2113         int ret, i;
2114         struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2115         enum ehea_eq_type eq_type = EHEA_EQ;
2116
2117         port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2118                                    EHEA_MAX_ENTRIES_EQ, 1);
2119         if (!port->qp_eq) {
2120                 ret = -EINVAL;
2121                 ehea_error("ehea_create_eq failed (qp_eq)");
2122                 goto out_kill_eq;
2123         }
2124
2125         pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2126         pr_cfg.max_entries_scq = sq_entries * 2;
2127         pr_cfg.max_entries_sq = sq_entries;
2128         pr_cfg.max_entries_rq1 = rq1_entries;
2129         pr_cfg.max_entries_rq2 = rq2_entries;
2130         pr_cfg.max_entries_rq3 = rq3_entries;
2131
2132         pr_cfg_small_rx.max_entries_rcq = 1;
2133         pr_cfg_small_rx.max_entries_scq = sq_entries;
2134         pr_cfg_small_rx.max_entries_sq = sq_entries;
2135         pr_cfg_small_rx.max_entries_rq1 = 1;
2136         pr_cfg_small_rx.max_entries_rq2 = 1;
2137         pr_cfg_small_rx.max_entries_rq3 = 1;
2138
2139         for (i = 0; i < def_qps; i++) {
2140                 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2141                 if (ret)
2142                         goto out_clean_pr;
2143         }
2144         for (i = def_qps; i < def_qps + add_tx_qps; i++) {
2145                 ret = ehea_init_port_res(port, &port->port_res[i],
2146                                          &pr_cfg_small_rx, i);
2147                 if (ret)
2148                         goto out_clean_pr;
2149         }
2150
2151         return 0;
2152
2153 out_clean_pr:
2154         while (--i >= 0)
2155                 ehea_clean_portres(port, &port->port_res[i]);
2156
2157 out_kill_eq:
2158         ehea_destroy_eq(port->qp_eq);
2159         return ret;
2160 }
2161
2162 static int ehea_clean_all_portres(struct ehea_port *port)
2163 {
2164         int ret = 0;
2165         int i;
2166
2167         for(i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++)
2168                 ret |= ehea_clean_portres(port, &port->port_res[i]);
2169
2170         ret |= ehea_destroy_eq(port->qp_eq);
2171
2172         return ret;
2173 }
2174
2175 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2176 {
2177         if (adapter->active_ports)
2178                 return;
2179
2180         ehea_rem_mr(&adapter->mr);
2181 }
2182
2183 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2184 {
2185         if (adapter->active_ports)
2186                 return 0;
2187
2188         return ehea_reg_kernel_mr(adapter, &adapter->mr);
2189 }
2190
2191 static int ehea_up(struct net_device *dev)
2192 {
2193         int ret, i;
2194         struct ehea_port *port = netdev_priv(dev);
2195
2196         if (port->state == EHEA_PORT_UP)
2197                 return 0;
2198
2199         ret = ehea_port_res_setup(port, port->num_def_qps,
2200                                   port->num_add_tx_qps);
2201         if (ret) {
2202                 ehea_error("port_res_failed");
2203                 goto out;
2204         }
2205
2206         /* Set default QP for this port */
2207         ret = ehea_configure_port(port);
2208         if (ret) {
2209                 ehea_error("ehea_configure_port failed. ret:%d", ret);
2210                 goto out_clean_pr;
2211         }
2212
2213         ret = ehea_reg_interrupts(dev);
2214         if (ret) {
2215                 ehea_error("reg_interrupts failed. ret:%d", ret);
2216                 goto out_clean_pr;
2217         }
2218
2219         for(i = 0; i < port->num_def_qps + port->num_add_tx_qps; i++) {
2220                 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2221                 if (ret) {
2222                         ehea_error("activate_qp failed");
2223                         goto out_free_irqs;
2224                 }
2225         }
2226
2227         for(i = 0; i < port->num_def_qps; i++) {
2228                 ret = ehea_fill_port_res(&port->port_res[i]);
2229                 if (ret) {
2230                         ehea_error("out_free_irqs");
2231                         goto out_free_irqs;
2232                 }
2233         }
2234
2235         ret = 0;
2236         port->state = EHEA_PORT_UP;
2237         goto out;
2238
2239 out_free_irqs:
2240         ehea_free_interrupts(dev);
2241
2242 out_clean_pr:
2243         ehea_clean_all_portres(port);
2244 out:
2245         if (ret)
2246                 ehea_info("Failed starting %s. ret=%i", dev->name, ret);
2247
2248         return ret;
2249 }
2250
2251 static int ehea_open(struct net_device *dev)
2252 {
2253         int ret;
2254         struct ehea_port *port = netdev_priv(dev);
2255
2256         down(&port->port_lock);
2257
2258         if (netif_msg_ifup(port))
2259                 ehea_info("enabling port %s", dev->name);
2260
2261         ret = ehea_up(dev);
2262         if (!ret)
2263                 netif_start_queue(dev);
2264
2265         up(&port->port_lock);
2266
2267         return ret;
2268 }
2269
2270 static int ehea_down(struct net_device *dev)
2271 {
2272         int ret, i;
2273         struct ehea_port *port = netdev_priv(dev);
2274
2275         if (port->state == EHEA_PORT_DOWN)
2276                 return 0;
2277
2278         ehea_drop_multicast_list(dev);
2279         ehea_free_interrupts(dev);
2280
2281         for (i = 0; i < port->num_def_qps; i++)
2282                 while (test_bit(__LINK_STATE_RX_SCHED,
2283                                 &port->port_res[i].d_netdev->state))
2284                         msleep(1);
2285
2286         port->state = EHEA_PORT_DOWN;
2287
2288         ret = ehea_clean_all_portres(port);
2289         if (ret)
2290                 ehea_info("Failed freeing resources for %s. ret=%i",
2291                           dev->name, ret);
2292
2293         return ret;
2294 }
2295
2296 static int ehea_stop(struct net_device *dev)
2297 {
2298         int ret;
2299         struct ehea_port *port = netdev_priv(dev);
2300
2301         if (netif_msg_ifdown(port))
2302                 ehea_info("disabling port %s", dev->name);
2303
2304         flush_workqueue(port->adapter->ehea_wq);
2305         down(&port->port_lock);
2306         netif_stop_queue(dev);
2307         ret = ehea_down(dev);
2308         up(&port->port_lock);
2309         return ret;
2310 }
2311
2312 static void ehea_reset_port(struct work_struct *work)
2313 {
2314         int ret;
2315         struct ehea_port *port =
2316                 container_of(work, struct ehea_port, reset_task);
2317         struct net_device *dev = port->netdev;
2318
2319         port->resets++;
2320         down(&port->port_lock);
2321         netif_stop_queue(dev);
2322         netif_poll_disable(dev);
2323
2324         ehea_down(dev);
2325
2326         ret = ehea_up(dev);
2327         if (ret)
2328                 goto out;
2329
2330         if (netif_msg_timer(port))
2331                 ehea_info("Device %s resetted successfully", dev->name);
2332
2333         netif_poll_enable(dev);
2334         netif_wake_queue(dev);
2335 out:
2336         up(&port->port_lock);
2337         return;
2338 }
2339
2340 static void ehea_rereg_mrs(struct work_struct *work)
2341 {
2342         int ret, i;
2343         struct ehea_adapter *adapter;
2344
2345         ehea_info("LPAR memory enlarged - re-initializing driver");
2346
2347         list_for_each_entry(adapter, &adapter_list, list)
2348                 if (adapter->active_ports) {
2349                         /* Shutdown all ports */
2350                         for (i = 0; i < EHEA_MAX_PORTS; i++) {
2351                                 struct ehea_port *port = adapter->port[i];
2352
2353                                 if (port) {
2354                                         struct net_device *dev = port->netdev;
2355
2356                                         if (dev->flags & IFF_UP) {
2357                                                 ehea_info("stopping %s",
2358                                                           dev->name);
2359                                                 down(&port->port_lock);
2360                                                 netif_stop_queue(dev);
2361                                                 netif_poll_disable(dev);
2362                                                 ehea_down(dev);
2363                                                 up(&port->port_lock);
2364                                         }
2365                                 }
2366                         }
2367
2368                         /* Unregister old memory region */
2369                         ret = ehea_rem_mr(&adapter->mr);
2370                         if (ret) {
2371                                 ehea_error("unregister MR failed - driver"
2372                                            " inoperable!");
2373                                 goto out;
2374                         }
2375                 }
2376
2377         ehea_destroy_busmap();
2378
2379         ret = ehea_create_busmap();
2380         if (ret)
2381                 goto out;
2382
2383         clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2384
2385         list_for_each_entry(adapter, &adapter_list, list)
2386                 if (adapter->active_ports) {
2387                         /* Register new memory region */
2388                         ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2389                         if (ret) {
2390                                 ehea_error("register MR failed - driver"
2391                                            " inoperable!");
2392                                 goto out;
2393                         }
2394
2395                         /* Restart all ports */
2396                         for (i = 0; i < EHEA_MAX_PORTS; i++) {
2397                                 struct ehea_port *port = adapter->port[i];
2398
2399                                 if (port) {
2400                                         struct net_device *dev = port->netdev;
2401
2402                                         if (dev->flags & IFF_UP) {
2403                                                 ehea_info("restarting %s",
2404                                                           dev->name);
2405                                                 down(&port->port_lock);
2406
2407                                                 ret = ehea_up(dev);
2408                                                 if (!ret) {
2409                                                         netif_poll_enable(dev);
2410                                                         netif_wake_queue(dev);
2411                                                 }
2412
2413                                                 up(&port->port_lock);
2414                                         }
2415                                 }
2416                         }
2417                 }
2418 out:
2419         return;
2420 }
2421
2422 static void ehea_tx_watchdog(struct net_device *dev)
2423 {
2424         struct ehea_port *port = netdev_priv(dev);
2425
2426         if (netif_carrier_ok(dev))
2427                 queue_work(port->adapter->ehea_wq, &port->reset_task);
2428 }
2429
2430 int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2431 {
2432         struct hcp_query_ehea *cb;
2433         u64 hret;
2434         int ret;
2435
2436         cb = kzalloc(PAGE_SIZE, GFP_KERNEL);
2437         if (!cb) {
2438                 ret = -ENOMEM;
2439                 goto out;
2440         }
2441
2442         hret = ehea_h_query_ehea(adapter->handle, cb);
2443
2444         if (hret != H_SUCCESS) {
2445                 ret = -EIO;
2446                 goto out_herr;
2447         }
2448
2449         adapter->max_mc_mac = cb->max_mc_mac - 1;
2450         ret = 0;
2451
2452 out_herr:
2453         kfree(cb);
2454 out:
2455         return ret;
2456 }
2457
2458 int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2459 {
2460         struct hcp_ehea_port_cb4 *cb4;
2461         u64 hret;
2462         int ret = 0;
2463
2464         *jumbo = 0;
2465
2466         /* (Try to) enable *jumbo frames */
2467         cb4 = kzalloc(PAGE_SIZE, GFP_KERNEL);
2468         if (!cb4) {
2469                 ehea_error("no mem for cb4");
2470                 ret = -ENOMEM;
2471                 goto out;
2472         } else {
2473                 hret = ehea_h_query_ehea_port(port->adapter->handle,
2474                                               port->logical_port_id,
2475                                               H_PORT_CB4,
2476                                               H_PORT_CB4_JUMBO, cb4);
2477                 if (hret == H_SUCCESS) {
2478                         if (cb4->jumbo_frame)
2479                                 *jumbo = 1;
2480                         else {
2481                                 cb4->jumbo_frame = 1;
2482                                 hret = ehea_h_modify_ehea_port(port->adapter->
2483                                                                handle,
2484                                                                port->
2485                                                                logical_port_id,
2486                                                                H_PORT_CB4,
2487                                                                H_PORT_CB4_JUMBO,
2488                                                                cb4);
2489                                 if (hret == H_SUCCESS)
2490                                         *jumbo = 1;
2491                         }
2492                 } else
2493                         ret = -EINVAL;
2494
2495                 kfree(cb4);
2496         }
2497 out:
2498         return ret;
2499 }
2500
2501 static ssize_t ehea_show_port_id(struct device *dev,
2502                                  struct device_attribute *attr, char *buf)
2503 {
2504         struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2505         return sprintf(buf, "%d", port->logical_port_id);
2506 }
2507
2508 static DEVICE_ATTR(log_port_id, S_IRUSR | S_IRGRP | S_IROTH, ehea_show_port_id,
2509                    NULL);
2510
2511 static void __devinit logical_port_release(struct device *dev)
2512 {
2513         struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2514         of_node_put(port->ofdev.node);
2515 }
2516
2517 static int ehea_driver_sysfs_add(struct device *dev,
2518                                  struct device_driver *driver)
2519 {
2520         int ret;
2521
2522         ret = sysfs_create_link(&driver->kobj, &dev->kobj,
2523                                 kobject_name(&dev->kobj));
2524         if (ret == 0) {
2525                 ret = sysfs_create_link(&dev->kobj, &driver->kobj,
2526                                         "driver");
2527                 if (ret)
2528                         sysfs_remove_link(&driver->kobj,
2529                                           kobject_name(&dev->kobj));
2530         }
2531         return ret;
2532 }
2533
2534 static void ehea_driver_sysfs_remove(struct device *dev,
2535                                      struct device_driver *driver)
2536 {
2537         struct device_driver *drv = driver;
2538
2539         if (drv) {
2540                 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
2541                 sysfs_remove_link(&dev->kobj, "driver");
2542         }
2543 }
2544
2545 static struct device *ehea_register_port(struct ehea_port *port,
2546                                          struct device_node *dn)
2547 {
2548         int ret;
2549
2550         port->ofdev.node = of_node_get(dn);
2551         port->ofdev.dev.parent = &port->adapter->ebus_dev->ofdev.dev;
2552         port->ofdev.dev.bus = &ibmebus_bus_type;
2553
2554         sprintf(port->ofdev.dev.bus_id, "port%d", port_name_cnt++);
2555         port->ofdev.dev.release = logical_port_release;
2556
2557         ret = of_device_register(&port->ofdev);
2558         if (ret) {
2559                 ehea_error("failed to register device. ret=%d", ret);
2560                 goto out;
2561         }
2562
2563         ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2564         if (ret) {
2565                 ehea_error("failed to register attributes, ret=%d", ret);
2566                 goto out_unreg_of_dev;
2567         }
2568
2569         ret = ehea_driver_sysfs_add(&port->ofdev.dev, &ehea_driver.driver);
2570         if (ret) {
2571                 ehea_error("failed to register sysfs driver link");
2572                 goto out_rem_dev_file;
2573         }
2574
2575         return &port->ofdev.dev;
2576
2577 out_rem_dev_file:
2578         device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2579 out_unreg_of_dev:
2580         of_device_unregister(&port->ofdev);
2581 out:
2582         return NULL;
2583 }
2584
2585 static void ehea_unregister_port(struct ehea_port *port)
2586 {
2587         ehea_driver_sysfs_remove(&port->ofdev.dev, &ehea_driver.driver);
2588         device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2589         of_device_unregister(&port->ofdev);
2590 }
2591
2592 struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2593                                          u32 logical_port_id,
2594                                          struct device_node *dn)
2595 {
2596         int ret;
2597         struct net_device *dev;
2598         struct ehea_port *port;
2599         struct device *port_dev;
2600         int jumbo;
2601
2602         /* allocate memory for the port structures */
2603         dev = alloc_etherdev(sizeof(struct ehea_port));
2604
2605         if (!dev) {
2606                 ehea_error("no mem for net_device");
2607                 ret = -ENOMEM;
2608                 goto out_err;
2609         }
2610
2611         port = netdev_priv(dev);
2612
2613         sema_init(&port->port_lock, 1);
2614         port->state = EHEA_PORT_DOWN;
2615         port->sig_comp_iv = sq_entries / 10;
2616
2617         port->adapter = adapter;
2618         port->netdev = dev;
2619         port->logical_port_id = logical_port_id;
2620
2621         port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2622
2623         port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2624         if (!port->mc_list) {
2625                 ret = -ENOMEM;
2626                 goto out_free_ethdev;
2627         }
2628
2629         INIT_LIST_HEAD(&port->mc_list->list);
2630
2631         ret = ehea_sense_port_attr(port);
2632         if (ret)
2633                 goto out_free_mc_list;
2634
2635         port_dev = ehea_register_port(port, dn);
2636         if (!port_dev)
2637                 goto out_free_mc_list;
2638
2639         SET_NETDEV_DEV(dev, port_dev);
2640
2641         /* initialize net_device structure */
2642         SET_MODULE_OWNER(dev);
2643
2644         memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
2645
2646         dev->open = ehea_open;
2647         dev->poll = ehea_poll_firstqueue;
2648 #ifdef CONFIG_NET_POLL_CONTROLLER
2649         dev->poll_controller = ehea_netpoll;
2650 #endif
2651         dev->weight = 64;
2652         dev->stop = ehea_stop;
2653         dev->hard_start_xmit = ehea_start_xmit;
2654         dev->get_stats = ehea_get_stats;
2655         dev->set_multicast_list = ehea_set_multicast_list;
2656         dev->set_mac_address = ehea_set_mac_addr;
2657         dev->change_mtu = ehea_change_mtu;
2658         dev->vlan_rx_register = ehea_vlan_rx_register;
2659         dev->vlan_rx_add_vid = ehea_vlan_rx_add_vid;
2660         dev->vlan_rx_kill_vid = ehea_vlan_rx_kill_vid;
2661         dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO
2662                       | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX
2663                       | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER
2664                       | NETIF_F_LLTX;
2665         dev->tx_timeout = &ehea_tx_watchdog;
2666         dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
2667
2668         INIT_WORK(&port->reset_task, ehea_reset_port);
2669
2670         ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2671         if (ret) {
2672                 ret = -EIO;
2673                 goto out_unreg_port;
2674         }
2675
2676         ehea_set_ethtool_ops(dev);
2677
2678         ret = register_netdev(dev);
2679         if (ret) {
2680                 ehea_error("register_netdev failed. ret=%d", ret);
2681                 goto out_dereg_bc;
2682         }
2683
2684         ret = ehea_get_jumboframe_status(port, &jumbo);
2685         if (ret)
2686                 ehea_error("failed determining jumbo frame status for %s",
2687                            port->netdev->name);
2688
2689         ehea_info("%s: Jumbo frames are %sabled", dev->name,
2690                   jumbo == 1 ? "en" : "dis");
2691
2692         adapter->active_ports++;
2693
2694         return port;
2695
2696 out_dereg_bc:
2697         ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2698
2699 out_unreg_port:
2700         ehea_unregister_port(port);
2701
2702 out_free_mc_list:
2703         kfree(port->mc_list);
2704
2705 out_free_ethdev:
2706         free_netdev(dev);
2707
2708 out_err:
2709         ehea_error("setting up logical port with id=%d failed, ret=%d",
2710                    logical_port_id, ret);
2711         return NULL;
2712 }
2713
2714 static void ehea_shutdown_single_port(struct ehea_port *port)
2715 {
2716         unregister_netdev(port->netdev);
2717         ehea_unregister_port(port);
2718         ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2719         kfree(port->mc_list);
2720         free_netdev(port->netdev);
2721         port->adapter->active_ports--;
2722 }
2723
2724 static int ehea_setup_ports(struct ehea_adapter *adapter)
2725 {
2726         struct device_node *lhea_dn;
2727         struct device_node *eth_dn = NULL;
2728
2729         const u32 *dn_log_port_id;
2730         int i = 0;
2731
2732         lhea_dn = adapter->ebus_dev->ofdev.node;
2733         while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
2734
2735                 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
2736                                                  NULL);
2737                 if (!dn_log_port_id) {
2738                         ehea_error("bad device node: eth_dn name=%s",
2739                                    eth_dn->full_name);
2740                         continue;
2741                 }
2742
2743                 if (ehea_add_adapter_mr(adapter)) {
2744                         ehea_error("creating MR failed");
2745                         of_node_put(eth_dn);
2746                         return -EIO;
2747                 }
2748
2749                 adapter->port[i] = ehea_setup_single_port(adapter,
2750                                                           *dn_log_port_id,
2751                                                           eth_dn);
2752                 if (adapter->port[i])
2753                         ehea_info("%s -> logical port id #%d",
2754                                   adapter->port[i]->netdev->name,
2755                                   *dn_log_port_id);
2756                 else
2757                         ehea_remove_adapter_mr(adapter);
2758
2759                 i++;
2760         };
2761
2762         return 0;
2763 }
2764
2765 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
2766                                            u32 logical_port_id)
2767 {
2768         struct device_node *lhea_dn;
2769         struct device_node *eth_dn = NULL;
2770         const u32 *dn_log_port_id;
2771
2772         lhea_dn = adapter->ebus_dev->ofdev.node;
2773         while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
2774
2775                 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
2776                                                  NULL);
2777                 if (dn_log_port_id)
2778                         if (*dn_log_port_id == logical_port_id)
2779                                 return eth_dn;
2780         };
2781
2782         return NULL;
2783 }
2784
2785 static ssize_t ehea_probe_port(struct device *dev,
2786                                struct device_attribute *attr,
2787                                const char *buf, size_t count)
2788 {
2789         struct ehea_adapter *adapter = dev->driver_data;
2790         struct ehea_port *port;
2791         struct device_node *eth_dn = NULL;
2792         int i;
2793
2794         u32 logical_port_id;
2795
2796         sscanf(buf, "%d", &logical_port_id);
2797
2798         port = ehea_get_port(adapter, logical_port_id);
2799
2800         if (port) {
2801                 ehea_info("adding port with logical port id=%d failed. port "
2802                           "already configured as %s.", logical_port_id,
2803                           port->netdev->name);
2804                 return -EINVAL;
2805         }
2806
2807         eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
2808
2809         if (!eth_dn) {
2810                 ehea_info("no logical port with id %d found", logical_port_id);
2811                 return -EINVAL;
2812         }
2813
2814         if (ehea_add_adapter_mr(adapter)) {
2815                 ehea_error("creating MR failed");
2816                 return -EIO;
2817         }
2818
2819         port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
2820
2821         of_node_put(eth_dn);
2822
2823         if (port) {
2824                 for (i=0; i < EHEA_MAX_PORTS; i++)
2825                         if (!adapter->port[i]) {
2826                                 adapter->port[i] = port;
2827                                 break;
2828                         }
2829
2830                 ehea_info("added %s (logical port id=%d)", port->netdev->name,
2831                           logical_port_id);
2832         } else {
2833                 ehea_remove_adapter_mr(adapter);
2834                 return -EIO;
2835         }
2836
2837         return (ssize_t) count;
2838 }
2839
2840 static ssize_t ehea_remove_port(struct device *dev,
2841                                 struct device_attribute *attr,
2842                                 const char *buf, size_t count)
2843 {
2844         struct ehea_adapter *adapter = dev->driver_data;
2845         struct ehea_port *port;
2846         int i;
2847         u32 logical_port_id;
2848
2849         sscanf(buf, "%d", &logical_port_id);
2850
2851         port = ehea_get_port(adapter, logical_port_id);
2852
2853         if (port) {
2854                 ehea_info("removed %s (logical port id=%d)", port->netdev->name,
2855                           logical_port_id);
2856
2857                 ehea_shutdown_single_port(port);
2858
2859                 for (i=0; i < EHEA_MAX_PORTS; i++)
2860                         if (adapter->port[i] == port) {
2861                                 adapter->port[i] = NULL;
2862                                 break;
2863                         }
2864         } else {
2865                 ehea_error("removing port with logical port id=%d failed. port "
2866                            "not configured.", logical_port_id);
2867                 return -EINVAL;
2868         }
2869
2870         ehea_remove_adapter_mr(adapter);
2871
2872         return (ssize_t) count;
2873 }
2874
2875 static DEVICE_ATTR(probe_port, S_IWUSR, NULL, ehea_probe_port);
2876 static DEVICE_ATTR(remove_port, S_IWUSR, NULL, ehea_remove_port);
2877
2878 int ehea_create_device_sysfs(struct ibmebus_dev *dev)
2879 {
2880         int ret = device_create_file(&dev->ofdev.dev, &dev_attr_probe_port);
2881         if (ret)
2882                 goto out;
2883
2884         ret = device_create_file(&dev->ofdev.dev, &dev_attr_remove_port);
2885 out:
2886         return ret;
2887 }
2888
2889 void ehea_remove_device_sysfs(struct ibmebus_dev *dev)
2890 {
2891         device_remove_file(&dev->ofdev.dev, &dev_attr_probe_port);
2892         device_remove_file(&dev->ofdev.dev, &dev_attr_remove_port);
2893 }
2894
2895 static int __devinit ehea_probe_adapter(struct ibmebus_dev *dev,
2896                                         const struct of_device_id *id)
2897 {
2898         struct ehea_adapter *adapter;
2899         const u64 *adapter_handle;
2900         int ret;
2901
2902         if (!dev || !dev->ofdev.node) {
2903                 ehea_error("Invalid ibmebus device probed");
2904                 return -EINVAL;
2905         }
2906
2907         adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
2908         if (!adapter) {
2909                 ret = -ENOMEM;
2910                 dev_err(&dev->ofdev.dev, "no mem for ehea_adapter\n");
2911                 goto out;
2912         }
2913
2914         list_add(&adapter->list, &adapter_list);
2915
2916         adapter->ebus_dev = dev;
2917
2918         adapter_handle = of_get_property(dev->ofdev.node, "ibm,hea-handle",
2919                                          NULL);
2920         if (adapter_handle)
2921                 adapter->handle = *adapter_handle;
2922
2923         if (!adapter->handle) {
2924                 dev_err(&dev->ofdev.dev, "failed getting handle for adapter"
2925                         " '%s'\n", dev->ofdev.node->full_name);
2926                 ret = -ENODEV;
2927                 goto out_free_ad;
2928         }
2929
2930         adapter->pd = EHEA_PD_ID;
2931
2932         dev->ofdev.dev.driver_data = adapter;
2933
2934
2935         /* initialize adapter and ports */
2936         /* get adapter properties */
2937         ret = ehea_sense_adapter_attr(adapter);
2938         if (ret) {
2939                 dev_err(&dev->ofdev.dev, "sense_adapter_attr failed: %d", ret);
2940                 goto out_free_ad;
2941         }
2942
2943         adapter->neq = ehea_create_eq(adapter,
2944                                       EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
2945         if (!adapter->neq) {
2946                 ret = -EIO;
2947                 dev_err(&dev->ofdev.dev, "NEQ creation failed");
2948                 goto out_free_ad;
2949         }
2950
2951         tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
2952                      (unsigned long)adapter);
2953
2954         ret = ibmebus_request_irq(NULL, adapter->neq->attr.ist1,
2955                                   ehea_interrupt_neq, IRQF_DISABLED,
2956                                   "ehea_neq", adapter);
2957         if (ret) {
2958                 dev_err(&dev->ofdev.dev, "requesting NEQ IRQ failed");
2959                 goto out_kill_eq;
2960         }
2961
2962         adapter->ehea_wq = create_workqueue("ehea_wq");
2963         if (!adapter->ehea_wq) {
2964                 ret = -EIO;
2965                 goto out_free_irq;
2966         }
2967
2968         ret = ehea_create_device_sysfs(dev);
2969         if (ret)
2970                 goto out_kill_wq;
2971
2972         ret = ehea_setup_ports(adapter);
2973         if (ret) {
2974                 dev_err(&dev->ofdev.dev, "setup_ports failed");
2975                 goto out_rem_dev_sysfs;
2976         }
2977
2978         ret = 0;
2979         goto out;
2980
2981 out_rem_dev_sysfs:
2982         ehea_remove_device_sysfs(dev);
2983
2984 out_kill_wq:
2985         destroy_workqueue(adapter->ehea_wq);
2986
2987 out_free_irq:
2988         ibmebus_free_irq(NULL, adapter->neq->attr.ist1, adapter);
2989
2990 out_kill_eq:
2991         ehea_destroy_eq(adapter->neq);
2992
2993 out_free_ad:
2994         kfree(adapter);
2995 out:
2996         return ret;
2997 }
2998
2999 static int __devexit ehea_remove(struct ibmebus_dev *dev)
3000 {
3001         struct ehea_adapter *adapter = dev->ofdev.dev.driver_data;
3002         int i;
3003
3004         for (i = 0; i < EHEA_MAX_PORTS; i++)
3005                 if (adapter->port[i]) {
3006                         ehea_shutdown_single_port(adapter->port[i]);
3007                         adapter->port[i] = NULL;
3008                 }
3009
3010         ehea_remove_device_sysfs(dev);
3011
3012         destroy_workqueue(adapter->ehea_wq);
3013
3014         ibmebus_free_irq(NULL, adapter->neq->attr.ist1, adapter);
3015         tasklet_kill(&adapter->neq_tasklet);
3016
3017         ehea_destroy_eq(adapter->neq);
3018         ehea_remove_adapter_mr(adapter);
3019         list_del(&adapter->list);
3020
3021         kfree(adapter);
3022
3023         return 0;
3024 }
3025
3026 static int check_module_parm(void)
3027 {
3028         int ret = 0;
3029
3030         if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3031             (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3032                 ehea_info("Bad parameter: rq1_entries");
3033                 ret = -EINVAL;
3034         }
3035         if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3036             (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3037                 ehea_info("Bad parameter: rq2_entries");
3038                 ret = -EINVAL;
3039         }
3040         if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3041             (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3042                 ehea_info("Bad parameter: rq3_entries");
3043                 ret = -EINVAL;
3044         }
3045         if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3046             (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3047                 ehea_info("Bad parameter: sq_entries");
3048                 ret = -EINVAL;
3049         }
3050
3051         return ret;
3052 }
3053
3054 static ssize_t ehea_show_capabilities(struct device_driver *drv,
3055                                       char *buf)
3056 {
3057         return sprintf(buf, "%d", EHEA_CAPABILITIES);
3058 }
3059
3060 static DRIVER_ATTR(capabilities, S_IRUSR | S_IRGRP | S_IROTH,
3061                    ehea_show_capabilities, NULL);
3062
3063 int __init ehea_module_init(void)
3064 {
3065         int ret;
3066
3067         printk(KERN_INFO "IBM eHEA ethernet device driver (Release %s)\n",
3068                DRV_VERSION);
3069
3070         ehea_driver_wq = create_workqueue("ehea_driver_wq");
3071
3072         INIT_WORK(&ehea_rereg_mr_task, ehea_rereg_mrs);
3073
3074         ret = check_module_parm();
3075         if (ret)
3076                 goto out;
3077
3078         ret = ehea_create_busmap();
3079         if (ret)
3080                 goto out;
3081
3082         ret = ibmebus_register_driver(&ehea_driver);
3083         if (ret) {
3084                 ehea_error("failed registering eHEA device driver on ebus");
3085                 goto out;
3086         }
3087
3088         ret = driver_create_file(&ehea_driver.driver,
3089                                  &driver_attr_capabilities);
3090         if (ret) {
3091                 ehea_error("failed to register capabilities attribute, ret=%d",
3092                            ret);
3093                 ibmebus_unregister_driver(&ehea_driver);
3094                 goto out;
3095         }
3096
3097 out:
3098         return ret;
3099 }
3100
3101 static void __exit ehea_module_exit(void)
3102 {
3103         destroy_workqueue(ehea_driver_wq);
3104         driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3105         ibmebus_unregister_driver(&ehea_driver);
3106         ehea_destroy_busmap();
3107 }
3108
3109 module_init(ehea_module_init);
3110 module_exit(ehea_module_exit);