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