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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 	eth_hw_addr_set(dev, mac_addr->sa_data);
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 		goto out;
2902 	}
2903 
2904 	ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2905 	if (ret) {
2906 		pr_err("failed to register attributes, ret=%d\n", ret);
2907 		goto out_unreg_of_dev;
2908 	}
2909 
2910 	return &port->ofdev.dev;
2911 
2912 out_unreg_of_dev:
2913 	of_device_unregister(&port->ofdev);
2914 out:
2915 	return NULL;
2916 }
2917 
2918 static void ehea_unregister_port(struct ehea_port *port)
2919 {
2920 	device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2921 	of_device_unregister(&port->ofdev);
2922 }
2923 
2924 static const struct net_device_ops ehea_netdev_ops = {
2925 	.ndo_open		= ehea_open,
2926 	.ndo_stop		= ehea_stop,
2927 	.ndo_start_xmit		= ehea_start_xmit,
2928 	.ndo_get_stats64	= ehea_get_stats64,
2929 	.ndo_set_mac_address	= ehea_set_mac_addr,
2930 	.ndo_validate_addr	= eth_validate_addr,
2931 	.ndo_set_rx_mode	= ehea_set_multicast_list,
2932 	.ndo_vlan_rx_add_vid	= ehea_vlan_rx_add_vid,
2933 	.ndo_vlan_rx_kill_vid	= ehea_vlan_rx_kill_vid,
2934 	.ndo_tx_timeout		= ehea_tx_watchdog,
2935 };
2936 
2937 static struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2938 					 u32 logical_port_id,
2939 					 struct device_node *dn)
2940 {
2941 	int ret;
2942 	struct net_device *dev;
2943 	struct ehea_port *port;
2944 	struct device *port_dev;
2945 	int jumbo;
2946 
2947 	/* allocate memory for the port structures */
2948 	dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
2949 
2950 	if (!dev) {
2951 		ret = -ENOMEM;
2952 		goto out_err;
2953 	}
2954 
2955 	port = netdev_priv(dev);
2956 
2957 	mutex_init(&port->port_lock);
2958 	port->state = EHEA_PORT_DOWN;
2959 	port->sig_comp_iv = sq_entries / 10;
2960 
2961 	port->adapter = adapter;
2962 	port->netdev = dev;
2963 	port->logical_port_id = logical_port_id;
2964 
2965 	port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2966 
2967 	port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2968 	if (!port->mc_list) {
2969 		ret = -ENOMEM;
2970 		goto out_free_ethdev;
2971 	}
2972 
2973 	INIT_LIST_HEAD(&port->mc_list->list);
2974 
2975 	ret = ehea_sense_port_attr(port);
2976 	if (ret)
2977 		goto out_free_mc_list;
2978 
2979 	netif_set_real_num_rx_queues(dev, port->num_def_qps);
2980 	netif_set_real_num_tx_queues(dev, port->num_def_qps);
2981 
2982 	port_dev = ehea_register_port(port, dn);
2983 	if (!port_dev)
2984 		goto out_free_mc_list;
2985 
2986 	SET_NETDEV_DEV(dev, port_dev);
2987 
2988 	/* initialize net_device structure */
2989 	eth_hw_addr_set(dev, (u8 *)&port->mac_addr);
2990 
2991 	dev->netdev_ops = &ehea_netdev_ops;
2992 	ehea_set_ethtool_ops(dev);
2993 
2994 	dev->hw_features = NETIF_F_SG | NETIF_F_TSO |
2995 		      NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_CTAG_TX;
2996 	dev->features = NETIF_F_SG | NETIF_F_TSO |
2997 		      NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
2998 		      NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
2999 		      NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM;
3000 	dev->vlan_features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HIGHDMA |
3001 			NETIF_F_IP_CSUM;
3002 	dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3003 
3004 	/* MTU range: 68 - 9022 */
3005 	dev->min_mtu = ETH_MIN_MTU;
3006 	dev->max_mtu = EHEA_MAX_PACKET_SIZE;
3007 
3008 	INIT_WORK(&port->reset_task, ehea_reset_port);
3009 	INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3010 
3011 	init_waitqueue_head(&port->swqe_avail_wq);
3012 	init_waitqueue_head(&port->restart_wq);
3013 
3014 	ret = register_netdev(dev);
3015 	if (ret) {
3016 		pr_err("register_netdev failed. ret=%d\n", ret);
3017 		goto out_unreg_port;
3018 	}
3019 
3020 	ret = ehea_get_jumboframe_status(port, &jumbo);
3021 	if (ret)
3022 		netdev_err(dev, "failed determining jumbo frame status\n");
3023 
3024 	netdev_info(dev, "Jumbo frames are %sabled\n",
3025 		    jumbo == 1 ? "en" : "dis");
3026 
3027 	adapter->active_ports++;
3028 
3029 	return port;
3030 
3031 out_unreg_port:
3032 	ehea_unregister_port(port);
3033 
3034 out_free_mc_list:
3035 	kfree(port->mc_list);
3036 
3037 out_free_ethdev:
3038 	free_netdev(dev);
3039 
3040 out_err:
3041 	pr_err("setting up logical port with id=%d failed, ret=%d\n",
3042 	       logical_port_id, ret);
3043 	return NULL;
3044 }
3045 
3046 static void ehea_shutdown_single_port(struct ehea_port *port)
3047 {
3048 	struct ehea_adapter *adapter = port->adapter;
3049 
3050 	cancel_work_sync(&port->reset_task);
3051 	cancel_delayed_work_sync(&port->stats_work);
3052 	unregister_netdev(port->netdev);
3053 	ehea_unregister_port(port);
3054 	kfree(port->mc_list);
3055 	free_netdev(port->netdev);
3056 	adapter->active_ports--;
3057 }
3058 
3059 static int ehea_setup_ports(struct ehea_adapter *adapter)
3060 {
3061 	struct device_node *lhea_dn;
3062 	struct device_node *eth_dn = NULL;
3063 
3064 	const u32 *dn_log_port_id;
3065 	int i = 0;
3066 
3067 	lhea_dn = adapter->ofdev->dev.of_node;
3068 	while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3069 
3070 		dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3071 						 NULL);
3072 		if (!dn_log_port_id) {
3073 			pr_err("bad device node: eth_dn name=%pOF\n", eth_dn);
3074 			continue;
3075 		}
3076 
3077 		if (ehea_add_adapter_mr(adapter)) {
3078 			pr_err("creating MR failed\n");
3079 			of_node_put(eth_dn);
3080 			return -EIO;
3081 		}
3082 
3083 		adapter->port[i] = ehea_setup_single_port(adapter,
3084 							  *dn_log_port_id,
3085 							  eth_dn);
3086 		if (adapter->port[i])
3087 			netdev_info(adapter->port[i]->netdev,
3088 				    "logical port id #%d\n", *dn_log_port_id);
3089 		else
3090 			ehea_remove_adapter_mr(adapter);
3091 
3092 		i++;
3093 	}
3094 	return 0;
3095 }
3096 
3097 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3098 					   u32 logical_port_id)
3099 {
3100 	struct device_node *lhea_dn;
3101 	struct device_node *eth_dn = NULL;
3102 	const u32 *dn_log_port_id;
3103 
3104 	lhea_dn = adapter->ofdev->dev.of_node;
3105 	while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3106 
3107 		dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3108 						 NULL);
3109 		if (dn_log_port_id)
3110 			if (*dn_log_port_id == logical_port_id)
3111 				return eth_dn;
3112 	}
3113 
3114 	return NULL;
3115 }
3116 
3117 static ssize_t probe_port_store(struct device *dev,
3118 			       struct device_attribute *attr,
3119 			       const char *buf, size_t count)
3120 {
3121 	struct ehea_adapter *adapter = dev_get_drvdata(dev);
3122 	struct ehea_port *port;
3123 	struct device_node *eth_dn = NULL;
3124 	int i;
3125 
3126 	u32 logical_port_id;
3127 
3128 	sscanf(buf, "%d", &logical_port_id);
3129 
3130 	port = ehea_get_port(adapter, logical_port_id);
3131 
3132 	if (port) {
3133 		netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3134 			    logical_port_id);
3135 		return -EINVAL;
3136 	}
3137 
3138 	eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3139 
3140 	if (!eth_dn) {
3141 		pr_info("no logical port with id %d found\n", logical_port_id);
3142 		return -EINVAL;
3143 	}
3144 
3145 	if (ehea_add_adapter_mr(adapter)) {
3146 		pr_err("creating MR failed\n");
3147 		of_node_put(eth_dn);
3148 		return -EIO;
3149 	}
3150 
3151 	port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3152 
3153 	of_node_put(eth_dn);
3154 
3155 	if (port) {
3156 		for (i = 0; i < EHEA_MAX_PORTS; i++)
3157 			if (!adapter->port[i]) {
3158 				adapter->port[i] = port;
3159 				break;
3160 			}
3161 
3162 		netdev_info(port->netdev, "added: (logical port id=%d)\n",
3163 			    logical_port_id);
3164 	} else {
3165 		ehea_remove_adapter_mr(adapter);
3166 		return -EIO;
3167 	}
3168 
3169 	return (ssize_t) count;
3170 }
3171 
3172 static ssize_t remove_port_store(struct device *dev,
3173 				 struct device_attribute *attr,
3174 				 const char *buf, size_t count)
3175 {
3176 	struct ehea_adapter *adapter = dev_get_drvdata(dev);
3177 	struct ehea_port *port;
3178 	int i;
3179 	u32 logical_port_id;
3180 
3181 	sscanf(buf, "%d", &logical_port_id);
3182 
3183 	port = ehea_get_port(adapter, logical_port_id);
3184 
3185 	if (port) {
3186 		netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3187 			    logical_port_id);
3188 
3189 		ehea_shutdown_single_port(port);
3190 
3191 		for (i = 0; i < EHEA_MAX_PORTS; i++)
3192 			if (adapter->port[i] == port) {
3193 				adapter->port[i] = NULL;
3194 				break;
3195 			}
3196 	} else {
3197 		pr_err("removing port with logical port id=%d failed. port not configured.\n",
3198 		       logical_port_id);
3199 		return -EINVAL;
3200 	}
3201 
3202 	ehea_remove_adapter_mr(adapter);
3203 
3204 	return (ssize_t) count;
3205 }
3206 
3207 static DEVICE_ATTR_WO(probe_port);
3208 static DEVICE_ATTR_WO(remove_port);
3209 
3210 static int ehea_create_device_sysfs(struct platform_device *dev)
3211 {
3212 	int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3213 	if (ret)
3214 		goto out;
3215 
3216 	ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3217 out:
3218 	return ret;
3219 }
3220 
3221 static void ehea_remove_device_sysfs(struct platform_device *dev)
3222 {
3223 	device_remove_file(&dev->dev, &dev_attr_probe_port);
3224 	device_remove_file(&dev->dev, &dev_attr_remove_port);
3225 }
3226 
3227 static int ehea_reboot_notifier(struct notifier_block *nb,
3228 				unsigned long action, void *unused)
3229 {
3230 	if (action == SYS_RESTART) {
3231 		pr_info("Reboot: freeing all eHEA resources\n");
3232 		ibmebus_unregister_driver(&ehea_driver);
3233 	}
3234 	return NOTIFY_DONE;
3235 }
3236 
3237 static struct notifier_block ehea_reboot_nb = {
3238 	.notifier_call = ehea_reboot_notifier,
3239 };
3240 
3241 static int ehea_mem_notifier(struct notifier_block *nb,
3242 			     unsigned long action, void *data)
3243 {
3244 	int ret = NOTIFY_BAD;
3245 	struct memory_notify *arg = data;
3246 
3247 	mutex_lock(&dlpar_mem_lock);
3248 
3249 	switch (action) {
3250 	case MEM_CANCEL_OFFLINE:
3251 		pr_info("memory offlining canceled");
3252 		fallthrough;	/* re-add canceled memory block */
3253 
3254 	case MEM_ONLINE:
3255 		pr_info("memory is going online");
3256 		set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3257 		if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3258 			goto out_unlock;
3259 		ehea_rereg_mrs();
3260 		break;
3261 
3262 	case MEM_GOING_OFFLINE:
3263 		pr_info("memory is going offline");
3264 		set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3265 		if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3266 			goto out_unlock;
3267 		ehea_rereg_mrs();
3268 		break;
3269 
3270 	default:
3271 		break;
3272 	}
3273 
3274 	ehea_update_firmware_handles();
3275 	ret = NOTIFY_OK;
3276 
3277 out_unlock:
3278 	mutex_unlock(&dlpar_mem_lock);
3279 	return ret;
3280 }
3281 
3282 static struct notifier_block ehea_mem_nb = {
3283 	.notifier_call = ehea_mem_notifier,
3284 };
3285 
3286 static void ehea_crash_handler(void)
3287 {
3288 	int i;
3289 
3290 	if (ehea_fw_handles.arr)
3291 		for (i = 0; i < ehea_fw_handles.num_entries; i++)
3292 			ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3293 					     ehea_fw_handles.arr[i].fwh,
3294 					     FORCE_FREE);
3295 
3296 	if (ehea_bcmc_regs.arr)
3297 		for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3298 			ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3299 					      ehea_bcmc_regs.arr[i].port_id,
3300 					      ehea_bcmc_regs.arr[i].reg_type,
3301 					      ehea_bcmc_regs.arr[i].macaddr,
3302 					      0, H_DEREG_BCMC);
3303 }
3304 
3305 static atomic_t ehea_memory_hooks_registered;
3306 
3307 /* Register memory hooks on probe of first adapter */
3308 static int ehea_register_memory_hooks(void)
3309 {
3310 	int ret = 0;
3311 
3312 	if (atomic_inc_return(&ehea_memory_hooks_registered) > 1)
3313 		return 0;
3314 
3315 	ret = ehea_create_busmap();
3316 	if (ret) {
3317 		pr_info("ehea_create_busmap failed\n");
3318 		goto out;
3319 	}
3320 
3321 	ret = register_reboot_notifier(&ehea_reboot_nb);
3322 	if (ret) {
3323 		pr_info("register_reboot_notifier failed\n");
3324 		goto out;
3325 	}
3326 
3327 	ret = register_memory_notifier(&ehea_mem_nb);
3328 	if (ret) {
3329 		pr_info("register_memory_notifier failed\n");
3330 		goto out2;
3331 	}
3332 
3333 	ret = crash_shutdown_register(ehea_crash_handler);
3334 	if (ret) {
3335 		pr_info("crash_shutdown_register failed\n");
3336 		goto out3;
3337 	}
3338 
3339 	return 0;
3340 
3341 out3:
3342 	unregister_memory_notifier(&ehea_mem_nb);
3343 out2:
3344 	unregister_reboot_notifier(&ehea_reboot_nb);
3345 out:
3346 	atomic_dec(&ehea_memory_hooks_registered);
3347 	return ret;
3348 }
3349 
3350 static void ehea_unregister_memory_hooks(void)
3351 {
3352 	/* Only remove the hooks if we've registered them */
3353 	if (atomic_read(&ehea_memory_hooks_registered) == 0)
3354 		return;
3355 
3356 	unregister_reboot_notifier(&ehea_reboot_nb);
3357 	if (crash_shutdown_unregister(ehea_crash_handler))
3358 		pr_info("failed unregistering crash handler\n");
3359 	unregister_memory_notifier(&ehea_mem_nb);
3360 }
3361 
3362 static int ehea_probe_adapter(struct platform_device *dev)
3363 {
3364 	struct ehea_adapter *adapter;
3365 	const u64 *adapter_handle;
3366 	int ret;
3367 	int i;
3368 
3369 	ret = ehea_register_memory_hooks();
3370 	if (ret)
3371 		return ret;
3372 
3373 	if (!dev || !dev->dev.of_node) {
3374 		pr_err("Invalid ibmebus device probed\n");
3375 		return -EINVAL;
3376 	}
3377 
3378 	adapter = devm_kzalloc(&dev->dev, sizeof(*adapter), GFP_KERNEL);
3379 	if (!adapter) {
3380 		ret = -ENOMEM;
3381 		dev_err(&dev->dev, "no mem for ehea_adapter\n");
3382 		goto out;
3383 	}
3384 
3385 	list_add(&adapter->list, &adapter_list);
3386 
3387 	adapter->ofdev = dev;
3388 
3389 	adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3390 					 NULL);
3391 	if (adapter_handle)
3392 		adapter->handle = *adapter_handle;
3393 
3394 	if (!adapter->handle) {
3395 		dev_err(&dev->dev, "failed getting handle for adapter"
3396 			" '%pOF'\n", dev->dev.of_node);
3397 		ret = -ENODEV;
3398 		goto out_free_ad;
3399 	}
3400 
3401 	adapter->pd = EHEA_PD_ID;
3402 
3403 	platform_set_drvdata(dev, adapter);
3404 
3405 
3406 	/* initialize adapter and ports */
3407 	/* get adapter properties */
3408 	ret = ehea_sense_adapter_attr(adapter);
3409 	if (ret) {
3410 		dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3411 		goto out_free_ad;
3412 	}
3413 
3414 	adapter->neq = ehea_create_eq(adapter,
3415 				      EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3416 	if (!adapter->neq) {
3417 		ret = -EIO;
3418 		dev_err(&dev->dev, "NEQ creation failed\n");
3419 		goto out_free_ad;
3420 	}
3421 
3422 	tasklet_setup(&adapter->neq_tasklet, ehea_neq_tasklet);
3423 
3424 	ret = ehea_create_device_sysfs(dev);
3425 	if (ret)
3426 		goto out_kill_eq;
3427 
3428 	ret = ehea_setup_ports(adapter);
3429 	if (ret) {
3430 		dev_err(&dev->dev, "setup_ports failed\n");
3431 		goto out_rem_dev_sysfs;
3432 	}
3433 
3434 	ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3435 				  ehea_interrupt_neq, 0,
3436 				  "ehea_neq", adapter);
3437 	if (ret) {
3438 		dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3439 		goto out_shutdown_ports;
3440 	}
3441 
3442 	/* Handle any events that might be pending. */
3443 	tasklet_hi_schedule(&adapter->neq_tasklet);
3444 
3445 	ret = 0;
3446 	goto out;
3447 
3448 out_shutdown_ports:
3449 	for (i = 0; i < EHEA_MAX_PORTS; i++)
3450 		if (adapter->port[i]) {
3451 			ehea_shutdown_single_port(adapter->port[i]);
3452 			adapter->port[i] = NULL;
3453 		}
3454 
3455 out_rem_dev_sysfs:
3456 	ehea_remove_device_sysfs(dev);
3457 
3458 out_kill_eq:
3459 	ehea_destroy_eq(adapter->neq);
3460 
3461 out_free_ad:
3462 	list_del(&adapter->list);
3463 
3464 out:
3465 	ehea_update_firmware_handles();
3466 
3467 	return ret;
3468 }
3469 
3470 static int ehea_remove(struct platform_device *dev)
3471 {
3472 	struct ehea_adapter *adapter = platform_get_drvdata(dev);
3473 	int i;
3474 
3475 	for (i = 0; i < EHEA_MAX_PORTS; i++)
3476 		if (adapter->port[i]) {
3477 			ehea_shutdown_single_port(adapter->port[i]);
3478 			adapter->port[i] = NULL;
3479 		}
3480 
3481 	ehea_remove_device_sysfs(dev);
3482 
3483 	ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3484 	tasklet_kill(&adapter->neq_tasklet);
3485 
3486 	ehea_destroy_eq(adapter->neq);
3487 	ehea_remove_adapter_mr(adapter);
3488 	list_del(&adapter->list);
3489 
3490 	ehea_update_firmware_handles();
3491 
3492 	return 0;
3493 }
3494 
3495 static int check_module_parm(void)
3496 {
3497 	int ret = 0;
3498 
3499 	if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3500 	    (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3501 		pr_info("Bad parameter: rq1_entries\n");
3502 		ret = -EINVAL;
3503 	}
3504 	if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3505 	    (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3506 		pr_info("Bad parameter: rq2_entries\n");
3507 		ret = -EINVAL;
3508 	}
3509 	if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3510 	    (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3511 		pr_info("Bad parameter: rq3_entries\n");
3512 		ret = -EINVAL;
3513 	}
3514 	if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3515 	    (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3516 		pr_info("Bad parameter: sq_entries\n");
3517 		ret = -EINVAL;
3518 	}
3519 
3520 	return ret;
3521 }
3522 
3523 static ssize_t capabilities_show(struct device_driver *drv, char *buf)
3524 {
3525 	return sprintf(buf, "%d", EHEA_CAPABILITIES);
3526 }
3527 
3528 static DRIVER_ATTR_RO(capabilities);
3529 
3530 static int __init ehea_module_init(void)
3531 {
3532 	int ret;
3533 
3534 	pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3535 
3536 	memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3537 	memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3538 
3539 	mutex_init(&ehea_fw_handles.lock);
3540 	spin_lock_init(&ehea_bcmc_regs.lock);
3541 
3542 	ret = check_module_parm();
3543 	if (ret)
3544 		goto out;
3545 
3546 	ret = ibmebus_register_driver(&ehea_driver);
3547 	if (ret) {
3548 		pr_err("failed registering eHEA device driver on ebus\n");
3549 		goto out;
3550 	}
3551 
3552 	ret = driver_create_file(&ehea_driver.driver,
3553 				 &driver_attr_capabilities);
3554 	if (ret) {
3555 		pr_err("failed to register capabilities attribute, ret=%d\n",
3556 		       ret);
3557 		goto out2;
3558 	}
3559 
3560 	return ret;
3561 
3562 out2:
3563 	ibmebus_unregister_driver(&ehea_driver);
3564 out:
3565 	return ret;
3566 }
3567 
3568 static void __exit ehea_module_exit(void)
3569 {
3570 	driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3571 	ibmebus_unregister_driver(&ehea_driver);
3572 	ehea_unregister_memory_hooks();
3573 	kfree(ehea_fw_handles.arr);
3574 	kfree(ehea_bcmc_regs.arr);
3575 	ehea_destroy_busmap();
3576 }
3577 
3578 module_init(ehea_module_init);
3579 module_exit(ehea_module_exit);
3580