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