xref: /linux/drivers/s390/net/qeth_l2_main.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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, 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 					(data_offset > 0) ? data_offset : 0);
899 	if (!elements) {
900 		if (data_offset >= 0)
901 			kmem_cache_free(qeth_core_header_cache, hdr);
902 		goto tx_drop;
903 	}
904 
905 	if (card->info.type != QETH_CARD_TYPE_IQD) {
906 		if (qeth_hdr_chk_and_bounce(new_skb, &hdr,
907 		    sizeof(struct qeth_hdr_layer2)))
908 			goto tx_drop;
909 		rc = qeth_do_send_packet(card, queue, new_skb, hdr,
910 					 elements);
911 	} else
912 		rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
913 					elements, data_offset, hd_len);
914 	if (!rc) {
915 		card->stats.tx_packets++;
916 		card->stats.tx_bytes += tx_bytes;
917 		if (card->options.performance_stats) {
918 			nr_frags = skb_shinfo(new_skb)->nr_frags;
919 			if (nr_frags) {
920 				card->perf_stats.sg_skbs_sent++;
921 				/* nr_frags + skb->data */
922 				card->perf_stats.sg_frags_sent += nr_frags + 1;
923 			}
924 		}
925 		if (new_skb != skb)
926 			dev_kfree_skb_any(skb);
927 		rc = NETDEV_TX_OK;
928 	} else {
929 		if (data_offset >= 0)
930 			kmem_cache_free(qeth_core_header_cache, hdr);
931 
932 		if (rc == -EBUSY) {
933 			if (new_skb != skb)
934 				dev_kfree_skb_any(new_skb);
935 			return NETDEV_TX_BUSY;
936 		} else
937 			goto tx_drop;
938 	}
939 
940 	netif_wake_queue(dev);
941 	if (card->options.performance_stats)
942 		card->perf_stats.outbound_time += qeth_get_micros() -
943 			card->perf_stats.outbound_start_time;
944 	return rc;
945 
946 tx_drop:
947 	card->stats.tx_dropped++;
948 	card->stats.tx_errors++;
949 	if ((new_skb != skb) && new_skb)
950 		dev_kfree_skb_any(new_skb);
951 	dev_kfree_skb_any(skb);
952 	netif_wake_queue(dev);
953 	return NETDEV_TX_OK;
954 }
955 
956 static int __qeth_l2_open(struct net_device *dev)
957 {
958 	struct qeth_card *card = dev->ml_priv;
959 	int rc = 0;
960 
961 	QETH_CARD_TEXT(card, 4, "qethopen");
962 	if (card->state == CARD_STATE_UP)
963 		return rc;
964 	if (card->state != CARD_STATE_SOFTSETUP)
965 		return -ENODEV;
966 
967 	if ((card->info.type != QETH_CARD_TYPE_OSN) &&
968 	     (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
969 		QETH_CARD_TEXT(card, 4, "nomacadr");
970 		return -EPERM;
971 	}
972 	card->data.state = CH_STATE_UP;
973 	card->state = CARD_STATE_UP;
974 	netif_start_queue(dev);
975 
976 	if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
977 		napi_enable(&card->napi);
978 		napi_schedule(&card->napi);
979 	} else
980 		rc = -EIO;
981 	return rc;
982 }
983 
984 static int qeth_l2_open(struct net_device *dev)
985 {
986 	struct qeth_card *card = dev->ml_priv;
987 
988 	QETH_CARD_TEXT(card, 5, "qethope_");
989 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
990 		QETH_CARD_TEXT(card, 3, "openREC");
991 		return -ERESTARTSYS;
992 	}
993 	return __qeth_l2_open(dev);
994 }
995 
996 static int qeth_l2_stop(struct net_device *dev)
997 {
998 	struct qeth_card *card = dev->ml_priv;
999 
1000 	QETH_CARD_TEXT(card, 4, "qethstop");
1001 	netif_tx_disable(dev);
1002 	if (card->state == CARD_STATE_UP) {
1003 		card->state = CARD_STATE_SOFTSETUP;
1004 		napi_disable(&card->napi);
1005 	}
1006 	return 0;
1007 }
1008 
1009 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
1010 {
1011 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1012 
1013 	qeth_l2_create_device_attributes(&gdev->dev);
1014 	INIT_LIST_HEAD(&card->vid_list);
1015 	hash_init(card->mac_htable);
1016 	card->options.layer2 = 1;
1017 	card->info.hwtrap = 0;
1018 	return 0;
1019 }
1020 
1021 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
1022 {
1023 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1024 
1025 	qeth_l2_remove_device_attributes(&cgdev->dev);
1026 	qeth_set_allowed_threads(card, 0, 1);
1027 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
1028 
1029 	if (cgdev->state == CCWGROUP_ONLINE)
1030 		qeth_l2_set_offline(cgdev);
1031 
1032 	if (card->dev) {
1033 		netif_napi_del(&card->napi);
1034 		unregister_netdev(card->dev);
1035 		card->dev = NULL;
1036 	}
1037 	return;
1038 }
1039 
1040 static const struct ethtool_ops qeth_l2_ethtool_ops = {
1041 	.get_link = ethtool_op_get_link,
1042 	.get_strings = qeth_core_get_strings,
1043 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
1044 	.get_sset_count = qeth_core_get_sset_count,
1045 	.get_drvinfo = qeth_core_get_drvinfo,
1046 	.get_settings = qeth_core_ethtool_get_settings,
1047 };
1048 
1049 static const struct ethtool_ops qeth_l2_osn_ops = {
1050 	.get_strings = qeth_core_get_strings,
1051 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
1052 	.get_sset_count = qeth_core_get_sset_count,
1053 	.get_drvinfo = qeth_core_get_drvinfo,
1054 };
1055 
1056 static const struct net_device_ops qeth_l2_netdev_ops = {
1057 	.ndo_open		= qeth_l2_open,
1058 	.ndo_stop		= qeth_l2_stop,
1059 	.ndo_get_stats		= qeth_get_stats,
1060 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
1061 	.ndo_validate_addr	= eth_validate_addr,
1062 	.ndo_set_rx_mode	= qeth_l2_set_rx_mode,
1063 	.ndo_do_ioctl	   	= qeth_l2_do_ioctl,
1064 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
1065 	.ndo_change_mtu	   	= qeth_change_mtu,
1066 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
1067 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
1068 	.ndo_tx_timeout	   	= qeth_tx_timeout,
1069 	.ndo_fix_features	= qeth_fix_features,
1070 	.ndo_set_features	= qeth_set_features
1071 };
1072 
1073 static int qeth_l2_setup_netdev(struct qeth_card *card)
1074 {
1075 	switch (card->info.type) {
1076 	case QETH_CARD_TYPE_IQD:
1077 		card->dev = alloc_netdev(0, "hsi%d", NET_NAME_UNKNOWN,
1078 					 ether_setup);
1079 		break;
1080 	case QETH_CARD_TYPE_OSN:
1081 		card->dev = alloc_netdev(0, "osn%d", NET_NAME_UNKNOWN,
1082 					 ether_setup);
1083 		card->dev->flags |= IFF_NOARP;
1084 		break;
1085 	default:
1086 		card->dev = alloc_etherdev(0);
1087 	}
1088 
1089 	if (!card->dev)
1090 		return -ENODEV;
1091 
1092 	card->dev->ml_priv = card;
1093 	card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
1094 	card->dev->mtu = card->info.initial_mtu;
1095 	card->dev->min_mtu = 64;
1096 	card->dev->max_mtu = ETH_MAX_MTU;
1097 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
1098 	card->dev->ethtool_ops =
1099 		(card->info.type != QETH_CARD_TYPE_OSN) ?
1100 		&qeth_l2_ethtool_ops : &qeth_l2_osn_ops;
1101 	card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1102 	if (card->info.type == QETH_CARD_TYPE_OSD && !card->info.guestlan) {
1103 		card->dev->hw_features = NETIF_F_SG;
1104 		card->dev->vlan_features = NETIF_F_SG;
1105 		/* OSA 3S and earlier has no RX/TX support */
1106 		if (qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM)) {
1107 			card->dev->hw_features |= NETIF_F_IP_CSUM;
1108 			card->dev->vlan_features |= NETIF_F_IP_CSUM;
1109 		}
1110 		if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM)) {
1111 			card->dev->hw_features |= NETIF_F_RXCSUM;
1112 			card->dev->vlan_features |= NETIF_F_RXCSUM;
1113 		}
1114 	}
1115 	card->info.broadcast_capable = 1;
1116 	qeth_l2_request_initial_mac(card);
1117 	card->dev->gso_max_size = (QETH_MAX_BUFFER_ELEMENTS(card) - 1) *
1118 				  PAGE_SIZE;
1119 	SET_NETDEV_DEV(card->dev, &card->gdev->dev);
1120 	netif_napi_add(card->dev, &card->napi, qeth_l2_poll, QETH_NAPI_WEIGHT);
1121 	netif_carrier_off(card->dev);
1122 	return register_netdev(card->dev);
1123 }
1124 
1125 static int qeth_l2_start_ipassists(struct qeth_card *card)
1126 {
1127 	/* configure isolation level */
1128 	if (qeth_set_access_ctrl_online(card, 0))
1129 		return -ENODEV;
1130 	return 0;
1131 }
1132 
1133 static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
1134 {
1135 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1136 	int rc = 0;
1137 	enum qeth_card_states recover_flag;
1138 
1139 	mutex_lock(&card->discipline_mutex);
1140 	mutex_lock(&card->conf_mutex);
1141 	QETH_DBF_TEXT(SETUP, 2, "setonlin");
1142 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1143 
1144 	recover_flag = card->state;
1145 	rc = qeth_core_hardsetup_card(card);
1146 	if (rc) {
1147 		QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
1148 		rc = -ENODEV;
1149 		goto out_remove;
1150 	}
1151 	qeth_bridgeport_query_support(card);
1152 	if (card->options.sbp.supported_funcs)
1153 		dev_info(&card->gdev->dev,
1154 		"The device represents a Bridge Capable Port\n");
1155 	qeth_trace_features(card);
1156 
1157 	if (!card->dev && qeth_l2_setup_netdev(card)) {
1158 		rc = -ENODEV;
1159 		goto out_remove;
1160 	}
1161 
1162 	if (card->info.type != QETH_CARD_TYPE_OSN)
1163 		qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
1164 
1165 	if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
1166 		if (card->info.hwtrap &&
1167 		    qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
1168 			card->info.hwtrap = 0;
1169 	} else
1170 		card->info.hwtrap = 0;
1171 
1172 	qeth_l2_setup_bridgeport_attrs(card);
1173 
1174 	card->state = CARD_STATE_HARDSETUP;
1175 	memset(&card->rx, 0, sizeof(struct qeth_rx));
1176 	qeth_print_status_message(card);
1177 
1178 	/* softsetup */
1179 	QETH_DBF_TEXT(SETUP, 2, "softsetp");
1180 
1181 	if ((card->info.type == QETH_CARD_TYPE_OSD) ||
1182 	    (card->info.type == QETH_CARD_TYPE_OSX)) {
1183 		rc = qeth_l2_start_ipassists(card);
1184 		if (rc)
1185 			goto out_remove;
1186 	}
1187 
1188 	if (card->info.type != QETH_CARD_TYPE_OSN &&
1189 	    card->info.type != QETH_CARD_TYPE_OSM)
1190 		qeth_l2_process_vlans(card);
1191 
1192 	netif_tx_disable(card->dev);
1193 
1194 	rc = qeth_init_qdio_queues(card);
1195 	if (rc) {
1196 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
1197 		rc = -ENODEV;
1198 		goto out_remove;
1199 	}
1200 	card->state = CARD_STATE_SOFTSETUP;
1201 	if (card->lan_online)
1202 		netif_carrier_on(card->dev);
1203 	else
1204 		netif_carrier_off(card->dev);
1205 
1206 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1207 	if (recover_flag == CARD_STATE_RECOVER) {
1208 		if (recovery_mode &&
1209 		    card->info.type != QETH_CARD_TYPE_OSN) {
1210 			__qeth_l2_open(card->dev);
1211 		} else {
1212 			rtnl_lock();
1213 			dev_open(card->dev);
1214 			rtnl_unlock();
1215 		}
1216 		/* this also sets saved unicast addresses */
1217 		qeth_l2_set_rx_mode(card->dev);
1218 		rtnl_lock();
1219 		qeth_recover_features(card->dev);
1220 		rtnl_unlock();
1221 	}
1222 	/* let user_space know that device is online */
1223 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
1224 	mutex_unlock(&card->conf_mutex);
1225 	mutex_unlock(&card->discipline_mutex);
1226 	return 0;
1227 
1228 out_remove:
1229 	qeth_l2_stop_card(card, 0);
1230 	ccw_device_set_offline(CARD_DDEV(card));
1231 	ccw_device_set_offline(CARD_WDEV(card));
1232 	ccw_device_set_offline(CARD_RDEV(card));
1233 	qdio_free(CARD_DDEV(card));
1234 	if (recover_flag == CARD_STATE_RECOVER)
1235 		card->state = CARD_STATE_RECOVER;
1236 	else
1237 		card->state = CARD_STATE_DOWN;
1238 	mutex_unlock(&card->conf_mutex);
1239 	mutex_unlock(&card->discipline_mutex);
1240 	return rc;
1241 }
1242 
1243 static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1244 {
1245 	return __qeth_l2_set_online(gdev, 0);
1246 }
1247 
1248 static int __qeth_l2_set_offline(struct ccwgroup_device *cgdev,
1249 					int recovery_mode)
1250 {
1251 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1252 	int rc = 0, rc2 = 0, rc3 = 0;
1253 	enum qeth_card_states recover_flag;
1254 
1255 	mutex_lock(&card->discipline_mutex);
1256 	mutex_lock(&card->conf_mutex);
1257 	QETH_DBF_TEXT(SETUP, 3, "setoffl");
1258 	QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
1259 
1260 	if (card->dev && netif_carrier_ok(card->dev))
1261 		netif_carrier_off(card->dev);
1262 	recover_flag = card->state;
1263 	if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
1264 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1265 		card->info.hwtrap = 1;
1266 	}
1267 	qeth_l2_stop_card(card, recovery_mode);
1268 	rc  = ccw_device_set_offline(CARD_DDEV(card));
1269 	rc2 = ccw_device_set_offline(CARD_WDEV(card));
1270 	rc3 = ccw_device_set_offline(CARD_RDEV(card));
1271 	if (!rc)
1272 		rc = (rc2) ? rc2 : rc3;
1273 	if (rc)
1274 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1275 	qdio_free(CARD_DDEV(card));
1276 	if (recover_flag == CARD_STATE_UP)
1277 		card->state = CARD_STATE_RECOVER;
1278 	/* let user_space know that device is offline */
1279 	kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
1280 	mutex_unlock(&card->conf_mutex);
1281 	mutex_unlock(&card->discipline_mutex);
1282 	return 0;
1283 }
1284 
1285 static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1286 {
1287 	return __qeth_l2_set_offline(cgdev, 0);
1288 }
1289 
1290 static int qeth_l2_recover(void *ptr)
1291 {
1292 	struct qeth_card *card;
1293 	int rc = 0;
1294 
1295 	card = (struct qeth_card *) ptr;
1296 	QETH_CARD_TEXT(card, 2, "recover1");
1297 	if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1298 		return 0;
1299 	QETH_CARD_TEXT(card, 2, "recover2");
1300 	dev_warn(&card->gdev->dev,
1301 		"A recovery process has been started for the device\n");
1302 	qeth_set_recovery_task(card);
1303 	__qeth_l2_set_offline(card->gdev, 1);
1304 	rc = __qeth_l2_set_online(card->gdev, 1);
1305 	if (!rc)
1306 		dev_info(&card->gdev->dev,
1307 			"Device successfully recovered!\n");
1308 	else {
1309 		qeth_close_dev(card);
1310 		dev_warn(&card->gdev->dev, "The qeth device driver "
1311 				"failed to recover an error on the device\n");
1312 	}
1313 	qeth_clear_recovery_task(card);
1314 	qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1315 	qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1316 	return 0;
1317 }
1318 
1319 static int __init qeth_l2_init(void)
1320 {
1321 	pr_info("register layer 2 discipline\n");
1322 	return 0;
1323 }
1324 
1325 static void __exit qeth_l2_exit(void)
1326 {
1327 	pr_info("unregister layer 2 discipline\n");
1328 }
1329 
1330 static void qeth_l2_shutdown(struct ccwgroup_device *gdev)
1331 {
1332 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1333 	qeth_set_allowed_threads(card, 0, 1);
1334 	if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
1335 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1336 	qeth_qdio_clear_card(card, 0);
1337 	qeth_clear_qdio_buffers(card);
1338 	qdio_free(CARD_DDEV(card));
1339 }
1340 
1341 static int qeth_l2_pm_suspend(struct ccwgroup_device *gdev)
1342 {
1343 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1344 
1345 	if (card->dev)
1346 		netif_device_detach(card->dev);
1347 	qeth_set_allowed_threads(card, 0, 1);
1348 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
1349 	if (gdev->state == CCWGROUP_OFFLINE)
1350 		return 0;
1351 	if (card->state == CARD_STATE_UP) {
1352 		if (card->info.hwtrap)
1353 			qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1354 		__qeth_l2_set_offline(card->gdev, 1);
1355 	} else
1356 		__qeth_l2_set_offline(card->gdev, 0);
1357 	return 0;
1358 }
1359 
1360 static int qeth_l2_pm_resume(struct ccwgroup_device *gdev)
1361 {
1362 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1363 	int rc = 0;
1364 
1365 	if (gdev->state == CCWGROUP_OFFLINE)
1366 		goto out;
1367 
1368 	if (card->state == CARD_STATE_RECOVER) {
1369 		rc = __qeth_l2_set_online(card->gdev, 1);
1370 		if (rc) {
1371 			rtnl_lock();
1372 			dev_close(card->dev);
1373 			rtnl_unlock();
1374 		}
1375 	} else
1376 		rc = __qeth_l2_set_online(card->gdev, 0);
1377 out:
1378 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1379 	if (card->dev)
1380 		netif_device_attach(card->dev);
1381 	if (rc)
1382 		dev_warn(&card->gdev->dev, "The qeth device driver "
1383 			"failed to recover an error on the device\n");
1384 	return rc;
1385 }
1386 
1387 /* Returns zero if the command is successfully "consumed" */
1388 static int qeth_l2_control_event(struct qeth_card *card,
1389 					struct qeth_ipa_cmd *cmd)
1390 {
1391 	switch (cmd->hdr.command) {
1392 	case IPA_CMD_SETBRIDGEPORT_OSA:
1393 	case IPA_CMD_SETBRIDGEPORT_IQD:
1394 		if (cmd->data.sbp.hdr.command_code ==
1395 				IPA_SBP_BRIDGE_PORT_STATE_CHANGE) {
1396 			qeth_bridge_state_change(card, cmd);
1397 			return 0;
1398 		} else
1399 			return 1;
1400 	case IPA_CMD_ADDRESS_CHANGE_NOTIF:
1401 		qeth_bridge_host_event(card, cmd);
1402 		return 0;
1403 	default:
1404 		return 1;
1405 	}
1406 }
1407 
1408 struct qeth_discipline qeth_l2_discipline = {
1409 	.start_poll = qeth_qdio_start_poll,
1410 	.input_handler = (qdio_handler_t *) qeth_qdio_input_handler,
1411 	.output_handler = (qdio_handler_t *) qeth_qdio_output_handler,
1412 	.recover = qeth_l2_recover,
1413 	.setup = qeth_l2_probe_device,
1414 	.remove = qeth_l2_remove_device,
1415 	.set_online = qeth_l2_set_online,
1416 	.set_offline = qeth_l2_set_offline,
1417 	.shutdown = qeth_l2_shutdown,
1418 	.freeze = qeth_l2_pm_suspend,
1419 	.thaw = qeth_l2_pm_resume,
1420 	.restore = qeth_l2_pm_resume,
1421 	.control_event_handler = qeth_l2_control_event,
1422 };
1423 EXPORT_SYMBOL_GPL(qeth_l2_discipline);
1424 
1425 static int qeth_osn_send_control_data(struct qeth_card *card, int len,
1426 			   struct qeth_cmd_buffer *iob)
1427 {
1428 	unsigned long flags;
1429 	int rc = 0;
1430 
1431 	QETH_CARD_TEXT(card, 5, "osndctrd");
1432 
1433 	wait_event(card->wait_q,
1434 		   atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1435 	qeth_prepare_control_data(card, len, iob);
1436 	QETH_CARD_TEXT(card, 6, "osnoirqp");
1437 	spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1438 	rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1439 			      (addr_t) iob, 0, 0);
1440 	spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1441 	if (rc) {
1442 		QETH_DBF_MESSAGE(2, "qeth_osn_send_control_data: "
1443 			   "ccw_device_start rc = %i\n", rc);
1444 		QETH_CARD_TEXT_(card, 2, " err%d", rc);
1445 		qeth_release_buffer(iob->channel, iob);
1446 		atomic_set(&card->write.irq_pending, 0);
1447 		wake_up(&card->wait_q);
1448 	}
1449 	return rc;
1450 }
1451 
1452 static int qeth_osn_send_ipa_cmd(struct qeth_card *card,
1453 			struct qeth_cmd_buffer *iob, int data_len)
1454 {
1455 	u16 s1, s2;
1456 
1457 	QETH_CARD_TEXT(card, 4, "osndipa");
1458 
1459 	qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1460 	s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1461 	s2 = (u16)data_len;
1462 	memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1463 	memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1464 	memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1465 	memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1466 	return qeth_osn_send_control_data(card, s1, iob);
1467 }
1468 
1469 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1470 {
1471 	struct qeth_cmd_buffer *iob;
1472 	struct qeth_card *card;
1473 	int rc;
1474 
1475 	if (!dev)
1476 		return -ENODEV;
1477 	card = dev->ml_priv;
1478 	if (!card)
1479 		return -ENODEV;
1480 	QETH_CARD_TEXT(card, 2, "osnsdmc");
1481 	if (!qeth_card_hw_is_reachable(card))
1482 		return -ENODEV;
1483 	iob = qeth_wait_for_buffer(&card->write);
1484 	memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1485 	rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1486 	return rc;
1487 }
1488 EXPORT_SYMBOL(qeth_osn_assist);
1489 
1490 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1491 		  int (*assist_cb)(struct net_device *, void *),
1492 		  int (*data_cb)(struct sk_buff *))
1493 {
1494 	struct qeth_card *card;
1495 
1496 	*dev = qeth_l2_netdev_by_devno(read_dev_no);
1497 	if (*dev == NULL)
1498 		return -ENODEV;
1499 	card = (*dev)->ml_priv;
1500 	if (!card)
1501 		return -ENODEV;
1502 	QETH_CARD_TEXT(card, 2, "osnreg");
1503 	if ((assist_cb == NULL) || (data_cb == NULL))
1504 		return -EINVAL;
1505 	card->osn_info.assist_cb = assist_cb;
1506 	card->osn_info.data_cb = data_cb;
1507 	return 0;
1508 }
1509 EXPORT_SYMBOL(qeth_osn_register);
1510 
1511 void qeth_osn_deregister(struct net_device *dev)
1512 {
1513 	struct qeth_card *card;
1514 
1515 	if (!dev)
1516 		return;
1517 	card = dev->ml_priv;
1518 	if (!card)
1519 		return;
1520 	QETH_CARD_TEXT(card, 2, "osndereg");
1521 	card->osn_info.assist_cb = NULL;
1522 	card->osn_info.data_cb = NULL;
1523 	return;
1524 }
1525 EXPORT_SYMBOL(qeth_osn_deregister);
1526 
1527 /* SETBRIDGEPORT support, async notifications */
1528 
1529 enum qeth_an_event_type {anev_reg_unreg, anev_abort, anev_reset};
1530 
1531 /**
1532  * qeth_bridge_emit_host_event() - bridgeport address change notification
1533  * @card:  qeth_card structure pointer, for udev events.
1534  * @evtype:  "normal" register/unregister, or abort, or reset. For abort
1535  *	      and reset token and addr_lnid are unused and may be NULL.
1536  * @code:  event bitmask: high order bit 0x80 value 1 means removal of an
1537  *			  object, 0 - addition of an object.
1538  *			  0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC.
1539  * @token: "network token" structure identifying physical address of the port.
1540  * @addr_lnid: pointer to structure with MAC address and VLAN ID.
1541  *
1542  * This function is called when registrations and deregistrations are
1543  * reported by the hardware, and also when notifications are enabled -
1544  * for all currently registered addresses.
1545  */
1546 static void qeth_bridge_emit_host_event(struct qeth_card *card,
1547 	enum qeth_an_event_type evtype,
1548 	u8 code, struct net_if_token *token, struct mac_addr_lnid *addr_lnid)
1549 {
1550 	char str[7][32];
1551 	char *env[8];
1552 	int i = 0;
1553 
1554 	switch (evtype) {
1555 	case anev_reg_unreg:
1556 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=%s",
1557 				(code & IPA_ADDR_CHANGE_CODE_REMOVAL)
1558 				? "deregister" : "register");
1559 		env[i] = str[i]; i++;
1560 		if (code & IPA_ADDR_CHANGE_CODE_VLANID) {
1561 			snprintf(str[i], sizeof(str[i]), "VLAN=%d",
1562 				addr_lnid->lnid);
1563 			env[i] = str[i]; i++;
1564 		}
1565 		if (code & IPA_ADDR_CHANGE_CODE_MACADDR) {
1566 			snprintf(str[i], sizeof(str[i]), "MAC=%pM",
1567 				addr_lnid->mac);
1568 			env[i] = str[i]; i++;
1569 		}
1570 		snprintf(str[i], sizeof(str[i]), "NTOK_BUSID=%x.%x.%04x",
1571 			token->cssid, token->ssid, token->devnum);
1572 		env[i] = str[i]; i++;
1573 		snprintf(str[i], sizeof(str[i]), "NTOK_IID=%02x", token->iid);
1574 		env[i] = str[i]; i++;
1575 		snprintf(str[i], sizeof(str[i]), "NTOK_CHPID=%02x",
1576 				token->chpid);
1577 		env[i] = str[i]; i++;
1578 		snprintf(str[i], sizeof(str[i]), "NTOK_CHID=%04x", token->chid);
1579 		env[i] = str[i]; i++;
1580 		break;
1581 	case anev_abort:
1582 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=abort");
1583 		env[i] = str[i]; i++;
1584 		break;
1585 	case anev_reset:
1586 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=reset");
1587 		env[i] = str[i]; i++;
1588 		break;
1589 	}
1590 	env[i] = NULL;
1591 	kobject_uevent_env(&card->gdev->dev.kobj, KOBJ_CHANGE, env);
1592 }
1593 
1594 struct qeth_bridge_state_data {
1595 	struct work_struct worker;
1596 	struct qeth_card *card;
1597 	struct qeth_sbp_state_change qports;
1598 };
1599 
1600 static void qeth_bridge_state_change_worker(struct work_struct *work)
1601 {
1602 	struct qeth_bridge_state_data *data =
1603 		container_of(work, struct qeth_bridge_state_data, worker);
1604 	/* We are only interested in the first entry - local port */
1605 	struct qeth_sbp_port_entry *entry = &data->qports.entry[0];
1606 	char env_locrem[32];
1607 	char env_role[32];
1608 	char env_state[32];
1609 	char *env[] = {
1610 		env_locrem,
1611 		env_role,
1612 		env_state,
1613 		NULL
1614 	};
1615 
1616 	/* Role should not change by itself, but if it did, */
1617 	/* information from the hardware is authoritative.  */
1618 	mutex_lock(&data->card->conf_mutex);
1619 	data->card->options.sbp.role = entry->role;
1620 	mutex_unlock(&data->card->conf_mutex);
1621 
1622 	snprintf(env_locrem, sizeof(env_locrem), "BRIDGEPORT=statechange");
1623 	snprintf(env_role, sizeof(env_role), "ROLE=%s",
1624 		(entry->role == QETH_SBP_ROLE_NONE) ? "none" :
1625 		(entry->role == QETH_SBP_ROLE_PRIMARY) ? "primary" :
1626 		(entry->role == QETH_SBP_ROLE_SECONDARY) ? "secondary" :
1627 		"<INVALID>");
1628 	snprintf(env_state, sizeof(env_state), "STATE=%s",
1629 		(entry->state == QETH_SBP_STATE_INACTIVE) ? "inactive" :
1630 		(entry->state == QETH_SBP_STATE_STANDBY) ? "standby" :
1631 		(entry->state == QETH_SBP_STATE_ACTIVE) ? "active" :
1632 		"<INVALID>");
1633 	kobject_uevent_env(&data->card->gdev->dev.kobj,
1634 				KOBJ_CHANGE, env);
1635 	kfree(data);
1636 }
1637 
1638 static void qeth_bridge_state_change(struct qeth_card *card,
1639 					struct qeth_ipa_cmd *cmd)
1640 {
1641 	struct qeth_sbp_state_change *qports =
1642 		 &cmd->data.sbp.data.state_change;
1643 	struct qeth_bridge_state_data *data;
1644 	int extrasize;
1645 
1646 	QETH_CARD_TEXT(card, 2, "brstchng");
1647 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1648 		QETH_CARD_TEXT_(card, 2, "BPsz%04x", qports->entry_length);
1649 		return;
1650 	}
1651 	extrasize = sizeof(struct qeth_sbp_port_entry) * qports->num_entries;
1652 	data = kzalloc(sizeof(struct qeth_bridge_state_data) + extrasize,
1653 		GFP_ATOMIC);
1654 	if (!data) {
1655 		QETH_CARD_TEXT(card, 2, "BPSalloc");
1656 		return;
1657 	}
1658 	INIT_WORK(&data->worker, qeth_bridge_state_change_worker);
1659 	data->card = card;
1660 	memcpy(&data->qports, qports,
1661 			sizeof(struct qeth_sbp_state_change) + extrasize);
1662 	queue_work(qeth_wq, &data->worker);
1663 }
1664 
1665 struct qeth_bridge_host_data {
1666 	struct work_struct worker;
1667 	struct qeth_card *card;
1668 	struct qeth_ipacmd_addr_change hostevs;
1669 };
1670 
1671 static void qeth_bridge_host_event_worker(struct work_struct *work)
1672 {
1673 	struct qeth_bridge_host_data *data =
1674 		container_of(work, struct qeth_bridge_host_data, worker);
1675 	int i;
1676 
1677 	if (data->hostevs.lost_event_mask) {
1678 		dev_info(&data->card->gdev->dev,
1679 "Address notification from the Bridge Port stopped %s (%s)\n",
1680 			data->card->dev->name,
1681 			(data->hostevs.lost_event_mask == 0x01)
1682 			? "Overflow"
1683 			: (data->hostevs.lost_event_mask == 0x02)
1684 			? "Bridge port state change"
1685 			: "Unknown reason");
1686 		mutex_lock(&data->card->conf_mutex);
1687 		data->card->options.sbp.hostnotification = 0;
1688 		mutex_unlock(&data->card->conf_mutex);
1689 		qeth_bridge_emit_host_event(data->card, anev_abort,
1690 			0, NULL, NULL);
1691 	} else
1692 		for (i = 0; i < data->hostevs.num_entries; i++) {
1693 			struct qeth_ipacmd_addr_change_entry *entry =
1694 					&data->hostevs.entry[i];
1695 			qeth_bridge_emit_host_event(data->card,
1696 					anev_reg_unreg,
1697 					entry->change_code,
1698 					&entry->token, &entry->addr_lnid);
1699 		}
1700 	kfree(data);
1701 }
1702 
1703 static void qeth_bridge_host_event(struct qeth_card *card,
1704 					struct qeth_ipa_cmd *cmd)
1705 {
1706 	struct qeth_ipacmd_addr_change *hostevs =
1707 		 &cmd->data.addrchange;
1708 	struct qeth_bridge_host_data *data;
1709 	int extrasize;
1710 
1711 	QETH_CARD_TEXT(card, 2, "brhostev");
1712 	if (cmd->hdr.return_code != 0x0000) {
1713 		if (cmd->hdr.return_code == 0x0010) {
1714 			if (hostevs->lost_event_mask == 0x00)
1715 				hostevs->lost_event_mask = 0xff;
1716 		} else {
1717 			QETH_CARD_TEXT_(card, 2, "BPHe%04x",
1718 				cmd->hdr.return_code);
1719 			return;
1720 		}
1721 	}
1722 	extrasize = sizeof(struct qeth_ipacmd_addr_change_entry) *
1723 						hostevs->num_entries;
1724 	data = kzalloc(sizeof(struct qeth_bridge_host_data) + extrasize,
1725 		GFP_ATOMIC);
1726 	if (!data) {
1727 		QETH_CARD_TEXT(card, 2, "BPHalloc");
1728 		return;
1729 	}
1730 	INIT_WORK(&data->worker, qeth_bridge_host_event_worker);
1731 	data->card = card;
1732 	memcpy(&data->hostevs, hostevs,
1733 			sizeof(struct qeth_ipacmd_addr_change) + extrasize);
1734 	queue_work(qeth_wq, &data->worker);
1735 }
1736 
1737 /* SETBRIDGEPORT support; sending commands */
1738 
1739 struct _qeth_sbp_cbctl {
1740 	u16 ipa_rc;
1741 	u16 cmd_rc;
1742 	union {
1743 		u32 supported;
1744 		struct {
1745 			enum qeth_sbp_roles *role;
1746 			enum qeth_sbp_states *state;
1747 		} qports;
1748 	} data;
1749 };
1750 
1751 /**
1752  * qeth_bridgeport_makerc() - derive "traditional" error from hardware codes.
1753  * @card:		      qeth_card structure pointer, for debug messages.
1754  * @cbctl:		      state structure with hardware return codes.
1755  * @setcmd:		      IPA command code
1756  *
1757  * Returns negative errno-compatible error indication or 0 on success.
1758  */
1759 static int qeth_bridgeport_makerc(struct qeth_card *card,
1760 	struct _qeth_sbp_cbctl *cbctl, enum qeth_ipa_sbp_cmd setcmd)
1761 {
1762 	int rc;
1763 	int is_iqd = (card->info.type == QETH_CARD_TYPE_IQD);
1764 
1765 	if ((is_iqd && (cbctl->ipa_rc == IPA_RC_SUCCESS)) ||
1766 	    (!is_iqd && (cbctl->ipa_rc == cbctl->cmd_rc)))
1767 		switch (cbctl->cmd_rc) {
1768 		case 0x0000:
1769 			rc = 0;
1770 			break;
1771 		case 0x2B04:
1772 		case 0x0004:
1773 			rc = -EOPNOTSUPP;
1774 			break;
1775 		case 0x2B0C:
1776 		case 0x000C: /* Not configured as bridge Port */
1777 			rc = -ENODEV; /* maybe not the best code here? */
1778 			dev_err(&card->gdev->dev,
1779 	"The device is not configured as a Bridge Port\n");
1780 			break;
1781 		case 0x2B10:
1782 		case 0x0010: /* OS mismatch */
1783 			rc = -EPERM;
1784 			dev_err(&card->gdev->dev,
1785 	"A Bridge Port is already configured by a different operating system\n");
1786 			break;
1787 		case 0x2B14:
1788 		case 0x0014: /* Another device is Primary */
1789 			switch (setcmd) {
1790 			case IPA_SBP_SET_PRIMARY_BRIDGE_PORT:
1791 				rc = -EEXIST;
1792 				dev_err(&card->gdev->dev,
1793 	"The LAN already has a primary Bridge Port\n");
1794 				break;
1795 			case IPA_SBP_SET_SECONDARY_BRIDGE_PORT:
1796 				rc = -EBUSY;
1797 				dev_err(&card->gdev->dev,
1798 	"The device is already a primary Bridge Port\n");
1799 				break;
1800 			default:
1801 				rc = -EIO;
1802 			}
1803 			break;
1804 		case 0x2B18:
1805 		case 0x0018: /* This device is currently Secondary */
1806 			rc = -EBUSY;
1807 			dev_err(&card->gdev->dev,
1808 	"The device is already a secondary Bridge Port\n");
1809 			break;
1810 		case 0x2B1C:
1811 		case 0x001C: /* Limit for Secondary devices reached */
1812 			rc = -EEXIST;
1813 			dev_err(&card->gdev->dev,
1814 	"The LAN cannot have more secondary Bridge Ports\n");
1815 			break;
1816 		case 0x2B24:
1817 		case 0x0024: /* This device is currently Primary */
1818 			rc = -EBUSY;
1819 			dev_err(&card->gdev->dev,
1820 	"The device is already a primary Bridge Port\n");
1821 			break;
1822 		case 0x2B20:
1823 		case 0x0020: /* Not authorized by zManager */
1824 			rc = -EACCES;
1825 			dev_err(&card->gdev->dev,
1826 	"The device is not authorized to be a Bridge Port\n");
1827 			break;
1828 		default:
1829 			rc = -EIO;
1830 		}
1831 	else
1832 		switch (cbctl->ipa_rc) {
1833 		case IPA_RC_NOTSUPP:
1834 			rc = -EOPNOTSUPP;
1835 			break;
1836 		case IPA_RC_UNSUPPORTED_COMMAND:
1837 			rc = -EOPNOTSUPP;
1838 			break;
1839 		default:
1840 			rc = -EIO;
1841 		}
1842 
1843 	if (rc) {
1844 		QETH_CARD_TEXT_(card, 2, "SBPi%04x", cbctl->ipa_rc);
1845 		QETH_CARD_TEXT_(card, 2, "SBPc%04x", cbctl->cmd_rc);
1846 	}
1847 	return rc;
1848 }
1849 
1850 static inline int ipa_cmd_sbp(struct qeth_card *card)
1851 {
1852 	return (card->info.type == QETH_CARD_TYPE_IQD) ?
1853 		IPA_CMD_SETBRIDGEPORT_IQD :
1854 		IPA_CMD_SETBRIDGEPORT_OSA;
1855 }
1856 
1857 static int qeth_bridgeport_query_support_cb(struct qeth_card *card,
1858 	struct qeth_reply *reply, unsigned long data)
1859 {
1860 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1861 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1862 	QETH_CARD_TEXT(card, 2, "brqsupcb");
1863 	cbctl->ipa_rc = cmd->hdr.return_code;
1864 	cbctl->cmd_rc = cmd->data.sbp.hdr.return_code;
1865 	if ((cbctl->ipa_rc == 0) && (cbctl->cmd_rc == 0)) {
1866 		cbctl->data.supported =
1867 			cmd->data.sbp.data.query_cmds_supp.supported_cmds;
1868 	} else {
1869 		cbctl->data.supported = 0;
1870 	}
1871 	return 0;
1872 }
1873 
1874 /**
1875  * qeth_bridgeport_query_support() - store bitmask of supported subfunctions.
1876  * @card:			     qeth_card structure pointer.
1877  *
1878  * Sets bitmask of supported setbridgeport subfunctions in the qeth_card
1879  * strucutre: card->options.sbp.supported_funcs.
1880  */
1881 static void qeth_bridgeport_query_support(struct qeth_card *card)
1882 {
1883 	struct qeth_cmd_buffer *iob;
1884 	struct qeth_ipa_cmd *cmd;
1885 	struct _qeth_sbp_cbctl cbctl;
1886 
1887 	QETH_CARD_TEXT(card, 2, "brqsuppo");
1888 	iob = qeth_get_ipacmd_buffer(card, ipa_cmd_sbp(card), 0);
1889 	if (!iob)
1890 		return;
1891 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1892 	cmd->data.sbp.hdr.cmdlength =
1893 		sizeof(struct qeth_ipacmd_sbp_hdr) +
1894 		sizeof(struct qeth_sbp_query_cmds_supp);
1895 	cmd->data.sbp.hdr.command_code =
1896 		IPA_SBP_QUERY_COMMANDS_SUPPORTED;
1897 	cmd->data.sbp.hdr.used_total = 1;
1898 	cmd->data.sbp.hdr.seq_no = 1;
1899 	if (qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_support_cb,
1900 							(void *)&cbctl) ||
1901 	    qeth_bridgeport_makerc(card, &cbctl,
1902 					IPA_SBP_QUERY_COMMANDS_SUPPORTED)) {
1903 		/* non-zero makerc signifies failure, and produce messages */
1904 		card->options.sbp.role = QETH_SBP_ROLE_NONE;
1905 		return;
1906 	}
1907 	card->options.sbp.supported_funcs = cbctl.data.supported;
1908 }
1909 
1910 static int qeth_bridgeport_query_ports_cb(struct qeth_card *card,
1911 	struct qeth_reply *reply, unsigned long data)
1912 {
1913 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1914 	struct qeth_sbp_query_ports *qports = &cmd->data.sbp.data.query_ports;
1915 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1916 
1917 	QETH_CARD_TEXT(card, 2, "brqprtcb");
1918 	cbctl->ipa_rc = cmd->hdr.return_code;
1919 	cbctl->cmd_rc = cmd->data.sbp.hdr.return_code;
1920 	if ((cbctl->ipa_rc != 0) || (cbctl->cmd_rc != 0))
1921 		return 0;
1922 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1923 		cbctl->cmd_rc = 0xffff;
1924 		QETH_CARD_TEXT_(card, 2, "SBPs%04x", qports->entry_length);
1925 		return 0;
1926 	}
1927 	/* first entry contains the state of the local port */
1928 	if (qports->num_entries > 0) {
1929 		if (cbctl->data.qports.role)
1930 			*cbctl->data.qports.role = qports->entry[0].role;
1931 		if (cbctl->data.qports.state)
1932 			*cbctl->data.qports.state = qports->entry[0].state;
1933 	}
1934 	return 0;
1935 }
1936 
1937 /**
1938  * qeth_bridgeport_query_ports() - query local bridgeport status.
1939  * @card:			   qeth_card structure pointer.
1940  * @role:   Role of the port: 0-none, 1-primary, 2-secondary.
1941  * @state:  State of the port: 0-inactive, 1-standby, 2-active.
1942  *
1943  * Returns negative errno-compatible error indication or 0 on success.
1944  *
1945  * 'role' and 'state' are not updated in case of hardware operation failure.
1946  */
1947 int qeth_bridgeport_query_ports(struct qeth_card *card,
1948 	enum qeth_sbp_roles *role, enum qeth_sbp_states *state)
1949 {
1950 	int rc = 0;
1951 	struct qeth_cmd_buffer *iob;
1952 	struct qeth_ipa_cmd *cmd;
1953 	struct _qeth_sbp_cbctl cbctl = {
1954 		.data = {
1955 			.qports = {
1956 				.role = role,
1957 				.state = state,
1958 			},
1959 		},
1960 	};
1961 
1962 	QETH_CARD_TEXT(card, 2, "brqports");
1963 	if (!(card->options.sbp.supported_funcs & IPA_SBP_QUERY_BRIDGE_PORTS))
1964 		return -EOPNOTSUPP;
1965 	iob = qeth_get_ipacmd_buffer(card, ipa_cmd_sbp(card), 0);
1966 	if (!iob)
1967 		return -ENOMEM;
1968 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1969 	cmd->data.sbp.hdr.cmdlength =
1970 		sizeof(struct qeth_ipacmd_sbp_hdr);
1971 	cmd->data.sbp.hdr.command_code =
1972 		IPA_SBP_QUERY_BRIDGE_PORTS;
1973 	cmd->data.sbp.hdr.used_total = 1;
1974 	cmd->data.sbp.hdr.seq_no = 1;
1975 	rc = qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_ports_cb,
1976 				(void *)&cbctl);
1977 	if (rc < 0)
1978 		return rc;
1979 	return qeth_bridgeport_makerc(card, &cbctl, IPA_SBP_QUERY_BRIDGE_PORTS);
1980 }
1981 EXPORT_SYMBOL_GPL(qeth_bridgeport_query_ports);
1982 
1983 static int qeth_bridgeport_set_cb(struct qeth_card *card,
1984 	struct qeth_reply *reply, unsigned long data)
1985 {
1986 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
1987 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1988 	QETH_CARD_TEXT(card, 2, "brsetrcb");
1989 	cbctl->ipa_rc = cmd->hdr.return_code;
1990 	cbctl->cmd_rc = cmd->data.sbp.hdr.return_code;
1991 	return 0;
1992 }
1993 
1994 /**
1995  * qeth_bridgeport_setrole() - Assign primary role to the port.
1996  * @card:		       qeth_card structure pointer.
1997  * @role:		       Role to assign.
1998  *
1999  * Returns negative errno-compatible error indication or 0 on success.
2000  */
2001 int qeth_bridgeport_setrole(struct qeth_card *card, enum qeth_sbp_roles role)
2002 {
2003 	int rc = 0;
2004 	int cmdlength;
2005 	struct qeth_cmd_buffer *iob;
2006 	struct qeth_ipa_cmd *cmd;
2007 	struct _qeth_sbp_cbctl cbctl;
2008 	enum qeth_ipa_sbp_cmd setcmd;
2009 
2010 	QETH_CARD_TEXT(card, 2, "brsetrol");
2011 	switch (role) {
2012 	case QETH_SBP_ROLE_NONE:
2013 		setcmd = IPA_SBP_RESET_BRIDGE_PORT_ROLE;
2014 		cmdlength =  sizeof(struct qeth_ipacmd_sbp_hdr) +
2015 			sizeof(struct qeth_sbp_reset_role);
2016 		break;
2017 	case QETH_SBP_ROLE_PRIMARY:
2018 		setcmd = IPA_SBP_SET_PRIMARY_BRIDGE_PORT;
2019 		cmdlength =  sizeof(struct qeth_ipacmd_sbp_hdr) +
2020 			sizeof(struct qeth_sbp_set_primary);
2021 		break;
2022 	case QETH_SBP_ROLE_SECONDARY:
2023 		setcmd = IPA_SBP_SET_SECONDARY_BRIDGE_PORT;
2024 		cmdlength =  sizeof(struct qeth_ipacmd_sbp_hdr) +
2025 			sizeof(struct qeth_sbp_set_secondary);
2026 		break;
2027 	default:
2028 		return -EINVAL;
2029 	}
2030 	if (!(card->options.sbp.supported_funcs & setcmd))
2031 		return -EOPNOTSUPP;
2032 	iob = qeth_get_ipacmd_buffer(card, ipa_cmd_sbp(card), 0);
2033 	if (!iob)
2034 		return -ENOMEM;
2035 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2036 	cmd->data.sbp.hdr.cmdlength = cmdlength;
2037 	cmd->data.sbp.hdr.command_code = setcmd;
2038 	cmd->data.sbp.hdr.used_total = 1;
2039 	cmd->data.sbp.hdr.seq_no = 1;
2040 	rc = qeth_send_ipa_cmd(card, iob, qeth_bridgeport_set_cb,
2041 				(void *)&cbctl);
2042 	if (rc < 0)
2043 		return rc;
2044 	return qeth_bridgeport_makerc(card, &cbctl, setcmd);
2045 }
2046 
2047 /**
2048  * qeth_anset_makerc() - derive "traditional" error from hardware codes.
2049  * @card:		      qeth_card structure pointer, for debug messages.
2050  *
2051  * Returns negative errno-compatible error indication or 0 on success.
2052  */
2053 static int qeth_anset_makerc(struct qeth_card *card, int pnso_rc, u16 response)
2054 {
2055 	int rc;
2056 
2057 	if (pnso_rc == 0)
2058 		switch (response) {
2059 		case 0x0001:
2060 			rc = 0;
2061 			break;
2062 		case 0x0004:
2063 		case 0x0100:
2064 		case 0x0106:
2065 			rc = -EOPNOTSUPP;
2066 			dev_err(&card->gdev->dev,
2067 				"Setting address notification failed\n");
2068 			break;
2069 		case 0x0107:
2070 			rc = -EAGAIN;
2071 			break;
2072 		default:
2073 			rc = -EIO;
2074 		}
2075 	else
2076 		rc = -EIO;
2077 
2078 	if (rc) {
2079 		QETH_CARD_TEXT_(card, 2, "SBPp%04x", pnso_rc);
2080 		QETH_CARD_TEXT_(card, 2, "SBPr%04x", response);
2081 	}
2082 	return rc;
2083 }
2084 
2085 static void qeth_bridgeport_an_set_cb(void *priv,
2086 		enum qdio_brinfo_entry_type type, void *entry)
2087 {
2088 	struct qeth_card *card = (struct qeth_card *)priv;
2089 	struct qdio_brinfo_entry_l2 *l2entry;
2090 	u8 code;
2091 
2092 	if (type != l2_addr_lnid) {
2093 		WARN_ON_ONCE(1);
2094 		return;
2095 	}
2096 
2097 	l2entry = (struct qdio_brinfo_entry_l2 *)entry;
2098 	code = IPA_ADDR_CHANGE_CODE_MACADDR;
2099 	if (l2entry->addr_lnid.lnid)
2100 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
2101 	qeth_bridge_emit_host_event(card, anev_reg_unreg, code,
2102 		(struct net_if_token *)&l2entry->nit,
2103 		(struct mac_addr_lnid *)&l2entry->addr_lnid);
2104 }
2105 
2106 /**
2107  * qeth_bridgeport_an_set() - Enable or disable bridgeport address notification
2108  * @card:		      qeth_card structure pointer.
2109  * @enable:		      0 - disable, non-zero - enable notifications
2110  *
2111  * Returns negative errno-compatible error indication or 0 on success.
2112  *
2113  * On enable, emits a series of address notifications udev events for all
2114  * currently registered hosts.
2115  */
2116 int qeth_bridgeport_an_set(struct qeth_card *card, int enable)
2117 {
2118 	int rc;
2119 	u16 response;
2120 	struct ccw_device *ddev;
2121 	struct subchannel_id schid;
2122 
2123 	if (!card)
2124 		return -EINVAL;
2125 	if (!card->options.sbp.supported_funcs)
2126 		return -EOPNOTSUPP;
2127 	ddev = CARD_DDEV(card);
2128 	ccw_device_get_schid(ddev, &schid);
2129 
2130 	if (enable) {
2131 		qeth_bridge_emit_host_event(card, anev_reset, 0, NULL, NULL);
2132 		rc = qdio_pnso_brinfo(schid, 1, &response,
2133 			qeth_bridgeport_an_set_cb, card);
2134 	} else
2135 		rc = qdio_pnso_brinfo(schid, 0, &response, NULL, NULL);
2136 	return qeth_anset_makerc(card, rc, response);
2137 }
2138 EXPORT_SYMBOL_GPL(qeth_bridgeport_an_set);
2139 
2140 module_init(qeth_l2_init);
2141 module_exit(qeth_l2_exit);
2142 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2143 MODULE_DESCRIPTION("qeth layer 2 discipline");
2144 MODULE_LICENSE("GPL");
2145