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