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