xref: /openbmc/linux/drivers/s390/net/qeth_l2_main.c (revision 1dd24dae)
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,
306 				   __be16 proto, u16 vid)
307 {
308 	struct qeth_card *card = dev->ml_priv;
309 	struct qeth_vlan_vid *id;
310 
311 	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
312 	if (!vid)
313 		return 0;
314 	if (card->info.type == QETH_CARD_TYPE_OSM) {
315 		QETH_CARD_TEXT(card, 3, "aidOSM");
316 		return 0;
317 	}
318 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
319 		QETH_CARD_TEXT(card, 3, "aidREC");
320 		return 0;
321 	}
322 	id = kmalloc(sizeof(struct qeth_vlan_vid), GFP_ATOMIC);
323 	if (id) {
324 		id->vid = vid;
325 		qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
326 		spin_lock_bh(&card->vlanlock);
327 		list_add_tail(&id->list, &card->vid_list);
328 		spin_unlock_bh(&card->vlanlock);
329 	} else {
330 		return -ENOMEM;
331 	}
332 	return 0;
333 }
334 
335 static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev,
336 				    __be16 proto, u16 vid)
337 {
338 	struct qeth_vlan_vid *id, *tmpid = NULL;
339 	struct qeth_card *card = dev->ml_priv;
340 
341 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
342 	if (card->info.type == QETH_CARD_TYPE_OSM) {
343 		QETH_CARD_TEXT(card, 3, "kidOSM");
344 		return 0;
345 	}
346 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
347 		QETH_CARD_TEXT(card, 3, "kidREC");
348 		return 0;
349 	}
350 	spin_lock_bh(&card->vlanlock);
351 	list_for_each_entry(id, &card->vid_list, list) {
352 		if (id->vid == vid) {
353 			list_del(&id->list);
354 			tmpid = id;
355 			break;
356 		}
357 	}
358 	spin_unlock_bh(&card->vlanlock);
359 	if (tmpid) {
360 		qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
361 		kfree(tmpid);
362 	}
363 	qeth_l2_set_multicast_list(card->dev);
364 	return 0;
365 }
366 
367 static int qeth_l2_stop_card(struct qeth_card *card, int recovery_mode)
368 {
369 	int rc = 0;
370 
371 	QETH_DBF_TEXT(SETUP , 2, "stopcard");
372 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
373 
374 	qeth_set_allowed_threads(card, 0, 1);
375 	if (card->read.state == CH_STATE_UP &&
376 	    card->write.state == CH_STATE_UP &&
377 	    (card->state == CARD_STATE_UP)) {
378 		if (recovery_mode &&
379 		    card->info.type != QETH_CARD_TYPE_OSN) {
380 			qeth_l2_stop(card->dev);
381 		} else {
382 			rtnl_lock();
383 			dev_close(card->dev);
384 			rtnl_unlock();
385 		}
386 		card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
387 		card->state = CARD_STATE_SOFTSETUP;
388 	}
389 	if (card->state == CARD_STATE_SOFTSETUP) {
390 		qeth_l2_del_all_mc(card, 0);
391 		qeth_clear_ipacmd_list(card);
392 		card->state = CARD_STATE_HARDSETUP;
393 	}
394 	if (card->state == CARD_STATE_HARDSETUP) {
395 		qeth_qdio_clear_card(card, 0);
396 		qeth_clear_qdio_buffers(card);
397 		qeth_clear_working_pool_list(card);
398 		card->state = CARD_STATE_DOWN;
399 	}
400 	if (card->state == CARD_STATE_DOWN) {
401 		qeth_clear_cmd_buffers(&card->read);
402 		qeth_clear_cmd_buffers(&card->write);
403 	}
404 	return rc;
405 }
406 
407 static int qeth_l2_process_inbound_buffer(struct qeth_card *card,
408 				int budget, int *done)
409 {
410 	int work_done = 0;
411 	struct sk_buff *skb;
412 	struct qeth_hdr *hdr;
413 	unsigned int len;
414 
415 	*done = 0;
416 	WARN_ON_ONCE(!budget);
417 	while (budget) {
418 		skb = qeth_core_get_next_skb(card,
419 			&card->qdio.in_q->bufs[card->rx.b_index],
420 			&card->rx.b_element, &card->rx.e_offset, &hdr);
421 		if (!skb) {
422 			*done = 1;
423 			break;
424 		}
425 		skb->dev = card->dev;
426 		switch (hdr->hdr.l2.id) {
427 		case QETH_HEADER_TYPE_LAYER2:
428 			skb->pkt_type = PACKET_HOST;
429 			skb->protocol = eth_type_trans(skb, skb->dev);
430 			skb->ip_summed = CHECKSUM_NONE;
431 			if (skb->protocol == htons(ETH_P_802_2))
432 				*((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
433 			len = skb->len;
434 			netif_receive_skb(skb);
435 			break;
436 		case QETH_HEADER_TYPE_OSN:
437 			if (card->info.type == QETH_CARD_TYPE_OSN) {
438 				skb_push(skb, sizeof(struct qeth_hdr));
439 				skb_copy_to_linear_data(skb, hdr,
440 						sizeof(struct qeth_hdr));
441 				len = skb->len;
442 				card->osn_info.data_cb(skb);
443 				break;
444 			}
445 			/* else unknown */
446 		default:
447 			dev_kfree_skb_any(skb);
448 			QETH_CARD_TEXT(card, 3, "inbunkno");
449 			QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
450 			continue;
451 		}
452 		work_done++;
453 		budget--;
454 		card->stats.rx_packets++;
455 		card->stats.rx_bytes += len;
456 	}
457 	return work_done;
458 }
459 
460 static int qeth_l2_poll(struct napi_struct *napi, int budget)
461 {
462 	struct qeth_card *card = container_of(napi, struct qeth_card, napi);
463 	int work_done = 0;
464 	struct qeth_qdio_buffer *buffer;
465 	int done;
466 	int new_budget = budget;
467 
468 	if (card->options.performance_stats) {
469 		card->perf_stats.inbound_cnt++;
470 		card->perf_stats.inbound_start_time = qeth_get_micros();
471 	}
472 
473 	while (1) {
474 		if (!card->rx.b_count) {
475 			card->rx.qdio_err = 0;
476 			card->rx.b_count = qdio_get_next_buffers(
477 				card->data.ccwdev, 0, &card->rx.b_index,
478 				&card->rx.qdio_err);
479 			if (card->rx.b_count <= 0) {
480 				card->rx.b_count = 0;
481 				break;
482 			}
483 			card->rx.b_element =
484 				&card->qdio.in_q->bufs[card->rx.b_index]
485 				.buffer->element[0];
486 			card->rx.e_offset = 0;
487 		}
488 
489 		while (card->rx.b_count) {
490 			buffer = &card->qdio.in_q->bufs[card->rx.b_index];
491 			if (!(card->rx.qdio_err &&
492 			    qeth_check_qdio_errors(card, buffer->buffer,
493 			    card->rx.qdio_err, "qinerr")))
494 				work_done += qeth_l2_process_inbound_buffer(
495 					card, new_budget, &done);
496 			else
497 				done = 1;
498 
499 			if (done) {
500 				if (card->options.performance_stats)
501 					card->perf_stats.bufs_rec++;
502 				qeth_put_buffer_pool_entry(card,
503 					buffer->pool_entry);
504 				qeth_queue_input_buffer(card, card->rx.b_index);
505 				card->rx.b_count--;
506 				if (card->rx.b_count) {
507 					card->rx.b_index =
508 						(card->rx.b_index + 1) %
509 						QDIO_MAX_BUFFERS_PER_Q;
510 					card->rx.b_element =
511 						&card->qdio.in_q
512 						->bufs[card->rx.b_index]
513 						.buffer->element[0];
514 					card->rx.e_offset = 0;
515 				}
516 			}
517 
518 			if (work_done >= budget)
519 				goto out;
520 			else
521 				new_budget = budget - work_done;
522 		}
523 	}
524 
525 	napi_complete(napi);
526 	if (qdio_start_irq(card->data.ccwdev, 0))
527 		napi_schedule(&card->napi);
528 out:
529 	if (card->options.performance_stats)
530 		card->perf_stats.inbound_time += qeth_get_micros() -
531 			card->perf_stats.inbound_start_time;
532 	return work_done;
533 }
534 
535 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
536 			   enum qeth_ipa_cmds ipacmd,
537 			   int (*reply_cb) (struct qeth_card *,
538 					    struct qeth_reply*,
539 					    unsigned long))
540 {
541 	struct qeth_ipa_cmd *cmd;
542 	struct qeth_cmd_buffer *iob;
543 
544 	QETH_CARD_TEXT(card, 2, "L2sdmac");
545 	iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
546 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
547 	cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
548 	memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
549 	return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
550 }
551 
552 static int qeth_l2_send_setmac_cb(struct qeth_card *card,
553 			   struct qeth_reply *reply,
554 			   unsigned long data)
555 {
556 	struct qeth_ipa_cmd *cmd;
557 
558 	QETH_CARD_TEXT(card, 2, "L2Smaccb");
559 	cmd = (struct qeth_ipa_cmd *) data;
560 	if (cmd->hdr.return_code) {
561 		QETH_CARD_TEXT_(card, 2, "L2er%x", cmd->hdr.return_code);
562 		card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
563 		switch (cmd->hdr.return_code) {
564 		case IPA_RC_L2_DUP_MAC:
565 		case IPA_RC_L2_DUP_LAYER3_MAC:
566 			dev_warn(&card->gdev->dev,
567 				"MAC address %pM already exists\n",
568 				cmd->data.setdelmac.mac);
569 			break;
570 		case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
571 		case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
572 			dev_warn(&card->gdev->dev,
573 				"MAC address %pM is not authorized\n",
574 				cmd->data.setdelmac.mac);
575 			break;
576 		default:
577 			break;
578 		}
579 	} else {
580 		card->info.mac_bits |= QETH_LAYER2_MAC_REGISTERED;
581 		memcpy(card->dev->dev_addr, cmd->data.setdelmac.mac,
582 		       OSA_ADDR_LEN);
583 		dev_info(&card->gdev->dev,
584 			"MAC address %pM successfully registered on device %s\n",
585 			card->dev->dev_addr, card->dev->name);
586 	}
587 	return 0;
588 }
589 
590 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
591 {
592 	QETH_CARD_TEXT(card, 2, "L2Setmac");
593 	return qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
594 					  qeth_l2_send_setmac_cb);
595 }
596 
597 static int qeth_l2_send_delmac_cb(struct qeth_card *card,
598 			   struct qeth_reply *reply,
599 			   unsigned long data)
600 {
601 	struct qeth_ipa_cmd *cmd;
602 
603 	QETH_CARD_TEXT(card, 2, "L2Dmaccb");
604 	cmd = (struct qeth_ipa_cmd *) data;
605 	if (cmd->hdr.return_code) {
606 		QETH_CARD_TEXT_(card, 2, "err%d", cmd->hdr.return_code);
607 		return 0;
608 	}
609 	card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
610 
611 	return 0;
612 }
613 
614 static int qeth_l2_send_delmac(struct qeth_card *card, __u8 *mac)
615 {
616 	QETH_CARD_TEXT(card, 2, "L2Delmac");
617 	if (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))
618 		return 0;
619 	return qeth_l2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
620 					  qeth_l2_send_delmac_cb);
621 }
622 
623 static int qeth_l2_request_initial_mac(struct qeth_card *card)
624 {
625 	int rc = 0;
626 	char vendor_pre[] = {0x02, 0x00, 0x00};
627 
628 	QETH_DBF_TEXT(SETUP, 2, "doL2init");
629 	QETH_DBF_TEXT_(SETUP, 2, "doL2%s", CARD_BUS_ID(card));
630 
631 	if (qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
632 		rc = qeth_query_setadapterparms(card);
633 		if (rc) {
634 			QETH_DBF_MESSAGE(2, "could not query adapter "
635 				"parameters on device %s: x%x\n",
636 				CARD_BUS_ID(card), rc);
637 		}
638 	}
639 
640 	if (card->info.type == QETH_CARD_TYPE_IQD ||
641 	    card->info.type == QETH_CARD_TYPE_OSM ||
642 	    card->info.type == QETH_CARD_TYPE_OSX ||
643 	    card->info.guestlan) {
644 		rc = qeth_setadpparms_change_macaddr(card);
645 		if (rc) {
646 			QETH_DBF_MESSAGE(2, "couldn't get MAC address on "
647 				"device %s: x%x\n", CARD_BUS_ID(card), rc);
648 			QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
649 			return rc;
650 		}
651 		QETH_DBF_HEX(SETUP, 2, card->dev->dev_addr, OSA_ADDR_LEN);
652 	} else {
653 		eth_random_addr(card->dev->dev_addr);
654 		memcpy(card->dev->dev_addr, vendor_pre, 3);
655 	}
656 	return 0;
657 }
658 
659 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
660 {
661 	struct sockaddr *addr = p;
662 	struct qeth_card *card = dev->ml_priv;
663 	int rc = 0;
664 
665 	QETH_CARD_TEXT(card, 3, "setmac");
666 
667 	if (qeth_l2_verify_dev(dev) != QETH_REAL_CARD) {
668 		QETH_CARD_TEXT(card, 3, "setmcINV");
669 		return -EOPNOTSUPP;
670 	}
671 
672 	if (card->info.type == QETH_CARD_TYPE_OSN ||
673 	    card->info.type == QETH_CARD_TYPE_OSM ||
674 	    card->info.type == QETH_CARD_TYPE_OSX) {
675 		QETH_CARD_TEXT(card, 3, "setmcTYP");
676 		return -EOPNOTSUPP;
677 	}
678 	QETH_CARD_HEX(card, 3, addr->sa_data, OSA_ADDR_LEN);
679 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
680 		QETH_CARD_TEXT(card, 3, "setmcREC");
681 		return -ERESTARTSYS;
682 	}
683 	rc = qeth_l2_send_delmac(card, &card->dev->dev_addr[0]);
684 	if (!rc || (rc == IPA_RC_L2_MAC_NOT_FOUND))
685 		rc = qeth_l2_send_setmac(card, addr->sa_data);
686 	return rc ? -EINVAL : 0;
687 }
688 
689 static void qeth_l2_set_multicast_list(struct net_device *dev)
690 {
691 	struct qeth_card *card = dev->ml_priv;
692 	struct netdev_hw_addr *ha;
693 
694 	if (card->info.type == QETH_CARD_TYPE_OSN)
695 		return ;
696 
697 	QETH_CARD_TEXT(card, 3, "setmulti");
698 	if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
699 	    (card->state != CARD_STATE_UP))
700 		return;
701 	qeth_l2_del_all_mc(card, 1);
702 	spin_lock_bh(&card->mclock);
703 	netdev_for_each_mc_addr(ha, dev)
704 		qeth_l2_add_mc(card, ha->addr, 0);
705 
706 	netdev_for_each_uc_addr(ha, dev)
707 		qeth_l2_add_mc(card, ha->addr, 1);
708 
709 	spin_unlock_bh(&card->mclock);
710 	if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
711 		return;
712 	qeth_setadp_promisc_mode(card);
713 }
714 
715 static int qeth_l2_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
716 {
717 	int rc;
718 	struct qeth_hdr *hdr = NULL;
719 	int elements = 0;
720 	struct qeth_card *card = dev->ml_priv;
721 	struct sk_buff *new_skb = skb;
722 	int ipv = qeth_get_ip_version(skb);
723 	int cast_type = qeth_l2_get_cast_type(card, skb);
724 	struct qeth_qdio_out_q *queue = card->qdio.out_qs
725 		[qeth_get_priority_queue(card, skb, ipv, cast_type)];
726 	int tx_bytes = skb->len;
727 	int data_offset = -1;
728 	int elements_needed = 0;
729 	int hd_len = 0;
730 
731 	if ((card->state != CARD_STATE_UP) || !card->lan_online) {
732 		card->stats.tx_carrier_errors++;
733 		goto tx_drop;
734 	}
735 
736 	if ((card->info.type == QETH_CARD_TYPE_OSN) &&
737 	    (skb->protocol == htons(ETH_P_IPV6)))
738 		goto tx_drop;
739 
740 	if (card->options.performance_stats) {
741 		card->perf_stats.outbound_cnt++;
742 		card->perf_stats.outbound_start_time = qeth_get_micros();
743 	}
744 	netif_stop_queue(dev);
745 
746 	if (card->info.type == QETH_CARD_TYPE_OSN)
747 		hdr = (struct qeth_hdr *)skb->data;
748 	else {
749 		if (card->info.type == QETH_CARD_TYPE_IQD) {
750 			new_skb = skb;
751 			data_offset = ETH_HLEN;
752 			hd_len = ETH_HLEN;
753 			hdr = kmem_cache_alloc(qeth_core_header_cache,
754 						GFP_ATOMIC);
755 			if (!hdr)
756 				goto tx_drop;
757 			elements_needed++;
758 			skb_reset_mac_header(new_skb);
759 			qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
760 			hdr->hdr.l2.pkt_length = new_skb->len;
761 			memcpy(((char *)hdr) + sizeof(struct qeth_hdr),
762 				skb_mac_header(new_skb), ETH_HLEN);
763 		} else {
764 			/* create a clone with writeable headroom */
765 			new_skb = skb_realloc_headroom(skb,
766 						sizeof(struct qeth_hdr));
767 			if (!new_skb)
768 				goto tx_drop;
769 			hdr = (struct qeth_hdr *)skb_push(new_skb,
770 						sizeof(struct qeth_hdr));
771 			skb_set_mac_header(new_skb, sizeof(struct qeth_hdr));
772 			qeth_l2_fill_header(card, hdr, new_skb, ipv, cast_type);
773 		}
774 	}
775 
776 	elements = qeth_get_elements_no(card, new_skb, elements_needed);
777 	if (!elements) {
778 		if (data_offset >= 0)
779 			kmem_cache_free(qeth_core_header_cache, hdr);
780 		goto tx_drop;
781 	}
782 
783 	if (card->info.type != QETH_CARD_TYPE_IQD) {
784 		if (qeth_hdr_chk_and_bounce(new_skb, &hdr,
785 		    sizeof(struct qeth_hdr_layer2)))
786 			goto tx_drop;
787 		rc = qeth_do_send_packet(card, queue, new_skb, hdr,
788 					 elements);
789 	} else
790 		rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
791 					elements, data_offset, hd_len);
792 	if (!rc) {
793 		card->stats.tx_packets++;
794 		card->stats.tx_bytes += tx_bytes;
795 		if (new_skb != skb)
796 			dev_kfree_skb_any(skb);
797 		rc = NETDEV_TX_OK;
798 	} else {
799 		if (data_offset >= 0)
800 			kmem_cache_free(qeth_core_header_cache, hdr);
801 
802 		if (rc == -EBUSY) {
803 			if (new_skb != skb)
804 				dev_kfree_skb_any(new_skb);
805 			return NETDEV_TX_BUSY;
806 		} else
807 			goto tx_drop;
808 	}
809 
810 	netif_wake_queue(dev);
811 	if (card->options.performance_stats)
812 		card->perf_stats.outbound_time += qeth_get_micros() -
813 			card->perf_stats.outbound_start_time;
814 	return rc;
815 
816 tx_drop:
817 	card->stats.tx_dropped++;
818 	card->stats.tx_errors++;
819 	if ((new_skb != skb) && new_skb)
820 		dev_kfree_skb_any(new_skb);
821 	dev_kfree_skb_any(skb);
822 	netif_wake_queue(dev);
823 	return NETDEV_TX_OK;
824 }
825 
826 static int __qeth_l2_open(struct net_device *dev)
827 {
828 	struct qeth_card *card = dev->ml_priv;
829 	int rc = 0;
830 
831 	QETH_CARD_TEXT(card, 4, "qethopen");
832 	if (card->state == CARD_STATE_UP)
833 		return rc;
834 	if (card->state != CARD_STATE_SOFTSETUP)
835 		return -ENODEV;
836 
837 	if ((card->info.type != QETH_CARD_TYPE_OSN) &&
838 	     (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
839 		QETH_CARD_TEXT(card, 4, "nomacadr");
840 		return -EPERM;
841 	}
842 	card->data.state = CH_STATE_UP;
843 	card->state = CARD_STATE_UP;
844 	netif_start_queue(dev);
845 
846 	if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
847 		napi_enable(&card->napi);
848 		napi_schedule(&card->napi);
849 	} else
850 		rc = -EIO;
851 	return rc;
852 }
853 
854 static int qeth_l2_open(struct net_device *dev)
855 {
856 	struct qeth_card *card = dev->ml_priv;
857 
858 	QETH_CARD_TEXT(card, 5, "qethope_");
859 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
860 		QETH_CARD_TEXT(card, 3, "openREC");
861 		return -ERESTARTSYS;
862 	}
863 	return __qeth_l2_open(dev);
864 }
865 
866 static int qeth_l2_stop(struct net_device *dev)
867 {
868 	struct qeth_card *card = dev->ml_priv;
869 
870 	QETH_CARD_TEXT(card, 4, "qethstop");
871 	netif_tx_disable(dev);
872 	if (card->state == CARD_STATE_UP) {
873 		card->state = CARD_STATE_SOFTSETUP;
874 		napi_disable(&card->napi);
875 	}
876 	return 0;
877 }
878 
879 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
880 {
881 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
882 
883 	INIT_LIST_HEAD(&card->vid_list);
884 	INIT_LIST_HEAD(&card->mc_list);
885 	card->options.layer2 = 1;
886 	card->info.hwtrap = 0;
887 	return 0;
888 }
889 
890 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
891 {
892 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
893 
894 	qeth_set_allowed_threads(card, 0, 1);
895 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
896 
897 	if (cgdev->state == CCWGROUP_ONLINE)
898 		qeth_l2_set_offline(cgdev);
899 
900 	if (card->dev) {
901 		unregister_netdev(card->dev);
902 		card->dev = NULL;
903 	}
904 	return;
905 }
906 
907 static const struct ethtool_ops qeth_l2_ethtool_ops = {
908 	.get_link = ethtool_op_get_link,
909 	.get_strings = qeth_core_get_strings,
910 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
911 	.get_sset_count = qeth_core_get_sset_count,
912 	.get_drvinfo = qeth_core_get_drvinfo,
913 	.get_settings = qeth_core_ethtool_get_settings,
914 };
915 
916 static const struct ethtool_ops qeth_l2_osn_ops = {
917 	.get_strings = qeth_core_get_strings,
918 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
919 	.get_sset_count = qeth_core_get_sset_count,
920 	.get_drvinfo = qeth_core_get_drvinfo,
921 };
922 
923 static const struct net_device_ops qeth_l2_netdev_ops = {
924 	.ndo_open		= qeth_l2_open,
925 	.ndo_stop		= qeth_l2_stop,
926 	.ndo_get_stats		= qeth_get_stats,
927 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
928 	.ndo_validate_addr	= eth_validate_addr,
929 	.ndo_set_rx_mode	= qeth_l2_set_multicast_list,
930 	.ndo_do_ioctl	   	= qeth_l2_do_ioctl,
931 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
932 	.ndo_change_mtu	   	= qeth_change_mtu,
933 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
934 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
935 	.ndo_tx_timeout	   	= qeth_tx_timeout,
936 };
937 
938 static int qeth_l2_setup_netdev(struct qeth_card *card)
939 {
940 	switch (card->info.type) {
941 	case QETH_CARD_TYPE_IQD:
942 		card->dev = alloc_netdev(0, "hsi%d", ether_setup);
943 		break;
944 	case QETH_CARD_TYPE_OSN:
945 		card->dev = alloc_netdev(0, "osn%d", ether_setup);
946 		card->dev->flags |= IFF_NOARP;
947 		break;
948 	default:
949 		card->dev = alloc_etherdev(0);
950 	}
951 
952 	if (!card->dev)
953 		return -ENODEV;
954 
955 	card->dev->ml_priv = card;
956 	card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
957 	card->dev->mtu = card->info.initial_mtu;
958 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
959 	if (card->info.type != QETH_CARD_TYPE_OSN)
960 		SET_ETHTOOL_OPS(card->dev, &qeth_l2_ethtool_ops);
961 	else
962 		SET_ETHTOOL_OPS(card->dev, &qeth_l2_osn_ops);
963 	card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
964 	card->info.broadcast_capable = 1;
965 	qeth_l2_request_initial_mac(card);
966 	SET_NETDEV_DEV(card->dev, &card->gdev->dev);
967 	netif_napi_add(card->dev, &card->napi, qeth_l2_poll, QETH_NAPI_WEIGHT);
968 	return register_netdev(card->dev);
969 }
970 
971 static int __qeth_l2_set_online(struct ccwgroup_device *gdev, int recovery_mode)
972 {
973 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
974 	int rc = 0;
975 	enum qeth_card_states recover_flag;
976 
977 	mutex_lock(&card->discipline_mutex);
978 	mutex_lock(&card->conf_mutex);
979 	QETH_DBF_TEXT(SETUP, 2, "setonlin");
980 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
981 
982 	recover_flag = card->state;
983 	rc = qeth_core_hardsetup_card(card);
984 	if (rc) {
985 		QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
986 		rc = -ENODEV;
987 		goto out_remove;
988 	}
989 	qeth_trace_features(card);
990 
991 	if (!card->dev && qeth_l2_setup_netdev(card)) {
992 		rc = -ENODEV;
993 		goto out_remove;
994 	}
995 
996 	if (card->info.type != QETH_CARD_TYPE_OSN)
997 		qeth_l2_send_setmac(card, &card->dev->dev_addr[0]);
998 
999 	if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
1000 		if (card->info.hwtrap &&
1001 		    qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
1002 			card->info.hwtrap = 0;
1003 	} else
1004 		card->info.hwtrap = 0;
1005 
1006 	card->state = CARD_STATE_HARDSETUP;
1007 	memset(&card->rx, 0, sizeof(struct qeth_rx));
1008 	qeth_print_status_message(card);
1009 
1010 	/* softsetup */
1011 	QETH_DBF_TEXT(SETUP, 2, "softsetp");
1012 
1013 	rc = qeth_send_startlan(card);
1014 	if (rc) {
1015 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1016 		if (rc == 0xe080) {
1017 			dev_warn(&card->gdev->dev,
1018 				"The LAN is offline\n");
1019 			card->lan_online = 0;
1020 			goto contin;
1021 		}
1022 		rc = -ENODEV;
1023 		goto out_remove;
1024 	} else
1025 		card->lan_online = 1;
1026 
1027 contin:
1028 	if ((card->info.type == QETH_CARD_TYPE_OSD) ||
1029 	    (card->info.type == QETH_CARD_TYPE_OSX)) {
1030 		/* configure isolation level */
1031 		rc = qeth_set_access_ctrl_online(card, 0);
1032 		if (rc) {
1033 			rc = -ENODEV;
1034 			goto out_remove;
1035 		}
1036 	}
1037 
1038 	if (card->info.type != QETH_CARD_TYPE_OSN &&
1039 	    card->info.type != QETH_CARD_TYPE_OSM)
1040 		qeth_l2_process_vlans(card);
1041 
1042 	netif_tx_disable(card->dev);
1043 
1044 	rc = qeth_init_qdio_queues(card);
1045 	if (rc) {
1046 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
1047 		rc = -ENODEV;
1048 		goto out_remove;
1049 	}
1050 	card->state = CARD_STATE_SOFTSETUP;
1051 	if (card->lan_online)
1052 		netif_carrier_on(card->dev);
1053 	else
1054 		netif_carrier_off(card->dev);
1055 
1056 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1057 	if (recover_flag == CARD_STATE_RECOVER) {
1058 		if (recovery_mode &&
1059 		    card->info.type != QETH_CARD_TYPE_OSN) {
1060 			__qeth_l2_open(card->dev);
1061 		} else {
1062 			rtnl_lock();
1063 			dev_open(card->dev);
1064 			rtnl_unlock();
1065 		}
1066 		/* this also sets saved unicast addresses */
1067 		qeth_l2_set_multicast_list(card->dev);
1068 	}
1069 	/* let user_space know that device is online */
1070 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
1071 	mutex_unlock(&card->conf_mutex);
1072 	mutex_unlock(&card->discipline_mutex);
1073 	return 0;
1074 
1075 out_remove:
1076 	qeth_l2_stop_card(card, 0);
1077 	ccw_device_set_offline(CARD_DDEV(card));
1078 	ccw_device_set_offline(CARD_WDEV(card));
1079 	ccw_device_set_offline(CARD_RDEV(card));
1080 	if (recover_flag == CARD_STATE_RECOVER)
1081 		card->state = CARD_STATE_RECOVER;
1082 	else
1083 		card->state = CARD_STATE_DOWN;
1084 	mutex_unlock(&card->conf_mutex);
1085 	mutex_unlock(&card->discipline_mutex);
1086 	return rc;
1087 }
1088 
1089 static int qeth_l2_set_online(struct ccwgroup_device *gdev)
1090 {
1091 	return __qeth_l2_set_online(gdev, 0);
1092 }
1093 
1094 static int __qeth_l2_set_offline(struct ccwgroup_device *cgdev,
1095 					int recovery_mode)
1096 {
1097 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1098 	int rc = 0, rc2 = 0, rc3 = 0;
1099 	enum qeth_card_states recover_flag;
1100 
1101 	mutex_lock(&card->discipline_mutex);
1102 	mutex_lock(&card->conf_mutex);
1103 	QETH_DBF_TEXT(SETUP, 3, "setoffl");
1104 	QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
1105 
1106 	if (card->dev && netif_carrier_ok(card->dev))
1107 		netif_carrier_off(card->dev);
1108 	recover_flag = card->state;
1109 	if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
1110 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1111 		card->info.hwtrap = 1;
1112 	}
1113 	qeth_l2_stop_card(card, recovery_mode);
1114 	rc  = ccw_device_set_offline(CARD_DDEV(card));
1115 	rc2 = ccw_device_set_offline(CARD_WDEV(card));
1116 	rc3 = ccw_device_set_offline(CARD_RDEV(card));
1117 	if (!rc)
1118 		rc = (rc2) ? rc2 : rc3;
1119 	if (rc)
1120 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1121 	if (recover_flag == CARD_STATE_UP)
1122 		card->state = CARD_STATE_RECOVER;
1123 	/* let user_space know that device is offline */
1124 	kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
1125 	mutex_unlock(&card->conf_mutex);
1126 	mutex_unlock(&card->discipline_mutex);
1127 	return 0;
1128 }
1129 
1130 static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
1131 {
1132 	return __qeth_l2_set_offline(cgdev, 0);
1133 }
1134 
1135 static int qeth_l2_recover(void *ptr)
1136 {
1137 	struct qeth_card *card;
1138 	int rc = 0;
1139 
1140 	card = (struct qeth_card *) ptr;
1141 	QETH_CARD_TEXT(card, 2, "recover1");
1142 	if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
1143 		return 0;
1144 	QETH_CARD_TEXT(card, 2, "recover2");
1145 	dev_warn(&card->gdev->dev,
1146 		"A recovery process has been started for the device\n");
1147 	qeth_set_recovery_task(card);
1148 	__qeth_l2_set_offline(card->gdev, 1);
1149 	rc = __qeth_l2_set_online(card->gdev, 1);
1150 	if (!rc)
1151 		dev_info(&card->gdev->dev,
1152 			"Device successfully recovered!\n");
1153 	else {
1154 		qeth_close_dev(card);
1155 		dev_warn(&card->gdev->dev, "The qeth device driver "
1156 				"failed to recover an error on the device\n");
1157 	}
1158 	qeth_clear_recovery_task(card);
1159 	qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1160 	qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1161 	return 0;
1162 }
1163 
1164 static int __init qeth_l2_init(void)
1165 {
1166 	pr_info("register layer 2 discipline\n");
1167 	return 0;
1168 }
1169 
1170 static void __exit qeth_l2_exit(void)
1171 {
1172 	pr_info("unregister layer 2 discipline\n");
1173 }
1174 
1175 static void qeth_l2_shutdown(struct ccwgroup_device *gdev)
1176 {
1177 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1178 	qeth_set_allowed_threads(card, 0, 1);
1179 	if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
1180 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1181 	qeth_qdio_clear_card(card, 0);
1182 	qeth_clear_qdio_buffers(card);
1183 }
1184 
1185 static int qeth_l2_pm_suspend(struct ccwgroup_device *gdev)
1186 {
1187 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1188 
1189 	if (card->dev)
1190 		netif_device_detach(card->dev);
1191 	qeth_set_allowed_threads(card, 0, 1);
1192 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
1193 	if (gdev->state == CCWGROUP_OFFLINE)
1194 		return 0;
1195 	if (card->state == CARD_STATE_UP) {
1196 		if (card->info.hwtrap)
1197 			qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
1198 		__qeth_l2_set_offline(card->gdev, 1);
1199 	} else
1200 		__qeth_l2_set_offline(card->gdev, 0);
1201 	return 0;
1202 }
1203 
1204 static int qeth_l2_pm_resume(struct ccwgroup_device *gdev)
1205 {
1206 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1207 	int rc = 0;
1208 
1209 	if (gdev->state == CCWGROUP_OFFLINE)
1210 		goto out;
1211 
1212 	if (card->state == CARD_STATE_RECOVER) {
1213 		rc = __qeth_l2_set_online(card->gdev, 1);
1214 		if (rc) {
1215 			rtnl_lock();
1216 			dev_close(card->dev);
1217 			rtnl_unlock();
1218 		}
1219 	} else
1220 		rc = __qeth_l2_set_online(card->gdev, 0);
1221 out:
1222 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1223 	if (card->dev)
1224 		netif_device_attach(card->dev);
1225 	if (rc)
1226 		dev_warn(&card->gdev->dev, "The qeth device driver "
1227 			"failed to recover an error on the device\n");
1228 	return rc;
1229 }
1230 
1231 struct qeth_discipline qeth_l2_discipline = {
1232 	.start_poll = qeth_qdio_start_poll,
1233 	.input_handler = (qdio_handler_t *) qeth_qdio_input_handler,
1234 	.output_handler = (qdio_handler_t *) qeth_qdio_output_handler,
1235 	.recover = qeth_l2_recover,
1236 	.setup = qeth_l2_probe_device,
1237 	.remove = qeth_l2_remove_device,
1238 	.set_online = qeth_l2_set_online,
1239 	.set_offline = qeth_l2_set_offline,
1240 	.shutdown = qeth_l2_shutdown,
1241 	.freeze = qeth_l2_pm_suspend,
1242 	.thaw = qeth_l2_pm_resume,
1243 	.restore = qeth_l2_pm_resume,
1244 };
1245 EXPORT_SYMBOL_GPL(qeth_l2_discipline);
1246 
1247 static int qeth_osn_send_control_data(struct qeth_card *card, int len,
1248 			   struct qeth_cmd_buffer *iob)
1249 {
1250 	unsigned long flags;
1251 	int rc = 0;
1252 
1253 	QETH_CARD_TEXT(card, 5, "osndctrd");
1254 
1255 	wait_event(card->wait_q,
1256 		   atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1257 	qeth_prepare_control_data(card, len, iob);
1258 	QETH_CARD_TEXT(card, 6, "osnoirqp");
1259 	spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1260 	rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1261 			      (addr_t) iob, 0, 0);
1262 	spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1263 	if (rc) {
1264 		QETH_DBF_MESSAGE(2, "qeth_osn_send_control_data: "
1265 			   "ccw_device_start rc = %i\n", rc);
1266 		QETH_CARD_TEXT_(card, 2, " err%d", rc);
1267 		qeth_release_buffer(iob->channel, iob);
1268 		atomic_set(&card->write.irq_pending, 0);
1269 		wake_up(&card->wait_q);
1270 	}
1271 	return rc;
1272 }
1273 
1274 static int qeth_osn_send_ipa_cmd(struct qeth_card *card,
1275 			struct qeth_cmd_buffer *iob, int data_len)
1276 {
1277 	u16 s1, s2;
1278 
1279 	QETH_CARD_TEXT(card, 4, "osndipa");
1280 
1281 	qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1282 	s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1283 	s2 = (u16)data_len;
1284 	memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1285 	memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1286 	memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1287 	memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1288 	return qeth_osn_send_control_data(card, s1, iob);
1289 }
1290 
1291 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1292 {
1293 	struct qeth_cmd_buffer *iob;
1294 	struct qeth_card *card;
1295 	int rc;
1296 
1297 	if (!dev)
1298 		return -ENODEV;
1299 	card = dev->ml_priv;
1300 	if (!card)
1301 		return -ENODEV;
1302 	QETH_CARD_TEXT(card, 2, "osnsdmc");
1303 	if ((card->state != CARD_STATE_UP) &&
1304 	    (card->state != CARD_STATE_SOFTSETUP))
1305 		return -ENODEV;
1306 	iob = qeth_wait_for_buffer(&card->write);
1307 	memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
1308 	rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
1309 	return rc;
1310 }
1311 EXPORT_SYMBOL(qeth_osn_assist);
1312 
1313 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1314 		  int (*assist_cb)(struct net_device *, void *),
1315 		  int (*data_cb)(struct sk_buff *))
1316 {
1317 	struct qeth_card *card;
1318 
1319 	*dev = qeth_l2_netdev_by_devno(read_dev_no);
1320 	if (*dev == NULL)
1321 		return -ENODEV;
1322 	card = (*dev)->ml_priv;
1323 	if (!card)
1324 		return -ENODEV;
1325 	QETH_CARD_TEXT(card, 2, "osnreg");
1326 	if ((assist_cb == NULL) || (data_cb == NULL))
1327 		return -EINVAL;
1328 	card->osn_info.assist_cb = assist_cb;
1329 	card->osn_info.data_cb = data_cb;
1330 	return 0;
1331 }
1332 EXPORT_SYMBOL(qeth_osn_register);
1333 
1334 void qeth_osn_deregister(struct net_device *dev)
1335 {
1336 	struct qeth_card *card;
1337 
1338 	if (!dev)
1339 		return;
1340 	card = dev->ml_priv;
1341 	if (!card)
1342 		return;
1343 	QETH_CARD_TEXT(card, 2, "osndereg");
1344 	card->osn_info.assist_cb = NULL;
1345 	card->osn_info.data_cb = NULL;
1346 	return;
1347 }
1348 EXPORT_SYMBOL(qeth_osn_deregister);
1349 
1350 module_init(qeth_l2_init);
1351 module_exit(qeth_l2_exit);
1352 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1353 MODULE_DESCRIPTION("qeth layer 2 discipline");
1354 MODULE_LICENSE("GPL");
1355