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