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