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