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;
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 		swqe = ehea_get_swqe(pr->qp, &swqe_index);
787 		memset(swqe, 0, SWQE_HEADER_SIZE);
788 		atomic_dec(&pr->swqe_avail);
789 
790 		swqe->tx_control |= EHEA_SWQE_PURGE;
791 		swqe->wr_id = SWQE_RESTART_CHECK;
792 		swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
793 		swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT;
794 		swqe->immediate_data_length = 80;
795 
796 		ehea_post_swqe(pr->qp, swqe);
797 
798 		ret = wait_event_timeout(port->restart_wq,
799 					 pr->sq_restart_flag == 0,
800 					 msecs_to_jiffies(100));
801 
802 		if (!ret) {
803 			pr_err("HW/SW queues out of sync\n");
804 			ehea_schedule_port_reset(pr->port);
805 			return;
806 		}
807 	}
808 }
809 
810 
811 static struct ehea_cqe *ehea_proc_cqes(struct ehea_port_res *pr, int my_quota)
812 {
813 	struct sk_buff *skb;
814 	struct ehea_cq *send_cq = pr->send_cq;
815 	struct ehea_cqe *cqe;
816 	int quota = my_quota;
817 	int cqe_counter = 0;
818 	int swqe_av = 0;
819 	int index;
820 	struct netdev_queue *txq = netdev_get_tx_queue(pr->port->netdev,
821 						pr - &pr->port->port_res[0]);
822 
823 	cqe = ehea_poll_cq(send_cq);
824 	while (cqe && (quota > 0)) {
825 		ehea_inc_cq(send_cq);
826 
827 		cqe_counter++;
828 		rmb();
829 
830 		if (cqe->wr_id == SWQE_RESTART_CHECK) {
831 			pr->sq_restart_flag = 1;
832 			swqe_av++;
833 			break;
834 		}
835 
836 		if (cqe->status & EHEA_CQE_STAT_ERR_MASK) {
837 			pr_err("Bad send completion status=0x%04X\n",
838 			       cqe->status);
839 
840 			if (netif_msg_tx_err(pr->port))
841 				ehea_dump(cqe, sizeof(*cqe), "Send CQE");
842 
843 			if (cqe->status & EHEA_CQE_STAT_RESET_MASK) {
844 				pr_err("Resetting port\n");
845 				ehea_schedule_port_reset(pr->port);
846 				break;
847 			}
848 		}
849 
850 		if (netif_msg_tx_done(pr->port))
851 			ehea_dump(cqe, sizeof(*cqe), "CQE");
852 
853 		if (likely(EHEA_BMASK_GET(EHEA_WR_ID_TYPE, cqe->wr_id)
854 			   == EHEA_SWQE2_TYPE)) {
855 
856 			index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, cqe->wr_id);
857 			skb = pr->sq_skba.arr[index];
858 			dev_consume_skb_any(skb);
859 			pr->sq_skba.arr[index] = NULL;
860 		}
861 
862 		swqe_av += EHEA_BMASK_GET(EHEA_WR_ID_REFILL, cqe->wr_id);
863 		quota--;
864 
865 		cqe = ehea_poll_cq(send_cq);
866 	}
867 
868 	ehea_update_feca(send_cq, cqe_counter);
869 	atomic_add(swqe_av, &pr->swqe_avail);
870 
871 	if (unlikely(netif_tx_queue_stopped(txq) &&
872 		     (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))) {
873 		__netif_tx_lock(txq, smp_processor_id());
874 		if (netif_tx_queue_stopped(txq) &&
875 		    (atomic_read(&pr->swqe_avail) >= pr->swqe_refill_th))
876 			netif_tx_wake_queue(txq);
877 		__netif_tx_unlock(txq);
878 	}
879 
880 	wake_up(&pr->port->swqe_avail_wq);
881 
882 	return cqe;
883 }
884 
885 #define EHEA_POLL_MAX_CQES 65535
886 
887 static int ehea_poll(struct napi_struct *napi, int budget)
888 {
889 	struct ehea_port_res *pr = container_of(napi, struct ehea_port_res,
890 						napi);
891 	struct net_device *dev = pr->port->netdev;
892 	struct ehea_cqe *cqe;
893 	struct ehea_cqe *cqe_skb = NULL;
894 	int wqe_index;
895 	int rx = 0;
896 
897 	cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
898 	rx += ehea_proc_rwqes(dev, pr, budget - rx);
899 
900 	while (rx != budget) {
901 		napi_complete(napi);
902 		ehea_reset_cq_ep(pr->recv_cq);
903 		ehea_reset_cq_ep(pr->send_cq);
904 		ehea_reset_cq_n1(pr->recv_cq);
905 		ehea_reset_cq_n1(pr->send_cq);
906 		rmb();
907 		cqe = ehea_poll_rq1(pr->qp, &wqe_index);
908 		cqe_skb = ehea_poll_cq(pr->send_cq);
909 
910 		if (!cqe && !cqe_skb)
911 			return rx;
912 
913 		if (!napi_reschedule(napi))
914 			return rx;
915 
916 		cqe_skb = ehea_proc_cqes(pr, EHEA_POLL_MAX_CQES);
917 		rx += ehea_proc_rwqes(dev, pr, budget - rx);
918 	}
919 
920 	return rx;
921 }
922 
923 static irqreturn_t ehea_recv_irq_handler(int irq, void *param)
924 {
925 	struct ehea_port_res *pr = param;
926 
927 	napi_schedule(&pr->napi);
928 
929 	return IRQ_HANDLED;
930 }
931 
932 static irqreturn_t ehea_qp_aff_irq_handler(int irq, void *param)
933 {
934 	struct ehea_port *port = param;
935 	struct ehea_eqe *eqe;
936 	struct ehea_qp *qp;
937 	u32 qp_token;
938 	u64 resource_type, aer, aerr;
939 	int reset_port = 0;
940 
941 	eqe = ehea_poll_eq(port->qp_eq);
942 
943 	while (eqe) {
944 		qp_token = EHEA_BMASK_GET(EHEA_EQE_QP_TOKEN, eqe->entry);
945 		pr_err("QP aff_err: entry=0x%llx, token=0x%x\n",
946 		       eqe->entry, qp_token);
947 
948 		qp = port->port_res[qp_token].qp;
949 
950 		resource_type = ehea_error_data(port->adapter, qp->fw_handle,
951 						&aer, &aerr);
952 
953 		if (resource_type == EHEA_AER_RESTYPE_QP) {
954 			if ((aer & EHEA_AER_RESET_MASK) ||
955 			    (aerr & EHEA_AERR_RESET_MASK))
956 				 reset_port = 1;
957 		} else
958 			reset_port = 1;   /* Reset in case of CQ or EQ error */
959 
960 		eqe = ehea_poll_eq(port->qp_eq);
961 	}
962 
963 	if (reset_port) {
964 		pr_err("Resetting port\n");
965 		ehea_schedule_port_reset(port);
966 	}
967 
968 	return IRQ_HANDLED;
969 }
970 
971 static struct ehea_port *ehea_get_port(struct ehea_adapter *adapter,
972 				       int logical_port)
973 {
974 	int i;
975 
976 	for (i = 0; i < EHEA_MAX_PORTS; i++)
977 		if (adapter->port[i])
978 			if (adapter->port[i]->logical_port_id == logical_port)
979 				return adapter->port[i];
980 	return NULL;
981 }
982 
983 int ehea_sense_port_attr(struct ehea_port *port)
984 {
985 	int ret;
986 	u64 hret;
987 	struct hcp_ehea_port_cb0 *cb0;
988 
989 	/* may be called via ehea_neq_tasklet() */
990 	cb0 = (void *)get_zeroed_page(GFP_ATOMIC);
991 	if (!cb0) {
992 		pr_err("no mem for cb0\n");
993 		ret = -ENOMEM;
994 		goto out;
995 	}
996 
997 	hret = ehea_h_query_ehea_port(port->adapter->handle,
998 				      port->logical_port_id, H_PORT_CB0,
999 				      EHEA_BMASK_SET(H_PORT_CB0_ALL, 0xFFFF),
1000 				      cb0);
1001 	if (hret != H_SUCCESS) {
1002 		ret = -EIO;
1003 		goto out_free;
1004 	}
1005 
1006 	/* MAC address */
1007 	port->mac_addr = cb0->port_mac_addr << 16;
1008 
1009 	if (!is_valid_ether_addr((u8 *)&port->mac_addr)) {
1010 		ret = -EADDRNOTAVAIL;
1011 		goto out_free;
1012 	}
1013 
1014 	/* Port speed */
1015 	switch (cb0->port_speed) {
1016 	case H_SPEED_10M_H:
1017 		port->port_speed = EHEA_SPEED_10M;
1018 		port->full_duplex = 0;
1019 		break;
1020 	case H_SPEED_10M_F:
1021 		port->port_speed = EHEA_SPEED_10M;
1022 		port->full_duplex = 1;
1023 		break;
1024 	case H_SPEED_100M_H:
1025 		port->port_speed = EHEA_SPEED_100M;
1026 		port->full_duplex = 0;
1027 		break;
1028 	case H_SPEED_100M_F:
1029 		port->port_speed = EHEA_SPEED_100M;
1030 		port->full_duplex = 1;
1031 		break;
1032 	case H_SPEED_1G_F:
1033 		port->port_speed = EHEA_SPEED_1G;
1034 		port->full_duplex = 1;
1035 		break;
1036 	case H_SPEED_10G_F:
1037 		port->port_speed = EHEA_SPEED_10G;
1038 		port->full_duplex = 1;
1039 		break;
1040 	default:
1041 		port->port_speed = 0;
1042 		port->full_duplex = 0;
1043 		break;
1044 	}
1045 
1046 	port->autoneg = 1;
1047 	port->num_mcs = cb0->num_default_qps;
1048 
1049 	/* Number of default QPs */
1050 	if (use_mcs)
1051 		port->num_def_qps = cb0->num_default_qps;
1052 	else
1053 		port->num_def_qps = 1;
1054 
1055 	if (!port->num_def_qps) {
1056 		ret = -EINVAL;
1057 		goto out_free;
1058 	}
1059 
1060 	ret = 0;
1061 out_free:
1062 	if (ret || netif_msg_probe(port))
1063 		ehea_dump(cb0, sizeof(*cb0), "ehea_sense_port_attr");
1064 	free_page((unsigned long)cb0);
1065 out:
1066 	return ret;
1067 }
1068 
1069 int ehea_set_portspeed(struct ehea_port *port, u32 port_speed)
1070 {
1071 	struct hcp_ehea_port_cb4 *cb4;
1072 	u64 hret;
1073 	int ret = 0;
1074 
1075 	cb4 = (void *)get_zeroed_page(GFP_KERNEL);
1076 	if (!cb4) {
1077 		pr_err("no mem for cb4\n");
1078 		ret = -ENOMEM;
1079 		goto out;
1080 	}
1081 
1082 	cb4->port_speed = port_speed;
1083 
1084 	netif_carrier_off(port->netdev);
1085 
1086 	hret = ehea_h_modify_ehea_port(port->adapter->handle,
1087 				       port->logical_port_id,
1088 				       H_PORT_CB4, H_PORT_CB4_SPEED, cb4);
1089 	if (hret == H_SUCCESS) {
1090 		port->autoneg = port_speed == EHEA_SPEED_AUTONEG ? 1 : 0;
1091 
1092 		hret = ehea_h_query_ehea_port(port->adapter->handle,
1093 					      port->logical_port_id,
1094 					      H_PORT_CB4, H_PORT_CB4_SPEED,
1095 					      cb4);
1096 		if (hret == H_SUCCESS) {
1097 			switch (cb4->port_speed) {
1098 			case H_SPEED_10M_H:
1099 				port->port_speed = EHEA_SPEED_10M;
1100 				port->full_duplex = 0;
1101 				break;
1102 			case H_SPEED_10M_F:
1103 				port->port_speed = EHEA_SPEED_10M;
1104 				port->full_duplex = 1;
1105 				break;
1106 			case H_SPEED_100M_H:
1107 				port->port_speed = EHEA_SPEED_100M;
1108 				port->full_duplex = 0;
1109 				break;
1110 			case H_SPEED_100M_F:
1111 				port->port_speed = EHEA_SPEED_100M;
1112 				port->full_duplex = 1;
1113 				break;
1114 			case H_SPEED_1G_F:
1115 				port->port_speed = EHEA_SPEED_1G;
1116 				port->full_duplex = 1;
1117 				break;
1118 			case H_SPEED_10G_F:
1119 				port->port_speed = EHEA_SPEED_10G;
1120 				port->full_duplex = 1;
1121 				break;
1122 			default:
1123 				port->port_speed = 0;
1124 				port->full_duplex = 0;
1125 				break;
1126 			}
1127 		} else {
1128 			pr_err("Failed sensing port speed\n");
1129 			ret = -EIO;
1130 		}
1131 	} else {
1132 		if (hret == H_AUTHORITY) {
1133 			pr_info("Hypervisor denied setting port speed\n");
1134 			ret = -EPERM;
1135 		} else {
1136 			ret = -EIO;
1137 			pr_err("Failed setting port speed\n");
1138 		}
1139 	}
1140 	if (!prop_carrier_state || (port->phy_link == EHEA_PHY_LINK_UP))
1141 		netif_carrier_on(port->netdev);
1142 
1143 	free_page((unsigned long)cb4);
1144 out:
1145 	return ret;
1146 }
1147 
1148 static void ehea_parse_eqe(struct ehea_adapter *adapter, u64 eqe)
1149 {
1150 	int ret;
1151 	u8 ec;
1152 	u8 portnum;
1153 	struct ehea_port *port;
1154 	struct net_device *dev;
1155 
1156 	ec = EHEA_BMASK_GET(NEQE_EVENT_CODE, eqe);
1157 	portnum = EHEA_BMASK_GET(NEQE_PORTNUM, eqe);
1158 	port = ehea_get_port(adapter, portnum);
1159 	if (!port) {
1160 		netdev_err(NULL, "unknown portnum %x\n", portnum);
1161 		return;
1162 	}
1163 	dev = port->netdev;
1164 
1165 	switch (ec) {
1166 	case EHEA_EC_PORTSTATE_CHG:	/* port state change */
1167 
1168 		if (EHEA_BMASK_GET(NEQE_PORT_UP, eqe)) {
1169 			if (!netif_carrier_ok(dev)) {
1170 				ret = ehea_sense_port_attr(port);
1171 				if (ret) {
1172 					netdev_err(dev, "failed resensing port attributes\n");
1173 					break;
1174 				}
1175 
1176 				netif_info(port, link, dev,
1177 					   "Logical port up: %dMbps %s Duplex\n",
1178 					   port->port_speed,
1179 					   port->full_duplex == 1 ?
1180 					   "Full" : "Half");
1181 
1182 				netif_carrier_on(dev);
1183 				netif_wake_queue(dev);
1184 			}
1185 		} else
1186 			if (netif_carrier_ok(dev)) {
1187 				netif_info(port, link, dev,
1188 					   "Logical port down\n");
1189 				netif_carrier_off(dev);
1190 				netif_tx_disable(dev);
1191 			}
1192 
1193 		if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PORT_UP, eqe)) {
1194 			port->phy_link = EHEA_PHY_LINK_UP;
1195 			netif_info(port, link, dev,
1196 				   "Physical port up\n");
1197 			if (prop_carrier_state)
1198 				netif_carrier_on(dev);
1199 		} else {
1200 			port->phy_link = EHEA_PHY_LINK_DOWN;
1201 			netif_info(port, link, dev,
1202 				   "Physical port down\n");
1203 			if (prop_carrier_state)
1204 				netif_carrier_off(dev);
1205 		}
1206 
1207 		if (EHEA_BMASK_GET(NEQE_EXTSWITCH_PRIMARY, eqe))
1208 			netdev_info(dev,
1209 				    "External switch port is primary port\n");
1210 		else
1211 			netdev_info(dev,
1212 				    "External switch port is backup port\n");
1213 
1214 		break;
1215 	case EHEA_EC_ADAPTER_MALFUNC:
1216 		netdev_err(dev, "Adapter malfunction\n");
1217 		break;
1218 	case EHEA_EC_PORT_MALFUNC:
1219 		netdev_info(dev, "Port malfunction\n");
1220 		netif_carrier_off(dev);
1221 		netif_tx_disable(dev);
1222 		break;
1223 	default:
1224 		netdev_err(dev, "unknown event code %x, eqe=0x%llX\n", ec, eqe);
1225 		break;
1226 	}
1227 }
1228 
1229 static void ehea_neq_tasklet(unsigned long data)
1230 {
1231 	struct ehea_adapter *adapter = (struct ehea_adapter *)data;
1232 	struct ehea_eqe *eqe;
1233 	u64 event_mask;
1234 
1235 	eqe = ehea_poll_eq(adapter->neq);
1236 	pr_debug("eqe=%p\n", eqe);
1237 
1238 	while (eqe) {
1239 		pr_debug("*eqe=%lx\n", (unsigned long) eqe->entry);
1240 		ehea_parse_eqe(adapter, eqe->entry);
1241 		eqe = ehea_poll_eq(adapter->neq);
1242 		pr_debug("next eqe=%p\n", eqe);
1243 	}
1244 
1245 	event_mask = EHEA_BMASK_SET(NELR_PORTSTATE_CHG, 1)
1246 		   | EHEA_BMASK_SET(NELR_ADAPTER_MALFUNC, 1)
1247 		   | EHEA_BMASK_SET(NELR_PORT_MALFUNC, 1);
1248 
1249 	ehea_h_reset_events(adapter->handle,
1250 			    adapter->neq->fw_handle, event_mask);
1251 }
1252 
1253 static irqreturn_t ehea_interrupt_neq(int irq, void *param)
1254 {
1255 	struct ehea_adapter *adapter = param;
1256 	tasklet_hi_schedule(&adapter->neq_tasklet);
1257 	return IRQ_HANDLED;
1258 }
1259 
1260 
1261 static int ehea_fill_port_res(struct ehea_port_res *pr)
1262 {
1263 	int ret;
1264 	struct ehea_qp_init_attr *init_attr = &pr->qp->init_attr;
1265 
1266 	ehea_init_fill_rq1(pr, pr->rq1_skba.len);
1267 
1268 	ret = ehea_refill_rq2(pr, init_attr->act_nr_rwqes_rq2 - 1);
1269 
1270 	ret |= ehea_refill_rq3(pr, init_attr->act_nr_rwqes_rq3 - 1);
1271 
1272 	return ret;
1273 }
1274 
1275 static int ehea_reg_interrupts(struct net_device *dev)
1276 {
1277 	struct ehea_port *port = netdev_priv(dev);
1278 	struct ehea_port_res *pr;
1279 	int i, ret;
1280 
1281 
1282 	snprintf(port->int_aff_name, EHEA_IRQ_NAME_SIZE - 1, "%s-aff",
1283 		 dev->name);
1284 
1285 	ret = ibmebus_request_irq(port->qp_eq->attr.ist1,
1286 				  ehea_qp_aff_irq_handler,
1287 				  0, port->int_aff_name, port);
1288 	if (ret) {
1289 		netdev_err(dev, "failed registering irq for qp_aff_irq_handler:ist=%X\n",
1290 			   port->qp_eq->attr.ist1);
1291 		goto out_free_qpeq;
1292 	}
1293 
1294 	netif_info(port, ifup, dev,
1295 		   "irq_handle 0x%X for function qp_aff_irq_handler registered\n",
1296 		   port->qp_eq->attr.ist1);
1297 
1298 
1299 	for (i = 0; i < port->num_def_qps; i++) {
1300 		pr = &port->port_res[i];
1301 		snprintf(pr->int_send_name, EHEA_IRQ_NAME_SIZE - 1,
1302 			 "%s-queue%d", dev->name, i);
1303 		ret = ibmebus_request_irq(pr->eq->attr.ist1,
1304 					  ehea_recv_irq_handler,
1305 					  0, pr->int_send_name, pr);
1306 		if (ret) {
1307 			netdev_err(dev, "failed registering irq for ehea_queue port_res_nr:%d, ist=%X\n",
1308 				   i, pr->eq->attr.ist1);
1309 			goto out_free_req;
1310 		}
1311 		netif_info(port, ifup, dev,
1312 			   "irq_handle 0x%X for function ehea_queue_int %d registered\n",
1313 			   pr->eq->attr.ist1, i);
1314 	}
1315 out:
1316 	return ret;
1317 
1318 
1319 out_free_req:
1320 	while (--i >= 0) {
1321 		u32 ist = port->port_res[i].eq->attr.ist1;
1322 		ibmebus_free_irq(ist, &port->port_res[i]);
1323 	}
1324 
1325 out_free_qpeq:
1326 	ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1327 	i = port->num_def_qps;
1328 
1329 	goto out;
1330 
1331 }
1332 
1333 static void ehea_free_interrupts(struct net_device *dev)
1334 {
1335 	struct ehea_port *port = netdev_priv(dev);
1336 	struct ehea_port_res *pr;
1337 	int i;
1338 
1339 	/* send */
1340 
1341 	for (i = 0; i < port->num_def_qps; i++) {
1342 		pr = &port->port_res[i];
1343 		ibmebus_free_irq(pr->eq->attr.ist1, pr);
1344 		netif_info(port, intr, dev,
1345 			   "free send irq for res %d with handle 0x%X\n",
1346 			   i, pr->eq->attr.ist1);
1347 	}
1348 
1349 	/* associated events */
1350 	ibmebus_free_irq(port->qp_eq->attr.ist1, port);
1351 	netif_info(port, intr, dev,
1352 		   "associated event interrupt for handle 0x%X freed\n",
1353 		   port->qp_eq->attr.ist1);
1354 }
1355 
1356 static int ehea_configure_port(struct ehea_port *port)
1357 {
1358 	int ret, i;
1359 	u64 hret, mask;
1360 	struct hcp_ehea_port_cb0 *cb0;
1361 
1362 	ret = -ENOMEM;
1363 	cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1364 	if (!cb0)
1365 		goto out;
1366 
1367 	cb0->port_rc = EHEA_BMASK_SET(PXLY_RC_VALID, 1)
1368 		     | EHEA_BMASK_SET(PXLY_RC_IP_CHKSUM, 1)
1369 		     | EHEA_BMASK_SET(PXLY_RC_TCP_UDP_CHKSUM, 1)
1370 		     | EHEA_BMASK_SET(PXLY_RC_VLAN_XTRACT, 1)
1371 		     | EHEA_BMASK_SET(PXLY_RC_VLAN_TAG_FILTER,
1372 				      PXLY_RC_VLAN_FILTER)
1373 		     | EHEA_BMASK_SET(PXLY_RC_JUMBO_FRAME, 1);
1374 
1375 	for (i = 0; i < port->num_mcs; i++)
1376 		if (use_mcs)
1377 			cb0->default_qpn_arr[i] =
1378 				port->port_res[i].qp->init_attr.qp_nr;
1379 		else
1380 			cb0->default_qpn_arr[i] =
1381 				port->port_res[0].qp->init_attr.qp_nr;
1382 
1383 	if (netif_msg_ifup(port))
1384 		ehea_dump(cb0, sizeof(*cb0), "ehea_configure_port");
1385 
1386 	mask = EHEA_BMASK_SET(H_PORT_CB0_PRC, 1)
1387 	     | EHEA_BMASK_SET(H_PORT_CB0_DEFQPNARRAY, 1);
1388 
1389 	hret = ehea_h_modify_ehea_port(port->adapter->handle,
1390 				       port->logical_port_id,
1391 				       H_PORT_CB0, mask, cb0);
1392 	ret = -EIO;
1393 	if (hret != H_SUCCESS)
1394 		goto out_free;
1395 
1396 	ret = 0;
1397 
1398 out_free:
1399 	free_page((unsigned long)cb0);
1400 out:
1401 	return ret;
1402 }
1403 
1404 static int ehea_gen_smrs(struct ehea_port_res *pr)
1405 {
1406 	int ret;
1407 	struct ehea_adapter *adapter = pr->port->adapter;
1408 
1409 	ret = ehea_gen_smr(adapter, &adapter->mr, &pr->send_mr);
1410 	if (ret)
1411 		goto out;
1412 
1413 	ret = ehea_gen_smr(adapter, &adapter->mr, &pr->recv_mr);
1414 	if (ret)
1415 		goto out_free;
1416 
1417 	return 0;
1418 
1419 out_free:
1420 	ehea_rem_mr(&pr->send_mr);
1421 out:
1422 	pr_err("Generating SMRS failed\n");
1423 	return -EIO;
1424 }
1425 
1426 static int ehea_rem_smrs(struct ehea_port_res *pr)
1427 {
1428 	if ((ehea_rem_mr(&pr->send_mr)) ||
1429 	    (ehea_rem_mr(&pr->recv_mr)))
1430 		return -EIO;
1431 	else
1432 		return 0;
1433 }
1434 
1435 static int ehea_init_q_skba(struct ehea_q_skb_arr *q_skba, int max_q_entries)
1436 {
1437 	int arr_size = sizeof(void *) * max_q_entries;
1438 
1439 	q_skba->arr = vzalloc(arr_size);
1440 	if (!q_skba->arr)
1441 		return -ENOMEM;
1442 
1443 	q_skba->len = max_q_entries;
1444 	q_skba->index = 0;
1445 	q_skba->os_skbs = 0;
1446 
1447 	return 0;
1448 }
1449 
1450 static int ehea_init_port_res(struct ehea_port *port, struct ehea_port_res *pr,
1451 			      struct port_res_cfg *pr_cfg, int queue_token)
1452 {
1453 	struct ehea_adapter *adapter = port->adapter;
1454 	enum ehea_eq_type eq_type = EHEA_EQ;
1455 	struct ehea_qp_init_attr *init_attr = NULL;
1456 	int ret = -EIO;
1457 	u64 tx_bytes, rx_bytes, tx_packets, rx_packets;
1458 
1459 	tx_bytes = pr->tx_bytes;
1460 	tx_packets = pr->tx_packets;
1461 	rx_bytes = pr->rx_bytes;
1462 	rx_packets = pr->rx_packets;
1463 
1464 	memset(pr, 0, sizeof(struct ehea_port_res));
1465 
1466 	pr->tx_bytes = tx_bytes;
1467 	pr->tx_packets = tx_packets;
1468 	pr->rx_bytes = rx_bytes;
1469 	pr->rx_packets = rx_packets;
1470 
1471 	pr->port = port;
1472 
1473 	pr->eq = ehea_create_eq(adapter, eq_type, EHEA_MAX_ENTRIES_EQ, 0);
1474 	if (!pr->eq) {
1475 		pr_err("create_eq failed (eq)\n");
1476 		goto out_free;
1477 	}
1478 
1479 	pr->recv_cq = ehea_create_cq(adapter, pr_cfg->max_entries_rcq,
1480 				     pr->eq->fw_handle,
1481 				     port->logical_port_id);
1482 	if (!pr->recv_cq) {
1483 		pr_err("create_cq failed (cq_recv)\n");
1484 		goto out_free;
1485 	}
1486 
1487 	pr->send_cq = ehea_create_cq(adapter, pr_cfg->max_entries_scq,
1488 				     pr->eq->fw_handle,
1489 				     port->logical_port_id);
1490 	if (!pr->send_cq) {
1491 		pr_err("create_cq failed (cq_send)\n");
1492 		goto out_free;
1493 	}
1494 
1495 	if (netif_msg_ifup(port))
1496 		pr_info("Send CQ: act_nr_cqes=%d, Recv CQ: act_nr_cqes=%d\n",
1497 			pr->send_cq->attr.act_nr_of_cqes,
1498 			pr->recv_cq->attr.act_nr_of_cqes);
1499 
1500 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1501 	if (!init_attr) {
1502 		ret = -ENOMEM;
1503 		pr_err("no mem for ehea_qp_init_attr\n");
1504 		goto out_free;
1505 	}
1506 
1507 	init_attr->low_lat_rq1 = 1;
1508 	init_attr->signalingtype = 1;	/* generate CQE if specified in WQE */
1509 	init_attr->rq_count = 3;
1510 	init_attr->qp_token = queue_token;
1511 	init_attr->max_nr_send_wqes = pr_cfg->max_entries_sq;
1512 	init_attr->max_nr_rwqes_rq1 = pr_cfg->max_entries_rq1;
1513 	init_attr->max_nr_rwqes_rq2 = pr_cfg->max_entries_rq2;
1514 	init_attr->max_nr_rwqes_rq3 = pr_cfg->max_entries_rq3;
1515 	init_attr->wqe_size_enc_sq = EHEA_SG_SQ;
1516 	init_attr->wqe_size_enc_rq1 = EHEA_SG_RQ1;
1517 	init_attr->wqe_size_enc_rq2 = EHEA_SG_RQ2;
1518 	init_attr->wqe_size_enc_rq3 = EHEA_SG_RQ3;
1519 	init_attr->rq2_threshold = EHEA_RQ2_THRESHOLD;
1520 	init_attr->rq3_threshold = EHEA_RQ3_THRESHOLD;
1521 	init_attr->port_nr = port->logical_port_id;
1522 	init_attr->send_cq_handle = pr->send_cq->fw_handle;
1523 	init_attr->recv_cq_handle = pr->recv_cq->fw_handle;
1524 	init_attr->aff_eq_handle = port->qp_eq->fw_handle;
1525 
1526 	pr->qp = ehea_create_qp(adapter, adapter->pd, init_attr);
1527 	if (!pr->qp) {
1528 		pr_err("create_qp failed\n");
1529 		ret = -EIO;
1530 		goto out_free;
1531 	}
1532 
1533 	if (netif_msg_ifup(port))
1534 		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",
1535 			init_attr->qp_nr,
1536 			init_attr->act_nr_send_wqes,
1537 			init_attr->act_nr_rwqes_rq1,
1538 			init_attr->act_nr_rwqes_rq2,
1539 			init_attr->act_nr_rwqes_rq3);
1540 
1541 	pr->sq_skba_size = init_attr->act_nr_send_wqes + 1;
1542 
1543 	ret = ehea_init_q_skba(&pr->sq_skba, pr->sq_skba_size);
1544 	ret |= ehea_init_q_skba(&pr->rq1_skba, init_attr->act_nr_rwqes_rq1 + 1);
1545 	ret |= ehea_init_q_skba(&pr->rq2_skba, init_attr->act_nr_rwqes_rq2 + 1);
1546 	ret |= ehea_init_q_skba(&pr->rq3_skba, init_attr->act_nr_rwqes_rq3 + 1);
1547 	if (ret)
1548 		goto out_free;
1549 
1550 	pr->swqe_refill_th = init_attr->act_nr_send_wqes / 10;
1551 	if (ehea_gen_smrs(pr) != 0) {
1552 		ret = -EIO;
1553 		goto out_free;
1554 	}
1555 
1556 	atomic_set(&pr->swqe_avail, init_attr->act_nr_send_wqes - 1);
1557 
1558 	kfree(init_attr);
1559 
1560 	netif_napi_add(pr->port->netdev, &pr->napi, ehea_poll, 64);
1561 
1562 	ret = 0;
1563 	goto out;
1564 
1565 out_free:
1566 	kfree(init_attr);
1567 	vfree(pr->sq_skba.arr);
1568 	vfree(pr->rq1_skba.arr);
1569 	vfree(pr->rq2_skba.arr);
1570 	vfree(pr->rq3_skba.arr);
1571 	ehea_destroy_qp(pr->qp);
1572 	ehea_destroy_cq(pr->send_cq);
1573 	ehea_destroy_cq(pr->recv_cq);
1574 	ehea_destroy_eq(pr->eq);
1575 out:
1576 	return ret;
1577 }
1578 
1579 static int ehea_clean_portres(struct ehea_port *port, struct ehea_port_res *pr)
1580 {
1581 	int ret, i;
1582 
1583 	if (pr->qp)
1584 		netif_napi_del(&pr->napi);
1585 
1586 	ret = ehea_destroy_qp(pr->qp);
1587 
1588 	if (!ret) {
1589 		ehea_destroy_cq(pr->send_cq);
1590 		ehea_destroy_cq(pr->recv_cq);
1591 		ehea_destroy_eq(pr->eq);
1592 
1593 		for (i = 0; i < pr->rq1_skba.len; i++)
1594 			if (pr->rq1_skba.arr[i])
1595 				dev_kfree_skb(pr->rq1_skba.arr[i]);
1596 
1597 		for (i = 0; i < pr->rq2_skba.len; i++)
1598 			if (pr->rq2_skba.arr[i])
1599 				dev_kfree_skb(pr->rq2_skba.arr[i]);
1600 
1601 		for (i = 0; i < pr->rq3_skba.len; i++)
1602 			if (pr->rq3_skba.arr[i])
1603 				dev_kfree_skb(pr->rq3_skba.arr[i]);
1604 
1605 		for (i = 0; i < pr->sq_skba.len; i++)
1606 			if (pr->sq_skba.arr[i])
1607 				dev_kfree_skb(pr->sq_skba.arr[i]);
1608 
1609 		vfree(pr->rq1_skba.arr);
1610 		vfree(pr->rq2_skba.arr);
1611 		vfree(pr->rq3_skba.arr);
1612 		vfree(pr->sq_skba.arr);
1613 		ret = ehea_rem_smrs(pr);
1614 	}
1615 	return ret;
1616 }
1617 
1618 static void write_swqe2_immediate(struct sk_buff *skb, struct ehea_swqe *swqe,
1619 				  u32 lkey)
1620 {
1621 	int skb_data_size = skb_headlen(skb);
1622 	u8 *imm_data = &swqe->u.immdata_desc.immediate_data[0];
1623 	struct ehea_vsgentry *sg1entry = &swqe->u.immdata_desc.sg_entry;
1624 	unsigned int immediate_len = SWQE2_MAX_IMM;
1625 
1626 	swqe->descriptors = 0;
1627 
1628 	if (skb_is_gso(skb)) {
1629 		swqe->tx_control |= EHEA_SWQE_TSO;
1630 		swqe->mss = skb_shinfo(skb)->gso_size;
1631 		/*
1632 		 * For TSO packets we only copy the headers into the
1633 		 * immediate area.
1634 		 */
1635 		immediate_len = ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1636 	}
1637 
1638 	if (skb_is_gso(skb) || skb_data_size >= SWQE2_MAX_IMM) {
1639 		skb_copy_from_linear_data(skb, imm_data, immediate_len);
1640 		swqe->immediate_data_length = immediate_len;
1641 
1642 		if (skb_data_size > immediate_len) {
1643 			sg1entry->l_key = lkey;
1644 			sg1entry->len = skb_data_size - immediate_len;
1645 			sg1entry->vaddr =
1646 				ehea_map_vaddr(skb->data + immediate_len);
1647 			swqe->descriptors++;
1648 		}
1649 	} else {
1650 		skb_copy_from_linear_data(skb, imm_data, skb_data_size);
1651 		swqe->immediate_data_length = skb_data_size;
1652 	}
1653 }
1654 
1655 static inline void write_swqe2_data(struct sk_buff *skb, struct net_device *dev,
1656 				    struct ehea_swqe *swqe, u32 lkey)
1657 {
1658 	struct ehea_vsgentry *sg_list, *sg1entry, *sgentry;
1659 	skb_frag_t *frag;
1660 	int nfrags, sg1entry_contains_frag_data, i;
1661 
1662 	nfrags = skb_shinfo(skb)->nr_frags;
1663 	sg1entry = &swqe->u.immdata_desc.sg_entry;
1664 	sg_list = (struct ehea_vsgentry *)&swqe->u.immdata_desc.sg_list;
1665 	sg1entry_contains_frag_data = 0;
1666 
1667 	write_swqe2_immediate(skb, swqe, lkey);
1668 
1669 	/* write descriptors */
1670 	if (nfrags > 0) {
1671 		if (swqe->descriptors == 0) {
1672 			/* sg1entry not yet used */
1673 			frag = &skb_shinfo(skb)->frags[0];
1674 
1675 			/* copy sg1entry data */
1676 			sg1entry->l_key = lkey;
1677 			sg1entry->len = skb_frag_size(frag);
1678 			sg1entry->vaddr =
1679 				ehea_map_vaddr(skb_frag_address(frag));
1680 			swqe->descriptors++;
1681 			sg1entry_contains_frag_data = 1;
1682 		}
1683 
1684 		for (i = sg1entry_contains_frag_data; i < nfrags; i++) {
1685 
1686 			frag = &skb_shinfo(skb)->frags[i];
1687 			sgentry = &sg_list[i - sg1entry_contains_frag_data];
1688 
1689 			sgentry->l_key = lkey;
1690 			sgentry->len = skb_frag_size(frag);
1691 			sgentry->vaddr = ehea_map_vaddr(skb_frag_address(frag));
1692 			swqe->descriptors++;
1693 		}
1694 	}
1695 }
1696 
1697 static int ehea_broadcast_reg_helper(struct ehea_port *port, u32 hcallid)
1698 {
1699 	int ret = 0;
1700 	u64 hret;
1701 	u8 reg_type;
1702 
1703 	/* De/Register untagged packets */
1704 	reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_UNTAGGED;
1705 	hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1706 				     port->logical_port_id,
1707 				     reg_type, port->mac_addr, 0, hcallid);
1708 	if (hret != H_SUCCESS) {
1709 		pr_err("%sregistering bc address failed (tagged)\n",
1710 		       hcallid == H_REG_BCMC ? "" : "de");
1711 		ret = -EIO;
1712 		goto out_herr;
1713 	}
1714 
1715 	/* De/Register VLAN packets */
1716 	reg_type = EHEA_BCMC_BROADCAST | EHEA_BCMC_VLANID_ALL;
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 (vlan)\n",
1722 		       hcallid == H_REG_BCMC ? "" : "de");
1723 		ret = -EIO;
1724 	}
1725 out_herr:
1726 	return ret;
1727 }
1728 
1729 static int ehea_set_mac_addr(struct net_device *dev, void *sa)
1730 {
1731 	struct ehea_port *port = netdev_priv(dev);
1732 	struct sockaddr *mac_addr = sa;
1733 	struct hcp_ehea_port_cb0 *cb0;
1734 	int ret;
1735 	u64 hret;
1736 
1737 	if (!is_valid_ether_addr(mac_addr->sa_data)) {
1738 		ret = -EADDRNOTAVAIL;
1739 		goto out;
1740 	}
1741 
1742 	cb0 = (void *)get_zeroed_page(GFP_KERNEL);
1743 	if (!cb0) {
1744 		pr_err("no mem for cb0\n");
1745 		ret = -ENOMEM;
1746 		goto out;
1747 	}
1748 
1749 	memcpy(&(cb0->port_mac_addr), &(mac_addr->sa_data[0]), ETH_ALEN);
1750 
1751 	cb0->port_mac_addr = cb0->port_mac_addr >> 16;
1752 
1753 	hret = ehea_h_modify_ehea_port(port->adapter->handle,
1754 				       port->logical_port_id, H_PORT_CB0,
1755 				       EHEA_BMASK_SET(H_PORT_CB0_MAC, 1), cb0);
1756 	if (hret != H_SUCCESS) {
1757 		ret = -EIO;
1758 		goto out_free;
1759 	}
1760 
1761 	memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
1762 
1763 	/* Deregister old MAC in pHYP */
1764 	if (port->state == EHEA_PORT_UP) {
1765 		ret = ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
1766 		if (ret)
1767 			goto out_upregs;
1768 	}
1769 
1770 	port->mac_addr = cb0->port_mac_addr << 16;
1771 
1772 	/* Register new MAC in pHYP */
1773 	if (port->state == EHEA_PORT_UP) {
1774 		ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
1775 		if (ret)
1776 			goto out_upregs;
1777 	}
1778 
1779 	ret = 0;
1780 
1781 out_upregs:
1782 	ehea_update_bcmc_registrations();
1783 out_free:
1784 	free_page((unsigned long)cb0);
1785 out:
1786 	return ret;
1787 }
1788 
1789 static void ehea_promiscuous_error(u64 hret, int enable)
1790 {
1791 	if (hret == H_AUTHORITY)
1792 		pr_info("Hypervisor denied %sabling promiscuous mode\n",
1793 			enable == 1 ? "en" : "dis");
1794 	else
1795 		pr_err("failed %sabling promiscuous mode\n",
1796 		       enable == 1 ? "en" : "dis");
1797 }
1798 
1799 static void ehea_promiscuous(struct net_device *dev, int enable)
1800 {
1801 	struct ehea_port *port = netdev_priv(dev);
1802 	struct hcp_ehea_port_cb7 *cb7;
1803 	u64 hret;
1804 
1805 	if (enable == port->promisc)
1806 		return;
1807 
1808 	cb7 = (void *)get_zeroed_page(GFP_ATOMIC);
1809 	if (!cb7) {
1810 		pr_err("no mem for cb7\n");
1811 		goto out;
1812 	}
1813 
1814 	/* Modify Pxs_DUCQPN in CB7 */
1815 	cb7->def_uc_qpn = enable == 1 ? port->port_res[0].qp->fw_handle : 0;
1816 
1817 	hret = ehea_h_modify_ehea_port(port->adapter->handle,
1818 				       port->logical_port_id,
1819 				       H_PORT_CB7, H_PORT_CB7_DUCQPN, cb7);
1820 	if (hret) {
1821 		ehea_promiscuous_error(hret, enable);
1822 		goto out;
1823 	}
1824 
1825 	port->promisc = enable;
1826 out:
1827 	free_page((unsigned long)cb7);
1828 }
1829 
1830 static u64 ehea_multicast_reg_helper(struct ehea_port *port, u64 mc_mac_addr,
1831 				     u32 hcallid)
1832 {
1833 	u64 hret;
1834 	u8 reg_type;
1835 
1836 	reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_UNTAGGED;
1837 	if (mc_mac_addr == 0)
1838 		reg_type |= EHEA_BCMC_SCOPE_ALL;
1839 
1840 	hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1841 				     port->logical_port_id,
1842 				     reg_type, mc_mac_addr, 0, hcallid);
1843 	if (hret)
1844 		goto out;
1845 
1846 	reg_type = EHEA_BCMC_MULTICAST | EHEA_BCMC_VLANID_ALL;
1847 	if (mc_mac_addr == 0)
1848 		reg_type |= EHEA_BCMC_SCOPE_ALL;
1849 
1850 	hret = ehea_h_reg_dereg_bcmc(port->adapter->handle,
1851 				     port->logical_port_id,
1852 				     reg_type, mc_mac_addr, 0, hcallid);
1853 out:
1854 	return hret;
1855 }
1856 
1857 static int ehea_drop_multicast_list(struct net_device *dev)
1858 {
1859 	struct ehea_port *port = netdev_priv(dev);
1860 	struct ehea_mc_list *mc_entry = port->mc_list;
1861 	struct list_head *pos;
1862 	struct list_head *temp;
1863 	int ret = 0;
1864 	u64 hret;
1865 
1866 	list_for_each_safe(pos, temp, &(port->mc_list->list)) {
1867 		mc_entry = list_entry(pos, struct ehea_mc_list, list);
1868 
1869 		hret = ehea_multicast_reg_helper(port, mc_entry->macaddr,
1870 						 H_DEREG_BCMC);
1871 		if (hret) {
1872 			pr_err("failed deregistering mcast MAC\n");
1873 			ret = -EIO;
1874 		}
1875 
1876 		list_del(pos);
1877 		kfree(mc_entry);
1878 	}
1879 	return ret;
1880 }
1881 
1882 static void ehea_allmulti(struct net_device *dev, int enable)
1883 {
1884 	struct ehea_port *port = netdev_priv(dev);
1885 	u64 hret;
1886 
1887 	if (!port->allmulti) {
1888 		if (enable) {
1889 			/* Enable ALLMULTI */
1890 			ehea_drop_multicast_list(dev);
1891 			hret = ehea_multicast_reg_helper(port, 0, H_REG_BCMC);
1892 			if (!hret)
1893 				port->allmulti = 1;
1894 			else
1895 				netdev_err(dev,
1896 					   "failed enabling IFF_ALLMULTI\n");
1897 		}
1898 	} else {
1899 		if (!enable) {
1900 			/* Disable ALLMULTI */
1901 			hret = ehea_multicast_reg_helper(port, 0, H_DEREG_BCMC);
1902 			if (!hret)
1903 				port->allmulti = 0;
1904 			else
1905 				netdev_err(dev,
1906 					   "failed disabling IFF_ALLMULTI\n");
1907 		}
1908 	}
1909 }
1910 
1911 static void ehea_add_multicast_entry(struct ehea_port *port, u8 *mc_mac_addr)
1912 {
1913 	struct ehea_mc_list *ehea_mcl_entry;
1914 	u64 hret;
1915 
1916 	ehea_mcl_entry = kzalloc(sizeof(*ehea_mcl_entry), GFP_ATOMIC);
1917 	if (!ehea_mcl_entry)
1918 		return;
1919 
1920 	INIT_LIST_HEAD(&ehea_mcl_entry->list);
1921 
1922 	memcpy(&ehea_mcl_entry->macaddr, mc_mac_addr, ETH_ALEN);
1923 
1924 	hret = ehea_multicast_reg_helper(port, ehea_mcl_entry->macaddr,
1925 					 H_REG_BCMC);
1926 	if (!hret)
1927 		list_add(&ehea_mcl_entry->list, &port->mc_list->list);
1928 	else {
1929 		pr_err("failed registering mcast MAC\n");
1930 		kfree(ehea_mcl_entry);
1931 	}
1932 }
1933 
1934 static void ehea_set_multicast_list(struct net_device *dev)
1935 {
1936 	struct ehea_port *port = netdev_priv(dev);
1937 	struct netdev_hw_addr *ha;
1938 	int ret;
1939 
1940 	ehea_promiscuous(dev, !!(dev->flags & IFF_PROMISC));
1941 
1942 	if (dev->flags & IFF_ALLMULTI) {
1943 		ehea_allmulti(dev, 1);
1944 		goto out;
1945 	}
1946 	ehea_allmulti(dev, 0);
1947 
1948 	if (!netdev_mc_empty(dev)) {
1949 		ret = ehea_drop_multicast_list(dev);
1950 		if (ret) {
1951 			/* Dropping the current multicast list failed.
1952 			 * Enabling ALL_MULTI is the best we can do.
1953 			 */
1954 			ehea_allmulti(dev, 1);
1955 		}
1956 
1957 		if (netdev_mc_count(dev) > port->adapter->max_mc_mac) {
1958 			pr_info("Mcast registration limit reached (0x%llx). Use ALLMULTI!\n",
1959 				port->adapter->max_mc_mac);
1960 			goto out;
1961 		}
1962 
1963 		netdev_for_each_mc_addr(ha, dev)
1964 			ehea_add_multicast_entry(port, ha->addr);
1965 
1966 	}
1967 out:
1968 	ehea_update_bcmc_registrations();
1969 }
1970 
1971 static void xmit_common(struct sk_buff *skb, struct ehea_swqe *swqe)
1972 {
1973 	swqe->tx_control |= EHEA_SWQE_IMM_DATA_PRESENT | EHEA_SWQE_CRC;
1974 
1975 	if (vlan_get_protocol(skb) != htons(ETH_P_IP))
1976 		return;
1977 
1978 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1979 		swqe->tx_control |= EHEA_SWQE_IP_CHECKSUM;
1980 
1981 	swqe->ip_start = skb_network_offset(skb);
1982 	swqe->ip_end = swqe->ip_start + ip_hdrlen(skb) - 1;
1983 
1984 	switch (ip_hdr(skb)->protocol) {
1985 	case IPPROTO_UDP:
1986 		if (skb->ip_summed == CHECKSUM_PARTIAL)
1987 			swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
1988 
1989 		swqe->tcp_offset = swqe->ip_end + 1 +
1990 				   offsetof(struct udphdr, check);
1991 		break;
1992 
1993 	case IPPROTO_TCP:
1994 		if (skb->ip_summed == CHECKSUM_PARTIAL)
1995 			swqe->tx_control |= EHEA_SWQE_TCP_CHECKSUM;
1996 
1997 		swqe->tcp_offset = swqe->ip_end + 1 +
1998 				   offsetof(struct tcphdr, check);
1999 		break;
2000 	}
2001 }
2002 
2003 static void ehea_xmit2(struct sk_buff *skb, struct net_device *dev,
2004 		       struct ehea_swqe *swqe, u32 lkey)
2005 {
2006 	swqe->tx_control |= EHEA_SWQE_DESCRIPTORS_PRESENT;
2007 
2008 	xmit_common(skb, swqe);
2009 
2010 	write_swqe2_data(skb, dev, swqe, lkey);
2011 }
2012 
2013 static void ehea_xmit3(struct sk_buff *skb, struct net_device *dev,
2014 		       struct ehea_swqe *swqe)
2015 {
2016 	u8 *imm_data = &swqe->u.immdata_nodesc.immediate_data[0];
2017 
2018 	xmit_common(skb, swqe);
2019 
2020 	if (!skb->data_len)
2021 		skb_copy_from_linear_data(skb, imm_data, skb->len);
2022 	else
2023 		skb_copy_bits(skb, 0, imm_data, skb->len);
2024 
2025 	swqe->immediate_data_length = skb->len;
2026 	dev_consume_skb_any(skb);
2027 }
2028 
2029 static netdev_tx_t ehea_start_xmit(struct sk_buff *skb, struct net_device *dev)
2030 {
2031 	struct ehea_port *port = netdev_priv(dev);
2032 	struct ehea_swqe *swqe;
2033 	u32 lkey;
2034 	int swqe_index;
2035 	struct ehea_port_res *pr;
2036 	struct netdev_queue *txq;
2037 
2038 	pr = &port->port_res[skb_get_queue_mapping(skb)];
2039 	txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2040 
2041 	swqe = ehea_get_swqe(pr->qp, &swqe_index);
2042 	memset(swqe, 0, SWQE_HEADER_SIZE);
2043 	atomic_dec(&pr->swqe_avail);
2044 
2045 	if (skb_vlan_tag_present(skb)) {
2046 		swqe->tx_control |= EHEA_SWQE_VLAN_INSERT;
2047 		swqe->vlan_tag = skb_vlan_tag_get(skb);
2048 	}
2049 
2050 	pr->tx_packets++;
2051 	pr->tx_bytes += skb->len;
2052 
2053 	if (skb->len <= SWQE3_MAX_IMM) {
2054 		u32 sig_iv = port->sig_comp_iv;
2055 		u32 swqe_num = pr->swqe_id_counter;
2056 		ehea_xmit3(skb, dev, swqe);
2057 		swqe->wr_id = EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE3_TYPE)
2058 			| EHEA_BMASK_SET(EHEA_WR_ID_COUNT, swqe_num);
2059 		if (pr->swqe_ll_count >= (sig_iv - 1)) {
2060 			swqe->wr_id |= EHEA_BMASK_SET(EHEA_WR_ID_REFILL,
2061 						      sig_iv);
2062 			swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2063 			pr->swqe_ll_count = 0;
2064 		} else
2065 			pr->swqe_ll_count += 1;
2066 	} else {
2067 		swqe->wr_id =
2068 			EHEA_BMASK_SET(EHEA_WR_ID_TYPE, EHEA_SWQE2_TYPE)
2069 		      | EHEA_BMASK_SET(EHEA_WR_ID_COUNT, pr->swqe_id_counter)
2070 		      | EHEA_BMASK_SET(EHEA_WR_ID_REFILL, 1)
2071 		      | EHEA_BMASK_SET(EHEA_WR_ID_INDEX, pr->sq_skba.index);
2072 		pr->sq_skba.arr[pr->sq_skba.index] = skb;
2073 
2074 		pr->sq_skba.index++;
2075 		pr->sq_skba.index &= (pr->sq_skba.len - 1);
2076 
2077 		lkey = pr->send_mr.lkey;
2078 		ehea_xmit2(skb, dev, swqe, lkey);
2079 		swqe->tx_control |= EHEA_SWQE_SIGNALLED_COMPLETION;
2080 	}
2081 	pr->swqe_id_counter += 1;
2082 
2083 	netif_info(port, tx_queued, dev,
2084 		   "post swqe on QP %d\n", pr->qp->init_attr.qp_nr);
2085 	if (netif_msg_tx_queued(port))
2086 		ehea_dump(swqe, 512, "swqe");
2087 
2088 	if (unlikely(test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))) {
2089 		netif_tx_stop_queue(txq);
2090 		swqe->tx_control |= EHEA_SWQE_PURGE;
2091 	}
2092 
2093 	ehea_post_swqe(pr->qp, swqe);
2094 
2095 	if (unlikely(atomic_read(&pr->swqe_avail) <= 1)) {
2096 		pr->p_stats.queue_stopped++;
2097 		netif_tx_stop_queue(txq);
2098 	}
2099 
2100 	return NETDEV_TX_OK;
2101 }
2102 
2103 static int ehea_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
2104 {
2105 	struct ehea_port *port = netdev_priv(dev);
2106 	struct ehea_adapter *adapter = port->adapter;
2107 	struct hcp_ehea_port_cb1 *cb1;
2108 	int index;
2109 	u64 hret;
2110 	int err = 0;
2111 
2112 	cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2113 	if (!cb1) {
2114 		pr_err("no mem for cb1\n");
2115 		err = -ENOMEM;
2116 		goto out;
2117 	}
2118 
2119 	hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2120 				      H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2121 	if (hret != H_SUCCESS) {
2122 		pr_err("query_ehea_port failed\n");
2123 		err = -EINVAL;
2124 		goto out;
2125 	}
2126 
2127 	index = (vid / 64);
2128 	cb1->vlan_filter[index] |= ((u64)(0x8000000000000000 >> (vid & 0x3F)));
2129 
2130 	hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2131 				       H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2132 	if (hret != H_SUCCESS) {
2133 		pr_err("modify_ehea_port failed\n");
2134 		err = -EINVAL;
2135 	}
2136 out:
2137 	free_page((unsigned long)cb1);
2138 	return err;
2139 }
2140 
2141 static int ehea_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
2142 {
2143 	struct ehea_port *port = netdev_priv(dev);
2144 	struct ehea_adapter *adapter = port->adapter;
2145 	struct hcp_ehea_port_cb1 *cb1;
2146 	int index;
2147 	u64 hret;
2148 	int err = 0;
2149 
2150 	cb1 = (void *)get_zeroed_page(GFP_KERNEL);
2151 	if (!cb1) {
2152 		pr_err("no mem for cb1\n");
2153 		err = -ENOMEM;
2154 		goto out;
2155 	}
2156 
2157 	hret = ehea_h_query_ehea_port(adapter->handle, port->logical_port_id,
2158 				      H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2159 	if (hret != H_SUCCESS) {
2160 		pr_err("query_ehea_port failed\n");
2161 		err = -EINVAL;
2162 		goto out;
2163 	}
2164 
2165 	index = (vid / 64);
2166 	cb1->vlan_filter[index] &= ~((u64)(0x8000000000000000 >> (vid & 0x3F)));
2167 
2168 	hret = ehea_h_modify_ehea_port(adapter->handle, port->logical_port_id,
2169 				       H_PORT_CB1, H_PORT_CB1_ALL, cb1);
2170 	if (hret != H_SUCCESS) {
2171 		pr_err("modify_ehea_port failed\n");
2172 		err = -EINVAL;
2173 	}
2174 out:
2175 	free_page((unsigned long)cb1);
2176 	return err;
2177 }
2178 
2179 static int ehea_activate_qp(struct ehea_adapter *adapter, struct ehea_qp *qp)
2180 {
2181 	int ret = -EIO;
2182 	u64 hret;
2183 	u16 dummy16 = 0;
2184 	u64 dummy64 = 0;
2185 	struct hcp_modify_qp_cb0 *cb0;
2186 
2187 	cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2188 	if (!cb0) {
2189 		ret = -ENOMEM;
2190 		goto out;
2191 	}
2192 
2193 	hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2194 				    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2195 	if (hret != H_SUCCESS) {
2196 		pr_err("query_ehea_qp failed (1)\n");
2197 		goto out;
2198 	}
2199 
2200 	cb0->qp_ctl_reg = H_QP_CR_STATE_INITIALIZED;
2201 	hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2202 				     EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2203 				     &dummy64, &dummy64, &dummy16, &dummy16);
2204 	if (hret != H_SUCCESS) {
2205 		pr_err("modify_ehea_qp failed (1)\n");
2206 		goto out;
2207 	}
2208 
2209 	hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2210 				    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2211 	if (hret != H_SUCCESS) {
2212 		pr_err("query_ehea_qp failed (2)\n");
2213 		goto out;
2214 	}
2215 
2216 	cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_INITIALIZED;
2217 	hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2218 				     EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2219 				     &dummy64, &dummy64, &dummy16, &dummy16);
2220 	if (hret != H_SUCCESS) {
2221 		pr_err("modify_ehea_qp failed (2)\n");
2222 		goto out;
2223 	}
2224 
2225 	hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2226 				    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2227 	if (hret != H_SUCCESS) {
2228 		pr_err("query_ehea_qp failed (3)\n");
2229 		goto out;
2230 	}
2231 
2232 	cb0->qp_ctl_reg = H_QP_CR_ENABLED | H_QP_CR_STATE_RDY2SND;
2233 	hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2234 				     EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG, 1), cb0,
2235 				     &dummy64, &dummy64, &dummy16, &dummy16);
2236 	if (hret != H_SUCCESS) {
2237 		pr_err("modify_ehea_qp failed (3)\n");
2238 		goto out;
2239 	}
2240 
2241 	hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2242 				    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF), cb0);
2243 	if (hret != H_SUCCESS) {
2244 		pr_err("query_ehea_qp failed (4)\n");
2245 		goto out;
2246 	}
2247 
2248 	ret = 0;
2249 out:
2250 	free_page((unsigned long)cb0);
2251 	return ret;
2252 }
2253 
2254 static int ehea_port_res_setup(struct ehea_port *port, int def_qps)
2255 {
2256 	int ret, i;
2257 	struct port_res_cfg pr_cfg, pr_cfg_small_rx;
2258 	enum ehea_eq_type eq_type = EHEA_EQ;
2259 
2260 	port->qp_eq = ehea_create_eq(port->adapter, eq_type,
2261 				   EHEA_MAX_ENTRIES_EQ, 1);
2262 	if (!port->qp_eq) {
2263 		ret = -EINVAL;
2264 		pr_err("ehea_create_eq failed (qp_eq)\n");
2265 		goto out_kill_eq;
2266 	}
2267 
2268 	pr_cfg.max_entries_rcq = rq1_entries + rq2_entries + rq3_entries;
2269 	pr_cfg.max_entries_scq = sq_entries * 2;
2270 	pr_cfg.max_entries_sq = sq_entries;
2271 	pr_cfg.max_entries_rq1 = rq1_entries;
2272 	pr_cfg.max_entries_rq2 = rq2_entries;
2273 	pr_cfg.max_entries_rq3 = rq3_entries;
2274 
2275 	pr_cfg_small_rx.max_entries_rcq = 1;
2276 	pr_cfg_small_rx.max_entries_scq = sq_entries;
2277 	pr_cfg_small_rx.max_entries_sq = sq_entries;
2278 	pr_cfg_small_rx.max_entries_rq1 = 1;
2279 	pr_cfg_small_rx.max_entries_rq2 = 1;
2280 	pr_cfg_small_rx.max_entries_rq3 = 1;
2281 
2282 	for (i = 0; i < def_qps; i++) {
2283 		ret = ehea_init_port_res(port, &port->port_res[i], &pr_cfg, i);
2284 		if (ret)
2285 			goto out_clean_pr;
2286 	}
2287 	for (i = def_qps; i < def_qps; i++) {
2288 		ret = ehea_init_port_res(port, &port->port_res[i],
2289 					 &pr_cfg_small_rx, i);
2290 		if (ret)
2291 			goto out_clean_pr;
2292 	}
2293 
2294 	return 0;
2295 
2296 out_clean_pr:
2297 	while (--i >= 0)
2298 		ehea_clean_portres(port, &port->port_res[i]);
2299 
2300 out_kill_eq:
2301 	ehea_destroy_eq(port->qp_eq);
2302 	return ret;
2303 }
2304 
2305 static int ehea_clean_all_portres(struct ehea_port *port)
2306 {
2307 	int ret = 0;
2308 	int i;
2309 
2310 	for (i = 0; i < port->num_def_qps; i++)
2311 		ret |= ehea_clean_portres(port, &port->port_res[i]);
2312 
2313 	ret |= ehea_destroy_eq(port->qp_eq);
2314 
2315 	return ret;
2316 }
2317 
2318 static void ehea_remove_adapter_mr(struct ehea_adapter *adapter)
2319 {
2320 	if (adapter->active_ports)
2321 		return;
2322 
2323 	ehea_rem_mr(&adapter->mr);
2324 }
2325 
2326 static int ehea_add_adapter_mr(struct ehea_adapter *adapter)
2327 {
2328 	if (adapter->active_ports)
2329 		return 0;
2330 
2331 	return ehea_reg_kernel_mr(adapter, &adapter->mr);
2332 }
2333 
2334 static int ehea_up(struct net_device *dev)
2335 {
2336 	int ret, i;
2337 	struct ehea_port *port = netdev_priv(dev);
2338 
2339 	if (port->state == EHEA_PORT_UP)
2340 		return 0;
2341 
2342 	ret = ehea_port_res_setup(port, port->num_def_qps);
2343 	if (ret) {
2344 		netdev_err(dev, "port_res_failed\n");
2345 		goto out;
2346 	}
2347 
2348 	/* Set default QP for this port */
2349 	ret = ehea_configure_port(port);
2350 	if (ret) {
2351 		netdev_err(dev, "ehea_configure_port failed. ret:%d\n", ret);
2352 		goto out_clean_pr;
2353 	}
2354 
2355 	ret = ehea_reg_interrupts(dev);
2356 	if (ret) {
2357 		netdev_err(dev, "reg_interrupts failed. ret:%d\n", ret);
2358 		goto out_clean_pr;
2359 	}
2360 
2361 	for (i = 0; i < port->num_def_qps; i++) {
2362 		ret = ehea_activate_qp(port->adapter, port->port_res[i].qp);
2363 		if (ret) {
2364 			netdev_err(dev, "activate_qp failed\n");
2365 			goto out_free_irqs;
2366 		}
2367 	}
2368 
2369 	for (i = 0; i < port->num_def_qps; i++) {
2370 		ret = ehea_fill_port_res(&port->port_res[i]);
2371 		if (ret) {
2372 			netdev_err(dev, "out_free_irqs\n");
2373 			goto out_free_irqs;
2374 		}
2375 	}
2376 
2377 	ret = ehea_broadcast_reg_helper(port, H_REG_BCMC);
2378 	if (ret) {
2379 		ret = -EIO;
2380 		goto out_free_irqs;
2381 	}
2382 
2383 	port->state = EHEA_PORT_UP;
2384 
2385 	ret = 0;
2386 	goto out;
2387 
2388 out_free_irqs:
2389 	ehea_free_interrupts(dev);
2390 
2391 out_clean_pr:
2392 	ehea_clean_all_portres(port);
2393 out:
2394 	if (ret)
2395 		netdev_info(dev, "Failed starting. ret=%i\n", ret);
2396 
2397 	ehea_update_bcmc_registrations();
2398 	ehea_update_firmware_handles();
2399 
2400 	return ret;
2401 }
2402 
2403 static void port_napi_disable(struct ehea_port *port)
2404 {
2405 	int i;
2406 
2407 	for (i = 0; i < port->num_def_qps; i++)
2408 		napi_disable(&port->port_res[i].napi);
2409 }
2410 
2411 static void port_napi_enable(struct ehea_port *port)
2412 {
2413 	int i;
2414 
2415 	for (i = 0; i < port->num_def_qps; i++)
2416 		napi_enable(&port->port_res[i].napi);
2417 }
2418 
2419 static int ehea_open(struct net_device *dev)
2420 {
2421 	int ret;
2422 	struct ehea_port *port = netdev_priv(dev);
2423 
2424 	mutex_lock(&port->port_lock);
2425 
2426 	netif_info(port, ifup, dev, "enabling port\n");
2427 
2428 	netif_carrier_off(dev);
2429 
2430 	ret = ehea_up(dev);
2431 	if (!ret) {
2432 		port_napi_enable(port);
2433 		netif_tx_start_all_queues(dev);
2434 	}
2435 
2436 	mutex_unlock(&port->port_lock);
2437 	schedule_delayed_work(&port->stats_work,
2438 			      round_jiffies_relative(msecs_to_jiffies(1000)));
2439 
2440 	return ret;
2441 }
2442 
2443 static int ehea_down(struct net_device *dev)
2444 {
2445 	int ret;
2446 	struct ehea_port *port = netdev_priv(dev);
2447 
2448 	if (port->state == EHEA_PORT_DOWN)
2449 		return 0;
2450 
2451 	ehea_drop_multicast_list(dev);
2452 	ehea_allmulti(dev, 0);
2453 	ehea_broadcast_reg_helper(port, H_DEREG_BCMC);
2454 
2455 	ehea_free_interrupts(dev);
2456 
2457 	port->state = EHEA_PORT_DOWN;
2458 
2459 	ehea_update_bcmc_registrations();
2460 
2461 	ret = ehea_clean_all_portres(port);
2462 	if (ret)
2463 		netdev_info(dev, "Failed freeing resources. ret=%i\n", ret);
2464 
2465 	ehea_update_firmware_handles();
2466 
2467 	return ret;
2468 }
2469 
2470 static int ehea_stop(struct net_device *dev)
2471 {
2472 	int ret;
2473 	struct ehea_port *port = netdev_priv(dev);
2474 
2475 	netif_info(port, ifdown, dev, "disabling port\n");
2476 
2477 	set_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2478 	cancel_work_sync(&port->reset_task);
2479 	cancel_delayed_work_sync(&port->stats_work);
2480 	mutex_lock(&port->port_lock);
2481 	netif_tx_stop_all_queues(dev);
2482 	port_napi_disable(port);
2483 	ret = ehea_down(dev);
2484 	mutex_unlock(&port->port_lock);
2485 	clear_bit(__EHEA_DISABLE_PORT_RESET, &port->flags);
2486 	return ret;
2487 }
2488 
2489 static void ehea_purge_sq(struct ehea_qp *orig_qp)
2490 {
2491 	struct ehea_qp qp = *orig_qp;
2492 	struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2493 	struct ehea_swqe *swqe;
2494 	int wqe_index;
2495 	int i;
2496 
2497 	for (i = 0; i < init_attr->act_nr_send_wqes; i++) {
2498 		swqe = ehea_get_swqe(&qp, &wqe_index);
2499 		swqe->tx_control |= EHEA_SWQE_PURGE;
2500 	}
2501 }
2502 
2503 static void ehea_flush_sq(struct ehea_port *port)
2504 {
2505 	int i;
2506 
2507 	for (i = 0; i < port->num_def_qps; i++) {
2508 		struct ehea_port_res *pr = &port->port_res[i];
2509 		int swqe_max = pr->sq_skba_size - 2 - pr->swqe_ll_count;
2510 		int ret;
2511 
2512 		ret = wait_event_timeout(port->swqe_avail_wq,
2513 			 atomic_read(&pr->swqe_avail) >= swqe_max,
2514 			 msecs_to_jiffies(100));
2515 
2516 		if (!ret) {
2517 			pr_err("WARNING: sq not flushed completely\n");
2518 			break;
2519 		}
2520 	}
2521 }
2522 
2523 static int ehea_stop_qps(struct net_device *dev)
2524 {
2525 	struct ehea_port *port = netdev_priv(dev);
2526 	struct ehea_adapter *adapter = port->adapter;
2527 	struct hcp_modify_qp_cb0 *cb0;
2528 	int ret = -EIO;
2529 	int dret;
2530 	int i;
2531 	u64 hret;
2532 	u64 dummy64 = 0;
2533 	u16 dummy16 = 0;
2534 
2535 	cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2536 	if (!cb0) {
2537 		ret = -ENOMEM;
2538 		goto out;
2539 	}
2540 
2541 	for (i = 0; i < (port->num_def_qps); i++) {
2542 		struct ehea_port_res *pr =  &port->port_res[i];
2543 		struct ehea_qp *qp = pr->qp;
2544 
2545 		/* Purge send queue */
2546 		ehea_purge_sq(qp);
2547 
2548 		/* Disable queue pair */
2549 		hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2550 					    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2551 					    cb0);
2552 		if (hret != H_SUCCESS) {
2553 			pr_err("query_ehea_qp failed (1)\n");
2554 			goto out;
2555 		}
2556 
2557 		cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2558 		cb0->qp_ctl_reg &= ~H_QP_CR_ENABLED;
2559 
2560 		hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2561 					     EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2562 							    1), cb0, &dummy64,
2563 					     &dummy64, &dummy16, &dummy16);
2564 		if (hret != H_SUCCESS) {
2565 			pr_err("modify_ehea_qp failed (1)\n");
2566 			goto out;
2567 		}
2568 
2569 		hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2570 					    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2571 					    cb0);
2572 		if (hret != H_SUCCESS) {
2573 			pr_err("query_ehea_qp failed (2)\n");
2574 			goto out;
2575 		}
2576 
2577 		/* deregister shared memory regions */
2578 		dret = ehea_rem_smrs(pr);
2579 		if (dret) {
2580 			pr_err("unreg shared memory region failed\n");
2581 			goto out;
2582 		}
2583 	}
2584 
2585 	ret = 0;
2586 out:
2587 	free_page((unsigned long)cb0);
2588 
2589 	return ret;
2590 }
2591 
2592 static void ehea_update_rqs(struct ehea_qp *orig_qp, struct ehea_port_res *pr)
2593 {
2594 	struct ehea_qp qp = *orig_qp;
2595 	struct ehea_qp_init_attr *init_attr = &qp.init_attr;
2596 	struct ehea_rwqe *rwqe;
2597 	struct sk_buff **skba_rq2 = pr->rq2_skba.arr;
2598 	struct sk_buff **skba_rq3 = pr->rq3_skba.arr;
2599 	struct sk_buff *skb;
2600 	u32 lkey = pr->recv_mr.lkey;
2601 
2602 
2603 	int i;
2604 	int index;
2605 
2606 	for (i = 0; i < init_attr->act_nr_rwqes_rq2 + 1; i++) {
2607 		rwqe = ehea_get_next_rwqe(&qp, 2);
2608 		rwqe->sg_list[0].l_key = lkey;
2609 		index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2610 		skb = skba_rq2[index];
2611 		if (skb)
2612 			rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2613 	}
2614 
2615 	for (i = 0; i < init_attr->act_nr_rwqes_rq3 + 1; i++) {
2616 		rwqe = ehea_get_next_rwqe(&qp, 3);
2617 		rwqe->sg_list[0].l_key = lkey;
2618 		index = EHEA_BMASK_GET(EHEA_WR_ID_INDEX, rwqe->wr_id);
2619 		skb = skba_rq3[index];
2620 		if (skb)
2621 			rwqe->sg_list[0].vaddr = ehea_map_vaddr(skb->data);
2622 	}
2623 }
2624 
2625 static int ehea_restart_qps(struct net_device *dev)
2626 {
2627 	struct ehea_port *port = netdev_priv(dev);
2628 	struct ehea_adapter *adapter = port->adapter;
2629 	int ret = 0;
2630 	int i;
2631 
2632 	struct hcp_modify_qp_cb0 *cb0;
2633 	u64 hret;
2634 	u64 dummy64 = 0;
2635 	u16 dummy16 = 0;
2636 
2637 	cb0 = (void *)get_zeroed_page(GFP_KERNEL);
2638 	if (!cb0) {
2639 		ret = -ENOMEM;
2640 		goto out;
2641 	}
2642 
2643 	for (i = 0; i < (port->num_def_qps); i++) {
2644 		struct ehea_port_res *pr =  &port->port_res[i];
2645 		struct ehea_qp *qp = pr->qp;
2646 
2647 		ret = ehea_gen_smrs(pr);
2648 		if (ret) {
2649 			netdev_err(dev, "creation of shared memory regions failed\n");
2650 			goto out;
2651 		}
2652 
2653 		ehea_update_rqs(qp, pr);
2654 
2655 		/* Enable queue pair */
2656 		hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2657 					    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2658 					    cb0);
2659 		if (hret != H_SUCCESS) {
2660 			netdev_err(dev, "query_ehea_qp failed (1)\n");
2661 			goto out;
2662 		}
2663 
2664 		cb0->qp_ctl_reg = (cb0->qp_ctl_reg & H_QP_CR_RES_STATE) << 8;
2665 		cb0->qp_ctl_reg |= H_QP_CR_ENABLED;
2666 
2667 		hret = ehea_h_modify_ehea_qp(adapter->handle, 0, qp->fw_handle,
2668 					     EHEA_BMASK_SET(H_QPCB0_QP_CTL_REG,
2669 							    1), cb0, &dummy64,
2670 					     &dummy64, &dummy16, &dummy16);
2671 		if (hret != H_SUCCESS) {
2672 			netdev_err(dev, "modify_ehea_qp failed (1)\n");
2673 			goto out;
2674 		}
2675 
2676 		hret = ehea_h_query_ehea_qp(adapter->handle, 0, qp->fw_handle,
2677 					    EHEA_BMASK_SET(H_QPCB0_ALL, 0xFFFF),
2678 					    cb0);
2679 		if (hret != H_SUCCESS) {
2680 			netdev_err(dev, "query_ehea_qp failed (2)\n");
2681 			goto out;
2682 		}
2683 
2684 		/* refill entire queue */
2685 		ehea_refill_rq1(pr, pr->rq1_skba.index, 0);
2686 		ehea_refill_rq2(pr, 0);
2687 		ehea_refill_rq3(pr, 0);
2688 	}
2689 out:
2690 	free_page((unsigned long)cb0);
2691 
2692 	return ret;
2693 }
2694 
2695 static void ehea_reset_port(struct work_struct *work)
2696 {
2697 	int ret;
2698 	struct ehea_port *port =
2699 		container_of(work, struct ehea_port, reset_task);
2700 	struct net_device *dev = port->netdev;
2701 
2702 	mutex_lock(&dlpar_mem_lock);
2703 	port->resets++;
2704 	mutex_lock(&port->port_lock);
2705 	netif_tx_disable(dev);
2706 
2707 	port_napi_disable(port);
2708 
2709 	ehea_down(dev);
2710 
2711 	ret = ehea_up(dev);
2712 	if (ret)
2713 		goto out;
2714 
2715 	ehea_set_multicast_list(dev);
2716 
2717 	netif_info(port, timer, dev, "reset successful\n");
2718 
2719 	port_napi_enable(port);
2720 
2721 	netif_tx_wake_all_queues(dev);
2722 out:
2723 	mutex_unlock(&port->port_lock);
2724 	mutex_unlock(&dlpar_mem_lock);
2725 }
2726 
2727 static void ehea_rereg_mrs(void)
2728 {
2729 	int ret, i;
2730 	struct ehea_adapter *adapter;
2731 
2732 	pr_info("LPAR memory changed - re-initializing driver\n");
2733 
2734 	list_for_each_entry(adapter, &adapter_list, list)
2735 		if (adapter->active_ports) {
2736 			/* Shutdown all ports */
2737 			for (i = 0; i < EHEA_MAX_PORTS; i++) {
2738 				struct ehea_port *port = adapter->port[i];
2739 				struct net_device *dev;
2740 
2741 				if (!port)
2742 					continue;
2743 
2744 				dev = port->netdev;
2745 
2746 				if (dev->flags & IFF_UP) {
2747 					mutex_lock(&port->port_lock);
2748 					netif_tx_disable(dev);
2749 					ehea_flush_sq(port);
2750 					ret = ehea_stop_qps(dev);
2751 					if (ret) {
2752 						mutex_unlock(&port->port_lock);
2753 						goto out;
2754 					}
2755 					port_napi_disable(port);
2756 					mutex_unlock(&port->port_lock);
2757 				}
2758 				reset_sq_restart_flag(port);
2759 			}
2760 
2761 			/* Unregister old memory region */
2762 			ret = ehea_rem_mr(&adapter->mr);
2763 			if (ret) {
2764 				pr_err("unregister MR failed - driver inoperable!\n");
2765 				goto out;
2766 			}
2767 		}
2768 
2769 	clear_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
2770 
2771 	list_for_each_entry(adapter, &adapter_list, list)
2772 		if (adapter->active_ports) {
2773 			/* Register new memory region */
2774 			ret = ehea_reg_kernel_mr(adapter, &adapter->mr);
2775 			if (ret) {
2776 				pr_err("register MR failed - driver inoperable!\n");
2777 				goto out;
2778 			}
2779 
2780 			/* Restart all ports */
2781 			for (i = 0; i < EHEA_MAX_PORTS; i++) {
2782 				struct ehea_port *port = adapter->port[i];
2783 
2784 				if (port) {
2785 					struct net_device *dev = port->netdev;
2786 
2787 					if (dev->flags & IFF_UP) {
2788 						mutex_lock(&port->port_lock);
2789 						ret = ehea_restart_qps(dev);
2790 						if (!ret) {
2791 							check_sqs(port);
2792 							port_napi_enable(port);
2793 							netif_tx_wake_all_queues(dev);
2794 						} else {
2795 							netdev_err(dev, "Unable to restart QPS\n");
2796 						}
2797 						mutex_unlock(&port->port_lock);
2798 					}
2799 				}
2800 			}
2801 		}
2802 	pr_info("re-initializing driver complete\n");
2803 out:
2804 	return;
2805 }
2806 
2807 static void ehea_tx_watchdog(struct net_device *dev)
2808 {
2809 	struct ehea_port *port = netdev_priv(dev);
2810 
2811 	if (netif_carrier_ok(dev) &&
2812 	    !test_bit(__EHEA_STOP_XFER, &ehea_driver_flags))
2813 		ehea_schedule_port_reset(port);
2814 }
2815 
2816 static int ehea_sense_adapter_attr(struct ehea_adapter *adapter)
2817 {
2818 	struct hcp_query_ehea *cb;
2819 	u64 hret;
2820 	int ret;
2821 
2822 	cb = (void *)get_zeroed_page(GFP_KERNEL);
2823 	if (!cb) {
2824 		ret = -ENOMEM;
2825 		goto out;
2826 	}
2827 
2828 	hret = ehea_h_query_ehea(adapter->handle, cb);
2829 
2830 	if (hret != H_SUCCESS) {
2831 		ret = -EIO;
2832 		goto out_herr;
2833 	}
2834 
2835 	adapter->max_mc_mac = cb->max_mc_mac - 1;
2836 	ret = 0;
2837 
2838 out_herr:
2839 	free_page((unsigned long)cb);
2840 out:
2841 	return ret;
2842 }
2843 
2844 static int ehea_get_jumboframe_status(struct ehea_port *port, int *jumbo)
2845 {
2846 	struct hcp_ehea_port_cb4 *cb4;
2847 	u64 hret;
2848 	int ret = 0;
2849 
2850 	*jumbo = 0;
2851 
2852 	/* (Try to) enable *jumbo frames */
2853 	cb4 = (void *)get_zeroed_page(GFP_KERNEL);
2854 	if (!cb4) {
2855 		pr_err("no mem for cb4\n");
2856 		ret = -ENOMEM;
2857 		goto out;
2858 	} else {
2859 		hret = ehea_h_query_ehea_port(port->adapter->handle,
2860 					      port->logical_port_id,
2861 					      H_PORT_CB4,
2862 					      H_PORT_CB4_JUMBO, cb4);
2863 		if (hret == H_SUCCESS) {
2864 			if (cb4->jumbo_frame)
2865 				*jumbo = 1;
2866 			else {
2867 				cb4->jumbo_frame = 1;
2868 				hret = ehea_h_modify_ehea_port(port->adapter->
2869 							       handle,
2870 							       port->
2871 							       logical_port_id,
2872 							       H_PORT_CB4,
2873 							       H_PORT_CB4_JUMBO,
2874 							       cb4);
2875 				if (hret == H_SUCCESS)
2876 					*jumbo = 1;
2877 			}
2878 		} else
2879 			ret = -EINVAL;
2880 
2881 		free_page((unsigned long)cb4);
2882 	}
2883 out:
2884 	return ret;
2885 }
2886 
2887 static ssize_t ehea_show_port_id(struct device *dev,
2888 				 struct device_attribute *attr, char *buf)
2889 {
2890 	struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2891 	return sprintf(buf, "%d", port->logical_port_id);
2892 }
2893 
2894 static DEVICE_ATTR(log_port_id, 0444, ehea_show_port_id, NULL);
2895 
2896 static void logical_port_release(struct device *dev)
2897 {
2898 	struct ehea_port *port = container_of(dev, struct ehea_port, ofdev.dev);
2899 	of_node_put(port->ofdev.dev.of_node);
2900 }
2901 
2902 static struct device *ehea_register_port(struct ehea_port *port,
2903 					 struct device_node *dn)
2904 {
2905 	int ret;
2906 
2907 	port->ofdev.dev.of_node = of_node_get(dn);
2908 	port->ofdev.dev.parent = &port->adapter->ofdev->dev;
2909 	port->ofdev.dev.bus = &ibmebus_bus_type;
2910 
2911 	dev_set_name(&port->ofdev.dev, "port%d", port_name_cnt++);
2912 	port->ofdev.dev.release = logical_port_release;
2913 
2914 	ret = of_device_register(&port->ofdev);
2915 	if (ret) {
2916 		pr_err("failed to register device. ret=%d\n", ret);
2917 		goto out;
2918 	}
2919 
2920 	ret = device_create_file(&port->ofdev.dev, &dev_attr_log_port_id);
2921 	if (ret) {
2922 		pr_err("failed to register attributes, ret=%d\n", ret);
2923 		goto out_unreg_of_dev;
2924 	}
2925 
2926 	return &port->ofdev.dev;
2927 
2928 out_unreg_of_dev:
2929 	of_device_unregister(&port->ofdev);
2930 out:
2931 	return NULL;
2932 }
2933 
2934 static void ehea_unregister_port(struct ehea_port *port)
2935 {
2936 	device_remove_file(&port->ofdev.dev, &dev_attr_log_port_id);
2937 	of_device_unregister(&port->ofdev);
2938 }
2939 
2940 static const struct net_device_ops ehea_netdev_ops = {
2941 	.ndo_open		= ehea_open,
2942 	.ndo_stop		= ehea_stop,
2943 	.ndo_start_xmit		= ehea_start_xmit,
2944 	.ndo_get_stats64	= ehea_get_stats64,
2945 	.ndo_set_mac_address	= ehea_set_mac_addr,
2946 	.ndo_validate_addr	= eth_validate_addr,
2947 	.ndo_set_rx_mode	= ehea_set_multicast_list,
2948 	.ndo_vlan_rx_add_vid	= ehea_vlan_rx_add_vid,
2949 	.ndo_vlan_rx_kill_vid	= ehea_vlan_rx_kill_vid,
2950 	.ndo_tx_timeout		= ehea_tx_watchdog,
2951 };
2952 
2953 static struct ehea_port *ehea_setup_single_port(struct ehea_adapter *adapter,
2954 					 u32 logical_port_id,
2955 					 struct device_node *dn)
2956 {
2957 	int ret;
2958 	struct net_device *dev;
2959 	struct ehea_port *port;
2960 	struct device *port_dev;
2961 	int jumbo;
2962 
2963 	/* allocate memory for the port structures */
2964 	dev = alloc_etherdev_mq(sizeof(struct ehea_port), EHEA_MAX_PORT_RES);
2965 
2966 	if (!dev) {
2967 		ret = -ENOMEM;
2968 		goto out_err;
2969 	}
2970 
2971 	port = netdev_priv(dev);
2972 
2973 	mutex_init(&port->port_lock);
2974 	port->state = EHEA_PORT_DOWN;
2975 	port->sig_comp_iv = sq_entries / 10;
2976 
2977 	port->adapter = adapter;
2978 	port->netdev = dev;
2979 	port->logical_port_id = logical_port_id;
2980 
2981 	port->msg_enable = netif_msg_init(msg_level, EHEA_MSG_DEFAULT);
2982 
2983 	port->mc_list = kzalloc(sizeof(struct ehea_mc_list), GFP_KERNEL);
2984 	if (!port->mc_list) {
2985 		ret = -ENOMEM;
2986 		goto out_free_ethdev;
2987 	}
2988 
2989 	INIT_LIST_HEAD(&port->mc_list->list);
2990 
2991 	ret = ehea_sense_port_attr(port);
2992 	if (ret)
2993 		goto out_free_mc_list;
2994 
2995 	netif_set_real_num_rx_queues(dev, port->num_def_qps);
2996 	netif_set_real_num_tx_queues(dev, port->num_def_qps);
2997 
2998 	port_dev = ehea_register_port(port, dn);
2999 	if (!port_dev)
3000 		goto out_free_mc_list;
3001 
3002 	SET_NETDEV_DEV(dev, port_dev);
3003 
3004 	/* initialize net_device structure */
3005 	memcpy(dev->dev_addr, &port->mac_addr, ETH_ALEN);
3006 
3007 	dev->netdev_ops = &ehea_netdev_ops;
3008 	ehea_set_ethtool_ops(dev);
3009 
3010 	dev->hw_features = NETIF_F_SG | NETIF_F_TSO |
3011 		      NETIF_F_IP_CSUM | NETIF_F_HW_VLAN_CTAG_TX;
3012 	dev->features = NETIF_F_SG | NETIF_F_TSO |
3013 		      NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
3014 		      NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3015 		      NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM;
3016 	dev->vlan_features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HIGHDMA |
3017 			NETIF_F_IP_CSUM;
3018 	dev->watchdog_timeo = EHEA_WATCH_DOG_TIMEOUT;
3019 
3020 	/* MTU range: 68 - 9022 */
3021 	dev->min_mtu = ETH_MIN_MTU;
3022 	dev->max_mtu = EHEA_MAX_PACKET_SIZE;
3023 
3024 	INIT_WORK(&port->reset_task, ehea_reset_port);
3025 	INIT_DELAYED_WORK(&port->stats_work, ehea_update_stats);
3026 
3027 	init_waitqueue_head(&port->swqe_avail_wq);
3028 	init_waitqueue_head(&port->restart_wq);
3029 
3030 	ret = register_netdev(dev);
3031 	if (ret) {
3032 		pr_err("register_netdev failed. ret=%d\n", ret);
3033 		goto out_unreg_port;
3034 	}
3035 
3036 	ret = ehea_get_jumboframe_status(port, &jumbo);
3037 	if (ret)
3038 		netdev_err(dev, "failed determining jumbo frame status\n");
3039 
3040 	netdev_info(dev, "Jumbo frames are %sabled\n",
3041 		    jumbo == 1 ? "en" : "dis");
3042 
3043 	adapter->active_ports++;
3044 
3045 	return port;
3046 
3047 out_unreg_port:
3048 	ehea_unregister_port(port);
3049 
3050 out_free_mc_list:
3051 	kfree(port->mc_list);
3052 
3053 out_free_ethdev:
3054 	free_netdev(dev);
3055 
3056 out_err:
3057 	pr_err("setting up logical port with id=%d failed, ret=%d\n",
3058 	       logical_port_id, ret);
3059 	return NULL;
3060 }
3061 
3062 static void ehea_shutdown_single_port(struct ehea_port *port)
3063 {
3064 	struct ehea_adapter *adapter = port->adapter;
3065 
3066 	cancel_work_sync(&port->reset_task);
3067 	cancel_delayed_work_sync(&port->stats_work);
3068 	unregister_netdev(port->netdev);
3069 	ehea_unregister_port(port);
3070 	kfree(port->mc_list);
3071 	free_netdev(port->netdev);
3072 	adapter->active_ports--;
3073 }
3074 
3075 static int ehea_setup_ports(struct ehea_adapter *adapter)
3076 {
3077 	struct device_node *lhea_dn;
3078 	struct device_node *eth_dn = NULL;
3079 
3080 	const u32 *dn_log_port_id;
3081 	int i = 0;
3082 
3083 	lhea_dn = adapter->ofdev->dev.of_node;
3084 	while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3085 
3086 		dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3087 						 NULL);
3088 		if (!dn_log_port_id) {
3089 			pr_err("bad device node: eth_dn name=%pOF\n", eth_dn);
3090 			continue;
3091 		}
3092 
3093 		if (ehea_add_adapter_mr(adapter)) {
3094 			pr_err("creating MR failed\n");
3095 			of_node_put(eth_dn);
3096 			return -EIO;
3097 		}
3098 
3099 		adapter->port[i] = ehea_setup_single_port(adapter,
3100 							  *dn_log_port_id,
3101 							  eth_dn);
3102 		if (adapter->port[i])
3103 			netdev_info(adapter->port[i]->netdev,
3104 				    "logical port id #%d\n", *dn_log_port_id);
3105 		else
3106 			ehea_remove_adapter_mr(adapter);
3107 
3108 		i++;
3109 	}
3110 	return 0;
3111 }
3112 
3113 static struct device_node *ehea_get_eth_dn(struct ehea_adapter *adapter,
3114 					   u32 logical_port_id)
3115 {
3116 	struct device_node *lhea_dn;
3117 	struct device_node *eth_dn = NULL;
3118 	const u32 *dn_log_port_id;
3119 
3120 	lhea_dn = adapter->ofdev->dev.of_node;
3121 	while ((eth_dn = of_get_next_child(lhea_dn, eth_dn))) {
3122 
3123 		dn_log_port_id = of_get_property(eth_dn, "ibm,hea-port-no",
3124 						 NULL);
3125 		if (dn_log_port_id)
3126 			if (*dn_log_port_id == logical_port_id)
3127 				return eth_dn;
3128 	}
3129 
3130 	return NULL;
3131 }
3132 
3133 static ssize_t ehea_probe_port(struct device *dev,
3134 			       struct device_attribute *attr,
3135 			       const char *buf, size_t count)
3136 {
3137 	struct ehea_adapter *adapter = dev_get_drvdata(dev);
3138 	struct ehea_port *port;
3139 	struct device_node *eth_dn = NULL;
3140 	int i;
3141 
3142 	u32 logical_port_id;
3143 
3144 	sscanf(buf, "%d", &logical_port_id);
3145 
3146 	port = ehea_get_port(adapter, logical_port_id);
3147 
3148 	if (port) {
3149 		netdev_info(port->netdev, "adding port with logical port id=%d failed: port already configured\n",
3150 			    logical_port_id);
3151 		return -EINVAL;
3152 	}
3153 
3154 	eth_dn = ehea_get_eth_dn(adapter, logical_port_id);
3155 
3156 	if (!eth_dn) {
3157 		pr_info("no logical port with id %d found\n", logical_port_id);
3158 		return -EINVAL;
3159 	}
3160 
3161 	if (ehea_add_adapter_mr(adapter)) {
3162 		pr_err("creating MR failed\n");
3163 		of_node_put(eth_dn);
3164 		return -EIO;
3165 	}
3166 
3167 	port = ehea_setup_single_port(adapter, logical_port_id, eth_dn);
3168 
3169 	of_node_put(eth_dn);
3170 
3171 	if (port) {
3172 		for (i = 0; i < EHEA_MAX_PORTS; i++)
3173 			if (!adapter->port[i]) {
3174 				adapter->port[i] = port;
3175 				break;
3176 			}
3177 
3178 		netdev_info(port->netdev, "added: (logical port id=%d)\n",
3179 			    logical_port_id);
3180 	} else {
3181 		ehea_remove_adapter_mr(adapter);
3182 		return -EIO;
3183 	}
3184 
3185 	return (ssize_t) count;
3186 }
3187 
3188 static ssize_t ehea_remove_port(struct device *dev,
3189 				struct device_attribute *attr,
3190 				const char *buf, size_t count)
3191 {
3192 	struct ehea_adapter *adapter = dev_get_drvdata(dev);
3193 	struct ehea_port *port;
3194 	int i;
3195 	u32 logical_port_id;
3196 
3197 	sscanf(buf, "%d", &logical_port_id);
3198 
3199 	port = ehea_get_port(adapter, logical_port_id);
3200 
3201 	if (port) {
3202 		netdev_info(port->netdev, "removed: (logical port id=%d)\n",
3203 			    logical_port_id);
3204 
3205 		ehea_shutdown_single_port(port);
3206 
3207 		for (i = 0; i < EHEA_MAX_PORTS; i++)
3208 			if (adapter->port[i] == port) {
3209 				adapter->port[i] = NULL;
3210 				break;
3211 			}
3212 	} else {
3213 		pr_err("removing port with logical port id=%d failed. port not configured.\n",
3214 		       logical_port_id);
3215 		return -EINVAL;
3216 	}
3217 
3218 	ehea_remove_adapter_mr(adapter);
3219 
3220 	return (ssize_t) count;
3221 }
3222 
3223 static DEVICE_ATTR(probe_port, 0200, NULL, ehea_probe_port);
3224 static DEVICE_ATTR(remove_port, 0200, NULL, ehea_remove_port);
3225 
3226 static int ehea_create_device_sysfs(struct platform_device *dev)
3227 {
3228 	int ret = device_create_file(&dev->dev, &dev_attr_probe_port);
3229 	if (ret)
3230 		goto out;
3231 
3232 	ret = device_create_file(&dev->dev, &dev_attr_remove_port);
3233 out:
3234 	return ret;
3235 }
3236 
3237 static void ehea_remove_device_sysfs(struct platform_device *dev)
3238 {
3239 	device_remove_file(&dev->dev, &dev_attr_probe_port);
3240 	device_remove_file(&dev->dev, &dev_attr_remove_port);
3241 }
3242 
3243 static int ehea_reboot_notifier(struct notifier_block *nb,
3244 				unsigned long action, void *unused)
3245 {
3246 	if (action == SYS_RESTART) {
3247 		pr_info("Reboot: freeing all eHEA resources\n");
3248 		ibmebus_unregister_driver(&ehea_driver);
3249 	}
3250 	return NOTIFY_DONE;
3251 }
3252 
3253 static struct notifier_block ehea_reboot_nb = {
3254 	.notifier_call = ehea_reboot_notifier,
3255 };
3256 
3257 static int ehea_mem_notifier(struct notifier_block *nb,
3258 			     unsigned long action, void *data)
3259 {
3260 	int ret = NOTIFY_BAD;
3261 	struct memory_notify *arg = data;
3262 
3263 	mutex_lock(&dlpar_mem_lock);
3264 
3265 	switch (action) {
3266 	case MEM_CANCEL_OFFLINE:
3267 		pr_info("memory offlining canceled");
3268 		/* Fall through: re-add canceled memory block */
3269 
3270 	case MEM_ONLINE:
3271 		pr_info("memory is going online");
3272 		set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3273 		if (ehea_add_sect_bmap(arg->start_pfn, arg->nr_pages))
3274 			goto out_unlock;
3275 		ehea_rereg_mrs();
3276 		break;
3277 
3278 	case MEM_GOING_OFFLINE:
3279 		pr_info("memory is going offline");
3280 		set_bit(__EHEA_STOP_XFER, &ehea_driver_flags);
3281 		if (ehea_rem_sect_bmap(arg->start_pfn, arg->nr_pages))
3282 			goto out_unlock;
3283 		ehea_rereg_mrs();
3284 		break;
3285 
3286 	default:
3287 		break;
3288 	}
3289 
3290 	ehea_update_firmware_handles();
3291 	ret = NOTIFY_OK;
3292 
3293 out_unlock:
3294 	mutex_unlock(&dlpar_mem_lock);
3295 	return ret;
3296 }
3297 
3298 static struct notifier_block ehea_mem_nb = {
3299 	.notifier_call = ehea_mem_notifier,
3300 };
3301 
3302 static void ehea_crash_handler(void)
3303 {
3304 	int i;
3305 
3306 	if (ehea_fw_handles.arr)
3307 		for (i = 0; i < ehea_fw_handles.num_entries; i++)
3308 			ehea_h_free_resource(ehea_fw_handles.arr[i].adh,
3309 					     ehea_fw_handles.arr[i].fwh,
3310 					     FORCE_FREE);
3311 
3312 	if (ehea_bcmc_regs.arr)
3313 		for (i = 0; i < ehea_bcmc_regs.num_entries; i++)
3314 			ehea_h_reg_dereg_bcmc(ehea_bcmc_regs.arr[i].adh,
3315 					      ehea_bcmc_regs.arr[i].port_id,
3316 					      ehea_bcmc_regs.arr[i].reg_type,
3317 					      ehea_bcmc_regs.arr[i].macaddr,
3318 					      0, H_DEREG_BCMC);
3319 }
3320 
3321 static atomic_t ehea_memory_hooks_registered;
3322 
3323 /* Register memory hooks on probe of first adapter */
3324 static int ehea_register_memory_hooks(void)
3325 {
3326 	int ret = 0;
3327 
3328 	if (atomic_inc_return(&ehea_memory_hooks_registered) > 1)
3329 		return 0;
3330 
3331 	ret = ehea_create_busmap();
3332 	if (ret) {
3333 		pr_info("ehea_create_busmap failed\n");
3334 		goto out;
3335 	}
3336 
3337 	ret = register_reboot_notifier(&ehea_reboot_nb);
3338 	if (ret) {
3339 		pr_info("register_reboot_notifier failed\n");
3340 		goto out;
3341 	}
3342 
3343 	ret = register_memory_notifier(&ehea_mem_nb);
3344 	if (ret) {
3345 		pr_info("register_memory_notifier failed\n");
3346 		goto out2;
3347 	}
3348 
3349 	ret = crash_shutdown_register(ehea_crash_handler);
3350 	if (ret) {
3351 		pr_info("crash_shutdown_register failed\n");
3352 		goto out3;
3353 	}
3354 
3355 	return 0;
3356 
3357 out3:
3358 	unregister_memory_notifier(&ehea_mem_nb);
3359 out2:
3360 	unregister_reboot_notifier(&ehea_reboot_nb);
3361 out:
3362 	atomic_dec(&ehea_memory_hooks_registered);
3363 	return ret;
3364 }
3365 
3366 static void ehea_unregister_memory_hooks(void)
3367 {
3368 	/* Only remove the hooks if we've registered them */
3369 	if (atomic_read(&ehea_memory_hooks_registered) == 0)
3370 		return;
3371 
3372 	unregister_reboot_notifier(&ehea_reboot_nb);
3373 	if (crash_shutdown_unregister(ehea_crash_handler))
3374 		pr_info("failed unregistering crash handler\n");
3375 	unregister_memory_notifier(&ehea_mem_nb);
3376 }
3377 
3378 static int ehea_probe_adapter(struct platform_device *dev)
3379 {
3380 	struct ehea_adapter *adapter;
3381 	const u64 *adapter_handle;
3382 	int ret;
3383 	int i;
3384 
3385 	ret = ehea_register_memory_hooks();
3386 	if (ret)
3387 		return ret;
3388 
3389 	if (!dev || !dev->dev.of_node) {
3390 		pr_err("Invalid ibmebus device probed\n");
3391 		return -EINVAL;
3392 	}
3393 
3394 	adapter = devm_kzalloc(&dev->dev, sizeof(*adapter), GFP_KERNEL);
3395 	if (!adapter) {
3396 		ret = -ENOMEM;
3397 		dev_err(&dev->dev, "no mem for ehea_adapter\n");
3398 		goto out;
3399 	}
3400 
3401 	list_add(&adapter->list, &adapter_list);
3402 
3403 	adapter->ofdev = dev;
3404 
3405 	adapter_handle = of_get_property(dev->dev.of_node, "ibm,hea-handle",
3406 					 NULL);
3407 	if (adapter_handle)
3408 		adapter->handle = *adapter_handle;
3409 
3410 	if (!adapter->handle) {
3411 		dev_err(&dev->dev, "failed getting handle for adapter"
3412 			" '%pOF'\n", dev->dev.of_node);
3413 		ret = -ENODEV;
3414 		goto out_free_ad;
3415 	}
3416 
3417 	adapter->pd = EHEA_PD_ID;
3418 
3419 	platform_set_drvdata(dev, adapter);
3420 
3421 
3422 	/* initialize adapter and ports */
3423 	/* get adapter properties */
3424 	ret = ehea_sense_adapter_attr(adapter);
3425 	if (ret) {
3426 		dev_err(&dev->dev, "sense_adapter_attr failed: %d\n", ret);
3427 		goto out_free_ad;
3428 	}
3429 
3430 	adapter->neq = ehea_create_eq(adapter,
3431 				      EHEA_NEQ, EHEA_MAX_ENTRIES_EQ, 1);
3432 	if (!adapter->neq) {
3433 		ret = -EIO;
3434 		dev_err(&dev->dev, "NEQ creation failed\n");
3435 		goto out_free_ad;
3436 	}
3437 
3438 	tasklet_init(&adapter->neq_tasklet, ehea_neq_tasklet,
3439 		     (unsigned long)adapter);
3440 
3441 	ret = ehea_create_device_sysfs(dev);
3442 	if (ret)
3443 		goto out_kill_eq;
3444 
3445 	ret = ehea_setup_ports(adapter);
3446 	if (ret) {
3447 		dev_err(&dev->dev, "setup_ports failed\n");
3448 		goto out_rem_dev_sysfs;
3449 	}
3450 
3451 	ret = ibmebus_request_irq(adapter->neq->attr.ist1,
3452 				  ehea_interrupt_neq, 0,
3453 				  "ehea_neq", adapter);
3454 	if (ret) {
3455 		dev_err(&dev->dev, "requesting NEQ IRQ failed\n");
3456 		goto out_shutdown_ports;
3457 	}
3458 
3459 	/* Handle any events that might be pending. */
3460 	tasklet_hi_schedule(&adapter->neq_tasklet);
3461 
3462 	ret = 0;
3463 	goto out;
3464 
3465 out_shutdown_ports:
3466 	for (i = 0; i < EHEA_MAX_PORTS; i++)
3467 		if (adapter->port[i]) {
3468 			ehea_shutdown_single_port(adapter->port[i]);
3469 			adapter->port[i] = NULL;
3470 		}
3471 
3472 out_rem_dev_sysfs:
3473 	ehea_remove_device_sysfs(dev);
3474 
3475 out_kill_eq:
3476 	ehea_destroy_eq(adapter->neq);
3477 
3478 out_free_ad:
3479 	list_del(&adapter->list);
3480 
3481 out:
3482 	ehea_update_firmware_handles();
3483 
3484 	return ret;
3485 }
3486 
3487 static int ehea_remove(struct platform_device *dev)
3488 {
3489 	struct ehea_adapter *adapter = platform_get_drvdata(dev);
3490 	int i;
3491 
3492 	for (i = 0; i < EHEA_MAX_PORTS; i++)
3493 		if (adapter->port[i]) {
3494 			ehea_shutdown_single_port(adapter->port[i]);
3495 			adapter->port[i] = NULL;
3496 		}
3497 
3498 	ehea_remove_device_sysfs(dev);
3499 
3500 	ibmebus_free_irq(adapter->neq->attr.ist1, adapter);
3501 	tasklet_kill(&adapter->neq_tasklet);
3502 
3503 	ehea_destroy_eq(adapter->neq);
3504 	ehea_remove_adapter_mr(adapter);
3505 	list_del(&adapter->list);
3506 
3507 	ehea_update_firmware_handles();
3508 
3509 	return 0;
3510 }
3511 
3512 static int check_module_parm(void)
3513 {
3514 	int ret = 0;
3515 
3516 	if ((rq1_entries < EHEA_MIN_ENTRIES_QP) ||
3517 	    (rq1_entries > EHEA_MAX_ENTRIES_RQ1)) {
3518 		pr_info("Bad parameter: rq1_entries\n");
3519 		ret = -EINVAL;
3520 	}
3521 	if ((rq2_entries < EHEA_MIN_ENTRIES_QP) ||
3522 	    (rq2_entries > EHEA_MAX_ENTRIES_RQ2)) {
3523 		pr_info("Bad parameter: rq2_entries\n");
3524 		ret = -EINVAL;
3525 	}
3526 	if ((rq3_entries < EHEA_MIN_ENTRIES_QP) ||
3527 	    (rq3_entries > EHEA_MAX_ENTRIES_RQ3)) {
3528 		pr_info("Bad parameter: rq3_entries\n");
3529 		ret = -EINVAL;
3530 	}
3531 	if ((sq_entries < EHEA_MIN_ENTRIES_QP) ||
3532 	    (sq_entries > EHEA_MAX_ENTRIES_SQ)) {
3533 		pr_info("Bad parameter: sq_entries\n");
3534 		ret = -EINVAL;
3535 	}
3536 
3537 	return ret;
3538 }
3539 
3540 static ssize_t capabilities_show(struct device_driver *drv, char *buf)
3541 {
3542 	return sprintf(buf, "%d", EHEA_CAPABILITIES);
3543 }
3544 
3545 static DRIVER_ATTR_RO(capabilities);
3546 
3547 static int __init ehea_module_init(void)
3548 {
3549 	int ret;
3550 
3551 	pr_info("IBM eHEA ethernet device driver (Release %s)\n", DRV_VERSION);
3552 
3553 	memset(&ehea_fw_handles, 0, sizeof(ehea_fw_handles));
3554 	memset(&ehea_bcmc_regs, 0, sizeof(ehea_bcmc_regs));
3555 
3556 	mutex_init(&ehea_fw_handles.lock);
3557 	spin_lock_init(&ehea_bcmc_regs.lock);
3558 
3559 	ret = check_module_parm();
3560 	if (ret)
3561 		goto out;
3562 
3563 	ret = ibmebus_register_driver(&ehea_driver);
3564 	if (ret) {
3565 		pr_err("failed registering eHEA device driver on ebus\n");
3566 		goto out;
3567 	}
3568 
3569 	ret = driver_create_file(&ehea_driver.driver,
3570 				 &driver_attr_capabilities);
3571 	if (ret) {
3572 		pr_err("failed to register capabilities attribute, ret=%d\n",
3573 		       ret);
3574 		goto out2;
3575 	}
3576 
3577 	return ret;
3578 
3579 out2:
3580 	ibmebus_unregister_driver(&ehea_driver);
3581 out:
3582 	return ret;
3583 }
3584 
3585 static void __exit ehea_module_exit(void)
3586 {
3587 	driver_remove_file(&ehea_driver.driver, &driver_attr_capabilities);
3588 	ibmebus_unregister_driver(&ehea_driver);
3589 	ehea_unregister_memory_hooks();
3590 	kfree(ehea_fw_handles.arr);
3591 	kfree(ehea_bcmc_regs.arr);
3592 	ehea_destroy_busmap();
3593 }
3594 
3595 module_init(ehea_module_init);
3596 module_exit(ehea_module_exit);
3597