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