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