xref: /openbmc/linux/drivers/s390/net/qeth_l2_main.c (revision ca1cfc3f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2009
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *		 Frank Pavlic <fpavlic@de.ibm.com>,
6  *		 Thomas Spatzier <tspat@de.ibm.com>,
7  *		 Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9 
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_bridge.h>
21 #include <linux/list.h>
22 #include <linux/hash.h>
23 #include <linux/hashtable.h>
24 #include <net/switchdev.h>
25 #include <asm/chsc.h>
26 #include <asm/css_chars.h>
27 #include <asm/setup.h>
28 #include "qeth_core.h"
29 #include "qeth_l2.h"
30 
31 static int qeth_l2_setdelmac_makerc(struct qeth_card *card, u16 retcode)
32 {
33 	int rc;
34 
35 	if (retcode)
36 		QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
37 	switch (retcode) {
38 	case IPA_RC_SUCCESS:
39 		rc = 0;
40 		break;
41 	case IPA_RC_L2_UNSUPPORTED_CMD:
42 		rc = -EOPNOTSUPP;
43 		break;
44 	case IPA_RC_L2_ADDR_TABLE_FULL:
45 		rc = -ENOSPC;
46 		break;
47 	case IPA_RC_L2_DUP_MAC:
48 	case IPA_RC_L2_DUP_LAYER3_MAC:
49 		rc = -EADDRINUSE;
50 		break;
51 	case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
52 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
53 		rc = -EADDRNOTAVAIL;
54 		break;
55 	case IPA_RC_L2_MAC_NOT_FOUND:
56 		rc = -ENOENT;
57 		break;
58 	default:
59 		rc = -EIO;
60 		break;
61 	}
62 	return rc;
63 }
64 
65 static int qeth_l2_send_setdelmac_cb(struct qeth_card *card,
66 				     struct qeth_reply *reply,
67 				     unsigned long data)
68 {
69 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
70 
71 	return qeth_l2_setdelmac_makerc(card, cmd->hdr.return_code);
72 }
73 
74 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
75 			   enum qeth_ipa_cmds ipacmd)
76 {
77 	struct qeth_ipa_cmd *cmd;
78 	struct qeth_cmd_buffer *iob;
79 
80 	QETH_CARD_TEXT(card, 2, "L2sdmac");
81 	iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
82 				 IPA_DATA_SIZEOF(setdelmac));
83 	if (!iob)
84 		return -ENOMEM;
85 	cmd = __ipa_cmd(iob);
86 	cmd->data.setdelmac.mac_length = ETH_ALEN;
87 	ether_addr_copy(cmd->data.setdelmac.mac, mac);
88 	return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelmac_cb, NULL);
89 }
90 
91 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
92 {
93 	int rc;
94 
95 	QETH_CARD_TEXT(card, 2, "L2Setmac");
96 	rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC);
97 	if (rc == 0) {
98 		dev_info(&card->gdev->dev,
99 			 "MAC address %pM successfully registered\n", mac);
100 	} else {
101 		switch (rc) {
102 		case -EADDRINUSE:
103 			dev_warn(&card->gdev->dev,
104 				"MAC address %pM already exists\n", mac);
105 			break;
106 		case -EADDRNOTAVAIL:
107 			dev_warn(&card->gdev->dev,
108 				"MAC address %pM is not authorized\n", mac);
109 			break;
110 		}
111 	}
112 	return rc;
113 }
114 
115 static int qeth_l2_write_mac(struct qeth_card *card, u8 *mac)
116 {
117 	enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
118 					IPA_CMD_SETGMAC : IPA_CMD_SETVMAC;
119 	int rc;
120 
121 	QETH_CARD_TEXT(card, 2, "L2Wmac");
122 	rc = qeth_l2_send_setdelmac(card, mac, cmd);
123 	if (rc == -EADDRINUSE)
124 		QETH_DBF_MESSAGE(2, "MAC already registered on device %x\n",
125 				 CARD_DEVID(card));
126 	else if (rc)
127 		QETH_DBF_MESSAGE(2, "Failed to register MAC on device %x: %d\n",
128 				 CARD_DEVID(card), rc);
129 	return rc;
130 }
131 
132 static int qeth_l2_remove_mac(struct qeth_card *card, u8 *mac)
133 {
134 	enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
135 					IPA_CMD_DELGMAC : IPA_CMD_DELVMAC;
136 	int rc;
137 
138 	QETH_CARD_TEXT(card, 2, "L2Rmac");
139 	rc = qeth_l2_send_setdelmac(card, mac, cmd);
140 	if (rc)
141 		QETH_DBF_MESSAGE(2, "Failed to delete MAC on device %u: %d\n",
142 				 CARD_DEVID(card), rc);
143 	return rc;
144 }
145 
146 static void qeth_l2_drain_rx_mode_cache(struct qeth_card *card)
147 {
148 	struct qeth_mac *mac;
149 	struct hlist_node *tmp;
150 	int i;
151 
152 	hash_for_each_safe(card->rx_mode_addrs, i, tmp, mac, hnode) {
153 		hash_del(&mac->hnode);
154 		kfree(mac);
155 	}
156 }
157 
158 static void qeth_l2_fill_header(struct qeth_qdio_out_q *queue,
159 				struct qeth_hdr *hdr, struct sk_buff *skb,
160 				int ipv, unsigned int data_len)
161 {
162 	int cast_type = qeth_get_ether_cast_type(skb);
163 	struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
164 
165 	hdr->hdr.l2.pkt_length = data_len;
166 
167 	if (skb_is_gso(skb)) {
168 		hdr->hdr.l2.id = QETH_HEADER_TYPE_L2_TSO;
169 	} else {
170 		hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
171 		if (skb->ip_summed == CHECKSUM_PARTIAL)
172 			qeth_tx_csum(skb, &hdr->hdr.l2.flags[1], ipv);
173 	}
174 
175 	/* set byte byte 3 to casting flags */
176 	if (cast_type == RTN_MULTICAST)
177 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
178 	else if (cast_type == RTN_BROADCAST)
179 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
180 	else
181 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
182 
183 	/* VSWITCH relies on the VLAN
184 	 * information to be present in
185 	 * the QDIO header */
186 	if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
187 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
188 		hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
189 	}
190 }
191 
192 static int qeth_l2_setdelvlan_makerc(struct qeth_card *card, u16 retcode)
193 {
194 	if (retcode)
195 		QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
196 
197 	switch (retcode) {
198 	case IPA_RC_SUCCESS:
199 		return 0;
200 	case IPA_RC_L2_INVALID_VLAN_ID:
201 		return -EINVAL;
202 	case IPA_RC_L2_DUP_VLAN_ID:
203 		return -EEXIST;
204 	case IPA_RC_L2_VLAN_ID_NOT_FOUND:
205 		return -ENOENT;
206 	case IPA_RC_L2_VLAN_ID_NOT_ALLOWED:
207 		return -EPERM;
208 	default:
209 		return -EIO;
210 	}
211 }
212 
213 static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
214 				      struct qeth_reply *reply,
215 				      unsigned long data)
216 {
217 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
218 
219 	QETH_CARD_TEXT(card, 2, "L2sdvcb");
220 	if (cmd->hdr.return_code) {
221 		QETH_DBF_MESSAGE(2, "Error in processing VLAN %u on device %x: %#x.\n",
222 				 cmd->data.setdelvlan.vlan_id,
223 				 CARD_DEVID(card), cmd->hdr.return_code);
224 		QETH_CARD_TEXT_(card, 2, "L2VL%4x", cmd->hdr.command);
225 	}
226 	return qeth_l2_setdelvlan_makerc(card, cmd->hdr.return_code);
227 }
228 
229 static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
230 				   enum qeth_ipa_cmds ipacmd)
231 {
232 	struct qeth_ipa_cmd *cmd;
233 	struct qeth_cmd_buffer *iob;
234 
235 	QETH_CARD_TEXT_(card, 4, "L2sdv%x", ipacmd);
236 	iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
237 				 IPA_DATA_SIZEOF(setdelvlan));
238 	if (!iob)
239 		return -ENOMEM;
240 	cmd = __ipa_cmd(iob);
241 	cmd->data.setdelvlan.vlan_id = i;
242 	return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelvlan_cb, NULL);
243 }
244 
245 static int qeth_l2_vlan_rx_add_vid(struct net_device *dev,
246 				   __be16 proto, u16 vid)
247 {
248 	struct qeth_card *card = dev->ml_priv;
249 
250 	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
251 	if (!vid)
252 		return 0;
253 
254 	return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
255 }
256 
257 static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev,
258 				    __be16 proto, u16 vid)
259 {
260 	struct qeth_card *card = dev->ml_priv;
261 
262 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
263 	if (!vid)
264 		return 0;
265 
266 	return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
267 }
268 
269 static void qeth_l2_set_pnso_mode(struct qeth_card *card,
270 				  enum qeth_pnso_mode mode)
271 {
272 	spin_lock_irq(get_ccwdev_lock(CARD_RDEV(card)));
273 	WRITE_ONCE(card->info.pnso_mode, mode);
274 	spin_unlock_irq(get_ccwdev_lock(CARD_RDEV(card)));
275 
276 	if (mode == QETH_PNSO_NONE)
277 		drain_workqueue(card->event_wq);
278 }
279 
280 static void qeth_l2_dev2br_fdb_flush(struct qeth_card *card)
281 {
282 	struct switchdev_notifier_fdb_info info;
283 
284 	QETH_CARD_TEXT(card, 2, "fdbflush");
285 
286 	info.addr = NULL;
287 	/* flush all VLANs: */
288 	info.vid = 0;
289 	info.added_by_user = false;
290 	info.offloaded = true;
291 
292 	call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
293 				 card->dev, &info.info, NULL);
294 }
295 
296 static int qeth_l2_request_initial_mac(struct qeth_card *card)
297 {
298 	int rc = 0;
299 
300 	QETH_CARD_TEXT(card, 2, "l2reqmac");
301 
302 	if (MACHINE_IS_VM) {
303 		rc = qeth_vm_request_mac(card);
304 		if (!rc)
305 			goto out;
306 		QETH_DBF_MESSAGE(2, "z/VM MAC Service failed on device %x: %#x\n",
307 				 CARD_DEVID(card), rc);
308 		QETH_CARD_TEXT_(card, 2, "err%04x", rc);
309 		/* fall back to alternative mechanism: */
310 	}
311 
312 	if (!IS_OSN(card)) {
313 		rc = qeth_setadpparms_change_macaddr(card);
314 		if (!rc)
315 			goto out;
316 		QETH_DBF_MESSAGE(2, "READ_MAC Assist failed on device %x: %#x\n",
317 				 CARD_DEVID(card), rc);
318 		QETH_CARD_TEXT_(card, 2, "1err%04x", rc);
319 		/* fall back once more: */
320 	}
321 
322 	/* some devices don't support a custom MAC address: */
323 	if (IS_OSM(card) || IS_OSX(card))
324 		return (rc) ? rc : -EADDRNOTAVAIL;
325 	eth_hw_addr_random(card->dev);
326 
327 out:
328 	QETH_CARD_HEX(card, 2, card->dev->dev_addr, card->dev->addr_len);
329 	return 0;
330 }
331 
332 static void qeth_l2_register_dev_addr(struct qeth_card *card)
333 {
334 	if (!is_valid_ether_addr(card->dev->dev_addr))
335 		qeth_l2_request_initial_mac(card);
336 
337 	if (!IS_OSN(card) && !qeth_l2_send_setmac(card, card->dev->dev_addr))
338 		card->info.dev_addr_is_registered = 1;
339 	else
340 		card->info.dev_addr_is_registered = 0;
341 }
342 
343 static int qeth_l2_validate_addr(struct net_device *dev)
344 {
345 	struct qeth_card *card = dev->ml_priv;
346 
347 	if (card->info.dev_addr_is_registered)
348 		return eth_validate_addr(dev);
349 
350 	QETH_CARD_TEXT(card, 4, "nomacadr");
351 	return -EPERM;
352 }
353 
354 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
355 {
356 	struct sockaddr *addr = p;
357 	struct qeth_card *card = dev->ml_priv;
358 	u8 old_addr[ETH_ALEN];
359 	int rc = 0;
360 
361 	QETH_CARD_TEXT(card, 3, "setmac");
362 
363 	if (IS_OSM(card) || IS_OSX(card)) {
364 		QETH_CARD_TEXT(card, 3, "setmcTYP");
365 		return -EOPNOTSUPP;
366 	}
367 	QETH_CARD_HEX(card, 3, addr->sa_data, ETH_ALEN);
368 	if (!is_valid_ether_addr(addr->sa_data))
369 		return -EADDRNOTAVAIL;
370 
371 	/* don't register the same address twice */
372 	if (ether_addr_equal_64bits(dev->dev_addr, addr->sa_data) &&
373 	    card->info.dev_addr_is_registered)
374 		return 0;
375 
376 	/* add the new address, switch over, drop the old */
377 	rc = qeth_l2_send_setmac(card, addr->sa_data);
378 	if (rc)
379 		return rc;
380 	ether_addr_copy(old_addr, dev->dev_addr);
381 	ether_addr_copy(dev->dev_addr, addr->sa_data);
382 
383 	if (card->info.dev_addr_is_registered)
384 		qeth_l2_remove_mac(card, old_addr);
385 	card->info.dev_addr_is_registered = 1;
386 	return 0;
387 }
388 
389 static void qeth_l2_promisc_to_bridge(struct qeth_card *card, bool enable)
390 {
391 	int role;
392 	int rc;
393 
394 	QETH_CARD_TEXT(card, 3, "pmisc2br");
395 
396 	if (enable) {
397 		if (card->options.sbp.reflect_promisc_primary)
398 			role = QETH_SBP_ROLE_PRIMARY;
399 		else
400 			role = QETH_SBP_ROLE_SECONDARY;
401 	} else
402 		role = QETH_SBP_ROLE_NONE;
403 
404 	rc = qeth_bridgeport_setrole(card, role);
405 	QETH_CARD_TEXT_(card, 2, "bpm%c%04x", enable ? '+' : '-', rc);
406 	if (!rc) {
407 		card->options.sbp.role = role;
408 		card->info.promisc_mode = enable;
409 	}
410 }
411 
412 static void qeth_l2_set_promisc_mode(struct qeth_card *card)
413 {
414 	bool enable = card->dev->flags & IFF_PROMISC;
415 
416 	if (card->info.promisc_mode == enable)
417 		return;
418 
419 	if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE)) {
420 		qeth_setadp_promisc_mode(card, enable);
421 	} else {
422 		mutex_lock(&card->sbp_lock);
423 		if (card->options.sbp.reflect_promisc)
424 			qeth_l2_promisc_to_bridge(card, enable);
425 		mutex_unlock(&card->sbp_lock);
426 	}
427 }
428 
429 /* New MAC address is added to the hash table and marked to be written on card
430  * only if there is not in the hash table storage already
431  *
432 */
433 static void qeth_l2_add_mac(struct qeth_card *card, struct netdev_hw_addr *ha)
434 {
435 	u32 mac_hash = get_unaligned((u32 *)(&ha->addr[2]));
436 	struct qeth_mac *mac;
437 
438 	hash_for_each_possible(card->rx_mode_addrs, mac, hnode, mac_hash) {
439 		if (ether_addr_equal_64bits(ha->addr, mac->mac_addr)) {
440 			mac->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
441 			return;
442 		}
443 	}
444 
445 	mac = kzalloc(sizeof(struct qeth_mac), GFP_ATOMIC);
446 	if (!mac)
447 		return;
448 
449 	ether_addr_copy(mac->mac_addr, ha->addr);
450 	mac->disp_flag = QETH_DISP_ADDR_ADD;
451 
452 	hash_add(card->rx_mode_addrs, &mac->hnode, mac_hash);
453 }
454 
455 static void qeth_l2_rx_mode_work(struct work_struct *work)
456 {
457 	struct qeth_card *card = container_of(work, struct qeth_card,
458 					      rx_mode_work);
459 	struct net_device *dev = card->dev;
460 	struct netdev_hw_addr *ha;
461 	struct qeth_mac *mac;
462 	struct hlist_node *tmp;
463 	int i;
464 	int rc;
465 
466 	QETH_CARD_TEXT(card, 3, "setmulti");
467 
468 	netif_addr_lock_bh(dev);
469 	netdev_for_each_mc_addr(ha, dev)
470 		qeth_l2_add_mac(card, ha);
471 	netdev_for_each_uc_addr(ha, dev)
472 		qeth_l2_add_mac(card, ha);
473 	netif_addr_unlock_bh(dev);
474 
475 	hash_for_each_safe(card->rx_mode_addrs, i, tmp, mac, hnode) {
476 		switch (mac->disp_flag) {
477 		case QETH_DISP_ADDR_DELETE:
478 			qeth_l2_remove_mac(card, mac->mac_addr);
479 			hash_del(&mac->hnode);
480 			kfree(mac);
481 			break;
482 		case QETH_DISP_ADDR_ADD:
483 			rc = qeth_l2_write_mac(card, mac->mac_addr);
484 			if (rc) {
485 				hash_del(&mac->hnode);
486 				kfree(mac);
487 				break;
488 			}
489 			fallthrough;
490 		default:
491 			/* for next call to set_rx_mode(): */
492 			mac->disp_flag = QETH_DISP_ADDR_DELETE;
493 		}
494 	}
495 
496 	qeth_l2_set_promisc_mode(card);
497 }
498 
499 static int qeth_l2_xmit_osn(struct qeth_card *card, struct sk_buff *skb,
500 			    struct qeth_qdio_out_q *queue)
501 {
502 	gfp_t gfp = GFP_ATOMIC | (skb_pfmemalloc(skb) ? __GFP_MEMALLOC : 0);
503 	struct qeth_hdr *hdr = (struct qeth_hdr *)skb->data;
504 	addr_t end = (addr_t)(skb->data + sizeof(*hdr));
505 	addr_t start = (addr_t)skb->data;
506 	unsigned int elements = 0;
507 	unsigned int hd_len = 0;
508 	int rc;
509 
510 	if (skb->protocol == htons(ETH_P_IPV6))
511 		return -EPROTONOSUPPORT;
512 
513 	if (qeth_get_elements_for_range(start, end) > 1) {
514 		/* Misaligned HW header, move it to its own buffer element. */
515 		hdr = kmem_cache_alloc(qeth_core_header_cache, gfp);
516 		if (!hdr)
517 			return -ENOMEM;
518 		hd_len = sizeof(*hdr);
519 		skb_copy_from_linear_data(skb, (char *)hdr, hd_len);
520 		elements++;
521 	}
522 
523 	elements += qeth_count_elements(skb, hd_len);
524 	if (elements > queue->max_elements) {
525 		rc = -E2BIG;
526 		goto out;
527 	}
528 
529 	rc = qeth_do_send_packet(card, queue, skb, hdr, hd_len, hd_len,
530 				 elements);
531 out:
532 	if (rc && hd_len)
533 		kmem_cache_free(qeth_core_header_cache, hdr);
534 	return rc;
535 }
536 
537 static netdev_tx_t qeth_l2_hard_start_xmit(struct sk_buff *skb,
538 					   struct net_device *dev)
539 {
540 	struct qeth_card *card = dev->ml_priv;
541 	u16 txq = skb_get_queue_mapping(skb);
542 	struct qeth_qdio_out_q *queue;
543 	int rc;
544 
545 	if (!skb_is_gso(skb))
546 		qdisc_skb_cb(skb)->pkt_len = skb->len;
547 	if (IS_IQD(card))
548 		txq = qeth_iqd_translate_txq(dev, txq);
549 	queue = card->qdio.out_qs[txq];
550 
551 	if (IS_OSN(card))
552 		rc = qeth_l2_xmit_osn(card, skb, queue);
553 	else
554 		rc = qeth_xmit(card, skb, queue, qeth_get_ip_version(skb),
555 			       qeth_l2_fill_header);
556 
557 	if (!rc)
558 		return NETDEV_TX_OK;
559 
560 	QETH_TXQ_STAT_INC(queue, tx_dropped);
561 	kfree_skb(skb);
562 	return NETDEV_TX_OK;
563 }
564 
565 static u16 qeth_l2_select_queue(struct net_device *dev, struct sk_buff *skb,
566 				struct net_device *sb_dev)
567 {
568 	struct qeth_card *card = dev->ml_priv;
569 
570 	if (IS_IQD(card))
571 		return qeth_iqd_select_queue(dev, skb,
572 					     qeth_get_ether_cast_type(skb),
573 					     sb_dev);
574 	if (qeth_uses_tx_prio_queueing(card))
575 		return qeth_get_priority_queue(card, skb);
576 
577 	return netdev_pick_tx(dev, skb, sb_dev);
578 }
579 
580 static void qeth_l2_set_rx_mode(struct net_device *dev)
581 {
582 	struct qeth_card *card = dev->ml_priv;
583 
584 	schedule_work(&card->rx_mode_work);
585 }
586 
587 /**
588  *	qeth_l2_pnso() - perform network subchannel operation
589  *	@card: qeth_card structure pointer
590  *	@oc: Operation Code
591  *	@cnc: Boolean Change-Notification Control
592  *	@cb: Callback function will be executed for each element
593  *		of the address list
594  *	@priv: Pointer to pass to the callback function.
595  *
596  *	Collects network information in a network address list and calls the
597  *	callback function for every entry in the list. If "change-notification-
598  *	control" is set, further changes in the address list will be reported
599  *	via the IPA command.
600  */
601 static int qeth_l2_pnso(struct qeth_card *card, u8 oc, int cnc,
602 			void (*cb)(void *priv, struct chsc_pnso_naid_l2 *entry),
603 			void *priv)
604 {
605 	struct ccw_device *ddev = CARD_DDEV(card);
606 	struct chsc_pnso_area *rr;
607 	u32 prev_instance = 0;
608 	int isfirstblock = 1;
609 	int i, size, elems;
610 	int rc;
611 
612 	rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
613 	if (rr == NULL)
614 		return -ENOMEM;
615 	do {
616 		QETH_CARD_TEXT(card, 2, "PNSO");
617 		/* on the first iteration, naihdr.resume_token will be zero */
618 		rc = ccw_device_pnso(ddev, rr, oc, rr->naihdr.resume_token,
619 				     cnc);
620 		if (rc)
621 			continue;
622 		if (cb == NULL)
623 			continue;
624 
625 		size = rr->naihdr.naids;
626 		if (size != sizeof(struct chsc_pnso_naid_l2)) {
627 			WARN_ON_ONCE(1);
628 			continue;
629 		}
630 
631 		elems = (rr->response.length - sizeof(struct chsc_header) -
632 			 sizeof(struct chsc_pnso_naihdr)) / size;
633 
634 		if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
635 			/* Inform the caller that they need to scrap */
636 			/* the data that was already reported via cb */
637 			rc = -EAGAIN;
638 			break;
639 		}
640 		isfirstblock = 0;
641 		prev_instance = rr->naihdr.instance;
642 		for (i = 0; i < elems; i++)
643 			(*cb)(priv, &rr->entries[i]);
644 	} while ((rc == -EBUSY) || (!rc && /* list stored */
645 		   /* resume token is non-zero => list incomplete */
646 		   (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
647 
648 	if (rc)
649 		QETH_CARD_TEXT_(card, 2, "PNrp%04x", rr->response.code);
650 
651 	free_page((unsigned long)rr);
652 	return rc;
653 }
654 
655 static bool qeth_is_my_net_if_token(struct qeth_card *card,
656 				    struct net_if_token *token)
657 {
658 	return ((card->info.ddev_devno == token->devnum) &&
659 		(card->info.cssid == token->cssid) &&
660 		(card->info.iid == token->iid) &&
661 		(card->info.ssid == token->ssid) &&
662 		(card->info.chpid == token->chpid) &&
663 		(card->info.chid == token->chid));
664 }
665 
666 /**
667  *	qeth_l2_dev2br_fdb_notify() - update fdb of master bridge
668  *	@card:	qeth_card structure pointer
669  *	@code:	event bitmask: high order bit 0x80 set to
670  *				1 - removal of an object
671  *				0 - addition of an object
672  *			       Object type(s):
673  *				0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC
674  *	@token: "network token" structure identifying 'physical' location
675  *		of the target
676  *	@addr_lnid: structure with MAC address and VLAN ID of the target
677  */
678 static void qeth_l2_dev2br_fdb_notify(struct qeth_card *card, u8 code,
679 				      struct net_if_token *token,
680 				      struct mac_addr_lnid *addr_lnid)
681 {
682 	struct switchdev_notifier_fdb_info info;
683 	u8 ntfy_mac[ETH_ALEN];
684 
685 	ether_addr_copy(ntfy_mac, addr_lnid->mac);
686 	/* Ignore VLAN only changes */
687 	if (!(code & IPA_ADDR_CHANGE_CODE_MACADDR))
688 		return;
689 	/* Ignore mcast entries */
690 	if (is_multicast_ether_addr(ntfy_mac))
691 		return;
692 	/* Ignore my own addresses */
693 	if (qeth_is_my_net_if_token(card, token))
694 		return;
695 
696 	info.addr = ntfy_mac;
697 	/* don't report VLAN IDs */
698 	info.vid = 0;
699 	info.added_by_user = false;
700 	info.offloaded = true;
701 
702 	if (code & IPA_ADDR_CHANGE_CODE_REMOVAL) {
703 		call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
704 					 card->dev, &info.info, NULL);
705 		QETH_CARD_TEXT(card, 4, "andelmac");
706 		QETH_CARD_TEXT_(card, 4,
707 				"mc%012lx", ether_addr_to_u64(ntfy_mac));
708 	} else {
709 		call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
710 					 card->dev, &info.info, NULL);
711 		QETH_CARD_TEXT(card, 4, "anaddmac");
712 		QETH_CARD_TEXT_(card, 4,
713 				"mc%012lx", ether_addr_to_u64(ntfy_mac));
714 	}
715 }
716 
717 static void qeth_l2_dev2br_an_set_cb(void *priv,
718 				     struct chsc_pnso_naid_l2 *entry)
719 {
720 	u8 code = IPA_ADDR_CHANGE_CODE_MACADDR;
721 	struct qeth_card *card = priv;
722 
723 	if (entry->addr_lnid.lnid < VLAN_N_VID)
724 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
725 	qeth_l2_dev2br_fdb_notify(card, code,
726 				  (struct net_if_token *)&entry->nit,
727 				  (struct mac_addr_lnid *)&entry->addr_lnid);
728 }
729 
730 /**
731  *	qeth_l2_dev2br_an_set() -
732  *	Enable or disable 'dev to bridge network address notification'
733  *	@card: qeth_card structure pointer
734  *	@enable: Enable or disable 'dev to bridge network address notification'
735  *
736  *	Returns negative errno-compatible error indication or 0 on success.
737  *
738  *	On enable, emits a series of address notifications for all
739  *	currently registered hosts.
740  *
741  *	Must be called under rtnl_lock
742  */
743 static int qeth_l2_dev2br_an_set(struct qeth_card *card, bool enable)
744 {
745 	int rc;
746 
747 	if (enable) {
748 		QETH_CARD_TEXT(card, 2, "anseton");
749 		rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 1,
750 				  qeth_l2_dev2br_an_set_cb, card);
751 		if (rc == -EAGAIN)
752 			/* address notification enabled, but inconsistent
753 			 * addresses reported -> disable address notification
754 			 */
755 			qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0,
756 				     NULL, NULL);
757 	} else {
758 		QETH_CARD_TEXT(card, 2, "ansetoff");
759 		rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0, NULL, NULL);
760 	}
761 
762 	return rc;
763 }
764 
765 static int qeth_l2_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
766 				  struct net_device *dev, u32 filter_mask,
767 				  int nlflags)
768 {
769 	struct qeth_priv *priv = netdev_priv(dev);
770 	struct qeth_card *card = dev->ml_priv;
771 	u16 mode = BRIDGE_MODE_UNDEF;
772 
773 	/* Do not even show qeth devs that cannot do bridge_setlink */
774 	if (!priv->brport_hw_features || !netif_device_present(dev) ||
775 	    qeth_bridgeport_is_in_use(card))
776 		return -EOPNOTSUPP;
777 
778 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
779 				       mode, priv->brport_features,
780 				       priv->brport_hw_features,
781 				       nlflags, filter_mask, NULL);
782 }
783 
784 static const struct nla_policy qeth_brport_policy[IFLA_BRPORT_MAX + 1] = {
785 	[IFLA_BRPORT_LEARNING_SYNC]	= { .type = NLA_U8 },
786 };
787 
788 /**
789  *	qeth_l2_bridge_setlink() - set bridgeport attributes
790  *	@dev: netdevice
791  *	@nlh: netlink message header
792  *	@flags: bridge flags (here: BRIDGE_FLAGS_SELF)
793  *	@extack: extended ACK report struct
794  *
795  *	Called under rtnl_lock
796  */
797 static int qeth_l2_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
798 				  u16 flags, struct netlink_ext_ack *extack)
799 {
800 	struct qeth_priv *priv = netdev_priv(dev);
801 	struct nlattr *bp_tb[IFLA_BRPORT_MAX + 1];
802 	struct qeth_card *card = dev->ml_priv;
803 	struct nlattr *attr, *nested_attr;
804 	bool enable, has_protinfo = false;
805 	int rem1, rem2;
806 	int rc;
807 
808 	if (!netif_device_present(dev))
809 		return -ENODEV;
810 	if (!(priv->brport_hw_features))
811 		return -EOPNOTSUPP;
812 
813 	nlmsg_for_each_attr(attr, nlh, sizeof(struct ifinfomsg), rem1) {
814 		if (nla_type(attr) == IFLA_PROTINFO) {
815 			rc = nla_parse_nested(bp_tb, IFLA_BRPORT_MAX, attr,
816 					      qeth_brport_policy, extack);
817 			if (rc)
818 				return rc;
819 			has_protinfo = true;
820 		} else if (nla_type(attr) == IFLA_AF_SPEC) {
821 			nla_for_each_nested(nested_attr, attr, rem2) {
822 				if (nla_type(nested_attr) == IFLA_BRIDGE_FLAGS)
823 					continue;
824 				NL_SET_ERR_MSG_ATTR(extack, nested_attr,
825 						    "Unsupported attribute");
826 				return -EINVAL;
827 			}
828 		} else {
829 			NL_SET_ERR_MSG_ATTR(extack, attr, "Unsupported attribute");
830 			return -EINVAL;
831 		}
832 	}
833 	if (!has_protinfo)
834 		return 0;
835 	if (!bp_tb[IFLA_BRPORT_LEARNING_SYNC])
836 		return -EINVAL;
837 	enable = !!nla_get_u8(bp_tb[IFLA_BRPORT_LEARNING_SYNC]);
838 
839 	if (enable == !!(priv->brport_features & BR_LEARNING_SYNC))
840 		return 0;
841 
842 	mutex_lock(&card->sbp_lock);
843 	/* do not change anything if BridgePort is enabled */
844 	if (qeth_bridgeport_is_in_use(card)) {
845 		NL_SET_ERR_MSG(extack, "n/a (BridgePort)");
846 		rc = -EBUSY;
847 	} else if (enable) {
848 		qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
849 		rc = qeth_l2_dev2br_an_set(card, true);
850 		if (rc)
851 			qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
852 		else
853 			priv->brport_features |= BR_LEARNING_SYNC;
854 	} else {
855 		rc = qeth_l2_dev2br_an_set(card, false);
856 		if (!rc) {
857 			qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
858 			priv->brport_features ^= BR_LEARNING_SYNC;
859 			qeth_l2_dev2br_fdb_flush(card);
860 		}
861 	}
862 	mutex_unlock(&card->sbp_lock);
863 
864 	return rc;
865 }
866 
867 static const struct net_device_ops qeth_l2_netdev_ops = {
868 	.ndo_open		= qeth_open,
869 	.ndo_stop		= qeth_stop,
870 	.ndo_get_stats64	= qeth_get_stats64,
871 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
872 	.ndo_features_check	= qeth_features_check,
873 	.ndo_select_queue	= qeth_l2_select_queue,
874 	.ndo_validate_addr	= qeth_l2_validate_addr,
875 	.ndo_set_rx_mode	= qeth_l2_set_rx_mode,
876 	.ndo_do_ioctl		= qeth_do_ioctl,
877 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
878 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
879 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
880 	.ndo_tx_timeout		= qeth_tx_timeout,
881 	.ndo_fix_features	= qeth_fix_features,
882 	.ndo_set_features	= qeth_set_features,
883 	.ndo_bridge_getlink	= qeth_l2_bridge_getlink,
884 	.ndo_bridge_setlink	= qeth_l2_bridge_setlink,
885 };
886 
887 static const struct net_device_ops qeth_osn_netdev_ops = {
888 	.ndo_open		= qeth_open,
889 	.ndo_stop		= qeth_stop,
890 	.ndo_get_stats64	= qeth_get_stats64,
891 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
892 	.ndo_validate_addr	= eth_validate_addr,
893 	.ndo_tx_timeout		= qeth_tx_timeout,
894 };
895 
896 static int qeth_l2_setup_netdev(struct qeth_card *card)
897 {
898 	if (IS_OSN(card)) {
899 		card->dev->netdev_ops = &qeth_osn_netdev_ops;
900 		card->dev->flags |= IFF_NOARP;
901 		goto add_napi;
902 	}
903 
904 	card->dev->needed_headroom = sizeof(struct qeth_hdr);
905 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
906 	card->dev->priv_flags |= IFF_UNICAST_FLT;
907 
908 	if (IS_OSM(card)) {
909 		card->dev->features |= NETIF_F_VLAN_CHALLENGED;
910 	} else {
911 		if (!IS_VM_NIC(card))
912 			card->dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
913 		card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
914 	}
915 
916 	if (IS_OSD(card) && !IS_VM_NIC(card)) {
917 		card->dev->features |= NETIF_F_SG;
918 		/* OSA 3S and earlier has no RX/TX support */
919 		if (qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM)) {
920 			card->dev->hw_features |= NETIF_F_IP_CSUM;
921 			card->dev->vlan_features |= NETIF_F_IP_CSUM;
922 		}
923 	}
924 	if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
925 		card->dev->hw_features |= NETIF_F_IPV6_CSUM;
926 		card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
927 	}
928 	if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM) ||
929 	    qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6)) {
930 		card->dev->hw_features |= NETIF_F_RXCSUM;
931 		card->dev->vlan_features |= NETIF_F_RXCSUM;
932 	}
933 	if (qeth_is_supported(card, IPA_OUTBOUND_TSO)) {
934 		card->dev->hw_features |= NETIF_F_TSO;
935 		card->dev->vlan_features |= NETIF_F_TSO;
936 	}
937 	if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
938 		card->dev->hw_features |= NETIF_F_TSO6;
939 		card->dev->vlan_features |= NETIF_F_TSO6;
940 	}
941 
942 	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6)) {
943 		card->dev->needed_headroom = sizeof(struct qeth_hdr_tso);
944 		netif_keep_dst(card->dev);
945 		netif_set_gso_max_size(card->dev,
946 				       PAGE_SIZE * (QDIO_MAX_ELEMENTS_PER_BUFFER - 1));
947 	}
948 
949 add_napi:
950 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
951 	return register_netdev(card->dev);
952 }
953 
954 static void qeth_l2_trace_features(struct qeth_card *card)
955 {
956 	/* Set BridgePort features */
957 	QETH_CARD_TEXT(card, 2, "featuSBP");
958 	QETH_CARD_HEX(card, 2, &card->options.sbp.supported_funcs,
959 		      sizeof(card->options.sbp.supported_funcs));
960 	/* VNIC Characteristics features */
961 	QETH_CARD_TEXT(card, 2, "feaVNICC");
962 	QETH_CARD_HEX(card, 2, &card->options.vnicc.sup_chars,
963 		      sizeof(card->options.vnicc.sup_chars));
964 }
965 
966 static void qeth_l2_setup_bridgeport_attrs(struct qeth_card *card)
967 {
968 	if (!card->options.sbp.reflect_promisc &&
969 	    card->options.sbp.role != QETH_SBP_ROLE_NONE) {
970 		/* Conditional to avoid spurious error messages */
971 		qeth_bridgeport_setrole(card, card->options.sbp.role);
972 		/* Let the callback function refresh the stored role value. */
973 		qeth_bridgeport_query_ports(card, &card->options.sbp.role,
974 					    NULL);
975 	}
976 	if (card->options.sbp.hostnotification) {
977 		if (qeth_bridgeport_an_set(card, 1))
978 			card->options.sbp.hostnotification = 0;
979 	}
980 }
981 
982 /**
983  *	qeth_l2_detect_dev2br_support() -
984  *	Detect whether this card supports 'dev to bridge fdb network address
985  *	change notification' and thus can support the learning_sync bridgeport
986  *	attribute
987  *	@card: qeth_card structure pointer
988  */
989 static void qeth_l2_detect_dev2br_support(struct qeth_card *card)
990 {
991 	struct qeth_priv *priv = netdev_priv(card->dev);
992 	bool dev2br_supported;
993 
994 	QETH_CARD_TEXT(card, 2, "d2brsup");
995 	if (!IS_IQD(card))
996 		return;
997 
998 	/* dev2br requires valid cssid,iid,chid */
999 	dev2br_supported = card->info.ids_valid &&
1000 			   css_general_characteristics.enarf;
1001 	QETH_CARD_TEXT_(card, 2, "D2Bsup%02x", dev2br_supported);
1002 
1003 	if (dev2br_supported)
1004 		priv->brport_hw_features |= BR_LEARNING_SYNC;
1005 	else
1006 		priv->brport_hw_features &= ~BR_LEARNING_SYNC;
1007 }
1008 
1009 static void qeth_l2_enable_brport_features(struct qeth_card *card)
1010 {
1011 	struct qeth_priv *priv = netdev_priv(card->dev);
1012 	int rc;
1013 
1014 	if (priv->brport_features & BR_LEARNING_SYNC) {
1015 		if (priv->brport_hw_features & BR_LEARNING_SYNC) {
1016 			qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1017 			rc = qeth_l2_dev2br_an_set(card, true);
1018 			if (rc == -EAGAIN) {
1019 				/* Recoverable error, retry once */
1020 				qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1021 				qeth_l2_dev2br_fdb_flush(card);
1022 				qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1023 				rc = qeth_l2_dev2br_an_set(card, true);
1024 			}
1025 			if (rc) {
1026 				netdev_err(card->dev,
1027 					   "failed to enable bridge learning_sync: %d\n",
1028 					   rc);
1029 				qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1030 				qeth_l2_dev2br_fdb_flush(card);
1031 				priv->brport_features ^= BR_LEARNING_SYNC;
1032 			}
1033 		} else {
1034 			dev_warn(&card->gdev->dev,
1035 				"bridge learning_sync not supported\n");
1036 			priv->brport_features ^= BR_LEARNING_SYNC;
1037 		}
1038 	}
1039 }
1040 
1041 #ifdef CONFIG_QETH_OSN
1042 static void qeth_osn_assist_cb(struct qeth_card *card,
1043 			       struct qeth_cmd_buffer *iob,
1044 			       unsigned int data_length)
1045 {
1046 	qeth_notify_cmd(iob, 0);
1047 	qeth_put_cmd(iob);
1048 }
1049 
1050 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1051 {
1052 	struct qeth_cmd_buffer *iob;
1053 	struct qeth_card *card;
1054 
1055 	if (data_len < 0)
1056 		return -EINVAL;
1057 	if (!dev)
1058 		return -ENODEV;
1059 	card = dev->ml_priv;
1060 	if (!card)
1061 		return -ENODEV;
1062 	QETH_CARD_TEXT(card, 2, "osnsdmc");
1063 	if (!qeth_card_hw_is_reachable(card))
1064 		return -ENODEV;
1065 
1066 	iob = qeth_alloc_cmd(&card->write, IPA_PDU_HEADER_SIZE + data_len, 1,
1067 			     QETH_IPA_TIMEOUT);
1068 	if (!iob)
1069 		return -ENOMEM;
1070 
1071 	qeth_prepare_ipa_cmd(card, iob, (u16) data_len, NULL);
1072 
1073 	memcpy(__ipa_cmd(iob), data, data_len);
1074 	iob->callback = qeth_osn_assist_cb;
1075 	return qeth_send_ipa_cmd(card, iob, NULL, NULL);
1076 }
1077 EXPORT_SYMBOL(qeth_osn_assist);
1078 
1079 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1080 		  int (*assist_cb)(struct net_device *, void *),
1081 		  int (*data_cb)(struct sk_buff *))
1082 {
1083 	struct qeth_card *card;
1084 	char bus_id[16];
1085 	u16 devno;
1086 
1087 	memcpy(&devno, read_dev_no, 2);
1088 	sprintf(bus_id, "0.0.%04x", devno);
1089 	card = qeth_get_card_by_busid(bus_id);
1090 	if (!card || !IS_OSN(card))
1091 		return -ENODEV;
1092 	*dev = card->dev;
1093 
1094 	QETH_CARD_TEXT(card, 2, "osnreg");
1095 	if ((assist_cb == NULL) || (data_cb == NULL))
1096 		return -EINVAL;
1097 	card->osn_info.assist_cb = assist_cb;
1098 	card->osn_info.data_cb = data_cb;
1099 	return 0;
1100 }
1101 EXPORT_SYMBOL(qeth_osn_register);
1102 
1103 void qeth_osn_deregister(struct net_device *dev)
1104 {
1105 	struct qeth_card *card;
1106 
1107 	if (!dev)
1108 		return;
1109 	card = dev->ml_priv;
1110 	if (!card)
1111 		return;
1112 	QETH_CARD_TEXT(card, 2, "osndereg");
1113 	card->osn_info.assist_cb = NULL;
1114 	card->osn_info.data_cb = NULL;
1115 }
1116 EXPORT_SYMBOL(qeth_osn_deregister);
1117 #endif
1118 
1119 /* SETBRIDGEPORT support, async notifications */
1120 
1121 enum qeth_an_event_type {anev_reg_unreg, anev_abort, anev_reset};
1122 
1123 /**
1124  * qeth_bridge_emit_host_event() - bridgeport address change notification
1125  * @card:  qeth_card structure pointer, for udev events.
1126  * @evtype:  "normal" register/unregister, or abort, or reset. For abort
1127  *	      and reset token and addr_lnid are unused and may be NULL.
1128  * @code:  event bitmask: high order bit 0x80 value 1 means removal of an
1129  *			  object, 0 - addition of an object.
1130  *			  0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC.
1131  * @token: "network token" structure identifying physical address of the port.
1132  * @addr_lnid: pointer to structure with MAC address and VLAN ID.
1133  *
1134  * This function is called when registrations and deregistrations are
1135  * reported by the hardware, and also when notifications are enabled -
1136  * for all currently registered addresses.
1137  */
1138 static void qeth_bridge_emit_host_event(struct qeth_card *card,
1139 					enum qeth_an_event_type evtype,
1140 					u8 code,
1141 					struct net_if_token *token,
1142 					struct mac_addr_lnid *addr_lnid)
1143 {
1144 	char str[7][32];
1145 	char *env[8];
1146 	int i = 0;
1147 
1148 	switch (evtype) {
1149 	case anev_reg_unreg:
1150 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=%s",
1151 				(code & IPA_ADDR_CHANGE_CODE_REMOVAL)
1152 				? "deregister" : "register");
1153 		env[i] = str[i]; i++;
1154 		if (code & IPA_ADDR_CHANGE_CODE_VLANID) {
1155 			snprintf(str[i], sizeof(str[i]), "VLAN=%d",
1156 				addr_lnid->lnid);
1157 			env[i] = str[i]; i++;
1158 		}
1159 		if (code & IPA_ADDR_CHANGE_CODE_MACADDR) {
1160 			snprintf(str[i], sizeof(str[i]), "MAC=%pM",
1161 				addr_lnid->mac);
1162 			env[i] = str[i]; i++;
1163 		}
1164 		snprintf(str[i], sizeof(str[i]), "NTOK_BUSID=%x.%x.%04x",
1165 			token->cssid, token->ssid, token->devnum);
1166 		env[i] = str[i]; i++;
1167 		snprintf(str[i], sizeof(str[i]), "NTOK_IID=%02x", token->iid);
1168 		env[i] = str[i]; i++;
1169 		snprintf(str[i], sizeof(str[i]), "NTOK_CHPID=%02x",
1170 				token->chpid);
1171 		env[i] = str[i]; i++;
1172 		snprintf(str[i], sizeof(str[i]), "NTOK_CHID=%04x", token->chid);
1173 		env[i] = str[i]; i++;
1174 		break;
1175 	case anev_abort:
1176 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=abort");
1177 		env[i] = str[i]; i++;
1178 		break;
1179 	case anev_reset:
1180 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=reset");
1181 		env[i] = str[i]; i++;
1182 		break;
1183 	}
1184 	env[i] = NULL;
1185 	kobject_uevent_env(&card->gdev->dev.kobj, KOBJ_CHANGE, env);
1186 }
1187 
1188 struct qeth_bridge_state_data {
1189 	struct work_struct worker;
1190 	struct qeth_card *card;
1191 	u8 role;
1192 	u8 state;
1193 };
1194 
1195 static void qeth_bridge_state_change_worker(struct work_struct *work)
1196 {
1197 	struct qeth_bridge_state_data *data =
1198 		container_of(work, struct qeth_bridge_state_data, worker);
1199 	char env_locrem[32];
1200 	char env_role[32];
1201 	char env_state[32];
1202 	char *env[] = {
1203 		env_locrem,
1204 		env_role,
1205 		env_state,
1206 		NULL
1207 	};
1208 
1209 	snprintf(env_locrem, sizeof(env_locrem), "BRIDGEPORT=statechange");
1210 	snprintf(env_role, sizeof(env_role), "ROLE=%s",
1211 		(data->role == QETH_SBP_ROLE_NONE) ? "none" :
1212 		(data->role == QETH_SBP_ROLE_PRIMARY) ? "primary" :
1213 		(data->role == QETH_SBP_ROLE_SECONDARY) ? "secondary" :
1214 		"<INVALID>");
1215 	snprintf(env_state, sizeof(env_state), "STATE=%s",
1216 		(data->state == QETH_SBP_STATE_INACTIVE) ? "inactive" :
1217 		(data->state == QETH_SBP_STATE_STANDBY) ? "standby" :
1218 		(data->state == QETH_SBP_STATE_ACTIVE) ? "active" :
1219 		"<INVALID>");
1220 	kobject_uevent_env(&data->card->gdev->dev.kobj,
1221 				KOBJ_CHANGE, env);
1222 	kfree(data);
1223 }
1224 
1225 static void qeth_bridge_state_change(struct qeth_card *card,
1226 					struct qeth_ipa_cmd *cmd)
1227 {
1228 	struct qeth_sbp_port_data *qports = &cmd->data.sbp.data.port_data;
1229 	struct qeth_bridge_state_data *data;
1230 
1231 	QETH_CARD_TEXT(card, 2, "brstchng");
1232 	if (qports->num_entries == 0) {
1233 		QETH_CARD_TEXT(card, 2, "BPempty");
1234 		return;
1235 	}
1236 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1237 		QETH_CARD_TEXT_(card, 2, "BPsz%04x", qports->entry_length);
1238 		return;
1239 	}
1240 
1241 	data = kzalloc(sizeof(*data), GFP_ATOMIC);
1242 	if (!data) {
1243 		QETH_CARD_TEXT(card, 2, "BPSalloc");
1244 		return;
1245 	}
1246 	INIT_WORK(&data->worker, qeth_bridge_state_change_worker);
1247 	data->card = card;
1248 	/* Information for the local port: */
1249 	data->role = qports->entry[0].role;
1250 	data->state = qports->entry[0].state;
1251 
1252 	queue_work(card->event_wq, &data->worker);
1253 }
1254 
1255 struct qeth_addr_change_data {
1256 	struct delayed_work dwork;
1257 	struct qeth_card *card;
1258 	struct qeth_ipacmd_addr_change ac_event;
1259 };
1260 
1261 static void qeth_l2_dev2br_worker(struct work_struct *work)
1262 {
1263 	struct delayed_work *dwork = to_delayed_work(work);
1264 	struct qeth_addr_change_data *data;
1265 	struct qeth_card *card;
1266 	struct qeth_priv *priv;
1267 	unsigned int i;
1268 	int rc;
1269 
1270 	data = container_of(dwork, struct qeth_addr_change_data, dwork);
1271 	card = data->card;
1272 	priv = netdev_priv(card->dev);
1273 
1274 	QETH_CARD_TEXT(card, 4, "dev2brew");
1275 
1276 	if (READ_ONCE(card->info.pnso_mode) == QETH_PNSO_NONE)
1277 		goto free;
1278 
1279 	/* Potential re-config in progress, try again later: */
1280 	if (!rtnl_trylock()) {
1281 		queue_delayed_work(card->event_wq, dwork,
1282 				   msecs_to_jiffies(100));
1283 		return;
1284 	}
1285 	if (!netif_device_present(card->dev))
1286 		goto out_unlock;
1287 
1288 	if (data->ac_event.lost_event_mask) {
1289 		QETH_DBF_MESSAGE(3,
1290 				 "Address change notification overflow on device %x\n",
1291 				 CARD_DEVID(card));
1292 		/* Card fdb and bridge fdb are out of sync, card has stopped
1293 		 * notifications (no need to drain_workqueue). Purge all
1294 		 * 'extern_learn' entries from the parent bridge and restart
1295 		 * the notifications.
1296 		 */
1297 		qeth_l2_dev2br_fdb_flush(card);
1298 		rc = qeth_l2_dev2br_an_set(card, true);
1299 		if (rc) {
1300 			/* TODO: if we want to retry after -EAGAIN, be
1301 			 * aware there could be stale entries in the
1302 			 * workqueue now, that need to be drained.
1303 			 * For now we give up:
1304 			 */
1305 			netdev_err(card->dev,
1306 				   "bridge learning_sync failed to recover: %d\n",
1307 				   rc);
1308 			WRITE_ONCE(card->info.pnso_mode,
1309 				   QETH_PNSO_NONE);
1310 			/* To remove fdb entries reported by an_set: */
1311 			qeth_l2_dev2br_fdb_flush(card);
1312 			priv->brport_features ^= BR_LEARNING_SYNC;
1313 		} else {
1314 			QETH_DBF_MESSAGE(3,
1315 					 "Address Notification resynced on device %x\n",
1316 					 CARD_DEVID(card));
1317 		}
1318 	} else {
1319 		for (i = 0; i < data->ac_event.num_entries; i++) {
1320 			struct qeth_ipacmd_addr_change_entry *entry =
1321 					&data->ac_event.entry[i];
1322 			qeth_l2_dev2br_fdb_notify(card,
1323 						  entry->change_code,
1324 						  &entry->token,
1325 						  &entry->addr_lnid);
1326 		}
1327 	}
1328 
1329 out_unlock:
1330 	rtnl_unlock();
1331 
1332 free:
1333 	kfree(data);
1334 }
1335 
1336 static void qeth_addr_change_event_worker(struct work_struct *work)
1337 {
1338 	struct delayed_work *dwork = to_delayed_work(work);
1339 	struct qeth_addr_change_data *data;
1340 	struct qeth_card *card;
1341 	int i;
1342 
1343 	data = container_of(dwork, struct qeth_addr_change_data, dwork);
1344 	card = data->card;
1345 
1346 	QETH_CARD_TEXT(data->card, 4, "adrchgew");
1347 
1348 	if (READ_ONCE(card->info.pnso_mode) == QETH_PNSO_NONE)
1349 		goto free;
1350 
1351 	if (data->ac_event.lost_event_mask) {
1352 		/* Potential re-config in progress, try again later: */
1353 		if (!mutex_trylock(&card->sbp_lock)) {
1354 			queue_delayed_work(card->event_wq, dwork,
1355 					   msecs_to_jiffies(100));
1356 			return;
1357 		}
1358 
1359 		dev_info(&data->card->gdev->dev,
1360 			 "Address change notification stopped on %s (%s)\n",
1361 			 netdev_name(card->dev),
1362 			(data->ac_event.lost_event_mask == 0x01)
1363 			? "Overflow"
1364 			: (data->ac_event.lost_event_mask == 0x02)
1365 			? "Bridge port state change"
1366 			: "Unknown reason");
1367 
1368 		data->card->options.sbp.hostnotification = 0;
1369 		card->info.pnso_mode = QETH_PNSO_NONE;
1370 		mutex_unlock(&data->card->sbp_lock);
1371 		qeth_bridge_emit_host_event(data->card, anev_abort,
1372 					    0, NULL, NULL);
1373 	} else
1374 		for (i = 0; i < data->ac_event.num_entries; i++) {
1375 			struct qeth_ipacmd_addr_change_entry *entry =
1376 					&data->ac_event.entry[i];
1377 			qeth_bridge_emit_host_event(data->card,
1378 						    anev_reg_unreg,
1379 						    entry->change_code,
1380 						    &entry->token,
1381 						    &entry->addr_lnid);
1382 		}
1383 
1384 free:
1385 	kfree(data);
1386 }
1387 
1388 static void qeth_addr_change_event(struct qeth_card *card,
1389 				   struct qeth_ipa_cmd *cmd)
1390 {
1391 	struct qeth_ipacmd_addr_change *hostevs =
1392 		 &cmd->data.addrchange;
1393 	struct qeth_addr_change_data *data;
1394 	int extrasize;
1395 
1396 	if (card->info.pnso_mode == QETH_PNSO_NONE)
1397 		return;
1398 
1399 	QETH_CARD_TEXT(card, 4, "adrchgev");
1400 	if (cmd->hdr.return_code != 0x0000) {
1401 		if (cmd->hdr.return_code == 0x0010) {
1402 			if (hostevs->lost_event_mask == 0x00)
1403 				hostevs->lost_event_mask = 0xff;
1404 		} else {
1405 			QETH_CARD_TEXT_(card, 2, "ACHN%04x",
1406 				cmd->hdr.return_code);
1407 			return;
1408 		}
1409 	}
1410 	extrasize = sizeof(struct qeth_ipacmd_addr_change_entry) *
1411 						hostevs->num_entries;
1412 	data = kzalloc(sizeof(struct qeth_addr_change_data) + extrasize,
1413 		       GFP_ATOMIC);
1414 	if (!data) {
1415 		QETH_CARD_TEXT(card, 2, "ACNalloc");
1416 		return;
1417 	}
1418 	if (card->info.pnso_mode == QETH_PNSO_BRIDGEPORT)
1419 		INIT_DELAYED_WORK(&data->dwork, qeth_addr_change_event_worker);
1420 	else
1421 		INIT_DELAYED_WORK(&data->dwork, qeth_l2_dev2br_worker);
1422 	data->card = card;
1423 	memcpy(&data->ac_event, hostevs,
1424 			sizeof(struct qeth_ipacmd_addr_change) + extrasize);
1425 	queue_delayed_work(card->event_wq, &data->dwork, 0);
1426 }
1427 
1428 /* SETBRIDGEPORT support; sending commands */
1429 
1430 struct _qeth_sbp_cbctl {
1431 	union {
1432 		u32 supported;
1433 		struct {
1434 			enum qeth_sbp_roles *role;
1435 			enum qeth_sbp_states *state;
1436 		} qports;
1437 	} data;
1438 };
1439 
1440 static int qeth_bridgeport_makerc(struct qeth_card *card,
1441 				  struct qeth_ipa_cmd *cmd)
1442 {
1443 	struct qeth_ipacmd_setbridgeport *sbp = &cmd->data.sbp;
1444 	enum qeth_ipa_sbp_cmd setcmd = sbp->hdr.command_code;
1445 	u16 ipa_rc = cmd->hdr.return_code;
1446 	u16 sbp_rc = sbp->hdr.return_code;
1447 	int rc;
1448 
1449 	if (ipa_rc == IPA_RC_SUCCESS && sbp_rc == IPA_RC_SUCCESS)
1450 		return 0;
1451 
1452 	if ((IS_IQD(card) && ipa_rc == IPA_RC_SUCCESS) ||
1453 	    (!IS_IQD(card) && ipa_rc == sbp_rc)) {
1454 		switch (sbp_rc) {
1455 		case IPA_RC_SUCCESS:
1456 			rc = 0;
1457 			break;
1458 		case IPA_RC_L2_UNSUPPORTED_CMD:
1459 		case IPA_RC_UNSUPPORTED_COMMAND:
1460 			rc = -EOPNOTSUPP;
1461 			break;
1462 		case IPA_RC_SBP_OSA_NOT_CONFIGURED:
1463 		case IPA_RC_SBP_IQD_NOT_CONFIGURED:
1464 			rc = -ENODEV; /* maybe not the best code here? */
1465 			dev_err(&card->gdev->dev,
1466 	"The device is not configured as a Bridge Port\n");
1467 			break;
1468 		case IPA_RC_SBP_OSA_OS_MISMATCH:
1469 		case IPA_RC_SBP_IQD_OS_MISMATCH:
1470 			rc = -EPERM;
1471 			dev_err(&card->gdev->dev,
1472 	"A Bridge Port is already configured by a different operating system\n");
1473 			break;
1474 		case IPA_RC_SBP_OSA_ANO_DEV_PRIMARY:
1475 		case IPA_RC_SBP_IQD_ANO_DEV_PRIMARY:
1476 			switch (setcmd) {
1477 			case IPA_SBP_SET_PRIMARY_BRIDGE_PORT:
1478 				rc = -EEXIST;
1479 				dev_err(&card->gdev->dev,
1480 	"The LAN already has a primary Bridge Port\n");
1481 				break;
1482 			case IPA_SBP_SET_SECONDARY_BRIDGE_PORT:
1483 				rc = -EBUSY;
1484 				dev_err(&card->gdev->dev,
1485 	"The device is already a primary Bridge Port\n");
1486 				break;
1487 			default:
1488 				rc = -EIO;
1489 			}
1490 			break;
1491 		case IPA_RC_SBP_OSA_CURRENT_SECOND:
1492 		case IPA_RC_SBP_IQD_CURRENT_SECOND:
1493 			rc = -EBUSY;
1494 			dev_err(&card->gdev->dev,
1495 	"The device is already a secondary Bridge Port\n");
1496 			break;
1497 		case IPA_RC_SBP_OSA_LIMIT_SECOND:
1498 		case IPA_RC_SBP_IQD_LIMIT_SECOND:
1499 			rc = -EEXIST;
1500 			dev_err(&card->gdev->dev,
1501 	"The LAN cannot have more secondary Bridge Ports\n");
1502 			break;
1503 		case IPA_RC_SBP_OSA_CURRENT_PRIMARY:
1504 		case IPA_RC_SBP_IQD_CURRENT_PRIMARY:
1505 			rc = -EBUSY;
1506 			dev_err(&card->gdev->dev,
1507 	"The device is already a primary Bridge Port\n");
1508 			break;
1509 		case IPA_RC_SBP_OSA_NOT_AUTHD_BY_ZMAN:
1510 		case IPA_RC_SBP_IQD_NOT_AUTHD_BY_ZMAN:
1511 			rc = -EACCES;
1512 			dev_err(&card->gdev->dev,
1513 	"The device is not authorized to be a Bridge Port\n");
1514 			break;
1515 		default:
1516 			rc = -EIO;
1517 		}
1518 	} else {
1519 		switch (ipa_rc) {
1520 		case IPA_RC_NOTSUPP:
1521 			rc = -EOPNOTSUPP;
1522 			break;
1523 		case IPA_RC_UNSUPPORTED_COMMAND:
1524 			rc = -EOPNOTSUPP;
1525 			break;
1526 		default:
1527 			rc = -EIO;
1528 		}
1529 	}
1530 
1531 	if (rc) {
1532 		QETH_CARD_TEXT_(card, 2, "SBPi%04x", ipa_rc);
1533 		QETH_CARD_TEXT_(card, 2, "SBPc%04x", sbp_rc);
1534 	}
1535 	return rc;
1536 }
1537 
1538 static struct qeth_cmd_buffer *qeth_sbp_build_cmd(struct qeth_card *card,
1539 						  enum qeth_ipa_sbp_cmd sbp_cmd,
1540 						  unsigned int data_length)
1541 {
1542 	enum qeth_ipa_cmds ipa_cmd = IS_IQD(card) ? IPA_CMD_SETBRIDGEPORT_IQD :
1543 						    IPA_CMD_SETBRIDGEPORT_OSA;
1544 	struct qeth_ipacmd_sbp_hdr *hdr;
1545 	struct qeth_cmd_buffer *iob;
1546 
1547 	iob = qeth_ipa_alloc_cmd(card, ipa_cmd, QETH_PROT_NONE,
1548 				 data_length +
1549 				 offsetof(struct qeth_ipacmd_setbridgeport,
1550 					  data));
1551 	if (!iob)
1552 		return iob;
1553 
1554 	hdr = &__ipa_cmd(iob)->data.sbp.hdr;
1555 	hdr->cmdlength = sizeof(*hdr) + data_length;
1556 	hdr->command_code = sbp_cmd;
1557 	hdr->used_total = 1;
1558 	hdr->seq_no = 1;
1559 	return iob;
1560 }
1561 
1562 static int qeth_bridgeport_query_support_cb(struct qeth_card *card,
1563 	struct qeth_reply *reply, unsigned long data)
1564 {
1565 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1566 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1567 	int rc;
1568 
1569 	QETH_CARD_TEXT(card, 2, "brqsupcb");
1570 	rc = qeth_bridgeport_makerc(card, cmd);
1571 	if (rc)
1572 		return rc;
1573 
1574 	cbctl->data.supported =
1575 		cmd->data.sbp.data.query_cmds_supp.supported_cmds;
1576 	return 0;
1577 }
1578 
1579 /**
1580  * qeth_bridgeport_query_support() - store bitmask of supported subfunctions.
1581  * @card:			     qeth_card structure pointer.
1582  *
1583  * Sets bitmask of supported setbridgeport subfunctions in the qeth_card
1584  * strucutre: card->options.sbp.supported_funcs.
1585  */
1586 static void qeth_bridgeport_query_support(struct qeth_card *card)
1587 {
1588 	struct qeth_cmd_buffer *iob;
1589 	struct _qeth_sbp_cbctl cbctl;
1590 
1591 	QETH_CARD_TEXT(card, 2, "brqsuppo");
1592 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_COMMANDS_SUPPORTED,
1593 				 SBP_DATA_SIZEOF(query_cmds_supp));
1594 	if (!iob)
1595 		return;
1596 
1597 	if (qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_support_cb,
1598 			      &cbctl)) {
1599 		card->options.sbp.role = QETH_SBP_ROLE_NONE;
1600 		card->options.sbp.supported_funcs = 0;
1601 		return;
1602 	}
1603 	card->options.sbp.supported_funcs = cbctl.data.supported;
1604 }
1605 
1606 static int qeth_bridgeport_query_ports_cb(struct qeth_card *card,
1607 	struct qeth_reply *reply, unsigned long data)
1608 {
1609 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1610 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1611 	struct qeth_sbp_port_data *qports;
1612 	int rc;
1613 
1614 	QETH_CARD_TEXT(card, 2, "brqprtcb");
1615 	rc = qeth_bridgeport_makerc(card, cmd);
1616 	if (rc)
1617 		return rc;
1618 
1619 	qports = &cmd->data.sbp.data.port_data;
1620 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1621 		QETH_CARD_TEXT_(card, 2, "SBPs%04x", qports->entry_length);
1622 		return -EINVAL;
1623 	}
1624 	/* first entry contains the state of the local port */
1625 	if (qports->num_entries > 0) {
1626 		if (cbctl->data.qports.role)
1627 			*cbctl->data.qports.role = qports->entry[0].role;
1628 		if (cbctl->data.qports.state)
1629 			*cbctl->data.qports.state = qports->entry[0].state;
1630 	}
1631 	return 0;
1632 }
1633 
1634 /**
1635  * qeth_bridgeport_query_ports() - query local bridgeport status.
1636  * @card:			   qeth_card structure pointer.
1637  * @role:   Role of the port: 0-none, 1-primary, 2-secondary.
1638  * @state:  State of the port: 0-inactive, 1-standby, 2-active.
1639  *
1640  * Returns negative errno-compatible error indication or 0 on success.
1641  *
1642  * 'role' and 'state' are not updated in case of hardware operation failure.
1643  */
1644 int qeth_bridgeport_query_ports(struct qeth_card *card,
1645 	enum qeth_sbp_roles *role, enum qeth_sbp_states *state)
1646 {
1647 	struct qeth_cmd_buffer *iob;
1648 	struct _qeth_sbp_cbctl cbctl = {
1649 		.data = {
1650 			.qports = {
1651 				.role = role,
1652 				.state = state,
1653 			},
1654 		},
1655 	};
1656 
1657 	QETH_CARD_TEXT(card, 2, "brqports");
1658 	if (!(card->options.sbp.supported_funcs & IPA_SBP_QUERY_BRIDGE_PORTS))
1659 		return -EOPNOTSUPP;
1660 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_BRIDGE_PORTS, 0);
1661 	if (!iob)
1662 		return -ENOMEM;
1663 
1664 	return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_ports_cb,
1665 				 &cbctl);
1666 }
1667 
1668 static int qeth_bridgeport_set_cb(struct qeth_card *card,
1669 	struct qeth_reply *reply, unsigned long data)
1670 {
1671 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
1672 
1673 	QETH_CARD_TEXT(card, 2, "brsetrcb");
1674 	return qeth_bridgeport_makerc(card, cmd);
1675 }
1676 
1677 /**
1678  * qeth_bridgeport_setrole() - Assign primary role to the port.
1679  * @card:		       qeth_card structure pointer.
1680  * @role:		       Role to assign.
1681  *
1682  * Returns negative errno-compatible error indication or 0 on success.
1683  */
1684 int qeth_bridgeport_setrole(struct qeth_card *card, enum qeth_sbp_roles role)
1685 {
1686 	struct qeth_cmd_buffer *iob;
1687 	enum qeth_ipa_sbp_cmd setcmd;
1688 	unsigned int cmdlength = 0;
1689 
1690 	QETH_CARD_TEXT(card, 2, "brsetrol");
1691 	switch (role) {
1692 	case QETH_SBP_ROLE_NONE:
1693 		setcmd = IPA_SBP_RESET_BRIDGE_PORT_ROLE;
1694 		break;
1695 	case QETH_SBP_ROLE_PRIMARY:
1696 		setcmd = IPA_SBP_SET_PRIMARY_BRIDGE_PORT;
1697 		cmdlength = SBP_DATA_SIZEOF(set_primary);
1698 		break;
1699 	case QETH_SBP_ROLE_SECONDARY:
1700 		setcmd = IPA_SBP_SET_SECONDARY_BRIDGE_PORT;
1701 		break;
1702 	default:
1703 		return -EINVAL;
1704 	}
1705 	if (!(card->options.sbp.supported_funcs & setcmd))
1706 		return -EOPNOTSUPP;
1707 	iob = qeth_sbp_build_cmd(card, setcmd, cmdlength);
1708 	if (!iob)
1709 		return -ENOMEM;
1710 
1711 	return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_set_cb, NULL);
1712 }
1713 
1714 static void qeth_bridgeport_an_set_cb(void *priv,
1715 				      struct chsc_pnso_naid_l2 *entry)
1716 {
1717 	struct qeth_card *card = (struct qeth_card *)priv;
1718 	u8 code;
1719 
1720 	code = IPA_ADDR_CHANGE_CODE_MACADDR;
1721 	if (entry->addr_lnid.lnid < VLAN_N_VID)
1722 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
1723 	qeth_bridge_emit_host_event(card, anev_reg_unreg, code,
1724 				    (struct net_if_token *)&entry->nit,
1725 				    (struct mac_addr_lnid *)&entry->addr_lnid);
1726 }
1727 
1728 /**
1729  * qeth_bridgeport_an_set() - Enable or disable bridgeport address notification
1730  * @card:		      qeth_card structure pointer.
1731  * @enable:		      0 - disable, non-zero - enable notifications
1732  *
1733  * Returns negative errno-compatible error indication or 0 on success.
1734  *
1735  * On enable, emits a series of address notifications udev events for all
1736  * currently registered hosts.
1737  */
1738 int qeth_bridgeport_an_set(struct qeth_card *card, int enable)
1739 {
1740 	int rc;
1741 
1742 	if (!card->options.sbp.supported_funcs)
1743 		return -EOPNOTSUPP;
1744 
1745 	if (enable) {
1746 		qeth_bridge_emit_host_event(card, anev_reset, 0, NULL, NULL);
1747 		qeth_l2_set_pnso_mode(card, QETH_PNSO_BRIDGEPORT);
1748 		rc = qeth_l2_pnso(card, PNSO_OC_NET_BRIDGE_INFO, 1,
1749 				  qeth_bridgeport_an_set_cb, card);
1750 		if (rc)
1751 			qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1752 	} else {
1753 		rc = qeth_l2_pnso(card, PNSO_OC_NET_BRIDGE_INFO, 0, NULL, NULL);
1754 		qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1755 	}
1756 	return rc;
1757 }
1758 
1759 /* VNIC Characteristics support */
1760 
1761 /* handle VNICC IPA command return codes; convert to error codes */
1762 static int qeth_l2_vnicc_makerc(struct qeth_card *card, u16 ipa_rc)
1763 {
1764 	int rc;
1765 
1766 	switch (ipa_rc) {
1767 	case IPA_RC_SUCCESS:
1768 		return ipa_rc;
1769 	case IPA_RC_L2_UNSUPPORTED_CMD:
1770 	case IPA_RC_NOTSUPP:
1771 		rc = -EOPNOTSUPP;
1772 		break;
1773 	case IPA_RC_VNICC_OOSEQ:
1774 		rc = -EALREADY;
1775 		break;
1776 	case IPA_RC_VNICC_VNICBP:
1777 		rc = -EBUSY;
1778 		break;
1779 	case IPA_RC_L2_ADDR_TABLE_FULL:
1780 		rc = -ENOSPC;
1781 		break;
1782 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
1783 		rc = -EACCES;
1784 		break;
1785 	default:
1786 		rc = -EIO;
1787 	}
1788 
1789 	QETH_CARD_TEXT_(card, 2, "err%04x", ipa_rc);
1790 	return rc;
1791 }
1792 
1793 /* generic VNICC request call back */
1794 static int qeth_l2_vnicc_request_cb(struct qeth_card *card,
1795 				    struct qeth_reply *reply,
1796 				    unsigned long data)
1797 {
1798 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1799 	struct qeth_ipacmd_vnicc *rep = &cmd->data.vnicc;
1800 	u32 sub_cmd = cmd->data.vnicc.hdr.sub_command;
1801 
1802 	QETH_CARD_TEXT(card, 2, "vniccrcb");
1803 	if (cmd->hdr.return_code)
1804 		return qeth_l2_vnicc_makerc(card, cmd->hdr.return_code);
1805 	/* return results to caller */
1806 	card->options.vnicc.sup_chars = rep->vnicc_cmds.supported;
1807 	card->options.vnicc.cur_chars = rep->vnicc_cmds.enabled;
1808 
1809 	if (sub_cmd == IPA_VNICC_QUERY_CMDS)
1810 		*(u32 *)reply->param = rep->data.query_cmds.sup_cmds;
1811 	else if (sub_cmd == IPA_VNICC_GET_TIMEOUT)
1812 		*(u32 *)reply->param = rep->data.getset_timeout.timeout;
1813 
1814 	return 0;
1815 }
1816 
1817 static struct qeth_cmd_buffer *qeth_l2_vnicc_build_cmd(struct qeth_card *card,
1818 						       u32 vnicc_cmd,
1819 						       unsigned int data_length)
1820 {
1821 	struct qeth_ipacmd_vnicc_hdr *hdr;
1822 	struct qeth_cmd_buffer *iob;
1823 
1824 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_VNICC, QETH_PROT_NONE,
1825 				 data_length +
1826 				 offsetof(struct qeth_ipacmd_vnicc, data));
1827 	if (!iob)
1828 		return NULL;
1829 
1830 	hdr = &__ipa_cmd(iob)->data.vnicc.hdr;
1831 	hdr->data_length = sizeof(*hdr) + data_length;
1832 	hdr->sub_command = vnicc_cmd;
1833 	return iob;
1834 }
1835 
1836 /* VNICC query VNIC characteristics request */
1837 static int qeth_l2_vnicc_query_chars(struct qeth_card *card)
1838 {
1839 	struct qeth_cmd_buffer *iob;
1840 
1841 	QETH_CARD_TEXT(card, 2, "vniccqch");
1842 	iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CHARS, 0);
1843 	if (!iob)
1844 		return -ENOMEM;
1845 
1846 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1847 }
1848 
1849 /* VNICC query sub commands request */
1850 static int qeth_l2_vnicc_query_cmds(struct qeth_card *card, u32 vnic_char,
1851 				    u32 *sup_cmds)
1852 {
1853 	struct qeth_cmd_buffer *iob;
1854 
1855 	QETH_CARD_TEXT(card, 2, "vniccqcm");
1856 	iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CMDS,
1857 				      VNICC_DATA_SIZEOF(query_cmds));
1858 	if (!iob)
1859 		return -ENOMEM;
1860 
1861 	__ipa_cmd(iob)->data.vnicc.data.query_cmds.vnic_char = vnic_char;
1862 
1863 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, sup_cmds);
1864 }
1865 
1866 /* VNICC enable/disable characteristic request */
1867 static int qeth_l2_vnicc_set_char(struct qeth_card *card, u32 vnic_char,
1868 				      u32 cmd)
1869 {
1870 	struct qeth_cmd_buffer *iob;
1871 
1872 	QETH_CARD_TEXT(card, 2, "vniccedc");
1873 	iob = qeth_l2_vnicc_build_cmd(card, cmd, VNICC_DATA_SIZEOF(set_char));
1874 	if (!iob)
1875 		return -ENOMEM;
1876 
1877 	__ipa_cmd(iob)->data.vnicc.data.set_char.vnic_char = vnic_char;
1878 
1879 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1880 }
1881 
1882 /* VNICC get/set timeout for characteristic request */
1883 static int qeth_l2_vnicc_getset_timeout(struct qeth_card *card, u32 vnicc,
1884 					u32 cmd, u32 *timeout)
1885 {
1886 	struct qeth_vnicc_getset_timeout *getset_timeout;
1887 	struct qeth_cmd_buffer *iob;
1888 
1889 	QETH_CARD_TEXT(card, 2, "vniccgst");
1890 	iob = qeth_l2_vnicc_build_cmd(card, cmd,
1891 				      VNICC_DATA_SIZEOF(getset_timeout));
1892 	if (!iob)
1893 		return -ENOMEM;
1894 
1895 	getset_timeout = &__ipa_cmd(iob)->data.vnicc.data.getset_timeout;
1896 	getset_timeout->vnic_char = vnicc;
1897 
1898 	if (cmd == IPA_VNICC_SET_TIMEOUT)
1899 		getset_timeout->timeout = *timeout;
1900 
1901 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, timeout);
1902 }
1903 
1904 /* recover user timeout setting */
1905 static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
1906 					  u32 *timeout)
1907 {
1908 	if (card->options.vnicc.sup_chars & vnicc &&
1909 	    card->options.vnicc.getset_timeout_sup & vnicc &&
1910 	    !qeth_l2_vnicc_getset_timeout(card, vnicc, IPA_VNICC_SET_TIMEOUT,
1911 					  timeout))
1912 		return false;
1913 	*timeout = QETH_VNICC_DEFAULT_TIMEOUT;
1914 	return true;
1915 }
1916 
1917 /* set current VNICC flag state; called from sysfs store function */
1918 int qeth_l2_vnicc_set_state(struct qeth_card *card, u32 vnicc, bool state)
1919 {
1920 	int rc = 0;
1921 	u32 cmd;
1922 
1923 	QETH_CARD_TEXT(card, 2, "vniccsch");
1924 
1925 	/* check if characteristic and enable/disable are supported */
1926 	if (!(card->options.vnicc.sup_chars & vnicc) ||
1927 	    !(card->options.vnicc.set_char_sup & vnicc))
1928 		return -EOPNOTSUPP;
1929 
1930 	if (qeth_bridgeport_is_in_use(card))
1931 		return -EBUSY;
1932 
1933 	/* set enable/disable command and store wanted characteristic */
1934 	if (state) {
1935 		cmd = IPA_VNICC_ENABLE;
1936 		card->options.vnicc.wanted_chars |= vnicc;
1937 	} else {
1938 		cmd = IPA_VNICC_DISABLE;
1939 		card->options.vnicc.wanted_chars &= ~vnicc;
1940 	}
1941 
1942 	/* do we need to do anything? */
1943 	if (card->options.vnicc.cur_chars == card->options.vnicc.wanted_chars)
1944 		return rc;
1945 
1946 	/* if card is not ready, simply stop here */
1947 	if (!qeth_card_hw_is_reachable(card)) {
1948 		if (state)
1949 			card->options.vnicc.cur_chars |= vnicc;
1950 		else
1951 			card->options.vnicc.cur_chars &= ~vnicc;
1952 		return rc;
1953 	}
1954 
1955 	rc = qeth_l2_vnicc_set_char(card, vnicc, cmd);
1956 	if (rc)
1957 		card->options.vnicc.wanted_chars =
1958 			card->options.vnicc.cur_chars;
1959 	else {
1960 		/* successful online VNICC change; handle special cases */
1961 		if (state && vnicc == QETH_VNICC_RX_BCAST)
1962 			card->options.vnicc.rx_bcast_enabled = true;
1963 		if (!state && vnicc == QETH_VNICC_LEARNING)
1964 			qeth_l2_vnicc_recover_timeout(card, vnicc,
1965 					&card->options.vnicc.learning_timeout);
1966 	}
1967 
1968 	return rc;
1969 }
1970 
1971 /* get current VNICC flag state; called from sysfs show function */
1972 int qeth_l2_vnicc_get_state(struct qeth_card *card, u32 vnicc, bool *state)
1973 {
1974 	int rc = 0;
1975 
1976 	QETH_CARD_TEXT(card, 2, "vniccgch");
1977 
1978 	/* check if characteristic is supported */
1979 	if (!(card->options.vnicc.sup_chars & vnicc))
1980 		return -EOPNOTSUPP;
1981 
1982 	if (qeth_bridgeport_is_in_use(card))
1983 		return -EBUSY;
1984 
1985 	/* if card is ready, query current VNICC state */
1986 	if (qeth_card_hw_is_reachable(card))
1987 		rc = qeth_l2_vnicc_query_chars(card);
1988 
1989 	*state = (card->options.vnicc.cur_chars & vnicc) ? true : false;
1990 	return rc;
1991 }
1992 
1993 /* set VNICC timeout; called from sysfs store function. Currently, only learning
1994  * supports timeout
1995  */
1996 int qeth_l2_vnicc_set_timeout(struct qeth_card *card, u32 timeout)
1997 {
1998 	int rc = 0;
1999 
2000 	QETH_CARD_TEXT(card, 2, "vniccsto");
2001 
2002 	/* check if characteristic and set_timeout are supported */
2003 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2004 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2005 		return -EOPNOTSUPP;
2006 
2007 	if (qeth_bridgeport_is_in_use(card))
2008 		return -EBUSY;
2009 
2010 	/* do we need to do anything? */
2011 	if (card->options.vnicc.learning_timeout == timeout)
2012 		return rc;
2013 
2014 	/* if card is not ready, simply store the value internally and return */
2015 	if (!qeth_card_hw_is_reachable(card)) {
2016 		card->options.vnicc.learning_timeout = timeout;
2017 		return rc;
2018 	}
2019 
2020 	/* send timeout value to card; if successful, store value internally */
2021 	rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2022 					  IPA_VNICC_SET_TIMEOUT, &timeout);
2023 	if (!rc)
2024 		card->options.vnicc.learning_timeout = timeout;
2025 
2026 	return rc;
2027 }
2028 
2029 /* get current VNICC timeout; called from sysfs show function. Currently, only
2030  * learning supports timeout
2031  */
2032 int qeth_l2_vnicc_get_timeout(struct qeth_card *card, u32 *timeout)
2033 {
2034 	int rc = 0;
2035 
2036 	QETH_CARD_TEXT(card, 2, "vniccgto");
2037 
2038 	/* check if characteristic and get_timeout are supported */
2039 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2040 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2041 		return -EOPNOTSUPP;
2042 
2043 	if (qeth_bridgeport_is_in_use(card))
2044 		return -EBUSY;
2045 
2046 	/* if card is ready, get timeout. Otherwise, just return stored value */
2047 	*timeout = card->options.vnicc.learning_timeout;
2048 	if (qeth_card_hw_is_reachable(card))
2049 		rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2050 						  IPA_VNICC_GET_TIMEOUT,
2051 						  timeout);
2052 
2053 	return rc;
2054 }
2055 
2056 /* check if VNICC is currently enabled */
2057 static bool _qeth_l2_vnicc_is_in_use(struct qeth_card *card)
2058 {
2059 	if (!card->options.vnicc.sup_chars)
2060 		return false;
2061 	/* default values are only OK if rx_bcast was not enabled by user
2062 	 * or the card is offline.
2063 	 */
2064 	if (card->options.vnicc.cur_chars == QETH_VNICC_DEFAULT) {
2065 		if (!card->options.vnicc.rx_bcast_enabled ||
2066 		    !qeth_card_hw_is_reachable(card))
2067 			return false;
2068 	}
2069 	return true;
2070 }
2071 
2072 /**
2073  *	qeth_bridgeport_allowed - are any qeth_bridgeport functions allowed?
2074  *	@card: qeth_card structure pointer
2075  *
2076  *	qeth_bridgeport functionality is mutually exclusive with usage of the
2077  *	VNIC Characteristics and dev2br address notifications
2078  */
2079 bool qeth_bridgeport_allowed(struct qeth_card *card)
2080 {
2081 	struct qeth_priv *priv = netdev_priv(card->dev);
2082 
2083 	return (!_qeth_l2_vnicc_is_in_use(card) &&
2084 		!(priv->brport_features & BR_LEARNING_SYNC));
2085 }
2086 
2087 /* recover user characteristic setting */
2088 static bool qeth_l2_vnicc_recover_char(struct qeth_card *card, u32 vnicc,
2089 				       bool enable)
2090 {
2091 	u32 cmd = enable ? IPA_VNICC_ENABLE : IPA_VNICC_DISABLE;
2092 
2093 	if (card->options.vnicc.sup_chars & vnicc &&
2094 	    card->options.vnicc.set_char_sup & vnicc &&
2095 	    !qeth_l2_vnicc_set_char(card, vnicc, cmd))
2096 		return false;
2097 	card->options.vnicc.wanted_chars &= ~vnicc;
2098 	card->options.vnicc.wanted_chars |= QETH_VNICC_DEFAULT & vnicc;
2099 	return true;
2100 }
2101 
2102 /* (re-)initialize VNICC */
2103 static void qeth_l2_vnicc_init(struct qeth_card *card)
2104 {
2105 	u32 *timeout = &card->options.vnicc.learning_timeout;
2106 	bool enable, error = false;
2107 	unsigned int chars_len, i;
2108 	unsigned long chars_tmp;
2109 	u32 sup_cmds, vnicc;
2110 
2111 	QETH_CARD_TEXT(card, 2, "vniccini");
2112 	/* reset rx_bcast */
2113 	card->options.vnicc.rx_bcast_enabled = 0;
2114 	/* initial query and storage of VNIC characteristics */
2115 	if (qeth_l2_vnicc_query_chars(card)) {
2116 		if (card->options.vnicc.wanted_chars != QETH_VNICC_DEFAULT ||
2117 		    *timeout != QETH_VNICC_DEFAULT_TIMEOUT)
2118 			dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2119 		/* fail quietly if user didn't change the default config */
2120 		card->options.vnicc.sup_chars = 0;
2121 		card->options.vnicc.cur_chars = 0;
2122 		card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2123 		return;
2124 	}
2125 	/* get supported commands for each supported characteristic */
2126 	chars_tmp = card->options.vnicc.sup_chars;
2127 	chars_len = sizeof(card->options.vnicc.sup_chars) * BITS_PER_BYTE;
2128 	for_each_set_bit(i, &chars_tmp, chars_len) {
2129 		vnicc = BIT(i);
2130 		if (qeth_l2_vnicc_query_cmds(card, vnicc, &sup_cmds)) {
2131 			sup_cmds = 0;
2132 			error = true;
2133 		}
2134 		if ((sup_cmds & IPA_VNICC_SET_TIMEOUT) &&
2135 		    (sup_cmds & IPA_VNICC_GET_TIMEOUT))
2136 			card->options.vnicc.getset_timeout_sup |= vnicc;
2137 		else
2138 			card->options.vnicc.getset_timeout_sup &= ~vnicc;
2139 		if ((sup_cmds & IPA_VNICC_ENABLE) &&
2140 		    (sup_cmds & IPA_VNICC_DISABLE))
2141 			card->options.vnicc.set_char_sup |= vnicc;
2142 		else
2143 			card->options.vnicc.set_char_sup &= ~vnicc;
2144 	}
2145 	/* enforce assumed default values and recover settings, if changed  */
2146 	error |= qeth_l2_vnicc_recover_timeout(card, QETH_VNICC_LEARNING,
2147 					       timeout);
2148 	/* Change chars, if necessary  */
2149 	chars_tmp = card->options.vnicc.wanted_chars ^
2150 		    card->options.vnicc.cur_chars;
2151 	chars_len = sizeof(card->options.vnicc.wanted_chars) * BITS_PER_BYTE;
2152 	for_each_set_bit(i, &chars_tmp, chars_len) {
2153 		vnicc = BIT(i);
2154 		enable = card->options.vnicc.wanted_chars & vnicc;
2155 		error |= qeth_l2_vnicc_recover_char(card, vnicc, enable);
2156 	}
2157 	if (error)
2158 		dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2159 }
2160 
2161 /* configure default values of VNIC characteristics */
2162 static void qeth_l2_vnicc_set_defaults(struct qeth_card *card)
2163 {
2164 	/* characteristics values */
2165 	card->options.vnicc.sup_chars = QETH_VNICC_ALL;
2166 	card->options.vnicc.cur_chars = QETH_VNICC_DEFAULT;
2167 	card->options.vnicc.learning_timeout = QETH_VNICC_DEFAULT_TIMEOUT;
2168 	/* supported commands */
2169 	card->options.vnicc.set_char_sup = QETH_VNICC_ALL;
2170 	card->options.vnicc.getset_timeout_sup = QETH_VNICC_LEARNING;
2171 	/* settings wanted by users */
2172 	card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2173 }
2174 
2175 static const struct device_type qeth_l2_devtype = {
2176 	.name = "qeth_layer2",
2177 	.groups = qeth_l2_attr_groups,
2178 };
2179 
2180 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
2181 {
2182 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2183 	int rc;
2184 
2185 	if (IS_OSN(card))
2186 		dev_notice(&gdev->dev, "OSN support will be dropped in 2021\n");
2187 
2188 	qeth_l2_vnicc_set_defaults(card);
2189 	mutex_init(&card->sbp_lock);
2190 
2191 	if (gdev->dev.type == &qeth_generic_devtype) {
2192 		rc = qeth_l2_create_device_attributes(&gdev->dev);
2193 		if (rc)
2194 			return rc;
2195 	}
2196 
2197 	INIT_WORK(&card->rx_mode_work, qeth_l2_rx_mode_work);
2198 	return 0;
2199 }
2200 
2201 static void qeth_l2_remove_device(struct ccwgroup_device *gdev)
2202 {
2203 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2204 
2205 	if (gdev->dev.type == &qeth_generic_devtype)
2206 		qeth_l2_remove_device_attributes(&gdev->dev);
2207 	qeth_set_allowed_threads(card, 0, 1);
2208 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2209 
2210 	if (gdev->state == CCWGROUP_ONLINE)
2211 		qeth_set_offline(card, false);
2212 
2213 	cancel_work_sync(&card->close_dev_work);
2214 	if (card->dev->reg_state == NETREG_REGISTERED)
2215 		unregister_netdev(card->dev);
2216 }
2217 
2218 static int qeth_l2_set_online(struct qeth_card *card, bool carrier_ok)
2219 {
2220 	struct net_device *dev = card->dev;
2221 	int rc = 0;
2222 
2223 	qeth_l2_detect_dev2br_support(card);
2224 
2225 	mutex_lock(&card->sbp_lock);
2226 	qeth_bridgeport_query_support(card);
2227 	if (card->options.sbp.supported_funcs) {
2228 		qeth_l2_setup_bridgeport_attrs(card);
2229 		dev_info(&card->gdev->dev,
2230 			 "The device represents a Bridge Capable Port\n");
2231 	}
2232 	mutex_unlock(&card->sbp_lock);
2233 
2234 	qeth_l2_register_dev_addr(card);
2235 
2236 	/* for the rx_bcast characteristic, init VNICC after setmac */
2237 	qeth_l2_vnicc_init(card);
2238 
2239 	qeth_l2_trace_features(card);
2240 
2241 	/* softsetup */
2242 	QETH_CARD_TEXT(card, 2, "softsetp");
2243 
2244 	card->state = CARD_STATE_SOFTSETUP;
2245 
2246 	qeth_set_allowed_threads(card, 0xffffffff, 0);
2247 
2248 	if (dev->reg_state != NETREG_REGISTERED) {
2249 		rc = qeth_l2_setup_netdev(card);
2250 		if (rc)
2251 			goto err_setup;
2252 
2253 		if (carrier_ok)
2254 			netif_carrier_on(dev);
2255 	} else {
2256 		rtnl_lock();
2257 		rc = qeth_set_real_num_tx_queues(card,
2258 						 qeth_tx_actual_queues(card));
2259 		if (rc) {
2260 			rtnl_unlock();
2261 			goto err_set_queues;
2262 		}
2263 
2264 		if (carrier_ok)
2265 			netif_carrier_on(dev);
2266 		else
2267 			netif_carrier_off(dev);
2268 
2269 		netif_device_attach(dev);
2270 		qeth_enable_hw_features(dev);
2271 		qeth_l2_enable_brport_features(card);
2272 
2273 		if (card->info.open_when_online) {
2274 			card->info.open_when_online = 0;
2275 			dev_open(dev, NULL);
2276 		}
2277 		rtnl_unlock();
2278 	}
2279 	return 0;
2280 
2281 err_set_queues:
2282 err_setup:
2283 	qeth_set_allowed_threads(card, 0, 1);
2284 	card->state = CARD_STATE_DOWN;
2285 	return rc;
2286 }
2287 
2288 static void qeth_l2_set_offline(struct qeth_card *card)
2289 {
2290 	struct qeth_priv *priv = netdev_priv(card->dev);
2291 
2292 	qeth_set_allowed_threads(card, 0, 1);
2293 	qeth_l2_drain_rx_mode_cache(card);
2294 
2295 	if (card->state == CARD_STATE_SOFTSETUP)
2296 		card->state = CARD_STATE_DOWN;
2297 
2298 	qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
2299 	if (priv->brport_features & BR_LEARNING_SYNC) {
2300 		rtnl_lock();
2301 		qeth_l2_dev2br_fdb_flush(card);
2302 		rtnl_unlock();
2303 	}
2304 }
2305 
2306 /* Returns zero if the command is successfully "consumed" */
2307 static int qeth_l2_control_event(struct qeth_card *card,
2308 				 struct qeth_ipa_cmd *cmd)
2309 {
2310 	switch (cmd->hdr.command) {
2311 	case IPA_CMD_SETBRIDGEPORT_OSA:
2312 	case IPA_CMD_SETBRIDGEPORT_IQD:
2313 		if (cmd->data.sbp.hdr.command_code ==
2314 		    IPA_SBP_BRIDGE_PORT_STATE_CHANGE) {
2315 			qeth_bridge_state_change(card, cmd);
2316 			return 0;
2317 		}
2318 
2319 		return 1;
2320 	case IPA_CMD_ADDRESS_CHANGE_NOTIF:
2321 		qeth_addr_change_event(card, cmd);
2322 		return 0;
2323 	default:
2324 		return 1;
2325 	}
2326 }
2327 
2328 const struct qeth_discipline qeth_l2_discipline = {
2329 	.devtype = &qeth_l2_devtype,
2330 	.setup = qeth_l2_probe_device,
2331 	.remove = qeth_l2_remove_device,
2332 	.set_online = qeth_l2_set_online,
2333 	.set_offline = qeth_l2_set_offline,
2334 	.do_ioctl = NULL,
2335 	.control_event_handler = qeth_l2_control_event,
2336 };
2337 EXPORT_SYMBOL_GPL(qeth_l2_discipline);
2338 
2339 static int __init qeth_l2_init(void)
2340 {
2341 	pr_info("register layer 2 discipline\n");
2342 	return 0;
2343 }
2344 
2345 static void __exit qeth_l2_exit(void)
2346 {
2347 	pr_info("unregister layer 2 discipline\n");
2348 }
2349 
2350 module_init(qeth_l2_init);
2351 module_exit(qeth_l2_exit);
2352 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2353 MODULE_DESCRIPTION("qeth layer 2 discipline");
2354 MODULE_LICENSE("GPL");
2355