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