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