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