xref: /openbmc/linux/drivers/s390/net/qeth_l2_main.c (revision 82e6fdd6)
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 = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
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 = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
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 		skb->dev = card->dev;
441 		switch (hdr->hdr.l2.id) {
442 		case QETH_HEADER_TYPE_LAYER2:
443 			skb->pkt_type = PACKET_HOST;
444 			skb->protocol = eth_type_trans(skb, skb->dev);
445 			if ((card->dev->features & NETIF_F_RXCSUM)
446 			   && ((hdr->hdr.l2.flags[1] &
447 				(QETH_HDR_EXT_CSUM_HDR_REQ |
448 				   QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
449 				(QETH_HDR_EXT_CSUM_HDR_REQ |
450 				   QETH_HDR_EXT_CSUM_TRANSP_REQ)))
451 				skb->ip_summed = CHECKSUM_UNNECESSARY;
452 			else
453 				skb->ip_summed = CHECKSUM_NONE;
454 			if (skb->protocol == htons(ETH_P_802_2))
455 				*((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
456 			len = skb->len;
457 			napi_gro_receive(&card->napi, skb);
458 			break;
459 		case QETH_HEADER_TYPE_OSN:
460 			if (card->info.type == QETH_CARD_TYPE_OSN) {
461 				skb_push(skb, sizeof(struct qeth_hdr));
462 				skb_copy_to_linear_data(skb, hdr,
463 						sizeof(struct qeth_hdr));
464 				len = skb->len;
465 				card->osn_info.data_cb(skb);
466 				break;
467 			}
468 			/* else unknown */
469 		default:
470 			dev_kfree_skb_any(skb);
471 			QETH_CARD_TEXT(card, 3, "inbunkno");
472 			QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
473 			continue;
474 		}
475 		work_done++;
476 		budget--;
477 		card->stats.rx_packets++;
478 		card->stats.rx_bytes += len;
479 	}
480 	return work_done;
481 }
482 
483 static int qeth_l2_request_initial_mac(struct qeth_card *card)
484 {
485 	int rc = 0;
486 	char vendor_pre[] = {0x02, 0x00, 0x00};
487 
488 	QETH_DBF_TEXT(SETUP, 2, "l2reqmac");
489 	QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
490 
491 	if (MACHINE_IS_VM) {
492 		rc = qeth_vm_request_mac(card);
493 		if (!rc)
494 			goto out;
495 		QETH_DBF_MESSAGE(2, "z/VM MAC Service failed on device %s: x%x\n",
496 				 CARD_BUS_ID(card), rc);
497 		QETH_DBF_TEXT_(SETUP, 2, "err%04x", rc);
498 		/* fall back to alternative mechanism: */
499 	}
500 
501 	if (card->info.type == QETH_CARD_TYPE_IQD ||
502 	    card->info.type == QETH_CARD_TYPE_OSM ||
503 	    card->info.type == QETH_CARD_TYPE_OSX ||
504 	    card->info.guestlan) {
505 		rc = qeth_setadpparms_change_macaddr(card);
506 		if (rc) {
507 			QETH_DBF_MESSAGE(2, "couldn't get MAC address on "
508 				"device %s: x%x\n", CARD_BUS_ID(card), rc);
509 			QETH_DBF_TEXT_(SETUP, 2, "1err%04x", rc);
510 			return rc;
511 		}
512 	} else {
513 		eth_random_addr(card->dev->dev_addr);
514 		memcpy(card->dev->dev_addr, vendor_pre, 3);
515 	}
516 out:
517 	QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, card->dev->addr_len);
518 	return 0;
519 }
520 
521 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
522 {
523 	struct sockaddr *addr = p;
524 	struct qeth_card *card = dev->ml_priv;
525 	int rc = 0;
526 
527 	QETH_CARD_TEXT(card, 3, "setmac");
528 
529 	if (card->info.type == QETH_CARD_TYPE_OSN ||
530 	    card->info.type == QETH_CARD_TYPE_OSM ||
531 	    card->info.type == QETH_CARD_TYPE_OSX) {
532 		QETH_CARD_TEXT(card, 3, "setmcTYP");
533 		return -EOPNOTSUPP;
534 	}
535 	QETH_CARD_HEX(card, 3, addr->sa_data, ETH_ALEN);
536 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
537 		QETH_CARD_TEXT(card, 3, "setmcREC");
538 		return -ERESTARTSYS;
539 	}
540 	rc = qeth_l2_send_delmac(card, &card->dev->dev_addr[0]);
541 	if (!rc || (rc == -ENOENT))
542 		rc = qeth_l2_send_setmac(card, addr->sa_data);
543 	return rc ? -EINVAL : 0;
544 }
545 
546 static void qeth_promisc_to_bridge(struct qeth_card *card)
547 {
548 	struct net_device *dev = card->dev;
549 	enum qeth_ipa_promisc_modes promisc_mode;
550 	int role;
551 	int rc;
552 
553 	QETH_CARD_TEXT(card, 3, "pmisc2br");
554 
555 	if (!card->options.sbp.reflect_promisc)
556 		return;
557 	promisc_mode = (dev->flags & IFF_PROMISC) ? SET_PROMISC_MODE_ON
558 						: SET_PROMISC_MODE_OFF;
559 	if (promisc_mode == card->info.promisc_mode)
560 		return;
561 
562 	if (promisc_mode == SET_PROMISC_MODE_ON) {
563 		if (card->options.sbp.reflect_promisc_primary)
564 			role = QETH_SBP_ROLE_PRIMARY;
565 		else
566 			role = QETH_SBP_ROLE_SECONDARY;
567 	} else
568 		role = QETH_SBP_ROLE_NONE;
569 
570 	rc = qeth_bridgeport_setrole(card, role);
571 	QETH_DBF_TEXT_(SETUP, 2, "bpm%c%04x",
572 			(promisc_mode == SET_PROMISC_MODE_ON) ? '+' : '-', rc);
573 	if (!rc) {
574 		card->options.sbp.role = role;
575 		card->info.promisc_mode = promisc_mode;
576 	}
577 
578 }
579 /* New MAC address is added to the hash table and marked to be written on card
580  * only if there is not in the hash table storage already
581  *
582 */
583 static void qeth_l2_add_mac(struct qeth_card *card, struct netdev_hw_addr *ha)
584 {
585 	u32 mac_hash = get_unaligned((u32 *)(&ha->addr[2]));
586 	struct qeth_mac *mac;
587 
588 	hash_for_each_possible(card->mac_htable, mac, hnode, mac_hash) {
589 		if (ether_addr_equal_64bits(ha->addr, mac->mac_addr)) {
590 			mac->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
591 			return;
592 		}
593 	}
594 
595 	mac = kzalloc(sizeof(struct qeth_mac), GFP_ATOMIC);
596 	if (!mac)
597 		return;
598 
599 	ether_addr_copy(mac->mac_addr, ha->addr);
600 	mac->disp_flag = QETH_DISP_ADDR_ADD;
601 
602 	hash_add(card->mac_htable, &mac->hnode, mac_hash);
603 }
604 
605 static void qeth_l2_set_rx_mode(struct net_device *dev)
606 {
607 	struct qeth_card *card = dev->ml_priv;
608 	struct netdev_hw_addr *ha;
609 	struct qeth_mac *mac;
610 	struct hlist_node *tmp;
611 	int i;
612 	int rc;
613 
614 	if (card->info.type == QETH_CARD_TYPE_OSN)
615 		return;
616 
617 	QETH_CARD_TEXT(card, 3, "setmulti");
618 	if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
619 	    (card->state != CARD_STATE_UP))
620 		return;
621 
622 	spin_lock_bh(&card->mclock);
623 
624 	netdev_for_each_mc_addr(ha, dev)
625 		qeth_l2_add_mac(card, ha);
626 	netdev_for_each_uc_addr(ha, dev)
627 		qeth_l2_add_mac(card, ha);
628 
629 	hash_for_each_safe(card->mac_htable, i, tmp, mac, hnode) {
630 		switch (mac->disp_flag) {
631 		case QETH_DISP_ADDR_DELETE:
632 			qeth_l2_remove_mac(card, mac->mac_addr);
633 			hash_del(&mac->hnode);
634 			kfree(mac);
635 			break;
636 		case QETH_DISP_ADDR_ADD:
637 			rc = qeth_l2_write_mac(card, mac->mac_addr);
638 			if (rc) {
639 				hash_del(&mac->hnode);
640 				kfree(mac);
641 				break;
642 			}
643 			/* fall through */
644 		default:
645 			/* for next call to set_rx_mode(): */
646 			mac->disp_flag = QETH_DISP_ADDR_DELETE;
647 		}
648 	}
649 
650 	spin_unlock_bh(&card->mclock);
651 
652 	if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
653 		qeth_setadp_promisc_mode(card);
654 	else
655 		qeth_promisc_to_bridge(card);
656 }
657 
658 static int qeth_l2_xmit_iqd(struct qeth_card *card, struct sk_buff *skb,
659 			    struct qeth_qdio_out_q *queue, int cast_type)
660 {
661 	unsigned int data_offset = ETH_HLEN;
662 	struct qeth_hdr *hdr;
663 	int rc;
664 
665 	hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
666 	if (!hdr)
667 		return -ENOMEM;
668 	qeth_l2_fill_header(hdr, skb, cast_type, skb->len);
669 	skb_copy_from_linear_data(skb, ((char *)hdr) + sizeof(*hdr),
670 				  data_offset);
671 
672 	if (!qeth_get_elements_no(card, skb, 1, data_offset)) {
673 		rc = -E2BIG;
674 		goto out;
675 	}
676 	rc = qeth_do_send_packet_fast(queue, skb, hdr, data_offset,
677 				      sizeof(*hdr) + data_offset);
678 out:
679 	if (rc)
680 		kmem_cache_free(qeth_core_header_cache, hdr);
681 	return rc;
682 }
683 
684 static int qeth_l2_xmit_osa(struct qeth_card *card, struct sk_buff *skb,
685 			    struct qeth_qdio_out_q *queue, int cast_type)
686 {
687 	int push_len = sizeof(struct qeth_hdr);
688 	unsigned int elements, nr_frags;
689 	unsigned int hdr_elements = 0;
690 	struct qeth_hdr *hdr = NULL;
691 	unsigned int hd_len = 0;
692 	int rc;
693 
694 	/* fix hardware limitation: as long as we do not have sbal
695 	 * chaining we can not send long frag lists
696 	 */
697 	if (!qeth_get_elements_no(card, skb, 0, 0)) {
698 		rc = skb_linearize(skb);
699 
700 		if (card->options.performance_stats) {
701 			if (rc)
702 				card->perf_stats.tx_linfail++;
703 			else
704 				card->perf_stats.tx_lin++;
705 		}
706 		if (rc)
707 			return rc;
708 	}
709 	nr_frags = skb_shinfo(skb)->nr_frags;
710 
711 	rc = skb_cow_head(skb, push_len);
712 	if (rc)
713 		return rc;
714 	push_len = qeth_push_hdr(skb, &hdr, push_len);
715 	if (push_len < 0)
716 		return push_len;
717 	if (!push_len) {
718 		/* hdr was allocated from cache */
719 		hd_len = sizeof(*hdr);
720 		hdr_elements = 1;
721 	}
722 	qeth_l2_fill_header(hdr, skb, cast_type, skb->len - push_len);
723 	if (skb->ip_summed == CHECKSUM_PARTIAL)
724 		qeth_l2_hdr_csum(card, hdr, skb);
725 
726 	elements = qeth_get_elements_no(card, skb, hdr_elements, 0);
727 	if (!elements) {
728 		rc = -E2BIG;
729 		goto out;
730 	}
731 	elements += hdr_elements;
732 
733 	/* TODO: remove the skb_orphan() once TX completion is fast enough */
734 	skb_orphan(skb);
735 	rc = qeth_do_send_packet(card, queue, skb, hdr, 0, hd_len, elements);
736 out:
737 	if (!rc) {
738 		if (card->options.performance_stats && nr_frags) {
739 			card->perf_stats.sg_skbs_sent++;
740 			/* nr_frags + skb->data */
741 			card->perf_stats.sg_frags_sent += nr_frags + 1;
742 		}
743 	} else {
744 		if (hd_len)
745 			kmem_cache_free(qeth_core_header_cache, hdr);
746 		if (rc == -EBUSY)
747 			/* roll back to ETH header */
748 			skb_pull(skb, push_len);
749 	}
750 	return rc;
751 }
752 
753 static int qeth_l2_xmit_osn(struct qeth_card *card, struct sk_buff *skb,
754 			    struct qeth_qdio_out_q *queue)
755 {
756 	unsigned int elements;
757 	struct qeth_hdr *hdr;
758 
759 	if (skb->protocol == htons(ETH_P_IPV6))
760 		return -EPROTONOSUPPORT;
761 
762 	hdr = (struct qeth_hdr *)skb->data;
763 	elements = qeth_get_elements_no(card, skb, 0, 0);
764 	if (!elements)
765 		return -E2BIG;
766 	if (qeth_hdr_chk_and_bounce(skb, &hdr, sizeof(*hdr)))
767 		return -EINVAL;
768 	return qeth_do_send_packet(card, queue, skb, hdr, 0, 0, elements);
769 }
770 
771 static netdev_tx_t qeth_l2_hard_start_xmit(struct sk_buff *skb,
772 					   struct net_device *dev)
773 {
774 	struct qeth_card *card = dev->ml_priv;
775 	int cast_type = qeth_l2_get_cast_type(card, skb);
776 	struct qeth_qdio_out_q *queue;
777 	int tx_bytes = skb->len;
778 	int rc;
779 
780 	if (card->qdio.do_prio_queueing || (cast_type &&
781 					card->info.is_multicast_different))
782 		queue = card->qdio.out_qs[qeth_get_priority_queue(card, skb,
783 					qeth_get_ip_version(skb), cast_type)];
784 	else
785 		queue = card->qdio.out_qs[card->qdio.default_out_queue];
786 
787 	if ((card->state != CARD_STATE_UP) || !card->lan_online) {
788 		card->stats.tx_carrier_errors++;
789 		goto tx_drop;
790 	}
791 
792 	if (card->options.performance_stats) {
793 		card->perf_stats.outbound_cnt++;
794 		card->perf_stats.outbound_start_time = qeth_get_micros();
795 	}
796 	netif_stop_queue(dev);
797 
798 	switch (card->info.type) {
799 	case QETH_CARD_TYPE_OSN:
800 		rc = qeth_l2_xmit_osn(card, skb, queue);
801 		break;
802 	case QETH_CARD_TYPE_IQD:
803 		rc = qeth_l2_xmit_iqd(card, skb, queue, cast_type);
804 		break;
805 	default:
806 		rc = qeth_l2_xmit_osa(card, skb, queue, cast_type);
807 	}
808 
809 	if (!rc) {
810 		card->stats.tx_packets++;
811 		card->stats.tx_bytes += tx_bytes;
812 		if (card->options.performance_stats)
813 			card->perf_stats.outbound_time += qeth_get_micros() -
814 				card->perf_stats.outbound_start_time;
815 		netif_wake_queue(dev);
816 		return NETDEV_TX_OK;
817 	} else if (rc == -EBUSY) {
818 		return NETDEV_TX_BUSY;
819 	} /* else fall through */
820 
821 tx_drop:
822 	card->stats.tx_dropped++;
823 	card->stats.tx_errors++;
824 	dev_kfree_skb_any(skb);
825 	netif_wake_queue(dev);
826 	return NETDEV_TX_OK;
827 }
828 
829 static int __qeth_l2_open(struct net_device *dev)
830 {
831 	struct qeth_card *card = dev->ml_priv;
832 	int rc = 0;
833 
834 	QETH_CARD_TEXT(card, 4, "qethopen");
835 	if (card->state == CARD_STATE_UP)
836 		return rc;
837 	if (card->state != CARD_STATE_SOFTSETUP)
838 		return -ENODEV;
839 
840 	if ((card->info.type != QETH_CARD_TYPE_OSN) &&
841 	     (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
842 		QETH_CARD_TEXT(card, 4, "nomacadr");
843 		return -EPERM;
844 	}
845 	card->data.state = CH_STATE_UP;
846 	card->state = CARD_STATE_UP;
847 	netif_start_queue(dev);
848 
849 	if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
850 		napi_enable(&card->napi);
851 		napi_schedule(&card->napi);
852 	} else
853 		rc = -EIO;
854 	return rc;
855 }
856 
857 static int qeth_l2_open(struct net_device *dev)
858 {
859 	struct qeth_card *card = dev->ml_priv;
860 
861 	QETH_CARD_TEXT(card, 5, "qethope_");
862 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
863 		QETH_CARD_TEXT(card, 3, "openREC");
864 		return -ERESTARTSYS;
865 	}
866 	return __qeth_l2_open(dev);
867 }
868 
869 static int qeth_l2_stop(struct net_device *dev)
870 {
871 	struct qeth_card *card = dev->ml_priv;
872 
873 	QETH_CARD_TEXT(card, 4, "qethstop");
874 	netif_tx_disable(dev);
875 	if (card->state == CARD_STATE_UP) {
876 		card->state = CARD_STATE_SOFTSETUP;
877 		napi_disable(&card->napi);
878 	}
879 	return 0;
880 }
881 
882 static const struct device_type qeth_l2_devtype = {
883 	.name = "qeth_layer2",
884 	.groups = qeth_l2_attr_groups,
885 };
886 
887 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
888 {
889 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
890 	int rc;
891 
892 	if (gdev->dev.type == &qeth_generic_devtype) {
893 		rc = qeth_l2_create_device_attributes(&gdev->dev);
894 		if (rc)
895 			return rc;
896 	}
897 	INIT_LIST_HEAD(&card->vid_list);
898 	hash_init(card->mac_htable);
899 	card->options.layer2 = 1;
900 	card->info.hwtrap = 0;
901 	qeth_l2_vnicc_set_defaults(card);
902 	return 0;
903 }
904 
905 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
906 {
907 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
908 
909 	if (cgdev->dev.type == &qeth_generic_devtype)
910 		qeth_l2_remove_device_attributes(&cgdev->dev);
911 	qeth_set_allowed_threads(card, 0, 1);
912 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
913 
914 	if (cgdev->state == CCWGROUP_ONLINE)
915 		qeth_l2_set_offline(cgdev);
916 
917 	if (card->dev) {
918 		netif_napi_del(&card->napi);
919 		unregister_netdev(card->dev);
920 		card->dev = NULL;
921 	}
922 	return;
923 }
924 
925 static const struct ethtool_ops qeth_l2_ethtool_ops = {
926 	.get_link = ethtool_op_get_link,
927 	.get_strings = qeth_core_get_strings,
928 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
929 	.get_sset_count = qeth_core_get_sset_count,
930 	.get_drvinfo = qeth_core_get_drvinfo,
931 	.get_link_ksettings = qeth_core_ethtool_get_link_ksettings,
932 };
933 
934 static const struct ethtool_ops qeth_l2_osn_ops = {
935 	.get_strings = qeth_core_get_strings,
936 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
937 	.get_sset_count = qeth_core_get_sset_count,
938 	.get_drvinfo = qeth_core_get_drvinfo,
939 };
940 
941 static const struct net_device_ops qeth_l2_netdev_ops = {
942 	.ndo_open		= qeth_l2_open,
943 	.ndo_stop		= qeth_l2_stop,
944 	.ndo_get_stats		= qeth_get_stats,
945 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
946 	.ndo_features_check	= qeth_features_check,
947 	.ndo_validate_addr	= eth_validate_addr,
948 	.ndo_set_rx_mode	= qeth_l2_set_rx_mode,
949 	.ndo_do_ioctl		= qeth_do_ioctl,
950 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
951 	.ndo_change_mtu	   	= qeth_change_mtu,
952 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
953 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
954 	.ndo_tx_timeout	   	= qeth_tx_timeout,
955 	.ndo_fix_features	= qeth_fix_features,
956 	.ndo_set_features	= qeth_set_features
957 };
958 
959 static int qeth_l2_setup_netdev(struct qeth_card *card)
960 {
961 	switch (card->info.type) {
962 	case QETH_CARD_TYPE_IQD:
963 		card->dev = alloc_netdev(0, "hsi%d", NET_NAME_UNKNOWN,
964 					 ether_setup);
965 		break;
966 	case QETH_CARD_TYPE_OSN:
967 		card->dev = alloc_netdev(0, "osn%d", NET_NAME_UNKNOWN,
968 					 ether_setup);
969 		break;
970 	default:
971 		card->dev = alloc_etherdev(0);
972 	}
973 
974 	if (!card->dev)
975 		return -ENODEV;
976 
977 	card->dev->ml_priv = card;
978 	card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
979 	card->dev->mtu = card->info.initial_mtu;
980 	card->dev->min_mtu = 64;
981 	card->dev->max_mtu = ETH_MAX_MTU;
982 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
983 	if (card->info.type == QETH_CARD_TYPE_OSN) {
984 		card->dev->ethtool_ops = &qeth_l2_osn_ops;
985 		card->dev->flags |= IFF_NOARP;
986 	} else {
987 		card->dev->ethtool_ops = &qeth_l2_ethtool_ops;
988 	}
989 
990 	if (card->info.type == QETH_CARD_TYPE_OSM)
991 		card->dev->features |= NETIF_F_VLAN_CHALLENGED;
992 	else
993 		card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
994 	if (card->info.type == QETH_CARD_TYPE_OSD && !card->info.guestlan) {
995 		card->dev->hw_features = NETIF_F_SG;
996 		card->dev->vlan_features = NETIF_F_SG;
997 		card->dev->features |= NETIF_F_SG;
998 		/* OSA 3S and earlier has no RX/TX support */
999 		if (qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM)) {
1000 			card->dev->hw_features |= NETIF_F_IP_CSUM;
1001 			card->dev->vlan_features |= NETIF_F_IP_CSUM;
1002 		}
1003 		if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM)) {
1004 			card->dev->hw_features |= NETIF_F_RXCSUM;
1005 			card->dev->vlan_features |= NETIF_F_RXCSUM;
1006 		}
1007 	}
1008 	if (card->info.type != QETH_CARD_TYPE_OSN &&
1009 	    card->info.type != QETH_CARD_TYPE_IQD) {
1010 		card->dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1011 		card->dev->needed_headroom = sizeof(struct qeth_hdr);
1012 	}
1013 
1014 	card->info.broadcast_capable = 1;
1015 	qeth_l2_request_initial_mac(card);
1016 	SET_NETDEV_DEV(card->dev, &card->gdev->dev);
1017 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
1018 	netif_carrier_off(card->dev);
1019 	return register_netdev(card->dev);
1020 }
1021 
1022 static int qeth_l2_start_ipassists(struct qeth_card *card)
1023 {
1024 	/* configure isolation level */
1025 	if (qeth_set_access_ctrl_online(card, 0))
1026 		return -ENODEV;
1027 	return 0;
1028 }
1029 
1030 static void qeth_l2_trace_features(struct qeth_card *card)
1031 {
1032 	/* Set BridgePort features */
1033 	QETH_CARD_TEXT(card, 2, "featuSBP");
1034 	QETH_CARD_HEX(card, 2, &card->options.sbp.supported_funcs,
1035 		      sizeof(card->options.sbp.supported_funcs));
1036 	/* VNIC Characteristics features */
1037 	QETH_CARD_TEXT(card, 2, "feaVNICC");
1038 	QETH_CARD_HEX(card, 2, &card->options.vnicc.sup_chars,
1039 		      sizeof(card->options.vnicc.sup_chars));
1040 }
1041 
1042 static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
1043 {
1044 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1045 	int rc = 0;
1046 	enum qeth_card_states recover_flag;
1047 
1048 	mutex_lock(&card->discipline_mutex);
1049 	mutex_lock(&card->conf_mutex);
1050 	QETH_DBF_TEXT(SETUP, 2, "setonlin");
1051 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1052 
1053 	recover_flag = card->state;
1054 	rc = qeth_core_hardsetup_card(card);
1055 	if (rc) {
1056 		QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
1057 		rc = -ENODEV;
1058 		goto out_remove;
1059 	}
1060 	qeth_bridgeport_query_support(card);
1061 	if (card->options.sbp.supported_funcs)
1062 		dev_info(&card->gdev->dev,
1063 		"The device represents a Bridge Capable Port\n");
1064 
1065 	if (!card->dev && qeth_l2_setup_netdev(card)) {
1066 		rc = -ENODEV;
1067 		goto out_remove;
1068 	}
1069 
1070 	if (card->info.type != QETH_CARD_TYPE_OSN)
1071 		qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
1072 
1073 	if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
1074 		if (card->info.hwtrap &&
1075 		    qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
1076 			card->info.hwtrap = 0;
1077 	} else
1078 		card->info.hwtrap = 0;
1079 
1080 	/* for the rx_bcast characteristic, init VNICC after setmac */
1081 	qeth_l2_vnicc_init(card);
1082 
1083 	qeth_trace_features(card);
1084 	qeth_l2_trace_features(card);
1085 
1086 	qeth_l2_setup_bridgeport_attrs(card);
1087 
1088 	card->state = CARD_STATE_HARDSETUP;
1089 	memset(&card->rx, 0, sizeof(struct qeth_rx));
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 	int rc;
1378 
1379 	if (!dev)
1380 		return -ENODEV;
1381 	card = dev->ml_priv;
1382 	if (!card)
1383 		return -ENODEV;
1384 	QETH_CARD_TEXT(card, 2, "osnsdmc");
1385 	if (!qeth_card_hw_is_reachable(card))
1386 		return -ENODEV;
1387 	iob = qeth_wait_for_buffer(&card->write);
1388 	memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1389 	rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1390 	return rc;
1391 }
1392 EXPORT_SYMBOL(qeth_osn_assist);
1393 
1394 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1395 		  int (*assist_cb)(struct net_device *, void *),
1396 		  int (*data_cb)(struct sk_buff *))
1397 {
1398 	struct qeth_card *card;
1399 
1400 	*dev = qeth_l2_netdev_by_devno(read_dev_no);
1401 	if (*dev == NULL)
1402 		return -ENODEV;
1403 	card = (*dev)->ml_priv;
1404 	if (!card)
1405 		return -ENODEV;
1406 	QETH_CARD_TEXT(card, 2, "osnreg");
1407 	if ((assist_cb == NULL) || (data_cb == NULL))
1408 		return -EINVAL;
1409 	card->osn_info.assist_cb = assist_cb;
1410 	card->osn_info.data_cb = data_cb;
1411 	return 0;
1412 }
1413 EXPORT_SYMBOL(qeth_osn_register);
1414 
1415 void qeth_osn_deregister(struct net_device *dev)
1416 {
1417 	struct qeth_card *card;
1418 
1419 	if (!dev)
1420 		return;
1421 	card = dev->ml_priv;
1422 	if (!card)
1423 		return;
1424 	QETH_CARD_TEXT(card, 2, "osndereg");
1425 	card->osn_info.assist_cb = NULL;
1426 	card->osn_info.data_cb = NULL;
1427 	return;
1428 }
1429 EXPORT_SYMBOL(qeth_osn_deregister);
1430 
1431 /* SETBRIDGEPORT support, async notifications */
1432 
1433 enum qeth_an_event_type {anev_reg_unreg, anev_abort, anev_reset};
1434 
1435 /**
1436  * qeth_bridge_emit_host_event() - bridgeport address change notification
1437  * @card:  qeth_card structure pointer, for udev events.
1438  * @evtype:  "normal" register/unregister, or abort, or reset. For abort
1439  *	      and reset token and addr_lnid are unused and may be NULL.
1440  * @code:  event bitmask: high order bit 0x80 value 1 means removal of an
1441  *			  object, 0 - addition of an object.
1442  *			  0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC.
1443  * @token: "network token" structure identifying physical address of the port.
1444  * @addr_lnid: pointer to structure with MAC address and VLAN ID.
1445  *
1446  * This function is called when registrations and deregistrations are
1447  * reported by the hardware, and also when notifications are enabled -
1448  * for all currently registered addresses.
1449  */
1450 static void qeth_bridge_emit_host_event(struct qeth_card *card,
1451 	enum qeth_an_event_type evtype,
1452 	u8 code, struct net_if_token *token, struct mac_addr_lnid *addr_lnid)
1453 {
1454 	char str[7][32];
1455 	char *env[8];
1456 	int i = 0;
1457 
1458 	switch (evtype) {
1459 	case anev_reg_unreg:
1460 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=%s",
1461 				(code & IPA_ADDR_CHANGE_CODE_REMOVAL)
1462 				? "deregister" : "register");
1463 		env[i] = str[i]; i++;
1464 		if (code & IPA_ADDR_CHANGE_CODE_VLANID) {
1465 			snprintf(str[i], sizeof(str[i]), "VLAN=%d",
1466 				addr_lnid->lnid);
1467 			env[i] = str[i]; i++;
1468 		}
1469 		if (code & IPA_ADDR_CHANGE_CODE_MACADDR) {
1470 			snprintf(str[i], sizeof(str[i]), "MAC=%pM",
1471 				addr_lnid->mac);
1472 			env[i] = str[i]; i++;
1473 		}
1474 		snprintf(str[i], sizeof(str[i]), "NTOK_BUSID=%x.%x.%04x",
1475 			token->cssid, token->ssid, token->devnum);
1476 		env[i] = str[i]; i++;
1477 		snprintf(str[i], sizeof(str[i]), "NTOK_IID=%02x", token->iid);
1478 		env[i] = str[i]; i++;
1479 		snprintf(str[i], sizeof(str[i]), "NTOK_CHPID=%02x",
1480 				token->chpid);
1481 		env[i] = str[i]; i++;
1482 		snprintf(str[i], sizeof(str[i]), "NTOK_CHID=%04x", token->chid);
1483 		env[i] = str[i]; i++;
1484 		break;
1485 	case anev_abort:
1486 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=abort");
1487 		env[i] = str[i]; i++;
1488 		break;
1489 	case anev_reset:
1490 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=reset");
1491 		env[i] = str[i]; i++;
1492 		break;
1493 	}
1494 	env[i] = NULL;
1495 	kobject_uevent_env(&card->gdev->dev.kobj, KOBJ_CHANGE, env);
1496 }
1497 
1498 struct qeth_bridge_state_data {
1499 	struct work_struct worker;
1500 	struct qeth_card *card;
1501 	struct qeth_sbp_state_change qports;
1502 };
1503 
1504 static void qeth_bridge_state_change_worker(struct work_struct *work)
1505 {
1506 	struct qeth_bridge_state_data *data =
1507 		container_of(work, struct qeth_bridge_state_data, worker);
1508 	/* We are only interested in the first entry - local port */
1509 	struct qeth_sbp_port_entry *entry = &data->qports.entry[0];
1510 	char env_locrem[32];
1511 	char env_role[32];
1512 	char env_state[32];
1513 	char *env[] = {
1514 		env_locrem,
1515 		env_role,
1516 		env_state,
1517 		NULL
1518 	};
1519 
1520 	/* Role should not change by itself, but if it did, */
1521 	/* information from the hardware is authoritative.  */
1522 	mutex_lock(&data->card->conf_mutex);
1523 	data->card->options.sbp.role = entry->role;
1524 	mutex_unlock(&data->card->conf_mutex);
1525 
1526 	snprintf(env_locrem, sizeof(env_locrem), "BRIDGEPORT=statechange");
1527 	snprintf(env_role, sizeof(env_role), "ROLE=%s",
1528 		(entry->role == QETH_SBP_ROLE_NONE) ? "none" :
1529 		(entry->role == QETH_SBP_ROLE_PRIMARY) ? "primary" :
1530 		(entry->role == QETH_SBP_ROLE_SECONDARY) ? "secondary" :
1531 		"<INVALID>");
1532 	snprintf(env_state, sizeof(env_state), "STATE=%s",
1533 		(entry->state == QETH_SBP_STATE_INACTIVE) ? "inactive" :
1534 		(entry->state == QETH_SBP_STATE_STANDBY) ? "standby" :
1535 		(entry->state == QETH_SBP_STATE_ACTIVE) ? "active" :
1536 		"<INVALID>");
1537 	kobject_uevent_env(&data->card->gdev->dev.kobj,
1538 				KOBJ_CHANGE, env);
1539 	kfree(data);
1540 }
1541 
1542 static void qeth_bridge_state_change(struct qeth_card *card,
1543 					struct qeth_ipa_cmd *cmd)
1544 {
1545 	struct qeth_sbp_state_change *qports =
1546 		 &cmd->data.sbp.data.state_change;
1547 	struct qeth_bridge_state_data *data;
1548 	int extrasize;
1549 
1550 	QETH_CARD_TEXT(card, 2, "brstchng");
1551 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1552 		QETH_CARD_TEXT_(card, 2, "BPsz%04x", qports->entry_length);
1553 		return;
1554 	}
1555 	extrasize = sizeof(struct qeth_sbp_port_entry) * qports->num_entries;
1556 	data = kzalloc(sizeof(struct qeth_bridge_state_data) + extrasize,
1557 		GFP_ATOMIC);
1558 	if (!data) {
1559 		QETH_CARD_TEXT(card, 2, "BPSalloc");
1560 		return;
1561 	}
1562 	INIT_WORK(&data->worker, qeth_bridge_state_change_worker);
1563 	data->card = card;
1564 	memcpy(&data->qports, qports,
1565 			sizeof(struct qeth_sbp_state_change) + extrasize);
1566 	queue_work(qeth_wq, &data->worker);
1567 }
1568 
1569 struct qeth_bridge_host_data {
1570 	struct work_struct worker;
1571 	struct qeth_card *card;
1572 	struct qeth_ipacmd_addr_change hostevs;
1573 };
1574 
1575 static void qeth_bridge_host_event_worker(struct work_struct *work)
1576 {
1577 	struct qeth_bridge_host_data *data =
1578 		container_of(work, struct qeth_bridge_host_data, worker);
1579 	int i;
1580 
1581 	if (data->hostevs.lost_event_mask) {
1582 		dev_info(&data->card->gdev->dev,
1583 "Address notification from the Bridge Port stopped %s (%s)\n",
1584 			data->card->dev->name,
1585 			(data->hostevs.lost_event_mask == 0x01)
1586 			? "Overflow"
1587 			: (data->hostevs.lost_event_mask == 0x02)
1588 			? "Bridge port state change"
1589 			: "Unknown reason");
1590 		mutex_lock(&data->card->conf_mutex);
1591 		data->card->options.sbp.hostnotification = 0;
1592 		mutex_unlock(&data->card->conf_mutex);
1593 		qeth_bridge_emit_host_event(data->card, anev_abort,
1594 			0, NULL, NULL);
1595 	} else
1596 		for (i = 0; i < data->hostevs.num_entries; i++) {
1597 			struct qeth_ipacmd_addr_change_entry *entry =
1598 					&data->hostevs.entry[i];
1599 			qeth_bridge_emit_host_event(data->card,
1600 					anev_reg_unreg,
1601 					entry->change_code,
1602 					&entry->token, &entry->addr_lnid);
1603 		}
1604 	kfree(data);
1605 }
1606 
1607 static void qeth_bridge_host_event(struct qeth_card *card,
1608 					struct qeth_ipa_cmd *cmd)
1609 {
1610 	struct qeth_ipacmd_addr_change *hostevs =
1611 		 &cmd->data.addrchange;
1612 	struct qeth_bridge_host_data *data;
1613 	int extrasize;
1614 
1615 	QETH_CARD_TEXT(card, 2, "brhostev");
1616 	if (cmd->hdr.return_code != 0x0000) {
1617 		if (cmd->hdr.return_code == 0x0010) {
1618 			if (hostevs->lost_event_mask == 0x00)
1619 				hostevs->lost_event_mask = 0xff;
1620 		} else {
1621 			QETH_CARD_TEXT_(card, 2, "BPHe%04x",
1622 				cmd->hdr.return_code);
1623 			return;
1624 		}
1625 	}
1626 	extrasize = sizeof(struct qeth_ipacmd_addr_change_entry) *
1627 						hostevs->num_entries;
1628 	data = kzalloc(sizeof(struct qeth_bridge_host_data) + extrasize,
1629 		GFP_ATOMIC);
1630 	if (!data) {
1631 		QETH_CARD_TEXT(card, 2, "BPHalloc");
1632 		return;
1633 	}
1634 	INIT_WORK(&data->worker, qeth_bridge_host_event_worker);
1635 	data->card = card;
1636 	memcpy(&data->hostevs, hostevs,
1637 			sizeof(struct qeth_ipacmd_addr_change) + extrasize);
1638 	queue_work(qeth_wq, &data->worker);
1639 }
1640 
1641 /* SETBRIDGEPORT support; sending commands */
1642 
1643 struct _qeth_sbp_cbctl {
1644 	u16 ipa_rc;
1645 	u16 cmd_rc;
1646 	union {
1647 		u32 supported;
1648 		struct {
1649 			enum qeth_sbp_roles *role;
1650 			enum qeth_sbp_states *state;
1651 		} qports;
1652 	} data;
1653 };
1654 
1655 /**
1656  * qeth_bridgeport_makerc() - derive "traditional" error from hardware codes.
1657  * @card:		      qeth_card structure pointer, for debug messages.
1658  * @cbctl:		      state structure with hardware return codes.
1659  * @setcmd:		      IPA command code
1660  *
1661  * Returns negative errno-compatible error indication or 0 on success.
1662  */
1663 static int qeth_bridgeport_makerc(struct qeth_card *card,
1664 	struct _qeth_sbp_cbctl *cbctl, enum qeth_ipa_sbp_cmd setcmd)
1665 {
1666 	int rc;
1667 	int is_iqd = (card->info.type == QETH_CARD_TYPE_IQD);
1668 
1669 	if ((is_iqd && (cbctl->ipa_rc == IPA_RC_SUCCESS)) ||
1670 	    (!is_iqd && (cbctl->ipa_rc == cbctl->cmd_rc)))
1671 		switch (cbctl->cmd_rc) {
1672 		case IPA_RC_SUCCESS:
1673 			rc = 0;
1674 			break;
1675 		case IPA_RC_L2_UNSUPPORTED_CMD:
1676 		case IPA_RC_UNSUPPORTED_COMMAND:
1677 			rc = -EOPNOTSUPP;
1678 			break;
1679 		case IPA_RC_SBP_OSA_NOT_CONFIGURED:
1680 		case IPA_RC_SBP_IQD_NOT_CONFIGURED:
1681 			rc = -ENODEV; /* maybe not the best code here? */
1682 			dev_err(&card->gdev->dev,
1683 	"The device is not configured as a Bridge Port\n");
1684 			break;
1685 		case IPA_RC_SBP_OSA_OS_MISMATCH:
1686 		case IPA_RC_SBP_IQD_OS_MISMATCH:
1687 			rc = -EPERM;
1688 			dev_err(&card->gdev->dev,
1689 	"A Bridge Port is already configured by a different operating system\n");
1690 			break;
1691 		case IPA_RC_SBP_OSA_ANO_DEV_PRIMARY:
1692 		case IPA_RC_SBP_IQD_ANO_DEV_PRIMARY:
1693 			switch (setcmd) {
1694 			case IPA_SBP_SET_PRIMARY_BRIDGE_PORT:
1695 				rc = -EEXIST;
1696 				dev_err(&card->gdev->dev,
1697 	"The LAN already has a primary Bridge Port\n");
1698 				break;
1699 			case IPA_SBP_SET_SECONDARY_BRIDGE_PORT:
1700 				rc = -EBUSY;
1701 				dev_err(&card->gdev->dev,
1702 	"The device is already a primary Bridge Port\n");
1703 				break;
1704 			default:
1705 				rc = -EIO;
1706 			}
1707 			break;
1708 		case IPA_RC_SBP_OSA_CURRENT_SECOND:
1709 		case IPA_RC_SBP_IQD_CURRENT_SECOND:
1710 			rc = -EBUSY;
1711 			dev_err(&card->gdev->dev,
1712 	"The device is already a secondary Bridge Port\n");
1713 			break;
1714 		case IPA_RC_SBP_OSA_LIMIT_SECOND:
1715 		case IPA_RC_SBP_IQD_LIMIT_SECOND:
1716 			rc = -EEXIST;
1717 			dev_err(&card->gdev->dev,
1718 	"The LAN cannot have more secondary Bridge Ports\n");
1719 			break;
1720 		case IPA_RC_SBP_OSA_CURRENT_PRIMARY:
1721 		case IPA_RC_SBP_IQD_CURRENT_PRIMARY:
1722 			rc = -EBUSY;
1723 			dev_err(&card->gdev->dev,
1724 	"The device is already a primary Bridge Port\n");
1725 			break;
1726 		case IPA_RC_SBP_OSA_NOT_AUTHD_BY_ZMAN:
1727 		case IPA_RC_SBP_IQD_NOT_AUTHD_BY_ZMAN:
1728 			rc = -EACCES;
1729 			dev_err(&card->gdev->dev,
1730 	"The device is not authorized to be a Bridge Port\n");
1731 			break;
1732 		default:
1733 			rc = -EIO;
1734 		}
1735 	else
1736 		switch (cbctl->ipa_rc) {
1737 		case IPA_RC_NOTSUPP:
1738 			rc = -EOPNOTSUPP;
1739 			break;
1740 		case IPA_RC_UNSUPPORTED_COMMAND:
1741 			rc = -EOPNOTSUPP;
1742 			break;
1743 		default:
1744 			rc = -EIO;
1745 		}
1746 
1747 	if (rc) {
1748 		QETH_CARD_TEXT_(card, 2, "SBPi%04x", cbctl->ipa_rc);
1749 		QETH_CARD_TEXT_(card, 2, "SBPc%04x", cbctl->cmd_rc);
1750 	}
1751 	return rc;
1752 }
1753 
1754 static struct qeth_cmd_buffer *qeth_sbp_build_cmd(struct qeth_card *card,
1755 						  enum qeth_ipa_sbp_cmd sbp_cmd,
1756 						  unsigned int cmd_length)
1757 {
1758 	enum qeth_ipa_cmds ipa_cmd = (card->info.type == QETH_CARD_TYPE_IQD) ?
1759 					IPA_CMD_SETBRIDGEPORT_IQD :
1760 					IPA_CMD_SETBRIDGEPORT_OSA;
1761 	struct qeth_cmd_buffer *iob;
1762 	struct qeth_ipa_cmd *cmd;
1763 
1764 	iob = qeth_get_ipacmd_buffer(card, ipa_cmd, 0);
1765 	if (!iob)
1766 		return iob;
1767 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1768 	cmd->data.sbp.hdr.cmdlength = sizeof(struct qeth_ipacmd_sbp_hdr) +
1769 				      cmd_length;
1770 	cmd->data.sbp.hdr.command_code = sbp_cmd;
1771 	cmd->data.sbp.hdr.used_total = 1;
1772 	cmd->data.sbp.hdr.seq_no = 1;
1773 	return iob;
1774 }
1775 
1776 static int qeth_bridgeport_query_support_cb(struct qeth_card *card,
1777 	struct qeth_reply *reply, unsigned long data)
1778 {
1779 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1780 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1781 	QETH_CARD_TEXT(card, 2, "brqsupcb");
1782 	cbctl->ipa_rc = cmd->hdr.return_code;
1783 	cbctl->cmd_rc = cmd->data.sbp.hdr.return_code;
1784 	if ((cbctl->ipa_rc == 0) && (cbctl->cmd_rc == 0)) {
1785 		cbctl->data.supported =
1786 			cmd->data.sbp.data.query_cmds_supp.supported_cmds;
1787 	} else {
1788 		cbctl->data.supported = 0;
1789 	}
1790 	return 0;
1791 }
1792 
1793 /**
1794  * qeth_bridgeport_query_support() - store bitmask of supported subfunctions.
1795  * @card:			     qeth_card structure pointer.
1796  *
1797  * Sets bitmask of supported setbridgeport subfunctions in the qeth_card
1798  * strucutre: card->options.sbp.supported_funcs.
1799  */
1800 static void qeth_bridgeport_query_support(struct qeth_card *card)
1801 {
1802 	struct qeth_cmd_buffer *iob;
1803 	struct _qeth_sbp_cbctl cbctl;
1804 
1805 	QETH_CARD_TEXT(card, 2, "brqsuppo");
1806 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_COMMANDS_SUPPORTED,
1807 				 sizeof(struct qeth_sbp_query_cmds_supp));
1808 	if (!iob)
1809 		return;
1810 	if (qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_support_cb,
1811 							(void *)&cbctl) ||
1812 	    qeth_bridgeport_makerc(card, &cbctl,
1813 					IPA_SBP_QUERY_COMMANDS_SUPPORTED)) {
1814 		/* non-zero makerc signifies failure, and produce messages */
1815 		card->options.sbp.role = QETH_SBP_ROLE_NONE;
1816 		return;
1817 	}
1818 	card->options.sbp.supported_funcs = cbctl.data.supported;
1819 }
1820 
1821 static int qeth_bridgeport_query_ports_cb(struct qeth_card *card,
1822 	struct qeth_reply *reply, unsigned long data)
1823 {
1824 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1825 	struct qeth_sbp_query_ports *qports = &cmd->data.sbp.data.query_ports;
1826 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1827 
1828 	QETH_CARD_TEXT(card, 2, "brqprtcb");
1829 	cbctl->ipa_rc = cmd->hdr.return_code;
1830 	cbctl->cmd_rc = cmd->data.sbp.hdr.return_code;
1831 	if ((cbctl->ipa_rc != 0) || (cbctl->cmd_rc != 0))
1832 		return 0;
1833 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1834 		cbctl->cmd_rc = 0xffff;
1835 		QETH_CARD_TEXT_(card, 2, "SBPs%04x", qports->entry_length);
1836 		return 0;
1837 	}
1838 	/* first entry contains the state of the local port */
1839 	if (qports->num_entries > 0) {
1840 		if (cbctl->data.qports.role)
1841 			*cbctl->data.qports.role = qports->entry[0].role;
1842 		if (cbctl->data.qports.state)
1843 			*cbctl->data.qports.state = qports->entry[0].state;
1844 	}
1845 	return 0;
1846 }
1847 
1848 /**
1849  * qeth_bridgeport_query_ports() - query local bridgeport status.
1850  * @card:			   qeth_card structure pointer.
1851  * @role:   Role of the port: 0-none, 1-primary, 2-secondary.
1852  * @state:  State of the port: 0-inactive, 1-standby, 2-active.
1853  *
1854  * Returns negative errno-compatible error indication or 0 on success.
1855  *
1856  * 'role' and 'state' are not updated in case of hardware operation failure.
1857  */
1858 int qeth_bridgeport_query_ports(struct qeth_card *card,
1859 	enum qeth_sbp_roles *role, enum qeth_sbp_states *state)
1860 {
1861 	int rc = 0;
1862 	struct qeth_cmd_buffer *iob;
1863 	struct _qeth_sbp_cbctl cbctl = {
1864 		.data = {
1865 			.qports = {
1866 				.role = role,
1867 				.state = state,
1868 			},
1869 		},
1870 	};
1871 
1872 	QETH_CARD_TEXT(card, 2, "brqports");
1873 	if (!(card->options.sbp.supported_funcs & IPA_SBP_QUERY_BRIDGE_PORTS))
1874 		return -EOPNOTSUPP;
1875 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_BRIDGE_PORTS, 0);
1876 	if (!iob)
1877 		return -ENOMEM;
1878 	rc = qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_ports_cb,
1879 				(void *)&cbctl);
1880 	if (rc < 0)
1881 		return rc;
1882 	return qeth_bridgeport_makerc(card, &cbctl, IPA_SBP_QUERY_BRIDGE_PORTS);
1883 }
1884 EXPORT_SYMBOL_GPL(qeth_bridgeport_query_ports);
1885 
1886 static int qeth_bridgeport_set_cb(struct qeth_card *card,
1887 	struct qeth_reply *reply, unsigned long data)
1888 {
1889 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
1890 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1891 	QETH_CARD_TEXT(card, 2, "brsetrcb");
1892 	cbctl->ipa_rc = cmd->hdr.return_code;
1893 	cbctl->cmd_rc = cmd->data.sbp.hdr.return_code;
1894 	return 0;
1895 }
1896 
1897 /**
1898  * qeth_bridgeport_setrole() - Assign primary role to the port.
1899  * @card:		       qeth_card structure pointer.
1900  * @role:		       Role to assign.
1901  *
1902  * Returns negative errno-compatible error indication or 0 on success.
1903  */
1904 int qeth_bridgeport_setrole(struct qeth_card *card, enum qeth_sbp_roles role)
1905 {
1906 	int rc = 0;
1907 	int cmdlength;
1908 	struct qeth_cmd_buffer *iob;
1909 	struct _qeth_sbp_cbctl cbctl;
1910 	enum qeth_ipa_sbp_cmd setcmd;
1911 
1912 	QETH_CARD_TEXT(card, 2, "brsetrol");
1913 	switch (role) {
1914 	case QETH_SBP_ROLE_NONE:
1915 		setcmd = IPA_SBP_RESET_BRIDGE_PORT_ROLE;
1916 		cmdlength = sizeof(struct qeth_sbp_reset_role);
1917 		break;
1918 	case QETH_SBP_ROLE_PRIMARY:
1919 		setcmd = IPA_SBP_SET_PRIMARY_BRIDGE_PORT;
1920 		cmdlength = sizeof(struct qeth_sbp_set_primary);
1921 		break;
1922 	case QETH_SBP_ROLE_SECONDARY:
1923 		setcmd = IPA_SBP_SET_SECONDARY_BRIDGE_PORT;
1924 		cmdlength = sizeof(struct qeth_sbp_set_secondary);
1925 		break;
1926 	default:
1927 		return -EINVAL;
1928 	}
1929 	if (!(card->options.sbp.supported_funcs & setcmd))
1930 		return -EOPNOTSUPP;
1931 	iob = qeth_sbp_build_cmd(card, setcmd, cmdlength);
1932 	if (!iob)
1933 		return -ENOMEM;
1934 	rc = qeth_send_ipa_cmd(card, iob, qeth_bridgeport_set_cb,
1935 				(void *)&cbctl);
1936 	if (rc < 0)
1937 		return rc;
1938 	return qeth_bridgeport_makerc(card, &cbctl, setcmd);
1939 }
1940 
1941 /**
1942  * qeth_anset_makerc() - derive "traditional" error from hardware codes.
1943  * @card:		      qeth_card structure pointer, for debug messages.
1944  *
1945  * Returns negative errno-compatible error indication or 0 on success.
1946  */
1947 static int qeth_anset_makerc(struct qeth_card *card, int pnso_rc, u16 response)
1948 {
1949 	int rc;
1950 
1951 	if (pnso_rc == 0)
1952 		switch (response) {
1953 		case 0x0001:
1954 			rc = 0;
1955 			break;
1956 		case 0x0004:
1957 		case 0x0100:
1958 		case 0x0106:
1959 			rc = -EOPNOTSUPP;
1960 			dev_err(&card->gdev->dev,
1961 				"Setting address notification failed\n");
1962 			break;
1963 		case 0x0107:
1964 			rc = -EAGAIN;
1965 			break;
1966 		default:
1967 			rc = -EIO;
1968 		}
1969 	else
1970 		rc = -EIO;
1971 
1972 	if (rc) {
1973 		QETH_CARD_TEXT_(card, 2, "SBPp%04x", pnso_rc);
1974 		QETH_CARD_TEXT_(card, 2, "SBPr%04x", response);
1975 	}
1976 	return rc;
1977 }
1978 
1979 static void qeth_bridgeport_an_set_cb(void *priv,
1980 		enum qdio_brinfo_entry_type type, void *entry)
1981 {
1982 	struct qeth_card *card = (struct qeth_card *)priv;
1983 	struct qdio_brinfo_entry_l2 *l2entry;
1984 	u8 code;
1985 
1986 	if (type != l2_addr_lnid) {
1987 		WARN_ON_ONCE(1);
1988 		return;
1989 	}
1990 
1991 	l2entry = (struct qdio_brinfo_entry_l2 *)entry;
1992 	code = IPA_ADDR_CHANGE_CODE_MACADDR;
1993 	if (l2entry->addr_lnid.lnid)
1994 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
1995 	qeth_bridge_emit_host_event(card, anev_reg_unreg, code,
1996 		(struct net_if_token *)&l2entry->nit,
1997 		(struct mac_addr_lnid *)&l2entry->addr_lnid);
1998 }
1999 
2000 /**
2001  * qeth_bridgeport_an_set() - Enable or disable bridgeport address notification
2002  * @card:		      qeth_card structure pointer.
2003  * @enable:		      0 - disable, non-zero - enable notifications
2004  *
2005  * Returns negative errno-compatible error indication or 0 on success.
2006  *
2007  * On enable, emits a series of address notifications udev events for all
2008  * currently registered hosts.
2009  */
2010 int qeth_bridgeport_an_set(struct qeth_card *card, int enable)
2011 {
2012 	int rc;
2013 	u16 response;
2014 	struct ccw_device *ddev;
2015 	struct subchannel_id schid;
2016 
2017 	if (!card)
2018 		return -EINVAL;
2019 	if (!card->options.sbp.supported_funcs)
2020 		return -EOPNOTSUPP;
2021 	ddev = CARD_DDEV(card);
2022 	ccw_device_get_schid(ddev, &schid);
2023 
2024 	if (enable) {
2025 		qeth_bridge_emit_host_event(card, anev_reset, 0, NULL, NULL);
2026 		rc = qdio_pnso_brinfo(schid, 1, &response,
2027 			qeth_bridgeport_an_set_cb, card);
2028 	} else
2029 		rc = qdio_pnso_brinfo(schid, 0, &response, NULL, NULL);
2030 	return qeth_anset_makerc(card, rc, response);
2031 }
2032 EXPORT_SYMBOL_GPL(qeth_bridgeport_an_set);
2033 
2034 static bool qeth_bridgeport_is_in_use(struct qeth_card *card)
2035 {
2036 	return (card->options.sbp.role || card->options.sbp.reflect_promisc ||
2037 		card->options.sbp.hostnotification);
2038 }
2039 
2040 /* VNIC Characteristics support */
2041 
2042 /* handle VNICC IPA command return codes; convert to error codes */
2043 static int qeth_l2_vnicc_makerc(struct qeth_card *card, int ipa_rc)
2044 {
2045 	int rc;
2046 
2047 	switch (ipa_rc) {
2048 	case IPA_RC_SUCCESS:
2049 		return ipa_rc;
2050 	case IPA_RC_L2_UNSUPPORTED_CMD:
2051 	case IPA_RC_NOTSUPP:
2052 		rc = -EOPNOTSUPP;
2053 		break;
2054 	case IPA_RC_VNICC_OOSEQ:
2055 		rc = -EALREADY;
2056 		break;
2057 	case IPA_RC_VNICC_VNICBP:
2058 		rc = -EBUSY;
2059 		break;
2060 	case IPA_RC_L2_ADDR_TABLE_FULL:
2061 		rc = -ENOSPC;
2062 		break;
2063 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
2064 		rc = -EACCES;
2065 		break;
2066 	default:
2067 		rc = -EIO;
2068 	}
2069 
2070 	QETH_CARD_TEXT_(card, 2, "err%04x", ipa_rc);
2071 	return rc;
2072 }
2073 
2074 /* generic VNICC request call back control */
2075 struct _qeth_l2_vnicc_request_cbctl {
2076 	u32 sub_cmd;
2077 	struct {
2078 		u32 vnic_char;
2079 		u32 timeout;
2080 	} param;
2081 	struct {
2082 		union{
2083 			u32 *sup_cmds;
2084 			u32 *timeout;
2085 		};
2086 	} result;
2087 };
2088 
2089 /* generic VNICC request call back */
2090 static int qeth_l2_vnicc_request_cb(struct qeth_card *card,
2091 				    struct qeth_reply *reply,
2092 				    unsigned long data)
2093 {
2094 	struct _qeth_l2_vnicc_request_cbctl *cbctl =
2095 		(struct _qeth_l2_vnicc_request_cbctl *) reply->param;
2096 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
2097 	struct qeth_ipacmd_vnicc *rep = &cmd->data.vnicc;
2098 
2099 	QETH_CARD_TEXT(card, 2, "vniccrcb");
2100 	if (cmd->hdr.return_code)
2101 		return 0;
2102 	/* return results to caller */
2103 	card->options.vnicc.sup_chars = rep->hdr.sup;
2104 	card->options.vnicc.cur_chars = rep->hdr.cur;
2105 
2106 	if (cbctl->sub_cmd == IPA_VNICC_QUERY_CMDS)
2107 		*cbctl->result.sup_cmds = rep->query_cmds.sup_cmds;
2108 
2109 	if (cbctl->sub_cmd == IPA_VNICC_GET_TIMEOUT)
2110 		*cbctl->result.timeout = rep->getset_timeout.timeout;
2111 
2112 	return 0;
2113 }
2114 
2115 /* generic VNICC request */
2116 static int qeth_l2_vnicc_request(struct qeth_card *card,
2117 				 struct _qeth_l2_vnicc_request_cbctl *cbctl)
2118 {
2119 	struct qeth_ipacmd_vnicc *req;
2120 	struct qeth_cmd_buffer *iob;
2121 	struct qeth_ipa_cmd *cmd;
2122 	int rc;
2123 
2124 	QETH_CARD_TEXT(card, 2, "vniccreq");
2125 
2126 	/* get new buffer for request */
2127 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_VNICC, 0);
2128 	if (!iob)
2129 		return -ENOMEM;
2130 
2131 	/* create header for request */
2132 	cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
2133 	req = &cmd->data.vnicc;
2134 
2135 	/* create sub command header for request */
2136 	req->sub_hdr.data_length = sizeof(req->sub_hdr);
2137 	req->sub_hdr.sub_command = cbctl->sub_cmd;
2138 
2139 	/* create sub command specific request fields */
2140 	switch (cbctl->sub_cmd) {
2141 	case IPA_VNICC_QUERY_CHARS:
2142 		break;
2143 	case IPA_VNICC_QUERY_CMDS:
2144 		req->sub_hdr.data_length += sizeof(req->query_cmds);
2145 		req->query_cmds.vnic_char = cbctl->param.vnic_char;
2146 		break;
2147 	case IPA_VNICC_ENABLE:
2148 	case IPA_VNICC_DISABLE:
2149 		req->sub_hdr.data_length += sizeof(req->set_char);
2150 		req->set_char.vnic_char = cbctl->param.vnic_char;
2151 		break;
2152 	case IPA_VNICC_SET_TIMEOUT:
2153 		req->getset_timeout.timeout = cbctl->param.timeout;
2154 		/* fallthrough */
2155 	case IPA_VNICC_GET_TIMEOUT:
2156 		req->sub_hdr.data_length += sizeof(req->getset_timeout);
2157 		req->getset_timeout.vnic_char = cbctl->param.vnic_char;
2158 		break;
2159 	default:
2160 		qeth_release_buffer(iob->channel, iob);
2161 		return -EOPNOTSUPP;
2162 	}
2163 
2164 	/* send request */
2165 	rc = qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb,
2166 			       (void *) cbctl);
2167 
2168 	return qeth_l2_vnicc_makerc(card, rc);
2169 }
2170 
2171 /* VNICC query VNIC characteristics request */
2172 static int qeth_l2_vnicc_query_chars(struct qeth_card *card)
2173 {
2174 	struct _qeth_l2_vnicc_request_cbctl cbctl;
2175 
2176 	/* prepare callback control */
2177 	cbctl.sub_cmd = IPA_VNICC_QUERY_CHARS;
2178 
2179 	QETH_CARD_TEXT(card, 2, "vniccqch");
2180 	return qeth_l2_vnicc_request(card, &cbctl);
2181 }
2182 
2183 /* VNICC query sub commands request */
2184 static int qeth_l2_vnicc_query_cmds(struct qeth_card *card, u32 vnic_char,
2185 				    u32 *sup_cmds)
2186 {
2187 	struct _qeth_l2_vnicc_request_cbctl cbctl;
2188 
2189 	/* prepare callback control */
2190 	cbctl.sub_cmd = IPA_VNICC_QUERY_CMDS;
2191 	cbctl.param.vnic_char = vnic_char;
2192 	cbctl.result.sup_cmds = sup_cmds;
2193 
2194 	QETH_CARD_TEXT(card, 2, "vniccqcm");
2195 	return qeth_l2_vnicc_request(card, &cbctl);
2196 }
2197 
2198 /* VNICC enable/disable characteristic request */
2199 static int qeth_l2_vnicc_set_char(struct qeth_card *card, u32 vnic_char,
2200 				      u32 cmd)
2201 {
2202 	struct _qeth_l2_vnicc_request_cbctl cbctl;
2203 
2204 	/* prepare callback control */
2205 	cbctl.sub_cmd = cmd;
2206 	cbctl.param.vnic_char = vnic_char;
2207 
2208 	QETH_CARD_TEXT(card, 2, "vniccedc");
2209 	return qeth_l2_vnicc_request(card, &cbctl);
2210 }
2211 
2212 /* VNICC get/set timeout for characteristic request */
2213 static int qeth_l2_vnicc_getset_timeout(struct qeth_card *card, u32 vnicc,
2214 					u32 cmd, u32 *timeout)
2215 {
2216 	struct _qeth_l2_vnicc_request_cbctl cbctl;
2217 
2218 	/* prepare callback control */
2219 	cbctl.sub_cmd = cmd;
2220 	cbctl.param.vnic_char = vnicc;
2221 	if (cmd == IPA_VNICC_SET_TIMEOUT)
2222 		cbctl.param.timeout = *timeout;
2223 	if (cmd == IPA_VNICC_GET_TIMEOUT)
2224 		cbctl.result.timeout = timeout;
2225 
2226 	QETH_CARD_TEXT(card, 2, "vniccgst");
2227 	return qeth_l2_vnicc_request(card, &cbctl);
2228 }
2229 
2230 /* set current VNICC flag state; called from sysfs store function */
2231 int qeth_l2_vnicc_set_state(struct qeth_card *card, u32 vnicc, bool state)
2232 {
2233 	int rc = 0;
2234 	u32 cmd;
2235 
2236 	QETH_CARD_TEXT(card, 2, "vniccsch");
2237 
2238 	/* do not change anything if BridgePort is enabled */
2239 	if (qeth_bridgeport_is_in_use(card))
2240 		return -EBUSY;
2241 
2242 	/* check if characteristic and enable/disable are supported */
2243 	if (!(card->options.vnicc.sup_chars & vnicc) ||
2244 	    !(card->options.vnicc.set_char_sup & vnicc))
2245 		return -EOPNOTSUPP;
2246 
2247 	/* set enable/disable command and store wanted characteristic */
2248 	if (state) {
2249 		cmd = IPA_VNICC_ENABLE;
2250 		card->options.vnicc.wanted_chars |= vnicc;
2251 	} else {
2252 		cmd = IPA_VNICC_DISABLE;
2253 		card->options.vnicc.wanted_chars &= ~vnicc;
2254 	}
2255 
2256 	/* do we need to do anything? */
2257 	if (card->options.vnicc.cur_chars == card->options.vnicc.wanted_chars)
2258 		return rc;
2259 
2260 	/* if card is not ready, simply stop here */
2261 	if (!qeth_card_hw_is_reachable(card)) {
2262 		if (state)
2263 			card->options.vnicc.cur_chars |= vnicc;
2264 		else
2265 			card->options.vnicc.cur_chars &= ~vnicc;
2266 		return rc;
2267 	}
2268 
2269 	rc = qeth_l2_vnicc_set_char(card, vnicc, cmd);
2270 	if (rc)
2271 		card->options.vnicc.wanted_chars =
2272 			card->options.vnicc.cur_chars;
2273 	else {
2274 		/* successful online VNICC change; handle special cases */
2275 		if (state && vnicc == QETH_VNICC_RX_BCAST)
2276 			card->options.vnicc.rx_bcast_enabled = true;
2277 		if (!state && vnicc == QETH_VNICC_LEARNING)
2278 			qeth_l2_vnicc_recover_timeout(card, vnicc,
2279 					&card->options.vnicc.learning_timeout);
2280 	}
2281 
2282 	return rc;
2283 }
2284 
2285 /* get current VNICC flag state; called from sysfs show function */
2286 int qeth_l2_vnicc_get_state(struct qeth_card *card, u32 vnicc, bool *state)
2287 {
2288 	int rc = 0;
2289 
2290 	QETH_CARD_TEXT(card, 2, "vniccgch");
2291 
2292 	/* do not get anything if BridgePort is enabled */
2293 	if (qeth_bridgeport_is_in_use(card))
2294 		return -EBUSY;
2295 
2296 	/* check if characteristic is supported */
2297 	if (!(card->options.vnicc.sup_chars & vnicc))
2298 		return -EOPNOTSUPP;
2299 
2300 	/* if card is ready, query current VNICC state */
2301 	if (qeth_card_hw_is_reachable(card))
2302 		rc = qeth_l2_vnicc_query_chars(card);
2303 
2304 	*state = (card->options.vnicc.cur_chars & vnicc) ? true : false;
2305 	return rc;
2306 }
2307 
2308 /* set VNICC timeout; called from sysfs store function. Currently, only learning
2309  * supports timeout
2310  */
2311 int qeth_l2_vnicc_set_timeout(struct qeth_card *card, u32 timeout)
2312 {
2313 	int rc = 0;
2314 
2315 	QETH_CARD_TEXT(card, 2, "vniccsto");
2316 
2317 	/* do not change anything if BridgePort is enabled */
2318 	if (qeth_bridgeport_is_in_use(card))
2319 		return -EBUSY;
2320 
2321 	/* check if characteristic and set_timeout are supported */
2322 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2323 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2324 		return -EOPNOTSUPP;
2325 
2326 	/* do we need to do anything? */
2327 	if (card->options.vnicc.learning_timeout == timeout)
2328 		return rc;
2329 
2330 	/* if card is not ready, simply store the value internally and return */
2331 	if (!qeth_card_hw_is_reachable(card)) {
2332 		card->options.vnicc.learning_timeout = timeout;
2333 		return rc;
2334 	}
2335 
2336 	/* send timeout value to card; if successful, store value internally */
2337 	rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2338 					  IPA_VNICC_SET_TIMEOUT, &timeout);
2339 	if (!rc)
2340 		card->options.vnicc.learning_timeout = timeout;
2341 
2342 	return rc;
2343 }
2344 
2345 /* get current VNICC timeout; called from sysfs show function. Currently, only
2346  * learning supports timeout
2347  */
2348 int qeth_l2_vnicc_get_timeout(struct qeth_card *card, u32 *timeout)
2349 {
2350 	int rc = 0;
2351 
2352 	QETH_CARD_TEXT(card, 2, "vniccgto");
2353 
2354 	/* do not get anything if BridgePort is enabled */
2355 	if (qeth_bridgeport_is_in_use(card))
2356 		return -EBUSY;
2357 
2358 	/* check if characteristic and get_timeout are supported */
2359 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2360 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2361 		return -EOPNOTSUPP;
2362 	/* if card is ready, get timeout. Otherwise, just return stored value */
2363 	*timeout = card->options.vnicc.learning_timeout;
2364 	if (qeth_card_hw_is_reachable(card))
2365 		rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2366 						  IPA_VNICC_GET_TIMEOUT,
2367 						  timeout);
2368 
2369 	return rc;
2370 }
2371 
2372 /* check if VNICC is currently enabled */
2373 bool qeth_l2_vnicc_is_in_use(struct qeth_card *card)
2374 {
2375 	/* if everything is turned off, VNICC is not active */
2376 	if (!card->options.vnicc.cur_chars)
2377 		return false;
2378 	/* default values are only OK if rx_bcast was not enabled by user
2379 	 * or the card is offline.
2380 	 */
2381 	if (card->options.vnicc.cur_chars == QETH_VNICC_DEFAULT) {
2382 		if (!card->options.vnicc.rx_bcast_enabled ||
2383 		    !qeth_card_hw_is_reachable(card))
2384 			return false;
2385 	}
2386 	return true;
2387 }
2388 
2389 /* recover user timeout setting */
2390 static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
2391 					  u32 *timeout)
2392 {
2393 	if (card->options.vnicc.sup_chars & vnicc &&
2394 	    card->options.vnicc.getset_timeout_sup & vnicc &&
2395 	    !qeth_l2_vnicc_getset_timeout(card, vnicc, IPA_VNICC_SET_TIMEOUT,
2396 					  timeout))
2397 		return false;
2398 	*timeout = QETH_VNICC_DEFAULT_TIMEOUT;
2399 	return true;
2400 }
2401 
2402 /* recover user characteristic setting */
2403 static bool qeth_l2_vnicc_recover_char(struct qeth_card *card, u32 vnicc,
2404 				       bool enable)
2405 {
2406 	u32 cmd = enable ? IPA_VNICC_ENABLE : IPA_VNICC_DISABLE;
2407 
2408 	if (card->options.vnicc.sup_chars & vnicc &&
2409 	    card->options.vnicc.set_char_sup & vnicc &&
2410 	    !qeth_l2_vnicc_set_char(card, vnicc, cmd))
2411 		return false;
2412 	card->options.vnicc.wanted_chars &= ~vnicc;
2413 	card->options.vnicc.wanted_chars |= QETH_VNICC_DEFAULT & vnicc;
2414 	return true;
2415 }
2416 
2417 /* (re-)initialize VNICC */
2418 static void qeth_l2_vnicc_init(struct qeth_card *card)
2419 {
2420 	u32 *timeout = &card->options.vnicc.learning_timeout;
2421 	unsigned int chars_len, i;
2422 	unsigned long chars_tmp;
2423 	u32 sup_cmds, vnicc;
2424 	bool enable, error;
2425 
2426 	QETH_CARD_TEXT(card, 2, "vniccini");
2427 	/* reset rx_bcast */
2428 	card->options.vnicc.rx_bcast_enabled = 0;
2429 	/* initial query and storage of VNIC characteristics */
2430 	if (qeth_l2_vnicc_query_chars(card)) {
2431 		if (card->options.vnicc.wanted_chars != QETH_VNICC_DEFAULT ||
2432 		    *timeout != QETH_VNICC_DEFAULT_TIMEOUT)
2433 			dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2434 		/* fail quietly if user didn't change the default config */
2435 		card->options.vnicc.sup_chars = 0;
2436 		card->options.vnicc.cur_chars = 0;
2437 		card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2438 		return;
2439 	}
2440 	/* get supported commands for each supported characteristic */
2441 	chars_tmp = card->options.vnicc.sup_chars;
2442 	chars_len = sizeof(card->options.vnicc.sup_chars) * BITS_PER_BYTE;
2443 	for_each_set_bit(i, &chars_tmp, chars_len) {
2444 		vnicc = BIT(i);
2445 		qeth_l2_vnicc_query_cmds(card, vnicc, &sup_cmds);
2446 		if (!(sup_cmds & IPA_VNICC_SET_TIMEOUT) ||
2447 		    !(sup_cmds & IPA_VNICC_GET_TIMEOUT))
2448 			card->options.vnicc.getset_timeout_sup &= ~vnicc;
2449 		if (!(sup_cmds & IPA_VNICC_ENABLE) ||
2450 		    !(sup_cmds & IPA_VNICC_DISABLE))
2451 			card->options.vnicc.set_char_sup &= ~vnicc;
2452 	}
2453 	/* enforce assumed default values and recover settings, if changed  */
2454 	error = qeth_l2_vnicc_recover_timeout(card, QETH_VNICC_LEARNING,
2455 					      timeout);
2456 	chars_tmp = card->options.vnicc.wanted_chars ^ QETH_VNICC_DEFAULT;
2457 	chars_tmp |= QETH_VNICC_BRIDGE_INVISIBLE;
2458 	chars_len = sizeof(card->options.vnicc.wanted_chars) * BITS_PER_BYTE;
2459 	for_each_set_bit(i, &chars_tmp, chars_len) {
2460 		vnicc = BIT(i);
2461 		enable = card->options.vnicc.wanted_chars & vnicc;
2462 		error |= qeth_l2_vnicc_recover_char(card, vnicc, enable);
2463 	}
2464 	if (error)
2465 		dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2466 }
2467 
2468 /* configure default values of VNIC characteristics */
2469 static void qeth_l2_vnicc_set_defaults(struct qeth_card *card)
2470 {
2471 	/* characteristics values */
2472 	card->options.vnicc.sup_chars = QETH_VNICC_ALL;
2473 	card->options.vnicc.cur_chars = QETH_VNICC_DEFAULT;
2474 	card->options.vnicc.learning_timeout = QETH_VNICC_DEFAULT_TIMEOUT;
2475 	/* supported commands */
2476 	card->options.vnicc.set_char_sup = QETH_VNICC_ALL;
2477 	card->options.vnicc.getset_timeout_sup = QETH_VNICC_LEARNING;
2478 	/* settings wanted by users */
2479 	card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2480 }
2481 
2482 module_init(qeth_l2_init);
2483 module_exit(qeth_l2_exit);
2484 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2485 MODULE_DESCRIPTION("qeth layer 2 discipline");
2486 MODULE_LICENSE("GPL");
2487