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