1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/net/ethernet/ibm/ehea/ehea_main.c
4 *
5 * eHEA ethernet device driver for IBM eServer System p
6 *
7 * (C) Copyright IBM Corp. 2006
8 *
9 * Authors:
10 * Christoph Raisch <raisch@de.ibm.com>
11 * Jan-Bernd Themann <themann@de.ibm.com>
12 * Thomas Klein <tklein@de.ibm.com>
13 */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/device.h>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 #include <linux/tcp.h>
21 #include <linux/udp.h>
22 #include <linux/if.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/if_ether.h>
26 #include <linux/notifier.h>
27 #include <linux/reboot.h>
28 #include <linux/memory.h>
29 #include <asm/kexec.h>
30 #include <linux/mutex.h>
31 #include <linux/prefetch.h>
32
33 #include <net/ip.h>
34
35 #include "ehea.h"
36 #include "ehea_qmr.h"
37 #include "ehea_phyp.h"
38
39
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
42 MODULE_DESCRIPTION("IBM eServer HEA Driver");
43 MODULE_VERSION(DRV_VERSION);
44
45
46 static int msg_level = -1;
47 static int rq1_entries = EHEA_DEF_ENTRIES_RQ1;
48 static int rq2_entries = EHEA_DEF_ENTRIES_RQ2;
49 static int rq3_entries = EHEA_DEF_ENTRIES_RQ3;
50 static int sq_entries = EHEA_DEF_ENTRIES_SQ;
51 static int use_mcs = 1;
52 static int prop_carrier_state;
53
54 module_param(msg_level, int, 0);
55 module_param(rq1_entries, int, 0);
56 module_param(rq2_entries, int, 0);
57 module_param(rq3_entries, int, 0);
58 module_param(sq_entries, int, 0);
59 module_param(prop_carrier_state, int, 0);
60 module_param(use_mcs, int, 0);
61
62 MODULE_PARM_DESC(msg_level, "msg_level");
63 MODULE_PARM_DESC(prop_carrier_state, "Propagate carrier state of physical "
64 "port to stack. 1:yes, 0:no. Default = 0 ");
65 MODULE_PARM_DESC(rq3_entries, "Number of entries for Receive Queue 3 "
66 "[2^x - 1], x = [7..14]. Default = "
67 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ3) ")");
68 MODULE_PARM_DESC(rq2_entries, "Number of entries for Receive Queue 2 "
69 "[2^x - 1], x = [7..14]. Default = "
70 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ2) ")");
71 MODULE_PARM_DESC(rq1_entries, "Number of entries for Receive Queue 1 "
72 "[2^x - 1], x = [7..14]. Default = "
73 __MODULE_STRING(EHEA_DEF_ENTRIES_RQ1) ")");
74 MODULE_PARM_DESC(sq_entries, " Number of entries for the Send Queue "
75 "[2^x - 1], x = [7..14]. Default = "
76 __MODULE_STRING(EHEA_DEF_ENTRIES_SQ) ")");
77 MODULE_PARM_DESC(use_mcs, " Multiple receive queues, 1: enable, 0: disable, "
78 "Default = 1");
79
80 static int port_name_cnt;
81 static LIST_HEAD(adapter_list);
82 static unsigned long ehea_driver_flags;
83 static DEFINE_MUTEX(dlpar_mem_lock);
84 static struct ehea_fw_handle_array ehea_fw_handles;
85 static struct ehea_bcmc_reg_array ehea_bcmc_regs;
86
87
88 static int ehea_probe_adapter(struct platform_device *dev);
89
90 static int ehea_remove(struct platform_device *dev);
91
92 static const struct of_device_id ehea_module_device_table[] = {
93 {
94 .name = "lhea",
95 .compatible = "IBM,lhea",
96 },
97 {
98 .type = "network",
99 .compatible = "IBM,lhea-ethernet",
100 },
101 {},
102 };
103 MODULE_DEVICE_TABLE(of, ehea_module_device_table);
104
105 static const struct of_device_id ehea_device_table[] = {
106 {
107 .name = "lhea",
108 .compatible = "IBM,lhea",
109 },
110 {},
111 };
112 MODULE_DEVICE_TABLE(of, ehea_device_table);
113
114 static struct platform_driver ehea_driver = {
115 .driver = {
116 .name = "ehea",
117 .owner = THIS_MODULE,
118 .of_match_table = ehea_device_table,
119 },
120 .probe = ehea_probe_adapter,
121 .remove = ehea_remove,
122 };
123
ehea_dump(void * adr,int len,char * msg)124 void ehea_dump(void *adr, int len, char *msg)
125 {
126 int x;
127 unsigned char *deb = adr;
128 for (x = 0; x < len; x += 16) {
129 pr_info("%s adr=%p ofs=%04x %016llx %016llx\n",
130 msg, deb, x, *((u64 *)&deb[0]), *((u64 *)&deb[8]));
131 deb += 16;
132 }
133 }
134
ehea_schedule_port_reset(struct ehea_port * port)135 static void ehea_schedule_port_reset(struct ehea_port *port)
136 {
137 if (!test_bit(__EHEA_DISABLE_PORT_RESET, &port->flags))
138 schedule_work(&port->reset_task);
139 }
140
ehea_update_firmware_handles(void)141 static void ehea_update_firmware_handles(void)
142 {
143 struct ehea_fw_handle_entry *arr = NULL;
144 struct ehea_adapter *adapter;
145 int num_adapters = 0;
146 int num_ports = 0;
147 int num_portres = 0;
148 int i = 0;
149 int num_fw_handles, k, l;
150
151 /* Determine number of handles */
152 mutex_lock(&ehea_fw_handles.lock);
153
154 list_for_each_entry(adapter, &adapter_list, list) {
155 num_adapters++;
156
157 for (k = 0; k < EHEA_MAX_PORTS; k++) {
158 struct ehea_port *port = adapter->port[k];
159
160 if (!port || (port->state != EHEA_PORT_UP))
161 continue;
162
163 num_ports++;
164 num_portres += port->num_def_qps;
165 }
166 }
167
168 num_fw_handles = num_adapters * EHEA_NUM_ADAPTER_FW_HANDLES +
169 num_ports * EHEA_NUM_PORT_FW_HANDLES +
170 num_portres * EHEA_NUM_PORTRES_FW_HANDLES;
171
172 if (num_fw_handles) {
173 arr = kcalloc(num_fw_handles, sizeof(*arr), GFP_KERNEL);
174 if (!arr)
175 goto out; /* Keep the existing array */
176 } else
177 goto out_update;
178
179 list_for_each_entry(adapter, &adapter_list, list) {
180 if (num_adapters == 0)
181 break;
182
183 for (k = 0; k < EHEA_MAX_PORTS; k++) {
184 struct ehea_port *port = adapter->port[k];
185
186 if (!port || (port->state != EHEA_PORT_UP) ||
187 (num_ports == 0))
188 continue;
189
190 for (l = 0; l < port->num_def_qps; l++) {
191 struct ehea_port_res *pr = &port->port_res[l];
192
193 arr[i].adh = adapter->handle;
194 arr[i++].fwh = pr->qp->fw_handle;
195 arr[i].adh = adapter->handle;
196 arr[i++].fwh = pr->send_cq->fw_handle;
197 arr[i].adh = adapter->handle;
198 arr[i++].fwh = pr->recv_cq->fw_handle;
199 arr[i].adh = adapter->handle;
200 arr[i++].fwh = pr->eq->fw_handle;
201 arr[i].adh = adapter->handle;
202 arr[i++].fwh = pr->send_mr.handle;
203 arr[i].adh = adapter->handle;
204 arr[i++].fwh = pr->recv_mr.handle;
205 }
206 arr[i].adh = adapter->handle;
207 arr[i++].fwh = port->qp_eq->fw_handle;
208 num_ports--;
209 }
210
211 arr[i].adh = adapter->handle;
212 arr[i++].fwh = adapter->neq->fw_handle;
213
214 if (adapter->mr.handle) {
215 arr[i].adh = adapter->handle;
216 arr[i++].fwh = adapter->mr.handle;
217 }
218 num_adapters--;
219 }
220
221 out_update:
222 kfree(ehea_fw_handles.arr);
223 ehea_fw_handles.arr = arr;
224 ehea_fw_handles.num_entries = i;
225 out:
226 mutex_unlock(&ehea_fw_handles.lock);
227 }
228
ehea_update_bcmc_registrations(void)229 static void ehea_update_bcmc_registrations(void)
230 {
231 unsigned long flags;
232 struct ehea_bcmc_reg_entry *arr = NULL;
233 struct ehea_adapter *adapter;
234 struct ehea_mc_list *mc_entry;
235 int num_registrations = 0;
236 int i = 0;
237 int k;
238
239 spin_lock_irqsave(&ehea_bcmc_regs.lock, flags);
240
241 /* Determine number of registrations */
242 list_for_each_entry(adapter, &adapter_list, list)
243 for (k = 0; k < EHEA_MAX_PORTS; k++) {
244 struct ehea_port *port = adapter->port[k];
245
246 if (!port || (port->state != EHEA_PORT_UP))
247 continue;
248
249 num_registrations += 2; /* Broadcast registrations */
250
251 list_for_each_entry(mc_entry, &port->mc_list->list,list)
252 num_registrations += 2;
253 }
254
255 if (num_registrations) {
256 arr = kcalloc(num_registrations, sizeof(*arr), GFP_ATOMIC);
257 if (!arr)
258 goto out; /* Keep the existing array */
259 } else
260 goto out_update;
261
262 list_for_each_entry(adapter, &adapter_list, list) {
263 for (k = 0; k < EHEA_MAX_PORTS; k++) {
264 struct ehea_port *port = adapter->port[k];
265
266 if (!port || (port->state != EHEA_PORT_UP))
267 continue;
268
269 if (num_registrations == 0)
270 goto out_update;
271
272 arr[i].adh = adapter->handle;
273 arr[i].port_id = port->logical_port_id;
274 arr[i].reg_type = EHEA_BCMC_BROADCAST |
275 EHEA_BCMC_UNTAGGED;
276 arr[i++].macaddr = port->mac_addr;
277
278 arr[i].adh = adapter->handle;
279 arr[i].port_id = port->logical_port_id;
280 arr[i].reg_type = EHEA_BCMC_BROADCAST |
281 EHEA_BCMC_VLANID_ALL;
282 arr[i++].macaddr = port->mac_addr;
283 num_registrations -= 2;
284
285 list_for_each_entry(mc_entry,
286 &port->mc_list->list, list) {
287 if (num_registrations == 0)
288 goto out_update;
289
290 arr[i].adh = adapter->handle;
291 arr[i].port_id = port->logical_port_id;
292 arr[i].reg_type = EHEA_BCMC_MULTICAST |
293 EHEA_BCMC_UNTAGGED;
294 if (mc_entry->macaddr == 0)
295 arr[i].reg_type |= EHEA_BCMC_SCOPE_ALL;
296 arr[i++].macaddr = mc_entry->macaddr;
297
298 arr[i].adh = adapter->handle;
299 arr[i].port_id = port->logical_port_id;
300 arr[i].reg_type = EHEA_BCMC_MULTICAST |
301 EHEA_BCMC_VLANID_ALL;
302 if (mc_entry->macaddr == 0)
303 arr[i].reg_type |= EHEA_BCMC_SCOPE_ALL;
304 arr[i++].macaddr = mc_entry->macaddr;
305 num_registrations -= 2;
306 }
307 }
308 }
309
310 out_update:
311 kfree(ehea_bcmc_regs.arr);
312 ehea_bcmc_regs.arr = arr;
313 ehea_bcmc_regs.num_entries = i;
314 out:
315 spin_unlock_irqrestore(&ehea_bcmc_regs.lock, flags);
316 }
317
ehea_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)318 static void ehea_get_stats64(struct net_device *dev,
319 struct rtnl_link_stats64 *stats)
320 {
321 struct ehea_port *port = netdev_priv(dev);
322 u64 rx_packets = 0, tx_packets = 0, rx_bytes = 0, tx_bytes = 0;
323 int i;
324
325 for (i = 0; i < port->num_def_qps; i++) {
326 rx_packets += port->port_res[i].rx_packets;
327 rx_bytes += port->port_res[i].rx_bytes;
328 }
329
330 for (i = 0; i < port->num_def_qps; i++) {
331 tx_packets += port->port_res[i].tx_packets;
332 tx_bytes += port->port_res[i].tx_bytes;
333 }
334
335 stats->tx_packets = tx_packets;
336 stats->rx_bytes = rx_bytes;
337 stats->tx_bytes = tx_bytes;
338 stats->rx_packets = rx_packets;
339
340 stats->multicast = port->stats.multicast;
341 stats->rx_errors = port->stats.rx_errors;
342 }
343
ehea_update_stats(struct work_struct * work)344 static void ehea_update_stats(struct work_struct *work)
345 {
346 struct ehea_port *port =
347 container_of(work, struct ehea_port, stats_work.work);
348 struct net_device *dev = port->netdev;
349 struct rtnl_link_stats64 *stats = &port->stats;
350 struct hcp_ehea_port_cb2 *cb2;
351 u64 hret;
352
353 cb2 = (void *)get_zeroed_page(GFP_KERNEL);
354 if (!cb2) {
355 netdev_err(dev, "No mem for cb2. Some interface statistics were not updated\n");
356 goto resched;
357 }
358
359 hret = ehea_h_query_ehea_port(port->adapter->handle,
360 port->logical_port_id,
361 H_PORT_CB2, H_PORT_CB2_ALL, cb2);
362 if (hret != H_SUCCESS) {
363 netdev_err(dev, "query_ehea_port failed\n");
364 goto out_herr;
365 }
366
367 if (netif_msg_hw(port))
368 ehea_dump(cb2, sizeof(*cb2), "net_device_stats");
369
370 stats->multicast = cb2->rxmcp;
371 stats->rx_errors = cb2->rxuerr;
372
373 out_herr:
374 free_page((unsigned long)cb2);
375 resched:
376 schedule_delayed_work(&port->stats_work,
377 round_jiffies_relative(msecs_to_jiffies(1000)));
378 }
379
ehea_refill_rq1(struct ehea_port_res * pr,int index,int nr_of_wqes)380 static void ehea_refill_rq1(struct ehea_port_res *pr, int index, int nr_of_wqes)
381 {
382 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
383 struct net_device *dev = pr->port->netdev;
384 int max_index_mask = pr->rq1_skba.len - 1;
385 int fill_wqes = pr->rq1_skba.os_skbs + nr_of_wqes;
386 int adder = 0;
387 int i;
388
389 pr->rq1_skba.os_skbs = 0;
390
391 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
392 if (nr_of_wqes > 0)
393 pr->rq1_skba.index = index;
394 pr->rq1_skba.os_skbs = fill_wqes;
395 return;
396 }
397
398 for (i = 0; i < fill_wqes; i++) {
399 if (!skb_arr_rq1[index]) {
400 skb_arr_rq1[index] = netdev_alloc_skb(dev,
401 EHEA_L_PKT_SIZE);
402 if (!skb_arr_rq1[index]) {
403 pr->rq1_skba.os_skbs = fill_wqes - i;
404 break;
405 }
406 }
407 index--;
408 index &= max_index_mask;
409 adder++;
410 }
411
412 if (adder == 0)
413 return;
414
415 /* Ring doorbell */
416 ehea_update_rq1a(pr->qp, adder);
417 }
418
ehea_init_fill_rq1(struct ehea_port_res * pr,int nr_rq1a)419 static void ehea_init_fill_rq1(struct ehea_port_res *pr, int nr_rq1a)
420 {
421 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
422 struct net_device *dev = pr->port->netdev;
423 int i;
424
425 if (nr_rq1a > pr->rq1_skba.len) {
426 netdev_err(dev, "NR_RQ1A bigger than skb array len\n");
427 return;
428 }
429
430 for (i = 0; i < nr_rq1a; i++) {
431 skb_arr_rq1[i] = netdev_alloc_skb(dev, EHEA_L_PKT_SIZE);
432 if (!skb_arr_rq1[i])
433 break;
434 }
435 /* Ring doorbell */
436 ehea_update_rq1a(pr->qp, i - 1);
437 }
438
ehea_refill_rq_def(struct ehea_port_res * pr,struct ehea_q_skb_arr * q_skba,int rq_nr,int num_wqes,int wqe_type,int packet_size)439 static int ehea_refill_rq_def(struct ehea_port_res *pr,
440 struct ehea_q_skb_arr *q_skba, int rq_nr,
441 int num_wqes, int wqe_type, int packet_size)
442 {
443 struct net_device *dev = pr->port->netdev;
444 struct ehea_qp *qp = pr->qp;
445 struct sk_buff **skb_arr = q_skba->arr;
446 struct ehea_rwqe *rwqe;
447 int i, index, max_index_mask, fill_wqes;
448 int adder = 0;
449 int ret = 0;
450
451 fill_wqes = q_skba->os_skbs + num_wqes;
452 q_skba->os_skbs = 0;
453
454 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
455 q_skba->os_skbs = fill_wqes;
456 return ret;
457 }
458
459 index = q_skba->index;
460 max_index_mask = q_skba->len - 1;
461 for (i = 0; i < fill_wqes; i++) {
462 u64 tmp_addr;
463 struct sk_buff *skb;
464
465 skb = netdev_alloc_skb_ip_align(dev, packet_size);
466 if (!skb) {
467 q_skba->os_skbs = fill_wqes - i;
468 if (q_skba->os_skbs == q_skba->len - 2) {
469 netdev_info(pr->port->netdev,
470 "rq%i ran dry - no mem for skb\n",
471 rq_nr);
472 ret = -ENOMEM;
473 }
474 break;
475 }
476
477 skb_arr[index] = skb;
478 tmp_addr = ehea_map_vaddr(skb->data);
479 if (tmp_addr == -1) {
480 dev_consume_skb_any(skb);
481 q_skba->os_skbs = fill_wqes - i;
482 ret = 0;
483 break;
484 }
485
486 rwqe = ehea_get_next_rwqe(qp, rq_nr);
487 rwqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, wqe_type)
488 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, index);
489 rwqe->sg_list[0].l_key = pr->recv_mr.lkey;
490 rwqe->sg_list[0].vaddr = tmp_addr;
491 rwqe->sg_list[0].len = packet_size;
492 rwqe->data_segments = 1;
493
494 index++;
495 index &= max_index_mask;
496 adder++;
497 }
498
499 q_skba->index = index;
500 if (adder == 0)
501 goto out;
502
503 /* Ring doorbell */
504 iosync();
505 if (rq_nr == 2)
506 ehea_update_rq2a(pr->qp, adder);
507 else
508 ehea_update_rq3a(pr->qp, adder);
509 out:
510 return ret;
511 }
512
513
ehea_refill_rq2(struct ehea_port_res * pr,int nr_of_wqes)514 static int ehea_refill_rq2(struct ehea_port_res *pr, int nr_of_wqes)
515 {
516 return ehea_refill_rq_def(pr, &pr->rq2_skba, 2,
517 nr_of_wqes, EHEA_RWQE2_TYPE,
518 EHEA_RQ2_PKT_SIZE);
519 }
520
521
ehea_refill_rq3(struct ehea_port_res * pr,int nr_of_wqes)522 static int ehea_refill_rq3(struct ehea_port_res *pr, int nr_of_wqes)
523 {
524 return ehea_refill_rq_def(pr, &pr->rq3_skba, 3,
525 nr_of_wqes, EHEA_RWQE3_TYPE,
526 EHEA_MAX_PACKET_SIZE);
527 }
528
ehea_check_cqe(struct ehea_cqe * cqe,int * rq_num)529 static inline int ehea_check_cqe(struct ehea_cqe *cqe, int *rq_num)
530 {
531 *rq_num = (cqe->type & EHEA_CQE_TYPE_RQ) >> 5;
532 if ((cqe->status & EHEA_CQE_STAT_ERR_MASK) == 0)
533 return 0;
534 if (((cqe->status & EHEA_CQE_STAT_ERR_TCP) != 0) &&
535 (cqe->header_length == 0))
536 return 0;
537 return -EINVAL;
538 }
539
ehea_fill_skb(struct net_device * dev,struct sk_buff * skb,struct ehea_cqe * cqe,struct ehea_port_res * pr)540 static inline void ehea_fill_skb(struct net_device *dev,
541 struct sk_buff *skb, struct ehea_cqe *cqe,
542 struct ehea_port_res *pr)
543 {
544 int length = cqe->num_bytes_transfered - 4; /*remove CRC */
545
546 skb_put(skb, length);
547 skb->protocol = eth_type_trans(skb, dev);
548
549 /* The packet was not an IPV4 packet so a complemented checksum was
550 calculated. The value is found in the Internet Checksum field. */
551 if (cqe->status & EHEA_CQE_BLIND_CKSUM) {
552 skb->ip_summed = CHECKSUM_COMPLETE;
553 skb->csum = csum_unfold(~cqe->inet_checksum_value);
554 } else
555 skb->ip_summed = CHECKSUM_UNNECESSARY;
556
557 skb_record_rx_queue(skb, pr - &pr->port->port_res[0]);
558 }
559
get_skb_by_index(struct sk_buff ** skb_array,int arr_len,struct ehea_cqe * cqe)560 static inline struct sk_buff *get_skb_by_index(struct sk_buff **skb_array,
561 int arr_len,
562 struct ehea_cqe *cqe)
563 {
564 int skb_index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
565 struct sk_buff *skb;
566 void *pref;
567 int x;
568
569 x = skb_index + 1;
570 x &= (arr_len - 1);
571
572 pref = skb_array[x];
573 if (pref) {
574 prefetchw(pref);
575 prefetchw(pref + EHEA_CACHE_LINE);
576
577 pref = (skb_array[x]->data);
578 prefetch(pref);
579 prefetch(pref + EHEA_CACHE_LINE);
580 prefetch(pref + EHEA_CACHE_LINE * 2);
581 prefetch(pref + EHEA_CACHE_LINE * 3);
582 }
583
584 skb = skb_array[skb_index];
585 skb_array[skb_index] = NULL;
586 return skb;
587 }
588
get_skb_by_index_ll(struct sk_buff ** skb_array,int arr_len,int wqe_index)589 static inline struct sk_buff *get_skb_by_index_ll(struct sk_buff **skb_array,
590 int arr_len, int wqe_index)
591 {
592 struct sk_buff *skb;
593 void *pref;
594 int x;
595
596 x = wqe_index + 1;
597 x &= (arr_len - 1);
598
599 pref = skb_array[x];
600 if (pref) {
601 prefetchw(pref);
602 prefetchw(pref + EHEA_CACHE_LINE);
603
604 pref = (skb_array[x]->data);
605 prefetchw(pref);
606 prefetchw(pref + EHEA_CACHE_LINE);
607 }
608
609 skb = skb_array[wqe_index];
610 skb_array[wqe_index] = NULL;
611 return skb;
612 }
613
ehea_treat_poll_error(struct ehea_port_res * pr,int rq,struct ehea_cqe * cqe,int * processed_rq2,int * processed_rq3)614 static int ehea_treat_poll_error(struct ehea_port_res *pr, int rq,
615 struct ehea_cqe *cqe, int *processed_rq2,
616 int *processed_rq3)
617 {
618 struct sk_buff *skb;
619
620 if (cqe->status & EHEA_CQE_STAT_ERR_TCP)
621 pr->p_stats.err_tcp_cksum++;
622 if (cqe->status & EHEA_CQE_STAT_ERR_IP)
623 pr->p_stats.err_ip_cksum++;
624 if (cqe->status & EHEA_CQE_STAT_ERR_CRC)
625 pr->p_stats.err_frame_crc++;
626
627 if (rq == 2) {
628 *processed_rq2 += 1;
629 skb = get_skb_by_index(pr->rq2_skba.arr, pr->rq2_skba.len, cqe);
630 dev_kfree_skb(skb);
631 } else if (rq == 3) {
632 *processed_rq3 += 1;
633 skb = get_skb_by_index(pr->rq3_skba.arr, pr->rq3_skba.len, cqe);
634 dev_kfree_skb(skb);
635 }
636
637 if (cqe->status & EHEA_CQE_STAT_FAT_ERR_MASK) {
638 if (netif_msg_rx_err(pr->port)) {
639 pr_err("Critical receive error for QP %d. Resetting port.\n",
640 pr->qp->init_attr.qp_nr);
641 ehea_dump(cqe, sizeof(*cqe), "CQE");
642 }
643 ehea_schedule_port_reset(pr->port);
644 return 1;
645 }
646
647 return 0;
648 }
649
ehea_proc_rwqes(struct net_device * dev,struct ehea_port_res * pr,int budget)650 static int ehea_proc_rwqes(struct net_device *dev,
651 struct ehea_port_res *pr,
652 int budget)
653 {
654 struct ehea_port *port = pr->port;
655 struct ehea_qp *qp = pr->qp;
656 struct ehea_cqe *cqe;
657 struct sk_buff *skb;
658 struct sk_buff **skb_arr_rq1 = pr->rq1_skba.arr;
659 struct sk_buff **skb_arr_rq2 = pr->rq2_skba.arr;
660 struct sk_buff **skb_arr_rq3 = pr->rq3_skba.arr;
661 int skb_arr_rq1_len = pr->rq1_skba.len;
662 int skb_arr_rq2_len = pr->rq2_skba.len;
663 int skb_arr_rq3_len = pr->rq3_skba.len;
664 int processed, processed_rq1, processed_rq2, processed_rq3;
665 u64 processed_bytes = 0;
666 int wqe_index, last_wqe_index, rq, port_reset;
667
668 processed = processed_rq1 = processed_rq2 = processed_rq3 = 0;
669 last_wqe_index = 0;
670
671 cqe = ehea_poll_rq1(qp, &wqe_index);
672 while ((processed < budget) && cqe) {
673 ehea_inc_rq1(qp);
674 processed_rq1++;
675 processed++;
676 if (netif_msg_rx_status(port))
677 ehea_dump(cqe, sizeof(*cqe), "CQE");
678
679 last_wqe_index = wqe_index;
680 rmb();
681 if (!ehea_check_cqe(cqe, &rq)) {
682 if (rq == 1) {
683 /* LL RQ1 */
684 skb = get_skb_by_index_ll(skb_arr_rq1,
685 skb_arr_rq1_len,
686 wqe_index);
687 if (unlikely(!skb)) {
688 netif_info(port, rx_err, dev,
689 "LL rq1: skb=NULL\n");
690
691 skb = netdev_alloc_skb(dev,
692 EHEA_L_PKT_SIZE);
693 if (!skb)
694 break;
695 }
696 skb_copy_to_linear_data(skb, ((char *)cqe) + 64,
697 cqe->num_bytes_transfered - 4);
698 ehea_fill_skb(dev, skb, cqe, pr);
699 } else if (rq == 2) {
700 /* RQ2 */
701 skb = get_skb_by_index(skb_arr_rq2,
702 skb_arr_rq2_len, cqe);
703 if (unlikely(!skb)) {
704 netif_err(port, rx_err, dev,
705 "rq2: skb=NULL\n");
706 break;
707 }
708 ehea_fill_skb(dev, skb, cqe, pr);
709 processed_rq2++;
710 } else {
711 /* RQ3 */
712 skb = get_skb_by_index(skb_arr_rq3,
713 skb_arr_rq3_len, cqe);
714 if (unlikely(!skb)) {
715 netif_err(port, rx_err, dev,
716 "rq3: skb=NULL\n");
717 break;
718 }
719 ehea_fill_skb(dev, skb, cqe, pr);
720 processed_rq3++;
721 }
722
723 processed_bytes += skb->len;
724
725 if (cqe->status & EHEA_CQE_VLAN_TAG_XTRACT)
726 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
727 cqe->vlan_tag);
728
729 napi_gro_receive(&pr->napi, skb);
730 } else {
731 pr->p_stats.poll_receive_errors++;
732 port_reset = ehea_treat_poll_error(pr, rq, cqe,
733 &processed_rq2,
734 &processed_rq3);
735 if (port_reset)
736 break;
737 }
738 cqe = ehea_poll_rq1(qp, &wqe_index);
739 }
740
741 pr->rx_packets += processed;
742 pr->rx_bytes += processed_bytes;
743
744 ehea_refill_rq1(pr, last_wqe_index, processed_rq1);
745 ehea_refill_rq2(pr, processed_rq2);
746 ehea_refill_rq3(pr, processed_rq3);
747
748 return processed;
749 }
750
751 #define SWQE_RESTART_CHECK 0xdeadbeaff00d0000ull
752
reset_sq_restart_flag(struct ehea_port * port)753 static void reset_sq_restart_flag(struct ehea_port *port)
754 {
755 int i;
756
757 for (i = 0; i < port->num_def_qps; i++) {
758 struct ehea_port_res *pr = &port->port_res[i];
759 pr->sq_restart_flag = 0;
760 }
761 wake_up(&port->restart_wq);
762 }
763
check_sqs(struct ehea_port * port)764 static void check_sqs(struct ehea_port *port)
765 {
766 struct ehea_swqe *swqe;
767 int swqe_index;
768 int i;
769
770 for (i = 0; i < port->num_def_qps; i++) {
771 struct ehea_port_res *pr = &port->port_res[i];
772 int ret;
773 swqe = ehea_get_swqe(pr->qp, &swqe_index);
774 memset(swqe, 0, SWQE_HEADER_SIZE);
775 atomic_dec(&pr->swqe_avail);
776
777 swqe->tx_control |= EHEA_SWQE_PURGE;
778 swqe->wr_id = SWQE_RESTART_CHECK;
779 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
780 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
781 swqe->immediate_data_length = 80;
782
783 ehea_post_swqe(pr->qp, swqe);
784
785 ret = wait_event_timeout(port->restart_wq,
786 pr->sq_restart_flag == 0,
787 msecs_to_jiffies(100));
788
789 if (!ret) {
790 pr_err("HW/SW queues out of sync\n");
791 ehea_schedule_port_reset(pr->port);
792 return;
793 }
794 }
795 }
796
797
ehea_proc_cqes(struct ehea_port_res * pr,int my_quota)798 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
799 {
800 struct sk_buff *skb;
801 struct ehea_cq *send_cq = pr->send_cq;
802 struct ehea_cqe *cqe;
803 int quota = my_quota;
804 int cqe_counter = 0;
805 int swqe_av = 0;
806 int index;
807 struct netdev_queue *txq = netdev_get_tx_queue(pr->port->netdev,
808 pr - &pr->port->port_res[0]);
809
810 cqe = ehea_poll_cq(send_cq);
811 while (cqe && (quota > 0)) {
812 ehea_inc_cq(send_cq);
813
814 cqe_counter++;
815 rmb();
816
817 if (cqe->wr_id == SWQE_RESTART_CHECK) {
818 pr->sq_restart_flag = 1;
819 swqe_av++;
820 break;
821 }
822
823 if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
824 pr_err("Bad send completion status=0x%04X\n",
825 cqe->status);
826
827 if (netif_msg_tx_err(pr->port))
828 ehea_dump(cqe, sizeof(*cqe), "Send CQE");
829
830 if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
831 pr_err("Resetting port\n");
832 ehea_schedule_port_reset(pr->port);
833 break;
834 }
835 }
836
837 if (netif_msg_tx_done(pr->port))
838 ehea_dump(cqe, sizeof(*cqe), "CQE");
839
840 if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
841 == EHEA_SWQE2_TYPE)) {
842
843 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
844 skb = pr->sq_skba.arr[index];
845 dev_consume_skb_any(skb);
846 pr->sq_skba.arr[index] = NULL;
847 }
848
849 swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
850 quota--;
851
852 cqe = ehea_poll_cq(send_cq);
853 }
854
855 ehea_update_feca(send_cq, cqe_counter);
856 atomic_add(swqe_av, &pr->swqe_avail);
857
858 if (unlikely(netif_tx_queue_stopped(txq) &&
859 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))) {
860 __netif_tx_lock(txq, smp_processor_id());
861 if (netif_tx_queue_stopped(txq) &&
862 (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))
863 netif_tx_wake_queue(txq);
864 __netif_tx_unlock(txq);
865 }
866
867 wake_up(&pr->port->swqe_avail_wq);
868
869 return cqe;
870 }
871
872 #define EHEA_POLL_MAX_CQES 65535
873
ehea_poll(struct napi_struct * napi,int budget)874 static int ehea_poll(struct napi_struct *napi, int budget)
875 {
876 struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
877 napi);
878 struct net_device *dev = pr->port->netdev;
879 struct ehea_cqe *cqe;
880 struct ehea_cqe *cqe_skb = NULL;
881 int wqe_index;
882 int rx = 0;
883
884 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
885 rx += ehea_proc_rwqes(dev, pr, budget - rx);
886
887 while (rx != budget) {
888 napi_complete(napi);
889 ehea_reset_cq_ep(pr->recv_cq);
890 ehea_reset_cq_ep(pr->send_cq);
891 ehea_reset_cq_n1(pr->recv_cq);
892 ehea_reset_cq_n1(pr->send_cq);
893 rmb();
894 cqe = ehea_poll_rq1(pr->qp, &wqe_index);
895 cqe_skb = ehea_poll_cq(pr->send_cq);
896
897 if (!cqe && !cqe_skb)
898 return rx;
899
900 if (!napi_reschedule(napi))
901 return rx;
902
903 cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
904 rx += ehea_proc_rwqes(dev, pr, budget - rx);
905 }
906
907 return rx;
908 }
909
ehea_recv_irq_handler(int irq,void * param)910 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
911 {
912 struct ehea_port_res *pr = param;
913
914 napi_schedule(&pr->napi);
915
916 return IRQ_HANDLED;
917 }
918
ehea_qp_aff_irq_handler(int irq,void * param)919 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
920 {
921 struct ehea_port *port = param;
922 struct ehea_eqe *eqe;
923 struct ehea_qp *qp;
924 u32 qp_token;
925 u64 resource_type, aer, aerr;
926 int reset_port = 0;
927
928 eqe = ehea_poll_eq(port->qp_eq);
929
930 while (eqe) {
931 qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
932 pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
933 eqe->entry, qp_token);
934
935 qp = port->port_res[qp_token].qp;
936
937 resource_type = ehea_error_data(port->adapter, qp->fw_handle,
938 &aer, &aerr);
939
940 if (resource_type == EHEA_AER_RESTYPE_QP) {
941 if ((aer & EHEA_AER_RESET_MASK) ||
942 (aerr & EHEA_AERR_RESET_MASK))
943 reset_port = 1;
944 } else
945 reset_port = 1; /* Reset in case of CQ or EQ error */
946
947 eqe = ehea_poll_eq(port->qp_eq);
948 }
949
950 if (reset_port) {
951 pr_err("Resetting port\n");
952 ehea_schedule_port_reset(port);
953 }
954
955 return IRQ_HANDLED;
956 }
957
ehea_get_port(struct ehea_adapter * adapter,int logical_port)958 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
959 int logical_port)
960 {
961 int i;
962
963 for (i = 0; i < EHEA_MAX_PORTS; i++)
964 if (adapter->port[i])
965 if (adapter->port[i]->logical_port_id == logical_port)
966 return adapter->port[i];
967 return NULL;
968 }
969
ehea_sense_port_attr(struct ehea_port * port)970 int ehea_sense_port_attr(struct ehea_port *port)
971 {
972 int ret;
973 u64 hret;
974 struct hcp_ehea_port_cb0 *cb0;
975
976 /* may be called via ehea_neq_tasklet() */
977 cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
978 if (!cb0) {
979 pr_err("no mem for cb0\n");
980 ret = -ENOMEM;
981 goto out;
982 }
983
984 hret = ehea_h_query_ehea_port(port->adapter->handle,
985 port->logical_port_id, H_PORT_CB0,
986 EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
987 cb0);
988 if (hret != H_SUCCESS) {
989 ret = -EIO;
990 goto out_free;
991 }
992
993 /* MAC address */
994 port->mac_addr = cb0->port_mac_addr << 16;
995
996 if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
997 ret = -EADDRNOTAVAIL;
998 goto out_free;
999 }
1000
1001 /* Port speed */
1002 switch (cb0->port_speed) {
1003 case H_SPEED_10M_H:
1004 port->port_speed = EHEA_SPEED_10M;
1005 port->full_duplex = 0;
1006 break;
1007 case H_SPEED_10M_F:
1008 port->port_speed = EHEA_SPEED_10M;
1009 port->full_duplex = 1;
1010 break;
1011 case H_SPEED_100M_H:
1012 port->port_speed = EHEA_SPEED_100M;
1013 port->full_duplex = 0;
1014 break;
1015 case H_SPEED_100M_F:
1016 port->port_speed = EHEA_SPEED_100M;
1017 port->full_duplex = 1;
1018 break;
1019 case H_SPEED_1G_F:
1020 port->port_speed = EHEA_SPEED_1G;
1021 port->full_duplex = 1;
1022 break;
1023 case H_SPEED_10G_F:
1024 port->port_speed = EHEA_SPEED_10G;
1025 port->full_duplex = 1;
1026 break;
1027 default:
1028 port->port_speed = 0;
1029 port->full_duplex = 0;
1030 break;
1031 }
1032
1033 port->autoneg = 1;
1034 port->num_mcs = cb0->num_default_qps;
1035
1036 /* Number of default QPs */
1037 if (use_mcs)
1038 port->num_def_qps = cb0->num_default_qps;
1039 else
1040 port->num_def_qps = 1;
1041
1042 if (!port->num_def_qps) {
1043 ret = -EINVAL;
1044 goto out_free;
1045 }
1046
1047 ret = 0;
1048 out_free:
1049 if (ret || netif_msg_probe(port))
1050 ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1051 free_page((unsigned long)cb0);
1052 out:
1053 return ret;
1054 }
1055
ehea_set_portspeed(struct ehea_port * port,u32 port_speed)1056 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1057 {
1058 struct hcp_ehea_port_cb4 *cb4;
1059 u64 hret;
1060 int ret = 0;
1061
1062 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1063 if (!cb4) {
1064 pr_err("no mem for cb4\n");
1065 ret = -ENOMEM;
1066 goto out;
1067 }
1068
1069 cb4->port_speed = port_speed;
1070
1071 netif_carrier_off(port->netdev);
1072
1073 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1074 port->logical_port_id,
1075 H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1076 if (hret == H_SUCCESS) {
1077 port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1078
1079 hret = ehea_h_query_ehea_port(port->adapter->handle,
1080 port->logical_port_id,
1081 H_PORT_CB4, H_PORT_CB4_SPEED,
1082 cb4);
1083 if (hret == H_SUCCESS) {
1084 switch (cb4->port_speed) {
1085 case H_SPEED_10M_H:
1086 port->port_speed = EHEA_SPEED_10M;
1087 port->full_duplex = 0;
1088 break;
1089 case H_SPEED_10M_F:
1090 port->port_speed = EHEA_SPEED_10M;
1091 port->full_duplex = 1;
1092 break;
1093 case H_SPEED_100M_H:
1094 port->port_speed = EHEA_SPEED_100M;
1095 port->full_duplex = 0;
1096 break;
1097 case H_SPEED_100M_F:
1098 port->port_speed = EHEA_SPEED_100M;
1099 port->full_duplex = 1;
1100 break;
1101 case H_SPEED_1G_F:
1102 port->port_speed = EHEA_SPEED_1G;
1103 port->full_duplex = 1;
1104 break;
1105 case H_SPEED_10G_F:
1106 port->port_speed = EHEA_SPEED_10G;
1107 port->full_duplex = 1;
1108 break;
1109 default:
1110 port->port_speed = 0;
1111 port->full_duplex = 0;
1112 break;
1113 }
1114 } else {
1115 pr_err("Failed sensing port speed\n");
1116 ret = -EIO;
1117 }
1118 } else {
1119 if (hret == H_AUTHORITY) {
1120 pr_info("Hypervisor denied setting port speed\n");
1121 ret = -EPERM;
1122 } else {
1123 ret = -EIO;
1124 pr_err("Failed setting port speed\n");
1125 }
1126 }
1127 if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1128 netif_carrier_on(port->netdev);
1129
1130 free_page((unsigned long)cb4);
1131 out:
1132 return ret;
1133 }
1134
ehea_parse_eqe(struct ehea_adapter * adapter,u64 eqe)1135 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1136 {
1137 int ret;
1138 u8 ec;
1139 u8 portnum;
1140 struct ehea_port *port;
1141 struct net_device *dev;
1142
1143 ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1144 portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1145 port = ehea_get_port(adapter, portnum);
1146 if (!port) {
1147 netdev_err(NULL, "unknown portnum %x\n", portnum);
1148 return;
1149 }
1150 dev = port->netdev;
1151
1152 switch (ec) {
1153 case EHEA_EC_PORTSTATE_CHG: /* port state change */
1154
1155 if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1156 if (!netif_carrier_ok(dev)) {
1157 ret = ehea_sense_port_attr(port);
1158 if (ret) {
1159 netdev_err(dev, "failed resensing port attributes\n");
1160 break;
1161 }
1162
1163 netif_info(port, link, dev,
1164 "Logical port up: %dMbps %s Duplex\n",
1165 port->port_speed,
1166 port->full_duplex == 1 ?
1167 "Full" : "Half");
1168
1169 netif_carrier_on(dev);
1170 netif_wake_queue(dev);
1171 }
1172 } else
1173 if (netif_carrier_ok(dev)) {
1174 netif_info(port, link, dev,
1175 "Logical port down\n");
1176 netif_carrier_off(dev);
1177 netif_tx_disable(dev);
1178 }
1179
1180 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1181 port->phy_link = EHEA_PHY_LINK_UP;
1182 netif_info(port, link, dev,
1183 "Physical port up\n");
1184 if (prop_carrier_state)
1185 netif_carrier_on(dev);
1186 } else {
1187 port->phy_link = EHEA_PHY_LINK_DOWN;
1188 netif_info(port, link, dev,
1189 "Physical port down\n");
1190 if (prop_carrier_state)
1191 netif_carrier_off(dev);
1192 }
1193
1194 if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1195 netdev_info(dev,
1196 "External switch port is primary port\n");
1197 else
1198 netdev_info(dev,
1199 "External switch port is backup port\n");
1200
1201 break;
1202 case EHEA_EC_ADAPTER_MALFUNC:
1203 netdev_err(dev, "Adapter malfunction\n");
1204 break;
1205 case EHEA_EC_PORT_MALFUNC:
1206 netdev_info(dev, "Port malfunction\n");
1207 netif_carrier_off(dev);
1208 netif_tx_disable(dev);
1209 break;
1210 default:
1211 netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1212 break;
1213 }
1214 }
1215
ehea_neq_tasklet(struct tasklet_struct * t)1216 static void ehea_neq_tasklet(struct tasklet_struct *t)
1217 {
1218 struct ehea_adapter *adapter = from_tasklet(adapter, t, neq_tasklet);
1219 struct ehea_eqe *eqe;
1220 u64 event_mask;
1221
1222 eqe = ehea_poll_eq(adapter->neq);
1223 pr_debug("eqe=%p\n", eqe);
1224
1225 while (eqe) {
1226 pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1227 ehea_parse_eqe(adapter, eqe->entry);
1228 eqe = ehea_poll_eq(adapter->neq);
1229 pr_debug("next eqe=%p\n", eqe);
1230 }
1231
1232 event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1233 | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1234 | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1235
1236 ehea_h_reset_events(adapter->handle,
1237 adapter->neq->fw_handle, event_mask);
1238 }
1239
ehea_interrupt_neq(int irq,void * param)1240 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1241 {
1242 struct ehea_adapter *adapter = param;
1243 tasklet_hi_schedule(&adapter->neq_tasklet);
1244 return IRQ_HANDLED;
1245 }
1246
1247
ehea_fill_port_res(struct ehea_port_res * pr)1248 static int ehea_fill_port_res(struct ehea_port_res *pr)
1249 {
1250 int ret;
1251 struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1252
1253 ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1254
1255 ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1256
1257 ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1258
1259 return ret;
1260 }
1261
ehea_reg_interrupts(struct net_device * dev)1262 static int ehea_reg_interrupts(struct net_device *dev)
1263 {
1264 struct ehea_port *port = netdev_priv(dev);
1265 struct ehea_port_res *pr;
1266 int i, ret;
1267
1268
1269 snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1270 dev->name);
1271
1272 ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1273 ehea_qp_aff_irq_handler,
1274 0, port->int_aff_name, port);
1275 if (ret) {
1276 netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1277 port->qp_eq->attr.ist1);
1278 goto out_free_qpeq;
1279 }
1280
1281 netif_info(port, ifup, dev,
1282 "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1283 port->qp_eq->attr.ist1);
1284
1285
1286 for (i = 0; i < port->num_def_qps; i++) {
1287 pr = &port->port_res[i];
1288 snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1289 "%s-queue%d", dev->name, i);
1290 ret = ibmebus_request_irq(pr->eq->attr.ist1,
1291 ehea_recv_irq_handler,
1292 0, pr->int_send_name, pr);
1293 if (ret) {
1294 netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1295 i, pr->eq->attr.ist1);
1296 goto out_free_req;
1297 }
1298 netif_info(port, ifup, dev,
1299 "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1300 pr->eq->attr.ist1, i);
1301 }
1302 out:
1303 return ret;
1304
1305
1306 out_free_req:
1307 while (--i >= 0) {
1308 u32 ist = port->port_res[i].eq->attr.ist1;
1309 ibmebus_free_irq(ist, &port->port_res[i]);
1310 }
1311
1312 out_free_qpeq:
1313 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1314 i = port->num_def_qps;
1315
1316 goto out;
1317
1318 }
1319
ehea_free_interrupts(struct net_device * dev)1320 static void ehea_free_interrupts(struct net_device *dev)
1321 {
1322 struct ehea_port *port = netdev_priv(dev);
1323 struct ehea_port_res *pr;
1324 int i;
1325
1326 /* send */
1327
1328 for (i = 0; i < port->num_def_qps; i++) {
1329 pr = &port->port_res[i];
1330 ibmebus_free_irq(pr->eq->attr.ist1, pr);
1331 netif_info(port, intr, dev,
1332 "free send irq for res %d with handle 0x%X\n",
1333 i, pr->eq->attr.ist1);
1334 }
1335
1336 /* associated events */
1337 ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1338 netif_info(port, intr, dev,
1339 "associated event interrupt for handle 0x%X freed\n",
1340 port->qp_eq->attr.ist1);
1341 }
1342
ehea_configure_port(struct ehea_port * port)1343 static int ehea_configure_port(struct ehea_port *port)
1344 {
1345 int ret, i;
1346 u64 hret, mask;
1347 struct hcp_ehea_port_cb0 *cb0;
1348
1349 ret = -ENOMEM;
1350 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1351 if (!cb0)
1352 goto out;
1353
1354 cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1355 | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1356 | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1357 | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1358 | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1359 PXLY_RC_VLAN_FILTER)
1360 | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1361
1362 for (i = 0; i < port->num_mcs; i++)
1363 if (use_mcs)
1364 cb0->default_qpn_arr[i] =
1365 port->port_res[i].qp->init_attr.qp_nr;
1366 else
1367 cb0->default_qpn_arr[i] =
1368 port->port_res[0].qp->init_attr.qp_nr;
1369
1370 if (netif_msg_ifup(port))
1371 ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1372
1373 mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1374 | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1375
1376 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1377 port->logical_port_id,
1378 H_PORT_CB0, mask, cb0);
1379 ret = -EIO;
1380 if (hret != H_SUCCESS)
1381 goto out_free;
1382
1383 ret = 0;
1384
1385 out_free:
1386 free_page((unsigned long)cb0);
1387 out:
1388 return ret;
1389 }
1390
ehea_gen_smrs(struct ehea_port_res * pr)1391 static int ehea_gen_smrs(struct ehea_port_res *pr)
1392 {
1393 int ret;
1394 struct ehea_adapter *adapter = pr->port->adapter;
1395
1396 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1397 if (ret)
1398 goto out;
1399
1400 ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1401 if (ret)
1402 goto out_free;
1403
1404 return 0;
1405
1406 out_free:
1407 ehea_rem_mr(&pr->send_mr);
1408 out:
1409 pr_err("Generating SMRS failed\n");
1410 return -EIO;
1411 }
1412
ehea_rem_smrs(struct ehea_port_res * pr)1413 static int ehea_rem_smrs(struct ehea_port_res *pr)
1414 {
1415 if ((ehea_rem_mr(&pr->send_mr)) ||
1416 (ehea_rem_mr(&pr->recv_mr)))
1417 return -EIO;
1418 else
1419 return 0;
1420 }
1421
ehea_init_q_skba(struct ehea_q_skb_arr * q_skba,int max_q_entries)1422 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1423 {
1424 int arr_size = sizeof(void *) * max_q_entries;
1425
1426 q_skba->arr = vzalloc(arr_size);
1427 if (!q_skba->arr)
1428 return -ENOMEM;
1429
1430 q_skba->len = max_q_entries;
1431 q_skba->index = 0;
1432 q_skba->os_skbs = 0;
1433
1434 return 0;
1435 }
1436
ehea_init_port_res(struct ehea_port * port,struct ehea_port_res * pr,struct port_res_cfg * pr_cfg,int queue_token)1437 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1438 struct port_res_cfg *pr_cfg, int queue_token)
1439 {
1440 struct ehea_adapter *adapter = port->adapter;
1441 enum ehea_eq_type eq_type = EHEA_EQ;
1442 struct ehea_qp_init_attr *init_attr = NULL;
1443 int ret = -EIO;
1444 u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1445
1446 tx_bytes = pr->tx_bytes;
1447 tx_packets = pr->tx_packets;
1448 rx_bytes = pr->rx_bytes;
1449 rx_packets = pr->rx_packets;
1450
1451 memset(pr, 0, sizeof(struct ehea_port_res));
1452
1453 pr->tx_bytes = tx_bytes;
1454 pr->tx_packets = tx_packets;
1455 pr->rx_bytes = rx_bytes;
1456 pr->rx_packets = rx_packets;
1457
1458 pr->port = port;
1459
1460 pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1461 if (!pr->eq) {
1462 pr_err("create_eq failed (eq)\n");
1463 goto out_free;
1464 }
1465
1466 pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1467 pr->eq->fw_handle,
1468 port->logical_port_id);
1469 if (!pr->recv_cq) {
1470 pr_err("create_cq failed (cq_recv)\n");
1471 goto out_free;
1472 }
1473
1474 pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1475 pr->eq->fw_handle,
1476 port->logical_port_id);
1477 if (!pr->send_cq) {
1478 pr_err("create_cq failed (cq_send)\n");
1479 goto out_free;
1480 }
1481
1482 if (netif_msg_ifup(port))
1483 pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1484 pr->send_cq->attr.act_nr_of_cqes,
1485 pr->recv_cq->attr.act_nr_of_cqes);
1486
1487 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1488 if (!init_attr) {
1489 ret = -ENOMEM;
1490 pr_err("no mem for ehea_qp_init_attr\n");
1491 goto out_free;
1492 }
1493
1494 init_attr->low_lat_rq1 = 1;
1495 init_attr->signalingtype = 1; /* generate CQE if specified in WQE */
1496 init_attr->rq_count = 3;
1497 init_attr->qp_token = queue_token;
1498 init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1499 init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1500 init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1501 init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1502 init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1503 init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1504 init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1505 init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1506 init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1507 init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1508 init_attr->port_nr = port->logical_port_id;
1509 init_attr->send_cq_handle = pr->send_cq->fw_handle;
1510 init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1511 init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1512
1513 pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1514 if (!pr->qp) {
1515 pr_err("create_qp failed\n");
1516 ret = -EIO;
1517 goto out_free;
1518 }
1519
1520 if (netif_msg_ifup(port))
1521 pr_info("QP: qp_nr=%d\n act_nr_snd_wqe=%d\n nr_rwqe_rq1=%d\n nr_rwqe_rq2=%d\n nr_rwqe_rq3=%d\n",
1522 init_attr->qp_nr,
1523 init_attr->act_nr_send_wqes,
1524 init_attr->act_nr_rwqes_rq1,
1525 init_attr->act_nr_rwqes_rq2,
1526 init_attr->act_nr_rwqes_rq3);
1527
1528 pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1529
1530 ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1531 ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1532 ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1533 ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1534 if (ret)
1535 goto out_free;
1536
1537 pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1538 if (ehea_gen_smrs(pr) != 0) {
1539 ret = -EIO;
1540 goto out_free;
1541 }
1542
1543 atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1544
1545 kfree(init_attr);
1546
1547 netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1548
1549 ret = 0;
1550 goto out;
1551
1552 out_free:
1553 kfree(init_attr);
1554 vfree(pr->sq_skba.arr);
1555 vfree(pr->rq1_skba.arr);
1556 vfree(pr->rq2_skba.arr);
1557 vfree(pr->rq3_skba.arr);
1558 ehea_destroy_qp(pr->qp);
1559 ehea_destroy_cq(pr->send_cq);
1560 ehea_destroy_cq(pr->recv_cq);
1561 ehea_destroy_eq(pr->eq);
1562 out:
1563 return ret;
1564 }
1565
ehea_clean_portres(struct ehea_port * port,struct ehea_port_res * pr)1566 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1567 {
1568 int ret, i;
1569
1570 if (pr->qp)
1571 netif_napi_del(&pr->napi);
1572
1573 ret = ehea_destroy_qp(pr->qp);
1574
1575 if (!ret) {
1576 ehea_destroy_cq(pr->send_cq);
1577 ehea_destroy_cq(pr->recv_cq);
1578 ehea_destroy_eq(pr->eq);
1579
1580 for (i = 0; i < pr->rq1_skba.len; i++)
1581 dev_kfree_skb(pr->rq1_skba.arr[i]);
1582
1583 for (i = 0; i < pr->rq2_skba.len; i++)
1584 dev_kfree_skb(pr->rq2_skba.arr[i]);
1585
1586 for (i = 0; i < pr->rq3_skba.len; i++)
1587 dev_kfree_skb(pr->rq3_skba.arr[i]);
1588
1589 for (i = 0; i < pr->sq_skba.len; i++)
1590 dev_kfree_skb(pr->sq_skba.arr[i]);
1591
1592 vfree(pr->rq1_skba.arr);
1593 vfree(pr->rq2_skba.arr);
1594 vfree(pr->rq3_skba.arr);
1595 vfree(pr->sq_skba.arr);
1596 ret = ehea_rem_smrs(pr);
1597 }
1598 return ret;
1599 }
1600
write_swqe2_immediate(struct sk_buff * skb,struct ehea_swqe * swqe,u32 lkey)1601 static void write_swqe2_immediate(struct sk_buff *skb, struct ehea_swqe *swqe,
1602 u32 lkey)
1603 {
1604 int skb_data_size = skb_headlen(skb);
1605 u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1606 struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1607 unsigned int immediate_len = SWQE2_MAX_IMM;
1608
1609 swqe->descriptors = 0;
1610
1611 if (skb_is_gso(skb)) {
1612 swqe->tx_control |= EHEA_SWQE_TSO;
1613 swqe->mss = skb_shinfo(skb)->gso_size;
1614 /*
1615 * For TSO packets we only copy the headers into the
1616 * immediate area.
1617 */
1618 immediate_len = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1619 }
1620
1621 if (skb_is_gso(skb) || skb_data_size >= SWQE2_MAX_IMM) {
1622 skb_copy_from_linear_data(skb, imm_data, immediate_len);
1623 swqe->immediate_data_length = immediate_len;
1624
1625 if (skb_data_size > immediate_len) {
1626 sg1entry->l_key = lkey;
1627 sg1entry->len = skb_data_size - immediate_len;
1628 sg1entry->vaddr =
1629 ehea_map_vaddr(skb->data + immediate_len);
1630 swqe->descriptors++;
1631 }
1632 } else {
1633 skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1634 swqe->immediate_data_length = skb_data_size;
1635 }
1636 }
1637
write_swqe2_data(struct sk_buff * skb,struct net_device * dev,struct ehea_swqe * swqe,u32 lkey)1638 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1639 struct ehea_swqe *swqe, u32 lkey)
1640 {
1641 struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1642 skb_frag_t *frag;
1643 int nfrags, sg1entry_contains_frag_data, i;
1644
1645 nfrags = skb_shinfo(skb)->nr_frags;
1646 sg1entry = &swqe->u.immdata_desc.sg_entry;
1647 sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1648 sg1entry_contains_frag_data = 0;
1649
1650 write_swqe2_immediate(skb, swqe, lkey);
1651
1652 /* write descriptors */
1653 if (nfrags > 0) {
1654 if (swqe->descriptors == 0) {
1655 /* sg1entry not yet used */
1656 frag = &skb_shinfo(skb)->frags[0];
1657
1658 /* copy sg1entry data */
1659 sg1entry->l_key = lkey;
1660 sg1entry->len = skb_frag_size(frag);
1661 sg1entry->vaddr =
1662 ehea_map_vaddr(skb_frag_address(frag));
1663 swqe->descriptors++;
1664 sg1entry_contains_frag_data = 1;
1665 }
1666
1667 for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1668
1669 frag = &skb_shinfo(skb)->frags[i];
1670 sgentry = &sg_list[i - sg1entry_contains_frag_data];
1671
1672 sgentry->l_key = lkey;
1673 sgentry->len = skb_frag_size(frag);
1674 sgentry->vaddr = ehea_map_vaddr(skb_frag_address(frag));
1675 swqe->descriptors++;
1676 }
1677 }
1678 }
1679
ehea_broadcast_reg_helper(struct ehea_port * port,u32 hcallid)1680 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1681 {
1682 int ret = 0;
1683 u64 hret;
1684 u8 reg_type;
1685
1686 /* De/Register untagged packets */
1687 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1688 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1689 port->logical_port_id,
1690 reg_type, port->mac_addr, 0, hcallid);
1691 if (hret != H_SUCCESS) {
1692 pr_err("%sregistering bc address failed (tagged)\n",
1693 hcallid == H_REG_BCMC ? "" : "de");
1694 ret = -EIO;
1695 goto out_herr;
1696 }
1697
1698 /* De/Register VLAN packets */
1699 reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
1700 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1701 port->logical_port_id,
1702 reg_type, port->mac_addr, 0, hcallid);
1703 if (hret != H_SUCCESS) {
1704 pr_err("%sregistering bc address failed (vlan)\n",
1705 hcallid == H_REG_BCMC ? "" : "de");
1706 ret = -EIO;
1707 }
1708 out_herr:
1709 return ret;
1710 }
1711
ehea_set_mac_addr(struct net_device * dev,void * sa)1712 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1713 {
1714 struct ehea_port *port = netdev_priv(dev);
1715 struct sockaddr *mac_addr = sa;
1716 struct hcp_ehea_port_cb0 *cb0;
1717 int ret;
1718 u64 hret;
1719
1720 if (!is_valid_ether_addr(mac_addr->sa_data)) {
1721 ret = -EADDRNOTAVAIL;
1722 goto out;
1723 }
1724
1725 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1726 if (!cb0) {
1727 pr_err("no mem for cb0\n");
1728 ret = -ENOMEM;
1729 goto out;
1730 }
1731
1732 memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1733
1734 cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1735
1736 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1737 port->logical_port_id, H_PORT_CB0,
1738 EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1739 if (hret != H_SUCCESS) {
1740 ret = -EIO;
1741 goto out_free;
1742 }
1743
1744 memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1745
1746 /* Deregister old MAC in pHYP */
1747 if (port->state == EHEA_PORT_UP) {
1748 ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1749 if (ret)
1750 goto out_upregs;
1751 }
1752
1753 port->mac_addr = cb0->port_mac_addr << 16;
1754
1755 /* Register new MAC in pHYP */
1756 if (port->state == EHEA_PORT_UP) {
1757 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1758 if (ret)
1759 goto out_upregs;
1760 }
1761
1762 ret = 0;
1763
1764 out_upregs:
1765 ehea_update_bcmc_registrations();
1766 out_free:
1767 free_page((unsigned long)cb0);
1768 out:
1769 return ret;
1770 }
1771
ehea_promiscuous_error(u64 hret,int enable)1772 static void ehea_promiscuous_error(u64 hret, int enable)
1773 {
1774 if (hret == H_AUTHORITY)
1775 pr_info("Hypervisor denied %sabling promiscuous mode\n",
1776 enable == 1 ? "en" : "dis");
1777 else
1778 pr_err("failed %sabling promiscuous mode\n",
1779 enable == 1 ? "en" : "dis");
1780 }
1781
ehea_promiscuous(struct net_device * dev,int enable)1782 static void ehea_promiscuous(struct net_device *dev, int enable)
1783 {
1784 struct ehea_port *port = netdev_priv(dev);
1785 struct hcp_ehea_port_cb7 *cb7;
1786 u64 hret;
1787
1788 if (enable == port->promisc)
1789 return;
1790
1791 cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1792 if (!cb7) {
1793 pr_err("no mem for cb7\n");
1794 goto out;
1795 }
1796
1797 /* Modify Pxs_DUCQPN in CB7 */
1798 cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1799
1800 hret = ehea_h_modify_ehea_port(port->adapter->handle,
1801 port->logical_port_id,
1802 H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1803 if (hret) {
1804 ehea_promiscuous_error(hret, enable);
1805 goto out;
1806 }
1807
1808 port->promisc = enable;
1809 out:
1810 free_page((unsigned long)cb7);
1811 }
1812
ehea_multicast_reg_helper(struct ehea_port * port,u64 mc_mac_addr,u32 hcallid)1813 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1814 u32 hcallid)
1815 {
1816 u64 hret;
1817 u8 reg_type;
1818
1819 reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_UNTAGGED;
1820 if (mc_mac_addr == 0)
1821 reg_type |= EHEA_BCMC_SCOPE_ALL;
1822
1823 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1824 port->logical_port_id,
1825 reg_type, mc_mac_addr, 0, hcallid);
1826 if (hret)
1827 goto out;
1828
1829 reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_VLANID_ALL;
1830 if (mc_mac_addr == 0)
1831 reg_type |= EHEA_BCMC_SCOPE_ALL;
1832
1833 hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1834 port->logical_port_id,
1835 reg_type, mc_mac_addr, 0, hcallid);
1836 out:
1837 return hret;
1838 }
1839
ehea_drop_multicast_list(struct net_device * dev)1840 static int ehea_drop_multicast_list(struct net_device *dev)
1841 {
1842 struct ehea_port *port = netdev_priv(dev);
1843 struct ehea_mc_list *mc_entry = port->mc_list;
1844 struct list_head *pos;
1845 struct list_head *temp;
1846 int ret = 0;
1847 u64 hret;
1848
1849 list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1850 mc_entry = list_entry(pos, struct ehea_mc_list, list);
1851
1852 hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1853 H_DEREG_BCMC);
1854 if (hret) {
1855 pr_err("failed deregistering mcast MAC\n");
1856 ret = -EIO;
1857 }
1858
1859 list_del(pos);
1860 kfree(mc_entry);
1861 }
1862 return ret;
1863 }
1864
ehea_allmulti(struct net_device * dev,int enable)1865 static void ehea_allmulti(struct net_device *dev, int enable)
1866 {
1867 struct ehea_port *port = netdev_priv(dev);
1868 u64 hret;
1869
1870 if (!port->allmulti) {
1871 if (enable) {
1872 /* Enable ALLMULTI */
1873 ehea_drop_multicast_list(dev);
1874 hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1875 if (!hret)
1876 port->allmulti = 1;
1877 else
1878 netdev_err(dev,
1879 "failed enabling IFF_ALLMULTI\n");
1880 }
1881 } else {
1882 if (!enable) {
1883 /* Disable ALLMULTI */
1884 hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1885 if (!hret)
1886 port->allmulti = 0;
1887 else
1888 netdev_err(dev,
1889 "failed disabling IFF_ALLMULTI\n");
1890 }
1891 }
1892 }
1893
ehea_add_multicast_entry(struct ehea_port * port,u8 * mc_mac_addr)1894 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
1895 {
1896 struct ehea_mc_list *ehea_mcl_entry;
1897 u64 hret;
1898
1899 ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1900 if (!ehea_mcl_entry)
1901 return;
1902
1903 INIT_LIST_HEAD(&ehea_mcl_entry->list);
1904
1905 memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1906
1907 hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1908 H_REG_BCMC);
1909 if (!hret)
1910 list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1911 else {
1912 pr_err("failed registering mcast MAC\n");
1913 kfree(ehea_mcl_entry);
1914 }
1915 }
1916
ehea_set_multicast_list(struct net_device * dev)1917 static void ehea_set_multicast_list(struct net_device *dev)
1918 {
1919 struct ehea_port *port = netdev_priv(dev);
1920 struct netdev_hw_addr *ha;
1921 int ret;
1922
1923 ehea_promiscuous(dev, !!(dev->flags & IFF_PROMISC));
1924
1925 if (dev->flags & IFF_ALLMULTI) {
1926 ehea_allmulti(dev, 1);
1927 goto out;
1928 }
1929 ehea_allmulti(dev, 0);
1930
1931 if (!netdev_mc_empty(dev)) {
1932 ret = ehea_drop_multicast_list(dev);
1933 if (ret) {
1934 /* Dropping the current multicast list failed.
1935 * Enabling ALL_MULTI is the best we can do.
1936 */
1937 ehea_allmulti(dev, 1);
1938 }
1939
1940 if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
1941 pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
1942 port->adapter->max_mc_mac);
1943 goto out;
1944 }
1945
1946 netdev_for_each_mc_addr(ha, dev)
1947 ehea_add_multicast_entry(port, ha->addr);
1948
1949 }
1950 out:
1951 ehea_update_bcmc_registrations();
1952 }
1953
xmit_common(struct sk_buff * skb,struct ehea_swqe * swqe)1954 static void xmit_common(struct sk_buff *skb, struct ehea_swqe *swqe)
1955 {
1956 swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT | EHEA_SWQE_CRC;
1957
1958 if (vlan_get_protocol(skb) != htons(ETH_P_IP))
1959 return;
1960
1961 if (skb->ip_summed == CHECKSUM_PARTIAL)
1962 swqe->tx_control |= EHEA_SWQE_IP_CHECKSUM;
1963
1964 swqe->ip_start = skb_network_offset(skb);
1965 swqe->ip_end = swqe->ip_start + ip_hdrlen(skb) - 1;
1966
1967 switch (ip_hdr(skb)->protocol) {
1968 case IPPROTO_UDP:
1969 if (skb->ip_summed == CHECKSUM_PARTIAL)
1970 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
1971
1972 swqe->tcp_offset = swqe->ip_end + 1 +
1973 offsetof(struct udphdr, check);
1974 break;
1975
1976 case IPPROTO_TCP:
1977 if (skb->ip_summed == CHECKSUM_PARTIAL)
1978 swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
1979
1980 swqe->tcp_offset = swqe->ip_end + 1 +
1981 offsetof(struct tcphdr, check);
1982 break;
1983 }
1984 }
1985
ehea_xmit2(struct sk_buff * skb,struct net_device * dev,struct ehea_swqe * swqe,u32 lkey)1986 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
1987 struct ehea_swqe *swqe, u32 lkey)
1988 {
1989 swqe->tx_control |= EHEA_SWQE_DESCRIPTORS_PRESENT;
1990
1991 xmit_common(skb, swqe);
1992
1993 write_swqe2_data(skb, dev, swqe, lkey);
1994 }
1995
ehea_xmit3(struct sk_buff * skb,struct net_device * dev,struct ehea_swqe * swqe)1996 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
1997 struct ehea_swqe *swqe)
1998 {
1999 u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2000
2001 xmit_common(skb, swqe);
2002
2003 if (!skb->data_len)
2004 skb_copy_from_linear_data(skb, imm_data, skb->len);
2005 else
2006 skb_copy_bits(skb, 0, imm_data, skb->len);
2007
2008 swqe->immediate_data_length = skb->len;
2009 dev_consume_skb_any(skb);
2010 }
2011
ehea_start_xmit(struct sk_buff * skb,struct net_device * dev)2012 static netdev_tx_t ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2013 {
2014 struct ehea_port *port = netdev_priv(dev);
2015 struct ehea_swqe *swqe;
2016 u32 lkey;
2017 int swqe_index;
2018 struct ehea_port_res *pr;
2019 struct netdev_queue *txq;
2020
2021 pr = &port->port_res[skb_get_queue_mapping(skb)];
2022 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2023
2024 swqe = ehea_get_swqe(pr->qp, &swqe_index);
2025 memset(swqe, 0, SWQE_HEADER_SIZE);
2026 atomic_dec(&pr->swqe_avail);
2027
2028 if (skb_vlan_tag_present(skb)) {
2029 swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2030 swqe->vlan_tag = skb_vlan_tag_get(skb);
2031 }
2032
2033 pr->tx_packets++;
2034 pr->tx_bytes += skb->len;
2035
2036 if (skb->len <= SWQE3_MAX_IMM) {
2037 u32 sig_iv = port->sig_comp_iv;
2038 u32 swqe_num = pr->swqe_id_counter;
2039 ehea_xmit3(skb, dev, swqe);
2040 swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2041 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2042 if (pr->swqe_ll_count >= (sig_iv - 1)) {
2043 swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2044 sig_iv);
2045 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2046 pr->swqe_ll_count = 0;
2047 } else
2048 pr->swqe_ll_count += 1;
2049 } else {
2050 swqe->wr_id =
2051 EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2052 | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2053 | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2054 | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2055 pr->sq_skba.arr[pr->sq_skba.index] = skb;
2056
2057 pr->sq_skba.index++;
2058 pr->sq_skba.index &= (pr->sq_skba.len - 1);
2059
2060 lkey = pr->send_mr.lkey;
2061 ehea_xmit2(skb, dev, swqe, lkey);
2062 swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2063 }
2064 pr->swqe_id_counter += 1;
2065
2066 netif_info(port, tx_queued, dev,
2067 "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2068 if (netif_msg_tx_queued(port))
2069 ehea_dump(swqe, 512, "swqe");
2070
2071 if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2072 netif_tx_stop_queue(txq);
2073 swqe->tx_control |= EHEA_SWQE_PURGE;
2074 }
2075
2076 ehea_post_swqe(pr->qp, swqe);
2077
2078 if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2079 pr->p_stats.queue_stopped++;
2080 netif_tx_stop_queue(txq);
2081 }
2082
2083 return NETDEV_TX_OK;
2084 }
2085
ehea_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)2086 static int ehea_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
2087 {
2088 struct ehea_port *port = netdev_priv(dev);
2089 struct ehea_adapter *adapter = port->adapter;
2090 struct hcp_ehea_port_cb1 *cb1;
2091 int index;
2092 u64 hret;
2093 int err = 0;
2094
2095 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2096 if (!cb1) {
2097 pr_err("no mem for cb1\n");
2098 err = -ENOMEM;
2099 goto out;
2100 }
2101
2102 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2103 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2104 if (hret != H_SUCCESS) {
2105 pr_err("query_ehea_port failed\n");
2106 err = -EINVAL;
2107 goto out;
2108 }
2109
2110 index = (vid / 64);
2111 cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2112
2113 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2114 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2115 if (hret != H_SUCCESS) {
2116 pr_err("modify_ehea_port failed\n");
2117 err = -EINVAL;
2118 }
2119 out:
2120 free_page((unsigned long)cb1);
2121 return err;
2122 }
2123
ehea_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)2124 static int ehea_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
2125 {
2126 struct ehea_port *port = netdev_priv(dev);
2127 struct ehea_adapter *adapter = port->adapter;
2128 struct hcp_ehea_port_cb1 *cb1;
2129 int index;
2130 u64 hret;
2131 int err = 0;
2132
2133 cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2134 if (!cb1) {
2135 pr_err("no mem for cb1\n");
2136 err = -ENOMEM;
2137 goto out;
2138 }
2139
2140 hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2141 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2142 if (hret != H_SUCCESS) {
2143 pr_err("query_ehea_port failed\n");
2144 err = -EINVAL;
2145 goto out;
2146 }
2147
2148 index = (vid / 64);
2149 cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2150
2151 hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2152 H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2153 if (hret != H_SUCCESS) {
2154 pr_err("modify_ehea_port failed\n");
2155 err = -EINVAL;
2156 }
2157 out:
2158 free_page((unsigned long)cb1);
2159 return err;
2160 }
2161
ehea_activate_qp(struct ehea_adapter * adapter,struct ehea_qp * qp)2162 static int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2163 {
2164 int ret = -EIO;
2165 u64 hret;
2166 u16 dummy16 = 0;
2167 u64 dummy64 = 0;
2168 struct hcp_modify_qp_cb0 *cb0;
2169
2170 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2171 if (!cb0) {
2172 ret = -ENOMEM;
2173 goto out;
2174 }
2175
2176 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2177 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2178 if (hret != H_SUCCESS) {
2179 pr_err("query_ehea_qp failed (1)\n");
2180 goto out;
2181 }
2182
2183 cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2184 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2185 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2186 &dummy64, &dummy64, &dummy16, &dummy16);
2187 if (hret != H_SUCCESS) {
2188 pr_err("modify_ehea_qp failed (1)\n");
2189 goto out;
2190 }
2191
2192 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2193 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2194 if (hret != H_SUCCESS) {
2195 pr_err("query_ehea_qp failed (2)\n");
2196 goto out;
2197 }
2198
2199 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2200 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2201 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2202 &dummy64, &dummy64, &dummy16, &dummy16);
2203 if (hret != H_SUCCESS) {
2204 pr_err("modify_ehea_qp failed (2)\n");
2205 goto out;
2206 }
2207
2208 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2209 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2210 if (hret != H_SUCCESS) {
2211 pr_err("query_ehea_qp failed (3)\n");
2212 goto out;
2213 }
2214
2215 cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2216 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2217 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2218 &dummy64, &dummy64, &dummy16, &dummy16);
2219 if (hret != H_SUCCESS) {
2220 pr_err("modify_ehea_qp failed (3)\n");
2221 goto out;
2222 }
2223
2224 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2225 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2226 if (hret != H_SUCCESS) {
2227 pr_err("query_ehea_qp failed (4)\n");
2228 goto out;
2229 }
2230
2231 ret = 0;
2232 out:
2233 free_page((unsigned long)cb0);
2234 return ret;
2235 }
2236
ehea_port_res_setup(struct ehea_port * port,int def_qps)2237 static int ehea_port_res_setup(struct ehea_port *port, int def_qps)
2238 {
2239 int ret, i;
2240 struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2241 enum ehea_eq_type eq_type = EHEA_EQ;
2242
2243 port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2244 EHEA_MAX_ENTRIES_EQ, 1);
2245 if (!port->qp_eq) {
2246 ret = -EINVAL;
2247 pr_err("ehea_create_eq failed (qp_eq)\n");
2248 goto out_kill_eq;
2249 }
2250
2251 pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2252 pr_cfg.max_entries_scq = sq_entries * 2;
2253 pr_cfg.max_entries_sq = sq_entries;
2254 pr_cfg.max_entries_rq1 = rq1_entries;
2255 pr_cfg.max_entries_rq2 = rq2_entries;
2256 pr_cfg.max_entries_rq3 = rq3_entries;
2257
2258 pr_cfg_small_rx.max_entries_rcq = 1;
2259 pr_cfg_small_rx.max_entries_scq = sq_entries;
2260 pr_cfg_small_rx.max_entries_sq = sq_entries;
2261 pr_cfg_small_rx.max_entries_rq1 = 1;
2262 pr_cfg_small_rx.max_entries_rq2 = 1;
2263 pr_cfg_small_rx.max_entries_rq3 = 1;
2264
2265 for (i = 0; i < def_qps; i++) {
2266 ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2267 if (ret)
2268 goto out_clean_pr;
2269 }
2270 for (i = def_qps; i < def_qps; i++) {
2271 ret = ehea_init_port_res(port, &port->port_res[i],
2272 &pr_cfg_small_rx, i);
2273 if (ret)
2274 goto out_clean_pr;
2275 }
2276
2277 return 0;
2278
2279 out_clean_pr:
2280 while (--i >= 0)
2281 ehea_clean_portres(port, &port->port_res[i]);
2282
2283 out_kill_eq:
2284 ehea_destroy_eq(port->qp_eq);
2285 return ret;
2286 }
2287
ehea_clean_all_portres(struct ehea_port * port)2288 static int ehea_clean_all_portres(struct ehea_port *port)
2289 {
2290 int ret = 0;
2291 int i;
2292
2293 for (i = 0; i < port->num_def_qps; i++)
2294 ret |= ehea_clean_portres(port, &port->port_res[i]);
2295
2296 ret |= ehea_destroy_eq(port->qp_eq);
2297
2298 return ret;
2299 }
2300
ehea_remove_adapter_mr(struct ehea_adapter * adapter)2301 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2302 {
2303 if (adapter->active_ports)
2304 return;
2305
2306 ehea_rem_mr(&adapter->mr);
2307 }
2308
ehea_add_adapter_mr(struct ehea_adapter * adapter)2309 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2310 {
2311 if (adapter->active_ports)
2312 return 0;
2313
2314 return ehea_reg_kernel_mr(adapter, &adapter->mr);
2315 }
2316
ehea_up(struct net_device * dev)2317 static int ehea_up(struct net_device *dev)
2318 {
2319 int ret, i;
2320 struct ehea_port *port = netdev_priv(dev);
2321
2322 if (port->state == EHEA_PORT_UP)
2323 return 0;
2324
2325 ret = ehea_port_res_setup(port, port->num_def_qps);
2326 if (ret) {
2327 netdev_err(dev, "port_res_failed\n");
2328 goto out;
2329 }
2330
2331 /* Set default QP for this port */
2332 ret = ehea_configure_port(port);
2333 if (ret) {
2334 netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2335 goto out_clean_pr;
2336 }
2337
2338 ret = ehea_reg_interrupts(dev);
2339 if (ret) {
2340 netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2341 goto out_clean_pr;
2342 }
2343
2344 for (i = 0; i < port->num_def_qps; i++) {
2345 ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2346 if (ret) {
2347 netdev_err(dev, "activate_qp failed\n");
2348 goto out_free_irqs;
2349 }
2350 }
2351
2352 for (i = 0; i < port->num_def_qps; i++) {
2353 ret = ehea_fill_port_res(&port->port_res[i]);
2354 if (ret) {
2355 netdev_err(dev, "out_free_irqs\n");
2356 goto out_free_irqs;
2357 }
2358 }
2359
2360 ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2361 if (ret) {
2362 ret = -EIO;
2363 goto out_free_irqs;
2364 }
2365
2366 port->state = EHEA_PORT_UP;
2367
2368 ret = 0;
2369 goto out;
2370
2371 out_free_irqs:
2372 ehea_free_interrupts(dev);
2373
2374 out_clean_pr:
2375 ehea_clean_all_portres(port);
2376 out:
2377 if (ret)
2378 netdev_info(dev, "Failed starting. ret=%i\n", ret);
2379
2380 ehea_update_bcmc_registrations();
2381 ehea_update_firmware_handles();
2382
2383 return ret;
2384 }
2385
port_napi_disable(struct ehea_port * port)2386 static void port_napi_disable(struct ehea_port *port)
2387 {
2388 int i;
2389
2390 for (i = 0; i < port->num_def_qps; i++)
2391 napi_disable(&port->port_res[i].napi);
2392 }
2393
port_napi_enable(struct ehea_port * port)2394 static void port_napi_enable(struct ehea_port *port)
2395 {
2396 int i;
2397
2398 for (i = 0; i < port->num_def_qps; i++)
2399 napi_enable(&port->port_res[i].napi);
2400 }
2401
ehea_open(struct net_device * dev)2402 static int ehea_open(struct net_device *dev)
2403 {
2404 int ret;
2405 struct ehea_port *port = netdev_priv(dev);
2406
2407 mutex_lock(&port->port_lock);
2408
2409 netif_info(port, ifup, dev, "enabling port\n");
2410
2411 netif_carrier_off(dev);
2412
2413 ret = ehea_up(dev);
2414 if (!ret) {
2415 port_napi_enable(port);
2416 netif_tx_start_all_queues(dev);
2417 }
2418
2419 mutex_unlock(&port->port_lock);
2420 schedule_delayed_work(&port->stats_work,
2421 round_jiffies_relative(msecs_to_jiffies(1000)));
2422
2423 return ret;
2424 }
2425
ehea_down(struct net_device * dev)2426 static int ehea_down(struct net_device *dev)
2427 {
2428 int ret;
2429 struct ehea_port *port = netdev_priv(dev);
2430
2431 if (port->state == EHEA_PORT_DOWN)
2432 return 0;
2433
2434 ehea_drop_multicast_list(dev);
2435 ehea_allmulti(dev, 0);
2436 ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2437
2438 ehea_free_interrupts(dev);
2439
2440 port->state = EHEA_PORT_DOWN;
2441
2442 ehea_update_bcmc_registrations();
2443
2444 ret = ehea_clean_all_portres(port);
2445 if (ret)
2446 netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2447
2448 ehea_update_firmware_handles();
2449
2450 return ret;
2451 }
2452
ehea_stop(struct net_device * dev)2453 static int ehea_stop(struct net_device *dev)
2454 {
2455 int ret;
2456 struct ehea_port *port = netdev_priv(dev);
2457
2458 netif_info(port, ifdown, dev, "disabling port\n");
2459
2460 set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2461 cancel_work_sync(&port->reset_task);
2462 cancel_delayed_work_sync(&port->stats_work);
2463 mutex_lock(&port->port_lock);
2464 netif_tx_stop_all_queues(dev);
2465 port_napi_disable(port);
2466 ret = ehea_down(dev);
2467 mutex_unlock(&port->port_lock);
2468 clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2469 return ret;
2470 }
2471
ehea_purge_sq(struct ehea_qp * orig_qp)2472 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2473 {
2474 struct ehea_qp qp = *orig_qp;
2475 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2476 struct ehea_swqe *swqe;
2477 int wqe_index;
2478 int i;
2479
2480 for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2481 swqe = ehea_get_swqe(&qp, &wqe_index);
2482 swqe->tx_control |= EHEA_SWQE_PURGE;
2483 }
2484 }
2485
ehea_flush_sq(struct ehea_port * port)2486 static void ehea_flush_sq(struct ehea_port *port)
2487 {
2488 int i;
2489
2490 for (i = 0; i < port->num_def_qps; i++) {
2491 struct ehea_port_res *pr = &port->port_res[i];
2492 int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2493 int ret;
2494
2495 ret = wait_event_timeout(port->swqe_avail_wq,
2496 atomic_read(&pr->swqe_avail) >= swqe_max,
2497 msecs_to_jiffies(100));
2498
2499 if (!ret) {
2500 pr_err("WARNING: sq not flushed completely\n");
2501 break;
2502 }
2503 }
2504 }
2505
ehea_stop_qps(struct net_device * dev)2506 static int ehea_stop_qps(struct net_device *dev)
2507 {
2508 struct ehea_port *port = netdev_priv(dev);
2509 struct ehea_adapter *adapter = port->adapter;
2510 struct hcp_modify_qp_cb0 *cb0;
2511 int ret = -EIO;
2512 int dret;
2513 int i;
2514 u64 hret;
2515 u64 dummy64 = 0;
2516 u16 dummy16 = 0;
2517
2518 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2519 if (!cb0) {
2520 ret = -ENOMEM;
2521 goto out;
2522 }
2523
2524 for (i = 0; i < (port->num_def_qps); i++) {
2525 struct ehea_port_res *pr = &port->port_res[i];
2526 struct ehea_qp *qp = pr->qp;
2527
2528 /* Purge send queue */
2529 ehea_purge_sq(qp);
2530
2531 /* Disable queue pair */
2532 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2533 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2534 cb0);
2535 if (hret != H_SUCCESS) {
2536 pr_err("query_ehea_qp failed (1)\n");
2537 goto out;
2538 }
2539
2540 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2541 cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2542
2543 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2544 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2545 1), cb0, &dummy64,
2546 &dummy64, &dummy16, &dummy16);
2547 if (hret != H_SUCCESS) {
2548 pr_err("modify_ehea_qp failed (1)\n");
2549 goto out;
2550 }
2551
2552 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2553 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2554 cb0);
2555 if (hret != H_SUCCESS) {
2556 pr_err("query_ehea_qp failed (2)\n");
2557 goto out;
2558 }
2559
2560 /* deregister shared memory regions */
2561 dret = ehea_rem_smrs(pr);
2562 if (dret) {
2563 pr_err("unreg shared memory region failed\n");
2564 goto out;
2565 }
2566 }
2567
2568 ret = 0;
2569 out:
2570 free_page((unsigned long)cb0);
2571
2572 return ret;
2573 }
2574
ehea_update_rqs(struct ehea_qp * orig_qp,struct ehea_port_res * pr)2575 static void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2576 {
2577 struct ehea_qp qp = *orig_qp;
2578 struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2579 struct ehea_rwqe *rwqe;
2580 struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2581 struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2582 struct sk_buff *skb;
2583 u32 lkey = pr->recv_mr.lkey;
2584
2585
2586 int i;
2587 int index;
2588
2589 for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2590 rwqe = ehea_get_next_rwqe(&qp, 2);
2591 rwqe->sg_list[0].l_key = lkey;
2592 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2593 skb = skba_rq2[index];
2594 if (skb)
2595 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2596 }
2597
2598 for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2599 rwqe = ehea_get_next_rwqe(&qp, 3);
2600 rwqe->sg_list[0].l_key = lkey;
2601 index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2602 skb = skba_rq3[index];
2603 if (skb)
2604 rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2605 }
2606 }
2607
ehea_restart_qps(struct net_device * dev)2608 static int ehea_restart_qps(struct net_device *dev)
2609 {
2610 struct ehea_port *port = netdev_priv(dev);
2611 struct ehea_adapter *adapter = port->adapter;
2612 int ret = 0;
2613 int i;
2614
2615 struct hcp_modify_qp_cb0 *cb0;
2616 u64 hret;
2617 u64 dummy64 = 0;
2618 u16 dummy16 = 0;
2619
2620 cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2621 if (!cb0)
2622 return -ENOMEM;
2623
2624 for (i = 0; i < (port->num_def_qps); i++) {
2625 struct ehea_port_res *pr = &port->port_res[i];
2626 struct ehea_qp *qp = pr->qp;
2627
2628 ret = ehea_gen_smrs(pr);
2629 if (ret) {
2630 netdev_err(dev, "creation of shared memory regions failed\n");
2631 goto out;
2632 }
2633
2634 ehea_update_rqs(qp, pr);
2635
2636 /* Enable queue pair */
2637 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2638 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2639 cb0);
2640 if (hret != H_SUCCESS) {
2641 netdev_err(dev, "query_ehea_qp failed (1)\n");
2642 ret = -EFAULT;
2643 goto out;
2644 }
2645
2646 cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2647 cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2648
2649 hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2650 EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2651 1), cb0, &dummy64,
2652 &dummy64, &dummy16, &dummy16);
2653 if (hret != H_SUCCESS) {
2654 netdev_err(dev, "modify_ehea_qp failed (1)\n");
2655 ret = -EFAULT;
2656 goto out;
2657 }
2658
2659 hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2660 EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2661 cb0);
2662 if (hret != H_SUCCESS) {
2663 netdev_err(dev, "query_ehea_qp failed (2)\n");
2664 ret = -EFAULT;
2665 goto out;
2666 }
2667
2668 /* refill entire queue */
2669 ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2670 ehea_refill_rq2(pr, 0);
2671 ehea_refill_rq3(pr, 0);
2672 }
2673 out:
2674 free_page((unsigned long)cb0);
2675
2676 return ret;
2677 }
2678
ehea_reset_port(struct work_struct * work)2679 static void ehea_reset_port(struct work_struct *work)
2680 {
2681 int ret;
2682 struct ehea_port *port =
2683 container_of(work, struct ehea_port, reset_task);
2684 struct net_device *dev = port->netdev;
2685
2686 mutex_lock(&dlpar_mem_lock);
2687 port->resets++;
2688 mutex_lock(&port->port_lock);
2689 netif_tx_disable(dev);
2690
2691 port_napi_disable(port);
2692
2693 ehea_down(dev);
2694
2695 ret = ehea_up(dev);
2696 if (ret)
2697 goto out;
2698
2699 ehea_set_multicast_list(dev);
2700
2701 netif_info(port, timer, dev, "reset successful\n");
2702
2703 port_napi_enable(port);
2704
2705 netif_tx_wake_all_queues(dev);
2706 out:
2707 mutex_unlock(&port->port_lock);
2708 mutex_unlock(&dlpar_mem_lock);
2709 }
2710
ehea_rereg_mrs(void)2711 static void ehea_rereg_mrs(void)
2712 {
2713 int ret, i;
2714 struct ehea_adapter *adapter;
2715
2716 pr_info("LPAR memory changed - re-initializing driver\n");
2717
2718 list_for_each_entry(adapter, &adapter_list, list)
2719 if (adapter->active_ports) {
2720 /* Shutdown all ports */
2721 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2722 struct ehea_port *port = adapter->port[i];
2723 struct net_device *dev;
2724
2725 if (!port)
2726 continue;
2727
2728 dev = port->netdev;
2729
2730 if (dev->flags & IFF_UP) {
2731 mutex_lock(&port->port_lock);
2732 netif_tx_disable(dev);
2733 ehea_flush_sq(port);
2734 ret = ehea_stop_qps(dev);
2735 if (ret) {
2736 mutex_unlock(&port->port_lock);
2737 goto out;
2738 }
2739 port_napi_disable(port);
2740 mutex_unlock(&port->port_lock);
2741 }
2742 reset_sq_restart_flag(port);
2743 }
2744
2745 /* Unregister old memory region */
2746 ret = ehea_rem_mr(&adapter->mr);
2747 if (ret) {
2748 pr_err("unregister MR failed - driver inoperable!\n");
2749 goto out;
2750 }
2751 }
2752
2753 clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2754
2755 list_for_each_entry(adapter, &adapter_list, list)
2756 if (adapter->active_ports) {
2757 /* Register new memory region */
2758 ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2759 if (ret) {
2760 pr_err("register MR failed - driver inoperable!\n");
2761 goto out;
2762 }
2763
2764 /* Restart all ports */
2765 for (i = 0; i < EHEA_MAX_PORTS; i++) {
2766 struct ehea_port *port = adapter->port[i];
2767
2768 if (port) {
2769 struct net_device *dev = port->netdev;
2770
2771 if (dev->flags & IFF_UP) {
2772 mutex_lock(&port->port_lock);
2773 ret = ehea_restart_qps(dev);
2774 if (!ret) {
2775 check_sqs(port);
2776 port_napi_enable(port);
2777 netif_tx_wake_all_queues(dev);
2778 } else {
2779 netdev_err(dev, "Unable to restart QPS\n");
2780 }
2781 mutex_unlock(&port->port_lock);
2782 }
2783 }
2784 }
2785 }
2786 pr_info("re-initializing driver complete\n");
2787 out:
2788 return;
2789 }
2790
ehea_tx_watchdog(struct net_device * dev,unsigned int txqueue)2791 static void ehea_tx_watchdog(struct net_device *dev, unsigned int txqueue)
2792 {
2793 struct ehea_port *port = netdev_priv(dev);
2794
2795 if (netif_carrier_ok(dev) &&
2796 !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2797 ehea_schedule_port_reset(port);
2798 }
2799
ehea_sense_adapter_attr(struct ehea_adapter * adapter)2800 static int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2801 {
2802 struct hcp_query_ehea *cb;
2803 u64 hret;
2804 int ret;
2805
2806 cb = (void *)get_zeroed_page(GFP_KERNEL);
2807 if (!cb) {
2808 ret = -ENOMEM;
2809 goto out;
2810 }
2811
2812 hret = ehea_h_query_ehea(adapter->handle, cb);
2813
2814 if (hret != H_SUCCESS) {
2815 ret = -EIO;
2816 goto out_herr;
2817 }
2818
2819 adapter->max_mc_mac = cb->max_mc_mac - 1;
2820 ret = 0;
2821
2822 out_herr:
2823 free_page((unsigned long)cb);
2824 out:
2825 return ret;
2826 }
2827
ehea_get_jumboframe_status(struct ehea_port * port,int * jumbo)2828 static int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2829 {
2830 struct hcp_ehea_port_cb4 *cb4;
2831 u64 hret;
2832 int ret = 0;
2833
2834 *jumbo = 0;
2835
2836 /* (Try to) enable *jumbo frames */
2837 cb4 = (void *)get_zeroed_page(GFP_KERNEL);
2838 if (!cb4) {
2839 pr_err("no mem for cb4\n");
2840 ret = -ENOMEM;
2841 goto out;
2842 } else {
2843 hret = ehea_h_query_ehea_port(port->adapter->handle,
2844 port->logical_port_id,
2845 H_PORT_CB4,
2846 H_PORT_CB4_JUMBO, cb4);
2847 if (hret == H_SUCCESS) {
2848 if (cb4->jumbo_frame)
2849 *jumbo = 1;
2850 else {
2851 cb4->jumbo_frame = 1;
2852 hret = ehea_h_modify_ehea_port(port->adapter->
2853 handle,
2854 port->
2855 logical_port_id,
2856 H_PORT_CB4,
2857 H_PORT_CB4_JUMBO,
2858 cb4);
2859 if (hret == H_SUCCESS)
2860 *jumbo = 1;
2861 }
2862 } else
2863 ret = -EINVAL;
2864
2865 free_page((unsigned long)cb4);
2866 }
2867 out:
2868 return ret;
2869 }
2870
log_port_id_show(struct device * dev,struct device_attribute * attr,char * buf)2871 static ssize_t log_port_id_show(struct device *dev,
2872 struct device_attribute *attr, char *buf)
2873 {
2874 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2875 return sprintf(buf, "%d", port->logical_port_id);
2876 }
2877
2878 static DEVICE_ATTR_RO(log_port_id);
2879
logical_port_release(struct device * dev)2880 static void logical_port_release(struct device *dev)
2881 {
2882 struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2883 of_node_put(port->ofdev.dev.of_node);
2884 }
2885
ehea_register_port(struct ehea_port * port,struct device_node * dn)2886 static struct device *ehea_register_port(struct ehea_port *port,
2887 struct device_node *dn)
2888 {
2889 int ret;
2890
2891 port->ofdev.dev.of_node = of_node_get(dn);
2892 port->ofdev.dev.parent = &port->adapter->ofdev->dev;
2893 port->ofdev.dev.bus = &ibmebus_bus_type;
2894
2895 dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
2896 port->ofdev.dev.release = logical_port_release;
2897
2898 ret = of_device_register(&port->ofdev);
2899 if (ret) {
2900 pr_err("failed to register device. ret=%d\n", ret);
2901 put_device(&port->ofdev.dev);
2902 goto out;
2903 }
2904
2905 ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2906 if (ret) {
2907 pr_err("failed to register attributes, ret=%d\n", ret);
2908 goto out_unreg_of_dev;
2909 }
2910
2911 return &port->ofdev.dev;
2912
2913 out_unreg_of_dev:
2914 of_device_unregister(&port->ofdev);
2915 out:
2916 return NULL;
2917 }
2918
ehea_unregister_port(struct ehea_port * port)2919 static void ehea_unregister_port(struct ehea_port *port)
2920 {
2921 device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2922 of_device_unregister(&port->ofdev);
2923 }
2924
2925 static const struct net_device_ops ehea_netdev_ops = {
2926 .ndo_open = ehea_open,
2927 .ndo_stop = ehea_stop,
2928 .ndo_start_xmit = ehea_start_xmit,
2929 .ndo_get_stats64 = ehea_get_stats64,
2930 .ndo_set_mac_address = ehea_set_mac_addr,
2931 .ndo_validate_addr = eth_validate_addr,
2932 .ndo_set_rx_mode = ehea_set_multicast_list,
2933 .ndo_vlan_rx_add_vid = ehea_vlan_rx_add_vid,
2934 .ndo_vlan_rx_kill_vid = ehea_vlan_rx_kill_vid,
2935 .ndo_tx_timeout = ehea_tx_watchdog,
2936 };
2937
ehea_setup_single_port(struct ehea_adapter * adapter,u32 logical_port_id,struct device_node * dn)2938 static struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2939 u32 logical_port_id,
2940 struct device_node *dn)
2941 {
2942 int ret;
2943 struct net_device *dev;
2944 struct ehea_port *port;
2945 struct device *port_dev;
2946 int jumbo;
2947
2948 /* allocate memory for the port structures */
2949 dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
2950
2951 if (!dev) {
2952 ret = -ENOMEM;
2953 goto out_err;
2954 }
2955
2956 port = netdev_priv(dev);
2957
2958 mutex_init(&port->port_lock);
2959 port->state = EHEA_PORT_DOWN;
2960 port->sig_comp_iv = sq_entries / 10;
2961
2962 port->adapter = adapter;
2963 port->netdev = dev;
2964 port->logical_port_id = logical_port_id;
2965
2966 port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2967
2968 port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2969 if (!port->mc_list) {
2970 ret = -ENOMEM;
2971 goto out_free_ethdev;
2972 }
2973
2974 INIT_LIST_HEAD(&port->mc_list->list);
2975
2976 ret = ehea_sense_port_attr(port);
2977 if (ret)
2978 goto out_free_mc_list;
2979
2980 netif_set_real_num_rx_queues(dev, port->num_def_qps);
2981 netif_set_real_num_tx_queues(dev, port->num_def_qps);
2982
2983 port_dev = ehea_register_port(port, dn);
2984 if (!port_dev)
2985 goto out_free_mc_list;
2986
2987 SET_NETDEV_DEV(dev, port_dev);
2988
2989 /* initialize net_device structure */
2990 memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
2991
2992 dev->netdev_ops = &ehea_netdev_ops;
2993 ehea_set_ethtool_ops(dev);
2994
2995 dev->hw_features = NETIF_F_SG | NETIF_F_TSO |
2996 NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_CTAG_TX;
2997 dev->features = NETIF_F_SG | NETIF_F_TSO |
2998 NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
2999 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3000 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM;
3001 dev->vlan_features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HIGHDMA |
3002 NETIF_F_IP_CSUM;
3003 dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3004
3005 /* MTU range: 68 - 9022 */
3006 dev->min_mtu = ETH_MIN_MTU;
3007 dev->max_mtu = EHEA_MAX_PACKET_SIZE;
3008
3009 INIT_WORK(&port->reset_task, ehea_reset_port);
3010 INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3011
3012 init_waitqueue_head(&port->swqe_avail_wq);
3013 init_waitqueue_head(&port->restart_wq);
3014
3015 ret = register_netdev(dev);
3016 if (ret) {
3017 pr_err("register_netdev failed. ret=%d\n", ret);
3018 goto out_unreg_port;
3019 }
3020
3021 ret = ehea_get_jumboframe_status(port, &jumbo);
3022 if (ret)
3023 netdev_err(dev, "failed determining jumbo frame status\n");
3024
3025 netdev_info(dev, "Jumbo frames are %sabled\n",
3026 jumbo == 1 ? "en" : "dis");
3027
3028 adapter->active_ports++;
3029
3030 return port;
3031
3032 out_unreg_port:
3033 ehea_unregister_port(port);
3034
3035 out_free_mc_list:
3036 kfree(port->mc_list);
3037
3038 out_free_ethdev:
3039 free_netdev(dev);
3040
3041 out_err:
3042 pr_err("setting up logical port with id=%d failed, ret=%d\n",
3043 logical_port_id, ret);
3044 return NULL;
3045 }
3046
ehea_shutdown_single_port(struct ehea_port * port)3047 static void ehea_shutdown_single_port(struct ehea_port *port)
3048 {
3049 struct ehea_adapter *adapter = port->adapter;
3050
3051 cancel_work_sync(&port->reset_task);
3052 cancel_delayed_work_sync(&port->stats_work);
3053 unregister_netdev(port->netdev);
3054 ehea_unregister_port(port);
3055 kfree(port->mc_list);
3056 free_netdev(port->netdev);
3057 adapter->active_ports--;
3058 }
3059
ehea_setup_ports(struct ehea_adapter * adapter)3060 static int ehea_setup_ports(struct ehea_adapter *adapter)
3061 {
3062 struct device_node *lhea_dn;
3063 struct device_node *eth_dn = NULL;
3064
3065 const u32 *dn_log_port_id;
3066 int i = 0;
3067
3068 lhea_dn = adapter->ofdev->dev.of_node;
3069 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3070
3071 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3072 NULL);
3073 if (!dn_log_port_id) {
3074 pr_err("bad device node: eth_dn name=%pOF\n", eth_dn);
3075 continue;
3076 }
3077
3078 if (ehea_add_adapter_mr(adapter)) {
3079 pr_err("creating MR failed\n");
3080 of_node_put(eth_dn);
3081 return -EIO;
3082 }
3083
3084 adapter->port[i] = ehea_setup_single_port(adapter,
3085 *dn_log_port_id,
3086 eth_dn);
3087 if (adapter->port[i])
3088 netdev_info(adapter->port[i]->netdev,
3089 "logical port id #%d\n", *dn_log_port_id);
3090 else
3091 ehea_remove_adapter_mr(adapter);
3092
3093 i++;
3094 }
3095 return 0;
3096 }
3097
ehea_get_eth_dn(struct ehea_adapter * adapter,u32 logical_port_id)3098 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3099 u32 logical_port_id)
3100 {
3101 struct device_node *lhea_dn;
3102 struct device_node *eth_dn = NULL;
3103 const u32 *dn_log_port_id;
3104
3105 lhea_dn = adapter->ofdev->dev.of_node;
3106 while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3107
3108 dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3109 NULL);
3110 if (dn_log_port_id)
3111 if (*dn_log_port_id == logical_port_id)
3112 return eth_dn;
3113 }
3114
3115 return NULL;
3116 }
3117
probe_port_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3118 static ssize_t probe_port_store(struct device *dev,
3119 struct device_attribute *attr,
3120 const char *buf, size_t count)
3121 {
3122 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3123 struct ehea_port *port;
3124 struct device_node *eth_dn = NULL;
3125 int i;
3126
3127 u32 logical_port_id;
3128
3129 sscanf(buf, "%d", &logical_port_id);
3130
3131 port = ehea_get_port(adapter, logical_port_id);
3132
3133 if (port) {
3134 netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3135 logical_port_id);
3136 return -EINVAL;
3137 }
3138
3139 eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3140
3141 if (!eth_dn) {
3142 pr_info("no logical port with id %d found\n", logical_port_id);
3143 return -EINVAL;
3144 }
3145
3146 if (ehea_add_adapter_mr(adapter)) {
3147 pr_err("creating MR failed\n");
3148 of_node_put(eth_dn);
3149 return -EIO;
3150 }
3151
3152 port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3153
3154 of_node_put(eth_dn);
3155
3156 if (port) {
3157 for (i = 0; i < EHEA_MAX_PORTS; i++)
3158 if (!adapter->port[i]) {
3159 adapter->port[i] = port;
3160 break;
3161 }
3162
3163 netdev_info(port->netdev, "added: (logical port id=%d)\n",
3164 logical_port_id);
3165 } else {
3166 ehea_remove_adapter_mr(adapter);
3167 return -EIO;
3168 }
3169
3170 return (ssize_t) count;
3171 }
3172
remove_port_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3173 static ssize_t remove_port_store(struct device *dev,
3174 struct device_attribute *attr,
3175 const char *buf, size_t count)
3176 {
3177 struct ehea_adapter *adapter = dev_get_drvdata(dev);
3178 struct ehea_port *port;
3179 int i;
3180 u32 logical_port_id;
3181
3182 sscanf(buf, "%d", &logical_port_id);
3183
3184 port = ehea_get_port(adapter, logical_port_id);
3185
3186 if (port) {
3187 netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3188 logical_port_id);
3189
3190 ehea_shutdown_single_port(port);
3191
3192 for (i = 0; i < EHEA_MAX_PORTS; i++)
3193 if (adapter->port[i] == port) {
3194 adapter->port[i] = NULL;
3195 break;
3196 }
3197 } else {
3198 pr_err("removing port with logical port id=%d failed. port not configured.\n",
3199 logical_port_id);
3200 return -EINVAL;
3201 }
3202
3203 ehea_remove_adapter_mr(adapter);
3204
3205 return (ssize_t) count;
3206 }
3207
3208 static DEVICE_ATTR_WO(probe_port);
3209 static DEVICE_ATTR_WO(remove_port);
3210
ehea_create_device_sysfs(struct platform_device * dev)3211 static int ehea_create_device_sysfs(struct platform_device *dev)
3212 {
3213 int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3214 if (ret)
3215 goto out;
3216
3217 ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3218 out:
3219 return ret;
3220 }
3221
ehea_remove_device_sysfs(struct platform_device * dev)3222 static void ehea_remove_device_sysfs(struct platform_device *dev)
3223 {
3224 device_remove_file(&dev->dev, &dev_attr_probe_port);
3225 device_remove_file(&dev->dev, &dev_attr_remove_port);
3226 }
3227
ehea_reboot_notifier(struct notifier_block * nb,unsigned long action,void * unused)3228 static int ehea_reboot_notifier(struct notifier_block *nb,
3229 unsigned long action, void *unused)
3230 {
3231 if (action == SYS_RESTART) {
3232 pr_info("Reboot: freeing all eHEA resources\n");
3233 ibmebus_unregister_driver(&ehea_driver);
3234 }
3235 return NOTIFY_DONE;
3236 }
3237
3238 static struct notifier_block ehea_reboot_nb = {
3239 .notifier_call = ehea_reboot_notifier,
3240 };
3241
ehea_mem_notifier(struct notifier_block * nb,unsigned long action,void * data)3242 static int ehea_mem_notifier(struct notifier_block *nb,
3243 unsigned long action, void *data)
3244 {
3245 int ret = NOTIFY_BAD;
3246 struct memory_notify *arg = data;
3247
3248 mutex_lock(&dlpar_mem_lock);
3249
3250 switch (action) {
3251 case MEM_CANCEL_OFFLINE:
3252 pr_info("memory offlining canceled");
3253 fallthrough; /* re-add canceled memory block */
3254
3255 case MEM_ONLINE:
3256 pr_info("memory is going online");
3257 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3258 if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3259 goto out_unlock;
3260 ehea_rereg_mrs();
3261 break;
3262
3263 case MEM_GOING_OFFLINE:
3264 pr_info("memory is going offline");
3265 set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3266 if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3267 goto out_unlock;
3268 ehea_rereg_mrs();
3269 break;
3270
3271 default:
3272 break;
3273 }
3274
3275 ehea_update_firmware_handles();
3276 ret = NOTIFY_OK;
3277
3278 out_unlock:
3279 mutex_unlock(&dlpar_mem_lock);
3280 return ret;
3281 }
3282
3283 static struct notifier_block ehea_mem_nb = {
3284 .notifier_call = ehea_mem_notifier,
3285 };
3286
ehea_crash_handler(void)3287 static void ehea_crash_handler(void)
3288 {
3289 int i;
3290
3291 if (ehea_fw_handles.arr)
3292 for (i = 0; i < ehea_fw_handles.num_entries; i++)
3293 ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3294 ehea_fw_handles.arr[i].fwh,
3295 FORCE_FREE);
3296
3297 if (ehea_bcmc_regs.arr)
3298 for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3299 ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3300 ehea_bcmc_regs.arr[i].port_id,
3301 ehea_bcmc_regs.arr[i].reg_type,
3302 ehea_bcmc_regs.arr[i].macaddr,
3303 0, H_DEREG_BCMC);
3304 }
3305
3306 static atomic_t ehea_memory_hooks_registered;
3307
3308 /* Register memory hooks on probe of first adapter */
ehea_register_memory_hooks(void)3309 static int ehea_register_memory_hooks(void)
3310 {
3311 int ret = 0;
3312
3313 if (atomic_inc_return(&ehea_memory_hooks_registered) > 1)
3314 return 0;
3315
3316 ret = ehea_create_busmap();
3317 if (ret) {
3318 pr_info("ehea_create_busmap failed\n");
3319 goto out;
3320 }
3321
3322 ret = register_reboot_notifier(&ehea_reboot_nb);
3323 if (ret) {
3324 pr_info("register_reboot_notifier failed\n");
3325 goto out;
3326 }
3327
3328 ret = register_memory_notifier(&ehea_mem_nb);
3329 if (ret) {
3330 pr_info("register_memory_notifier failed\n");
3331 goto out2;
3332 }
3333
3334 ret = crash_shutdown_register(ehea_crash_handler);
3335 if (ret) {
3336 pr_info("crash_shutdown_register failed\n");
3337 goto out3;
3338 }
3339
3340 return 0;
3341
3342 out3:
3343 unregister_memory_notifier(&ehea_mem_nb);
3344 out2:
3345 unregister_reboot_notifier(&ehea_reboot_nb);
3346 out:
3347 atomic_dec(&ehea_memory_hooks_registered);
3348 return ret;
3349 }
3350
ehea_unregister_memory_hooks(void)3351 static void ehea_unregister_memory_hooks(void)
3352 {
3353 /* Only remove the hooks if we've registered them */
3354 if (atomic_read(&ehea_memory_hooks_registered) == 0)
3355 return;
3356
3357 unregister_reboot_notifier(&ehea_reboot_nb);
3358 if (crash_shutdown_unregister(ehea_crash_handler))
3359 pr_info("failed unregistering crash handler\n");
3360 unregister_memory_notifier(&ehea_mem_nb);
3361 }
3362
ehea_probe_adapter(struct platform_device * dev)3363 static int ehea_probe_adapter(struct platform_device *dev)
3364 {
3365 struct ehea_adapter *adapter;
3366 const u64 *adapter_handle;
3367 int ret;
3368 int i;
3369
3370 ret = ehea_register_memory_hooks();
3371 if (ret)
3372 return ret;
3373
3374 if (!dev || !dev->dev.of_node) {
3375 pr_err("Invalid ibmebus device probed\n");
3376 return -EINVAL;
3377 }
3378
3379 adapter = devm_kzalloc(&dev->dev, sizeof(*adapter), GFP_KERNEL);
3380 if (!adapter) {
3381 ret = -ENOMEM;
3382 dev_err(&dev->dev, "no mem for ehea_adapter\n");
3383 goto out;
3384 }
3385
3386 list_add(&adapter->list, &adapter_list);
3387
3388 adapter->ofdev = dev;
3389
3390 adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3391 NULL);
3392 if (adapter_handle)
3393 adapter->handle = *adapter_handle;
3394
3395 if (!adapter->handle) {
3396 dev_err(&dev->dev, "failed getting handle for adapter"
3397 " '%pOF'\n", dev->dev.of_node);
3398 ret = -ENODEV;
3399 goto out_free_ad;
3400 }
3401
3402 adapter->pd = EHEA_PD_ID;
3403
3404 platform_set_drvdata(dev, adapter);
3405
3406
3407 /* initialize adapter and ports */
3408 /* get adapter properties */
3409 ret = ehea_sense_adapter_attr(adapter);
3410 if (ret) {
3411 dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3412 goto out_free_ad;
3413 }
3414
3415 adapter->neq = ehea_create_eq(adapter,
3416 EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3417 if (!adapter->neq) {
3418 ret = -EIO;
3419 dev_err(&dev->dev, "NEQ creation failed\n");
3420 goto out_free_ad;
3421 }
3422
3423 tasklet_setup(&adapter->neq_tasklet, ehea_neq_tasklet);
3424
3425 ret = ehea_create_device_sysfs(dev);
3426 if (ret)
3427 goto out_kill_eq;
3428
3429 ret = ehea_setup_ports(adapter);
3430 if (ret) {
3431 dev_err(&dev->dev, "setup_ports failed\n");
3432 goto out_rem_dev_sysfs;
3433 }
3434
3435 ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3436 ehea_interrupt_neq, 0,
3437 "ehea_neq", adapter);
3438 if (ret) {
3439 dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3440 goto out_shutdown_ports;
3441 }
3442
3443 /* Handle any events that might be pending. */
3444 tasklet_hi_schedule(&adapter->neq_tasklet);
3445
3446 ret = 0;
3447 goto out;
3448
3449 out_shutdown_ports:
3450 for (i = 0; i < EHEA_MAX_PORTS; i++)
3451 if (adapter->port[i]) {
3452 ehea_shutdown_single_port(adapter->port[i]);
3453 adapter->port[i] = NULL;
3454 }
3455
3456 out_rem_dev_sysfs:
3457 ehea_remove_device_sysfs(dev);
3458
3459 out_kill_eq:
3460 ehea_destroy_eq(adapter->neq);
3461
3462 out_free_ad:
3463 list_del(&adapter->list);
3464
3465 out:
3466 ehea_update_firmware_handles();
3467
3468 return ret;
3469 }
3470
ehea_remove(struct platform_device * dev)3471 static int ehea_remove(struct platform_device *dev)
3472 {
3473 struct ehea_adapter *adapter = platform_get_drvdata(dev);
3474 int i;
3475
3476 for (i = 0; i < EHEA_MAX_PORTS; i++)
3477 if (adapter->port[i]) {
3478 ehea_shutdown_single_port(adapter->port[i]);
3479 adapter->port[i] = NULL;
3480 }
3481
3482 ehea_remove_device_sysfs(dev);
3483
3484 ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3485 tasklet_kill(&adapter->neq_tasklet);
3486
3487 ehea_destroy_eq(adapter->neq);
3488 ehea_remove_adapter_mr(adapter);
3489 list_del(&adapter->list);
3490
3491 ehea_update_firmware_handles();
3492
3493 return 0;
3494 }
3495
check_module_parm(void)3496 static int check_module_parm(void)
3497 {
3498 int ret = 0;
3499
3500 if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3501 (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3502 pr_info("Bad parameter: rq1_entries\n");
3503 ret = -EINVAL;
3504 }
3505 if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3506 (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3507 pr_info("Bad parameter: rq2_entries\n");
3508 ret = -EINVAL;
3509 }
3510 if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3511 (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3512 pr_info("Bad parameter: rq3_entries\n");
3513 ret = -EINVAL;
3514 }
3515 if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3516 (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3517 pr_info("Bad parameter: sq_entries\n");
3518 ret = -EINVAL;
3519 }
3520
3521 return ret;
3522 }
3523
capabilities_show(struct device_driver * drv,char * buf)3524 static ssize_t capabilities_show(struct device_driver *drv, char *buf)
3525 {
3526 return sprintf(buf, "%d", EHEA_CAPABILITIES);
3527 }
3528
3529 static DRIVER_ATTR_RO(capabilities);
3530
ehea_module_init(void)3531 static int __init ehea_module_init(void)
3532 {
3533 int ret;
3534
3535 pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3536
3537 memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3538 memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3539
3540 mutex_init(&ehea_fw_handles.lock);
3541 spin_lock_init(&ehea_bcmc_regs.lock);
3542
3543 ret = check_module_parm();
3544 if (ret)
3545 goto out;
3546
3547 ret = ibmebus_register_driver(&ehea_driver);
3548 if (ret) {
3549 pr_err("failed registering eHEA device driver on ebus\n");
3550 goto out;
3551 }
3552
3553 ret = driver_create_file(&ehea_driver.driver,
3554 &driver_attr_capabilities);
3555 if (ret) {
3556 pr_err("failed to register capabilities attribute, ret=%d\n",
3557 ret);
3558 goto out2;
3559 }
3560
3561 return ret;
3562
3563 out2:
3564 ibmebus_unregister_driver(&ehea_driver);
3565 out:
3566 return ret;
3567 }
3568
ehea_module_exit(void)3569 static void __exit ehea_module_exit(void)
3570 {
3571 driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3572 ibmebus_unregister_driver(&ehea_driver);
3573 ehea_unregister_memory_hooks();
3574 kfree(ehea_fw_handles.arr);
3575 kfree(ehea_bcmc_regs.arr);
3576 ehea_destroy_busmap();
3577 }
3578
3579 module_init(ehea_module_init);
3580 module_exit(ehea_module_exit);
3581