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