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