xref: /openbmc/linux/drivers/s390/net/qeth_l2_main.c (revision 95e9fd10)
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 
25 static int qeth_l2_set_offline(struct ccwgroup_device *);
26 static int qeth_l2_stop(struct net_device *);
27 static int qeth_l2_send_delmac(struct qeth_card *, __u8 *);
28 static int qeth_l2_send_setdelmac(struct qeth_card *, __u8 *,
29 			   enum qeth_ipa_cmds,
30 			   int (*reply_cb) (struct qeth_card *,
31 					    struct qeth_reply*,
32 					    unsigned long));
33 static void qeth_l2_set_multicast_list(struct net_device *);
34 static int qeth_l2_recover(void *);
35 
36 static int qeth_l2_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
37 {
38 	struct qeth_card *card = dev->ml_priv;
39 	struct mii_ioctl_data *mii_data;
40 	int rc = 0;
41 
42 	if (!card)
43 		return -ENODEV;
44 
45 	if ((card->state != CARD_STATE_UP) &&
46 		(card->state != CARD_STATE_SOFTSETUP))
47 		return -ENODEV;
48 
49 	if (card->info.type == QETH_CARD_TYPE_OSN)
50 		return -EPERM;
51 
52 	switch (cmd) {
53 	case SIOC_QETH_ADP_SET_SNMP_CONTROL:
54 		rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
55 		break;
56 	case SIOC_QETH_GET_CARD_TYPE:
57 		if ((card->info.type == QETH_CARD_TYPE_OSD ||
58 		     card->info.type == QETH_CARD_TYPE_OSM ||
59 		     card->info.type == QETH_CARD_TYPE_OSX) &&
60 		    !card->info.guestlan)
61 			return 1;
62 		return 0;
63 		break;
64 	case SIOCGMIIPHY:
65 		mii_data = if_mii(rq);
66 		mii_data->phy_id = 0;
67 		break;
68 	case SIOCGMIIREG:
69 		mii_data = if_mii(rq);
70 		if (mii_data->phy_id != 0)
71 			rc = -EINVAL;
72 		else
73 			mii_data->val_out = qeth_mdio_read(dev,
74 				mii_data->phy_id, mii_data->reg_num);
75 		break;
76 	case SIOC_QETH_QUERY_OAT:
77 		rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
78 		break;
79 	default:
80 		rc = -EOPNOTSUPP;
81 	}
82 	if (rc)
83 		QETH_CARD_TEXT_(card, 2, "ioce%d", rc);
84 	return rc;
85 }
86 
87 static int qeth_l2_verify_dev(struct net_device *dev)
88 {
89 	struct qeth_card *card;
90 	unsigned long flags;
91 	int rc = 0;
92 
93 	read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
94 	list_for_each_entry(card, &qeth_core_card_list.list, list) {
95 		if (card->dev == dev) {
96 			rc = QETH_REAL_CARD;
97 			break;
98 		}
99 	}
100 	read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
101 
102 	return rc;
103 }
104 
105 static struct net_device *qeth_l2_netdev_by_devno(unsigned char *read_dev_no)
106 {
107 	struct qeth_card *card;
108 	struct net_device *ndev;
109 	__u16 temp_dev_no;
110 	unsigned long flags;
111 	struct ccw_dev_id read_devid;
112 
113 	ndev = NULL;
114 	memcpy(&temp_dev_no, read_dev_no, 2);
115 	read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
116 	list_for_each_entry(card, &qeth_core_card_list.list, list) {
117 		ccw_device_get_id(CARD_RDEV(card), &read_devid);
118 		if (read_devid.devno == temp_dev_no) {
119 			ndev = card->dev;
120 			break;
121 		}
122 	}
123 	read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
124 	return ndev;
125 }
126 
127 static int qeth_l2_send_setgroupmac_cb(struct qeth_card *card,
128 				struct qeth_reply *reply,
129 				unsigned long data)
130 {
131 	struct qeth_ipa_cmd *cmd;
132 	__u8 *mac;
133 
134 	QETH_CARD_TEXT(card, 2, "L2Sgmacb");
135 	cmd = (struct qeth_ipa_cmd *) data;
136 	mac = &cmd->data.setdelmac.mac[0];
137 	/* MAC already registered, needed in couple/uncouple case */
138 	if (cmd->hdr.return_code ==  IPA_RC_L2_DUP_MAC) {
139 		QETH_DBF_MESSAGE(2, "Group MAC %pM already existing on %s \n",
140 			  mac, QETH_CARD_IFNAME(card));
141 		cmd->hdr.return_code = 0;
142 	}
143 	if (cmd->hdr.return_code)
144 		QETH_DBF_MESSAGE(2, "Could not set group MAC %pM on %s: %x\n",
145 			  mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
146 	return 0;
147 }
148 
149 static int qeth_l2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
150 {
151 	QETH_CARD_TEXT(card, 2, "L2Sgmac");
152 	return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
153 					  qeth_l2_send_setgroupmac_cb);
154 }
155 
156 static int qeth_l2_send_delgroupmac_cb(struct qeth_card *card,
157 				struct qeth_reply *reply,
158 				unsigned long data)
159 {
160 	struct qeth_ipa_cmd *cmd;
161 	__u8 *mac;
162 
163 	QETH_CARD_TEXT(card, 2, "L2Dgmacb");
164 	cmd = (struct qeth_ipa_cmd *) data;
165 	mac = &cmd->data.setdelmac.mac[0];
166 	if (cmd->hdr.return_code)
167 		QETH_DBF_MESSAGE(2, "Could not delete group MAC %pM on %s: %x\n",
168 			  mac, QETH_CARD_IFNAME(card), cmd->hdr.return_code);
169 	return 0;
170 }
171 
172 static int qeth_l2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
173 {
174 	QETH_CARD_TEXT(card, 2, "L2Dgmac");
175 	return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
176 					  qeth_l2_send_delgroupmac_cb);
177 }
178 
179 static void qeth_l2_add_mc(struct qeth_card *card, __u8 *mac, int vmac)
180 {
181 	struct qeth_mc_mac *mc;
182 	int rc;
183 
184 	mc = kmalloc(sizeof(struct qeth_mc_mac), GFP_ATOMIC);
185 
186 	if (!mc)
187 		return;
188 
189 	memcpy(mc->mc_addr, mac, OSA_ADDR_LEN);
190 	mc->mc_addrlen = OSA_ADDR_LEN;
191 	mc->is_vmac = vmac;
192 
193 	if (vmac) {
194 		rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
195 					NULL);
196 	} else {
197 		rc = qeth_l2_send_setgroupmac(card, mac);
198 	}
199 
200 	if (!rc)
201 		list_add_tail(&mc->list, &card->mc_list);
202 	else
203 		kfree(mc);
204 }
205 
206 static void qeth_l2_del_all_mc(struct qeth_card *card, int del)
207 {
208 	struct qeth_mc_mac *mc, *tmp;
209 
210 	spin_lock_bh(&card->mclock);
211 	list_for_each_entry_safe(mc, tmp, &card->mc_list, list) {
212 		if (del) {
213 			if (mc->is_vmac)
214 				qeth_l2_send_setdelmac(card, mc->mc_addr,
215 					IPA_CMD_DELVMAC, NULL);
216 			else
217 				qeth_l2_send_delgroupmac(card, mc->mc_addr);
218 		}
219 		list_del(&mc->list);
220 		kfree(mc);
221 	}
222 	spin_unlock_bh(&card->mclock);
223 }
224 
225 static inline int qeth_l2_get_cast_type(struct qeth_card *card,
226 			struct sk_buff *skb)
227 {
228 	if (card->info.type == QETH_CARD_TYPE_OSN)
229 		return RTN_UNSPEC;
230 	if (is_broadcast_ether_addr(skb->data))
231 		return RTN_BROADCAST;
232 	if (is_multicast_ether_addr(skb->data))
233 		return RTN_MULTICAST;
234 	return RTN_UNSPEC;
235 }
236 
237 static void qeth_l2_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
238 			struct sk_buff *skb, int ipv, int cast_type)
239 {
240 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb_mac_header(skb);
241 
242 	memset(hdr, 0, sizeof(struct qeth_hdr));
243 	hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
244 
245 	/* set byte byte 3 to casting flags */
246 	if (cast_type == RTN_MULTICAST)
247 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
248 	else if (cast_type == RTN_BROADCAST)
249 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
250 	else
251 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
252 
253 	hdr->hdr.l2.pkt_length = skb->len-QETH_HEADER_SIZE;
254 	/* VSWITCH relies on the VLAN
255 	 * information to be present in
256 	 * the QDIO header */
257 	if (veth->h_vlan_proto == __constant_htons(ETH_P_8021Q)) {
258 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
259 		hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
260 	}
261 }
262 
263 static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
264 			struct qeth_reply *reply, unsigned long data)
265 {
266 	struct qeth_ipa_cmd *cmd;
267 
268 	QETH_CARD_TEXT(card, 2, "L2sdvcb");
269 	cmd = (struct qeth_ipa_cmd *) data;
270 	if (cmd->hdr.return_code) {
271 		QETH_DBF_MESSAGE(2, "Error in processing VLAN %i on %s: 0x%x. "
272 			  "Continuing\n", cmd->data.setdelvlan.vlan_id,
273 			  QETH_CARD_IFNAME(card), cmd->hdr.return_code);
274 		QETH_CARD_TEXT_(card, 2, "L2VL%4x", cmd->hdr.command);
275 		QETH_CARD_TEXT_(card, 2, "err%d", cmd->hdr.return_code);
276 	}
277 	return 0;
278 }
279 
280 static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
281 				enum qeth_ipa_cmds ipacmd)
282 {
283 	struct qeth_ipa_cmd *cmd;
284 	struct qeth_cmd_buffer *iob;
285 
286 	QETH_CARD_TEXT_(card, 4, "L2sdv%x", ipacmd);
287 	iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
288 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
289 	cmd->data.setdelvlan.vlan_id = i;
290 	return qeth_send_ipa_cmd(card, iob,
291 				 qeth_l2_send_setdelvlan_cb, NULL);
292 }
293 
294 static void qeth_l2_process_vlans(struct qeth_card *card)
295 {
296 	struct qeth_vlan_vid *id;
297 	QETH_CARD_TEXT(card, 3, "L2prcvln");
298 	spin_lock_bh(&card->vlanlock);
299 	list_for_each_entry(id, &card->vid_list, list) {
300 		qeth_l2_send_setdelvlan(card, id->vid, IPA_CMD_SETVLAN);
301 	}
302 	spin_unlock_bh(&card->vlanlock);
303 }
304 
305 static int qeth_l2_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
306 {
307 	struct qeth_card *card = dev->ml_priv;
308 	struct qeth_vlan_vid *id;
309 
310 	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
311 	if (!vid)
312 		return 0;
313 	if (card->info.type == QETH_CARD_TYPE_OSM) {
314 		QETH_CARD_TEXT(card, 3, "aidOSM");
315 		return 0;
316 	}
317 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
318 		QETH_CARD_TEXT(card, 3, "aidREC");
319 		return 0;
320 	}
321 	id = kmalloc(sizeof(struct qeth_vlan_vid), GFP_ATOMIC);
322 	if (id) {
323 		id->vid = vid;
324 		qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
325 		spin_lock_bh(&card->vlanlock);
326 		list_add_tail(&id->list, &card->vid_list);
327 		spin_unlock_bh(&card->vlanlock);
328 	} else {
329 		return -ENOMEM;
330 	}
331 	return 0;
332 }
333 
334 static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
335 {
336 	struct qeth_vlan_vid *id, *tmpid = NULL;
337 	struct qeth_card *card = dev->ml_priv;
338 
339 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
340 	if (card->info.type == QETH_CARD_TYPE_OSM) {
341 		QETH_CARD_TEXT(card, 3, "kidOSM");
342 		return 0;
343 	}
344 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
345 		QETH_CARD_TEXT(card, 3, "kidREC");
346 		return 0;
347 	}
348 	spin_lock_bh(&card->vlanlock);
349 	list_for_each_entry(id, &card->vid_list, list) {
350 		if (id->vid == vid) {
351 			list_del(&id->list);
352 			tmpid = id;
353 			break;
354 		}
355 	}
356 	spin_unlock_bh(&card->vlanlock);
357 	if (tmpid) {
358 		qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
359 		kfree(tmpid);
360 	}
361 	qeth_l2_set_multicast_list(card->dev);
362 	return 0;
363 }
364 
365 static int qeth_l2_stop_card(struct qeth_card *card, int recovery_mode)
366 {
367 	int rc = 0;
368 
369 	QETH_DBF_TEXT(SETUP , 2, "stopcard");
370 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
371 
372 	qeth_set_allowed_threads(card, 0, 1);
373 	if (card->read.state == CH_STATE_UP &&
374 	    card->write.state == CH_STATE_UP &&
375 	    (card->state == CARD_STATE_UP)) {
376 		if (recovery_mode &&
377 		    card->info.type != QETH_CARD_TYPE_OSN) {
378 			qeth_l2_stop(card->dev);
379 		} else {
380 			rtnl_lock();
381 			dev_close(card->dev);
382 			rtnl_unlock();
383 		}
384 		card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
385 		card->state = CARD_STATE_SOFTSETUP;
386 	}
387 	if (card->state == CARD_STATE_SOFTSETUP) {
388 		qeth_l2_del_all_mc(card, 0);
389 		qeth_clear_ipacmd_list(card);
390 		card->state = CARD_STATE_HARDSETUP;
391 	}
392 	if (card->state == CARD_STATE_HARDSETUP) {
393 		qeth_qdio_clear_card(card, 0);
394 		qeth_clear_qdio_buffers(card);
395 		qeth_clear_working_pool_list(card);
396 		card->state = CARD_STATE_DOWN;
397 	}
398 	if (card->state == CARD_STATE_DOWN) {
399 		qeth_clear_cmd_buffers(&card->read);
400 		qeth_clear_cmd_buffers(&card->write);
401 	}
402 	return rc;
403 }
404 
405 static int qeth_l2_process_inbound_buffer(struct qeth_card *card,
406 				int budget, int *done)
407 {
408 	int work_done = 0;
409 	struct sk_buff *skb;
410 	struct qeth_hdr *hdr;
411 	unsigned int len;
412 
413 	*done = 0;
414 	BUG_ON(!budget);
415 	while (budget) {
416 		skb = qeth_core_get_next_skb(card,
417 			&card->qdio.in_q->bufs[card->rx.b_index],
418 			&card->rx.b_element, &card->rx.e_offset, &hdr);
419 		if (!skb) {
420 			*done = 1;
421 			break;
422 		}
423 		skb->dev = card->dev;
424 		switch (hdr->hdr.l2.id) {
425 		case QETH_HEADER_TYPE_LAYER2:
426 			skb->pkt_type = PACKET_HOST;
427 			skb->protocol = eth_type_trans(skb, skb->dev);
428 			skb->ip_summed = CHECKSUM_NONE;
429 			if (skb->protocol == htons(ETH_P_802_2))
430 				*((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
431 			len = skb->len;
432 			netif_receive_skb(skb);
433 			break;
434 		case QETH_HEADER_TYPE_OSN:
435 			if (card->info.type == QETH_CARD_TYPE_OSN) {
436 				skb_push(skb, sizeof(struct qeth_hdr));
437 				skb_copy_to_linear_data(skb, hdr,
438 						sizeof(struct qeth_hdr));
439 				len = skb->len;
440 				card->osn_info.data_cb(skb);
441 				break;
442 			}
443 			/* else unknown */
444 		default:
445 			dev_kfree_skb_any(skb);
446 			QETH_CARD_TEXT(card, 3, "inbunkno");
447 			QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
448 			continue;
449 		}
450 		work_done++;
451 		budget--;
452 		card->stats.rx_packets++;
453 		card->stats.rx_bytes += len;
454 	}
455 	return work_done;
456 }
457 
458 static int qeth_l2_poll(struct napi_struct *napi, int budget)
459 {
460 	struct qeth_card *card = container_of(napi, struct qeth_card, napi);
461 	int work_done = 0;
462 	struct qeth_qdio_buffer *buffer;
463 	int done;
464 	int new_budget = budget;
465 
466 	if (card->options.performance_stats) {
467 		card->perf_stats.inbound_cnt++;
468 		card->perf_stats.inbound_start_time = qeth_get_micros();
469 	}
470 
471 	while (1) {
472 		if (!card->rx.b_count) {
473 			card->rx.qdio_err = 0;
474 			card->rx.b_count = qdio_get_next_buffers(
475 				card->data.ccwdev, 0, &card->rx.b_index,
476 				&card->rx.qdio_err);
477 			if (card->rx.b_count <= 0) {
478 				card->rx.b_count = 0;
479 				break;
480 			}
481 			card->rx.b_element =
482 				&card->qdio.in_q->bufs[card->rx.b_index]
483 				.buffer->element[0];
484 			card->rx.e_offset = 0;
485 		}
486 
487 		while (card->rx.b_count) {
488 			buffer = &card->qdio.in_q->bufs[card->rx.b_index];
489 			if (!(card->rx.qdio_err &&
490 			    qeth_check_qdio_errors(card, buffer->buffer,
491 			    card->rx.qdio_err, "qinerr")))
492 				work_done += qeth_l2_process_inbound_buffer(
493 					card, new_budget, &done);
494 			else
495 				done = 1;
496 
497 			if (done) {
498 				if (card->options.performance_stats)
499 					card->perf_stats.bufs_rec++;
500 				qeth_put_buffer_pool_entry(card,
501 					buffer->pool_entry);
502 				qeth_queue_input_buffer(card, card->rx.b_index);
503 				card->rx.b_count--;
504 				if (card->rx.b_count) {
505 					card->rx.b_index =
506 						(card->rx.b_index + 1) %
507 						QDIO_MAX_BUFFERS_PER_Q;
508 					card->rx.b_element =
509 						&card->qdio.in_q
510 						->bufs[card->rx.b_index]
511 						.buffer->element[0];
512 					card->rx.e_offset = 0;
513 				}
514 			}
515 
516 			if (work_done >= budget)
517 				goto out;
518 			else
519 				new_budget = budget - work_done;
520 		}
521 	}
522 
523 	napi_complete(napi);
524 	if (qdio_start_irq(card->data.ccwdev, 0))
525 		napi_schedule(&card->napi);
526 out:
527 	if (card->options.performance_stats)
528 		card->perf_stats.inbound_time += qeth_get_micros() -
529 			card->perf_stats.inbound_start_time;
530 	return work_done;
531 }
532 
533 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
534 			   enum qeth_ipa_cmds ipacmd,
535 			   int (*reply_cb) (struct qeth_card *,
536 					    struct qeth_reply*,
537 					    unsigned long))
538 {
539 	struct qeth_ipa_cmd *cmd;
540 	struct qeth_cmd_buffer *iob;
541 
542 	QETH_CARD_TEXT(card, 2, "L2sdmac");
543 	iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
544 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
545 	cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
546 	memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
547 	return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
548 }
549 
550 static int qeth_l2_send_setmac_cb(struct qeth_card *card,
551 			   struct qeth_reply *reply,
552 			   unsigned long data)
553 {
554 	struct qeth_ipa_cmd *cmd;
555 
556 	QETH_CARD_TEXT(card, 2, "L2Smaccb");
557 	cmd = (struct qeth_ipa_cmd *) data;
558 	if (cmd->hdr.return_code) {
559 		QETH_CARD_TEXT_(card, 2, "L2er%x", cmd->hdr.return_code);
560 		card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
561 		switch (cmd->hdr.return_code) {
562 		case IPA_RC_L2_DUP_MAC:
563 		case IPA_RC_L2_DUP_LAYER3_MAC:
564 			dev_warn(&card->gdev->dev,
565 				"MAC address %pM already exists\n",
566 				cmd->data.setdelmac.mac);
567 			break;
568 		case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
569 		case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
570 			dev_warn(&card->gdev->dev,
571 				"MAC address %pM is not authorized\n",
572 				cmd->data.setdelmac.mac);
573 			break;
574 		default:
575 			break;
576 		}
577 	} else {
578 		card->info.mac_bits |= QETH_LAYER2_MAC_REGISTERED;
579 		memcpy(card->dev->dev_addr, cmd->data.setdelmac.mac,
580 		       OSA_ADDR_LEN);
581 		dev_info(&card->gdev->dev,
582 			"MAC address %pM successfully registered on device %s\n",
583 			card->dev->dev_addr, card->dev->name);
584 	}
585 	return 0;
586 }
587 
588 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
589 {
590 	QETH_CARD_TEXT(card, 2, "L2Setmac");
591 	return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
592 					  qeth_l2_send_setmac_cb);
593 }
594 
595 static int qeth_l2_send_delmac_cb(struct qeth_card *card,
596 			   struct qeth_reply *reply,
597 			   unsigned long data)
598 {
599 	struct qeth_ipa_cmd *cmd;
600 
601 	QETH_CARD_TEXT(card, 2, "L2Dmaccb");
602 	cmd = (struct qeth_ipa_cmd *) data;
603 	if (cmd->hdr.return_code) {
604 		QETH_CARD_TEXT_(card, 2, "err%d", cmd->hdr.return_code);
605 		return 0;
606 	}
607 	card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
608 
609 	return 0;
610 }
611 
612 static int qeth_l2_send_delmac(struct qeth_card *card, __u8 *mac)
613 {
614 	QETH_CARD_TEXT(card, 2, "L2Delmac");
615 	if (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))
616 		return 0;
617 	return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
618 					  qeth_l2_send_delmac_cb);
619 }
620 
621 static int qeth_l2_request_initial_mac(struct qeth_card *card)
622 {
623 	int rc = 0;
624 	char vendor_pre[] = {0x02, 0x00, 0x00};
625 
626 	QETH_DBF_TEXT(SETUP, 2, "doL2init");
627 	QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
628 
629 	rc = qeth_query_setadapterparms(card);
630 	if (rc) {
631 		QETH_DBF_MESSAGE(2, "could not query adapter parameters on "
632 			"device %s: x%x\n", CARD_BUS_ID(card), rc);
633 	}
634 
635 	if (card->info.type == QETH_CARD_TYPE_IQD ||
636 	    card->info.type == QETH_CARD_TYPE_OSM ||
637 	    card->info.type == QETH_CARD_TYPE_OSX ||
638 	    card->info.guestlan) {
639 		rc = qeth_setadpparms_change_macaddr(card);
640 		if (rc) {
641 			QETH_DBF_MESSAGE(2, "couldn't get MAC address on "
642 				"device %s: x%x\n", CARD_BUS_ID(card), rc);
643 			QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
644 			return rc;
645 		}
646 		QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, OSA_ADDR_LEN);
647 	} else {
648 		eth_random_addr(card->dev->dev_addr);
649 		memcpy(card->dev->dev_addr, vendor_pre, 3);
650 	}
651 	return 0;
652 }
653 
654 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
655 {
656 	struct sockaddr *addr = p;
657 	struct qeth_card *card = dev->ml_priv;
658 	int rc = 0;
659 
660 	QETH_CARD_TEXT(card, 3, "setmac");
661 
662 	if (qeth_l2_verify_dev(dev) != QETH_REAL_CARD) {
663 		QETH_CARD_TEXT(card, 3, "setmcINV");
664 		return -EOPNOTSUPP;
665 	}
666 
667 	if (card->info.type == QETH_CARD_TYPE_OSN ||
668 	    card->info.type == QETH_CARD_TYPE_OSM ||
669 	    card->info.type == QETH_CARD_TYPE_OSX) {
670 		QETH_CARD_TEXT(card, 3, "setmcTYP");
671 		return -EOPNOTSUPP;
672 	}
673 	QETH_CARD_HEX(card, 3, addr->sa_data, OSA_ADDR_LEN);
674 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
675 		QETH_CARD_TEXT(card, 3, "setmcREC");
676 		return -ERESTARTSYS;
677 	}
678 	rc = qeth_l2_send_delmac(card, &card->dev->dev_addr[0]);
679 	if (!rc)
680 		rc = qeth_l2_send_setmac(card, addr->sa_data);
681 	return rc ? -EINVAL : 0;
682 }
683 
684 static void qeth_l2_set_multicast_list(struct net_device *dev)
685 {
686 	struct qeth_card *card = dev->ml_priv;
687 	struct netdev_hw_addr *ha;
688 
689 	if (card->info.type == QETH_CARD_TYPE_OSN)
690 		return ;
691 
692 	QETH_CARD_TEXT(card, 3, "setmulti");
693 	if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
694 	    (card->state != CARD_STATE_UP))
695 		return;
696 	qeth_l2_del_all_mc(card, 1);
697 	spin_lock_bh(&card->mclock);
698 	netdev_for_each_mc_addr(ha, dev)
699 		qeth_l2_add_mc(card, ha->addr, 0);
700 
701 	netdev_for_each_uc_addr(ha, dev)
702 		qeth_l2_add_mc(card, ha->addr, 1);
703 
704 	spin_unlock_bh(&card->mclock);
705 	if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
706 		return;
707 	qeth_setadp_promisc_mode(card);
708 }
709 
710 static int qeth_l2_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
711 {
712 	int rc;
713 	struct qeth_hdr *hdr = NULL;
714 	int elements = 0;
715 	struct qeth_card *card = dev->ml_priv;
716 	struct sk_buff *new_skb = skb;
717 	int ipv = qeth_get_ip_version(skb);
718 	int cast_type = qeth_l2_get_cast_type(card, skb);
719 	struct qeth_qdio_out_q *queue = card->qdio.out_qs
720 		[qeth_get_priority_queue(card, skb, ipv, cast_type)];
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->state != CARD_STATE_UP) || !card->lan_online) {
727 		card->stats.tx_carrier_errors++;
728 		goto tx_drop;
729 	}
730 
731 	if ((card->info.type == QETH_CARD_TYPE_OSN) &&
732 	    (skb->protocol == htons(ETH_P_IPV6)))
733 		goto tx_drop;
734 
735 	if (card->options.performance_stats) {
736 		card->perf_stats.outbound_cnt++;
737 		card->perf_stats.outbound_start_time = qeth_get_micros();
738 	}
739 	netif_stop_queue(dev);
740 
741 	if (card->info.type == QETH_CARD_TYPE_OSN)
742 		hdr = (struct qeth_hdr *)skb->data;
743 	else {
744 		if (card->info.type == QETH_CARD_TYPE_IQD) {
745 			new_skb = skb;
746 			data_offset = ETH_HLEN;
747 			hd_len = ETH_HLEN;
748 			hdr = kmem_cache_alloc(qeth_core_header_cache,
749 						GFP_ATOMIC);
750 			if (!hdr)
751 				goto tx_drop;
752 			elements_needed++;
753 			skb_reset_mac_header(new_skb);
754 			qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
755 			hdr->hdr.l2.pkt_length = new_skb->len;
756 			memcpy(((char *)hdr) + sizeof(struct qeth_hdr),
757 				skb_mac_header(new_skb), ETH_HLEN);
758 		} else {
759 			/* create a clone with writeable headroom */
760 			new_skb = skb_realloc_headroom(skb,
761 						sizeof(struct qeth_hdr));
762 			if (!new_skb)
763 				goto tx_drop;
764 			hdr = (struct qeth_hdr *)skb_push(new_skb,
765 						sizeof(struct qeth_hdr));
766 			skb_set_mac_header(new_skb, sizeof(struct qeth_hdr));
767 			qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
768 		}
769 	}
770 
771 	elements = qeth_get_elements_no(card, (void *)hdr, new_skb,
772 						elements_needed);
773 	if (!elements) {
774 		if (data_offset >= 0)
775 			kmem_cache_free(qeth_core_header_cache, hdr);
776 		goto tx_drop;
777 	}
778 
779 	if (card->info.type != QETH_CARD_TYPE_IQD) {
780 		if (qeth_hdr_chk_and_bounce(new_skb,
781 		    sizeof(struct qeth_hdr_layer2)))
782 			goto tx_drop;
783 		rc = qeth_do_send_packet(card, queue, new_skb, hdr,
784 					 elements);
785 	} else
786 		rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
787 					elements, data_offset, hd_len);
788 	if (!rc) {
789 		card->stats.tx_packets++;
790 		card->stats.tx_bytes += tx_bytes;
791 		if (new_skb != skb)
792 			dev_kfree_skb_any(skb);
793 		rc = NETDEV_TX_OK;
794 	} else {
795 		if (data_offset >= 0)
796 			kmem_cache_free(qeth_core_header_cache, hdr);
797 
798 		if (rc == -EBUSY) {
799 			if (new_skb != skb)
800 				dev_kfree_skb_any(new_skb);
801 			return NETDEV_TX_BUSY;
802 		} else
803 			goto tx_drop;
804 	}
805 
806 	netif_wake_queue(dev);
807 	if (card->options.performance_stats)
808 		card->perf_stats.outbound_time += qeth_get_micros() -
809 			card->perf_stats.outbound_start_time;
810 	return rc;
811 
812 tx_drop:
813 	card->stats.tx_dropped++;
814 	card->stats.tx_errors++;
815 	if ((new_skb != skb) && new_skb)
816 		dev_kfree_skb_any(new_skb);
817 	dev_kfree_skb_any(skb);
818 	netif_wake_queue(dev);
819 	return NETDEV_TX_OK;
820 }
821 
822 static int __qeth_l2_open(struct net_device *dev)
823 {
824 	struct qeth_card *card = dev->ml_priv;
825 	int rc = 0;
826 
827 	QETH_CARD_TEXT(card, 4, "qethopen");
828 	if (card->state == CARD_STATE_UP)
829 		return rc;
830 	if (card->state != CARD_STATE_SOFTSETUP)
831 		return -ENODEV;
832 
833 	if ((card->info.type != QETH_CARD_TYPE_OSN) &&
834 	     (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
835 		QETH_CARD_TEXT(card, 4, "nomacadr");
836 		return -EPERM;
837 	}
838 	card->data.state = CH_STATE_UP;
839 	card->state = CARD_STATE_UP;
840 	netif_start_queue(dev);
841 
842 	if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
843 		napi_enable(&card->napi);
844 		napi_schedule(&card->napi);
845 	} else
846 		rc = -EIO;
847 	return rc;
848 }
849 
850 static int qeth_l2_open(struct net_device *dev)
851 {
852 	struct qeth_card *card = dev->ml_priv;
853 
854 	QETH_CARD_TEXT(card, 5, "qethope_");
855 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
856 		QETH_CARD_TEXT(card, 3, "openREC");
857 		return -ERESTARTSYS;
858 	}
859 	return __qeth_l2_open(dev);
860 }
861 
862 static int qeth_l2_stop(struct net_device *dev)
863 {
864 	struct qeth_card *card = dev->ml_priv;
865 
866 	QETH_CARD_TEXT(card, 4, "qethstop");
867 	netif_tx_disable(dev);
868 	if (card->state == CARD_STATE_UP) {
869 		card->state = CARD_STATE_SOFTSETUP;
870 		napi_disable(&card->napi);
871 	}
872 	return 0;
873 }
874 
875 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
876 {
877 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
878 
879 	INIT_LIST_HEAD(&card->vid_list);
880 	INIT_LIST_HEAD(&card->mc_list);
881 	card->options.layer2 = 1;
882 	card->info.hwtrap = 0;
883 	return 0;
884 }
885 
886 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
887 {
888 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
889 
890 	qeth_set_allowed_threads(card, 0, 1);
891 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
892 
893 	if (cgdev->state == CCWGROUP_ONLINE)
894 		qeth_l2_set_offline(cgdev);
895 
896 	if (card->dev) {
897 		unregister_netdev(card->dev);
898 		card->dev = NULL;
899 	}
900 	return;
901 }
902 
903 static const struct ethtool_ops qeth_l2_ethtool_ops = {
904 	.get_link = ethtool_op_get_link,
905 	.get_strings = qeth_core_get_strings,
906 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
907 	.get_sset_count = qeth_core_get_sset_count,
908 	.get_drvinfo = qeth_core_get_drvinfo,
909 	.get_settings = qeth_core_ethtool_get_settings,
910 };
911 
912 static const struct ethtool_ops qeth_l2_osn_ops = {
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 };
918 
919 static const struct net_device_ops qeth_l2_netdev_ops = {
920 	.ndo_open		= qeth_l2_open,
921 	.ndo_stop		= qeth_l2_stop,
922 	.ndo_get_stats		= qeth_get_stats,
923 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
924 	.ndo_validate_addr	= eth_validate_addr,
925 	.ndo_set_rx_mode	= qeth_l2_set_multicast_list,
926 	.ndo_do_ioctl	   	= qeth_l2_do_ioctl,
927 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
928 	.ndo_change_mtu	   	= qeth_change_mtu,
929 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
930 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
931 	.ndo_tx_timeout	   	= qeth_tx_timeout,
932 };
933 
934 static int qeth_l2_setup_netdev(struct qeth_card *card)
935 {
936 	switch (card->info.type) {
937 	case QETH_CARD_TYPE_IQD:
938 		card->dev = alloc_netdev(0, "hsi%d", ether_setup);
939 		break;
940 	case QETH_CARD_TYPE_OSN:
941 		card->dev = alloc_netdev(0, "osn%d", ether_setup);
942 		card->dev->flags |= IFF_NOARP;
943 		break;
944 	default:
945 		card->dev = alloc_etherdev(0);
946 	}
947 
948 	if (!card->dev)
949 		return -ENODEV;
950 
951 	card->dev->ml_priv = card;
952 	card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
953 	card->dev->mtu = card->info.initial_mtu;
954 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
955 	if (card->info.type != QETH_CARD_TYPE_OSN)
956 		SET_ETHTOOL_OPS(card->dev, &qeth_l2_ethtool_ops);
957 	else
958 		SET_ETHTOOL_OPS(card->dev, &qeth_l2_osn_ops);
959 	card->dev->features |= NETIF_F_HW_VLAN_FILTER;
960 	card->info.broadcast_capable = 1;
961 	qeth_l2_request_initial_mac(card);
962 	SET_NETDEV_DEV(card->dev, &card->gdev->dev);
963 	netif_napi_add(card->dev, &card->napi, qeth_l2_poll, QETH_NAPI_WEIGHT);
964 	return register_netdev(card->dev);
965 }
966 
967 static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
968 {
969 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
970 	int rc = 0;
971 	enum qeth_card_states recover_flag;
972 
973 	BUG_ON(!card);
974 	mutex_lock(&card->discipline_mutex);
975 	mutex_lock(&card->conf_mutex);
976 	QETH_DBF_TEXT(SETUP, 2, "setonlin");
977 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
978 
979 	recover_flag = card->state;
980 	rc = qeth_core_hardsetup_card(card);
981 	if (rc) {
982 		QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
983 		rc = -ENODEV;
984 		goto out_remove;
985 	}
986 
987 	if (!card->dev && qeth_l2_setup_netdev(card)) {
988 		rc = -ENODEV;
989 		goto out_remove;
990 	}
991 
992 	if (card->info.type != QETH_CARD_TYPE_OSN)
993 		qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
994 
995 	if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
996 		if (card->info.hwtrap &&
997 		    qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
998 			card->info.hwtrap = 0;
999 	} else
1000 		card->info.hwtrap = 0;
1001 
1002 	card->state = CARD_STATE_HARDSETUP;
1003 	memset(&card->rx, 0, sizeof(struct qeth_rx));
1004 	qeth_print_status_message(card);
1005 
1006 	/* softsetup */
1007 	QETH_DBF_TEXT(SETUP, 2, "softsetp");
1008 
1009 	rc = qeth_send_startlan(card);
1010 	if (rc) {
1011 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1012 		if (rc == 0xe080) {
1013 			dev_warn(&card->gdev->dev,
1014 				"The LAN is offline\n");
1015 			card->lan_online = 0;
1016 			goto contin;
1017 		}
1018 		rc = -ENODEV;
1019 		goto out_remove;
1020 	} else
1021 		card->lan_online = 1;
1022 
1023 contin:
1024 	if ((card->info.type == QETH_CARD_TYPE_OSD) ||
1025 	    (card->info.type == QETH_CARD_TYPE_OSX))
1026 		/* configure isolation level */
1027 		qeth_set_access_ctrl_online(card);
1028 
1029 	if (card->info.type != QETH_CARD_TYPE_OSN &&
1030 	    card->info.type != QETH_CARD_TYPE_OSM)
1031 		qeth_l2_process_vlans(card);
1032 
1033 	netif_tx_disable(card->dev);
1034 
1035 	rc = qeth_init_qdio_queues(card);
1036 	if (rc) {
1037 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
1038 		rc = -ENODEV;
1039 		goto out_remove;
1040 	}
1041 	card->state = CARD_STATE_SOFTSETUP;
1042 	if (card->lan_online)
1043 		netif_carrier_on(card->dev);
1044 	else
1045 		netif_carrier_off(card->dev);
1046 
1047 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1048 	if (recover_flag == CARD_STATE_RECOVER) {
1049 		if (recovery_mode &&
1050 		    card->info.type != QETH_CARD_TYPE_OSN) {
1051 			__qeth_l2_open(card->dev);
1052 		} else {
1053 			rtnl_lock();
1054 			dev_open(card->dev);
1055 			rtnl_unlock();
1056 		}
1057 		/* this also sets saved unicast addresses */
1058 		qeth_l2_set_multicast_list(card->dev);
1059 	}
1060 	/* let user_space know that device is online */
1061 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
1062 	mutex_unlock(&card->conf_mutex);
1063 	mutex_unlock(&card->discipline_mutex);
1064 	return 0;
1065 
1066 out_remove:
1067 	qeth_l2_stop_card(card, 0);
1068 	ccw_device_set_offline(CARD_DDEV(card));
1069 	ccw_device_set_offline(CARD_WDEV(card));
1070 	ccw_device_set_offline(CARD_RDEV(card));
1071 	if (recover_flag == CARD_STATE_RECOVER)
1072 		card->state = CARD_STATE_RECOVER;
1073 	else
1074 		card->state = CARD_STATE_DOWN;
1075 	mutex_unlock(&card->conf_mutex);
1076 	mutex_unlock(&card->discipline_mutex);
1077 	return rc;
1078 }
1079 
1080 static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1081 {
1082 	return __qeth_l2_set_online(gdev, 0);
1083 }
1084 
1085 static int __qeth_l2_set_offline(struct ccwgroup_device *cgdev,
1086 					int recovery_mode)
1087 {
1088 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1089 	int rc = 0, rc2 = 0, rc3 = 0;
1090 	enum qeth_card_states recover_flag;
1091 
1092 	mutex_lock(&card->discipline_mutex);
1093 	mutex_lock(&card->conf_mutex);
1094 	QETH_DBF_TEXT(SETUP, 3, "setoffl");
1095 	QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
1096 
1097 	if (card->dev && netif_carrier_ok(card->dev))
1098 		netif_carrier_off(card->dev);
1099 	recover_flag = card->state;
1100 	if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
1101 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1102 		card->info.hwtrap = 1;
1103 	}
1104 	qeth_l2_stop_card(card, recovery_mode);
1105 	rc  = ccw_device_set_offline(CARD_DDEV(card));
1106 	rc2 = ccw_device_set_offline(CARD_WDEV(card));
1107 	rc3 = ccw_device_set_offline(CARD_RDEV(card));
1108 	if (!rc)
1109 		rc = (rc2) ? rc2 : rc3;
1110 	if (rc)
1111 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1112 	if (recover_flag == CARD_STATE_UP)
1113 		card->state = CARD_STATE_RECOVER;
1114 	/* let user_space know that device is offline */
1115 	kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
1116 	mutex_unlock(&card->conf_mutex);
1117 	mutex_unlock(&card->discipline_mutex);
1118 	return 0;
1119 }
1120 
1121 static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1122 {
1123 	return __qeth_l2_set_offline(cgdev, 0);
1124 }
1125 
1126 static int qeth_l2_recover(void *ptr)
1127 {
1128 	struct qeth_card *card;
1129 	int rc = 0;
1130 
1131 	card = (struct qeth_card *) ptr;
1132 	QETH_CARD_TEXT(card, 2, "recover1");
1133 	if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1134 		return 0;
1135 	QETH_CARD_TEXT(card, 2, "recover2");
1136 	dev_warn(&card->gdev->dev,
1137 		"A recovery process has been started for the device\n");
1138 	__qeth_l2_set_offline(card->gdev, 1);
1139 	rc = __qeth_l2_set_online(card->gdev, 1);
1140 	if (!rc)
1141 		dev_info(&card->gdev->dev,
1142 			"Device successfully recovered!\n");
1143 	else {
1144 		rtnl_lock();
1145 		dev_close(card->dev);
1146 		rtnl_unlock();
1147 		dev_warn(&card->gdev->dev, "The qeth device driver "
1148 			"failed to recover an error on the device\n");
1149 	}
1150 	qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1151 	qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1152 	return 0;
1153 }
1154 
1155 static int __init qeth_l2_init(void)
1156 {
1157 	pr_info("register layer 2 discipline\n");
1158 	return 0;
1159 }
1160 
1161 static void __exit qeth_l2_exit(void)
1162 {
1163 	pr_info("unregister layer 2 discipline\n");
1164 }
1165 
1166 static void qeth_l2_shutdown(struct ccwgroup_device *gdev)
1167 {
1168 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1169 	qeth_set_allowed_threads(card, 0, 1);
1170 	if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
1171 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1172 	qeth_qdio_clear_card(card, 0);
1173 	qeth_clear_qdio_buffers(card);
1174 }
1175 
1176 static int qeth_l2_pm_suspend(struct ccwgroup_device *gdev)
1177 {
1178 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1179 
1180 	if (card->dev)
1181 		netif_device_detach(card->dev);
1182 	qeth_set_allowed_threads(card, 0, 1);
1183 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
1184 	if (gdev->state == CCWGROUP_OFFLINE)
1185 		return 0;
1186 	if (card->state == CARD_STATE_UP) {
1187 		if (card->info.hwtrap)
1188 			qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1189 		__qeth_l2_set_offline(card->gdev, 1);
1190 	} else
1191 		__qeth_l2_set_offline(card->gdev, 0);
1192 	return 0;
1193 }
1194 
1195 static int qeth_l2_pm_resume(struct ccwgroup_device *gdev)
1196 {
1197 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1198 	int rc = 0;
1199 
1200 	if (gdev->state == CCWGROUP_OFFLINE)
1201 		goto out;
1202 
1203 	if (card->state == CARD_STATE_RECOVER) {
1204 		rc = __qeth_l2_set_online(card->gdev, 1);
1205 		if (rc) {
1206 			rtnl_lock();
1207 			dev_close(card->dev);
1208 			rtnl_unlock();
1209 		}
1210 	} else
1211 		rc = __qeth_l2_set_online(card->gdev, 0);
1212 out:
1213 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1214 	if (card->dev)
1215 		netif_device_attach(card->dev);
1216 	if (rc)
1217 		dev_warn(&card->gdev->dev, "The qeth device driver "
1218 			"failed to recover an error on the device\n");
1219 	return rc;
1220 }
1221 
1222 struct qeth_discipline qeth_l2_discipline = {
1223 	.start_poll = qeth_qdio_start_poll,
1224 	.input_handler = (qdio_handler_t *) qeth_qdio_input_handler,
1225 	.output_handler = (qdio_handler_t *) qeth_qdio_output_handler,
1226 	.recover = qeth_l2_recover,
1227 	.setup = qeth_l2_probe_device,
1228 	.remove = qeth_l2_remove_device,
1229 	.set_online = qeth_l2_set_online,
1230 	.set_offline = qeth_l2_set_offline,
1231 	.shutdown = qeth_l2_shutdown,
1232 	.freeze = qeth_l2_pm_suspend,
1233 	.thaw = qeth_l2_pm_resume,
1234 	.restore = qeth_l2_pm_resume,
1235 };
1236 EXPORT_SYMBOL_GPL(qeth_l2_discipline);
1237 
1238 static int qeth_osn_send_control_data(struct qeth_card *card, int len,
1239 			   struct qeth_cmd_buffer *iob)
1240 {
1241 	unsigned long flags;
1242 	int rc = 0;
1243 
1244 	QETH_CARD_TEXT(card, 5, "osndctrd");
1245 
1246 	wait_event(card->wait_q,
1247 		   atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1248 	qeth_prepare_control_data(card, len, iob);
1249 	QETH_CARD_TEXT(card, 6, "osnoirqp");
1250 	spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1251 	rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1252 			      (addr_t) iob, 0, 0);
1253 	spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1254 	if (rc) {
1255 		QETH_DBF_MESSAGE(2, "qeth_osn_send_control_data: "
1256 			   "ccw_device_start rc = %i\n", rc);
1257 		QETH_CARD_TEXT_(card, 2, " err%d", rc);
1258 		qeth_release_buffer(iob->channel, iob);
1259 		atomic_set(&card->write.irq_pending, 0);
1260 		wake_up(&card->wait_q);
1261 	}
1262 	return rc;
1263 }
1264 
1265 static int qeth_osn_send_ipa_cmd(struct qeth_card *card,
1266 			struct qeth_cmd_buffer *iob, int data_len)
1267 {
1268 	u16 s1, s2;
1269 
1270 	QETH_CARD_TEXT(card, 4, "osndipa");
1271 
1272 	qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1273 	s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1274 	s2 = (u16)data_len;
1275 	memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1276 	memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1277 	memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1278 	memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1279 	return qeth_osn_send_control_data(card, s1, iob);
1280 }
1281 
1282 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1283 {
1284 	struct qeth_cmd_buffer *iob;
1285 	struct qeth_card *card;
1286 	int rc;
1287 
1288 	if (!dev)
1289 		return -ENODEV;
1290 	card = dev->ml_priv;
1291 	if (!card)
1292 		return -ENODEV;
1293 	QETH_CARD_TEXT(card, 2, "osnsdmc");
1294 	if ((card->state != CARD_STATE_UP) &&
1295 	    (card->state != CARD_STATE_SOFTSETUP))
1296 		return -ENODEV;
1297 	iob = qeth_wait_for_buffer(&card->write);
1298 	memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1299 	rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1300 	return rc;
1301 }
1302 EXPORT_SYMBOL(qeth_osn_assist);
1303 
1304 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1305 		  int (*assist_cb)(struct net_device *, void *),
1306 		  int (*data_cb)(struct sk_buff *))
1307 {
1308 	struct qeth_card *card;
1309 
1310 	*dev = qeth_l2_netdev_by_devno(read_dev_no);
1311 	if (*dev == NULL)
1312 		return -ENODEV;
1313 	card = (*dev)->ml_priv;
1314 	if (!card)
1315 		return -ENODEV;
1316 	QETH_CARD_TEXT(card, 2, "osnreg");
1317 	if ((assist_cb == NULL) || (data_cb == NULL))
1318 		return -EINVAL;
1319 	card->osn_info.assist_cb = assist_cb;
1320 	card->osn_info.data_cb = data_cb;
1321 	return 0;
1322 }
1323 EXPORT_SYMBOL(qeth_osn_register);
1324 
1325 void qeth_osn_deregister(struct net_device *dev)
1326 {
1327 	struct qeth_card *card;
1328 
1329 	if (!dev)
1330 		return;
1331 	card = dev->ml_priv;
1332 	if (!card)
1333 		return;
1334 	QETH_CARD_TEXT(card, 2, "osndereg");
1335 	card->osn_info.assist_cb = NULL;
1336 	card->osn_info.data_cb = NULL;
1337 	return;
1338 }
1339 EXPORT_SYMBOL(qeth_osn_deregister);
1340 
1341 module_init(qeth_l2_init);
1342 module_exit(qeth_l2_exit);
1343 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1344 MODULE_DESCRIPTION("qeth layer 2 discipline");
1345 MODULE_LICENSE("GPL");
1346