1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2009
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *		 Frank Pavlic <fpavlic@de.ibm.com>,
6  *		 Thomas Spatzier <tspat@de.ibm.com>,
7  *		 Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9 
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/ip.h>
19 #include <linux/tcp.h>
20 #include <linux/mii.h>
21 #include <linux/kthread.h>
22 #include <linux/slab.h>
23 #include <linux/if_vlan.h>
24 #include <linux/netdevice.h>
25 #include <linux/netdev_features.h>
26 #include <linux/skbuff.h>
27 
28 #include <net/iucv/af_iucv.h>
29 #include <net/dsfield.h>
30 
31 #include <asm/ebcdic.h>
32 #include <asm/chpid.h>
33 #include <asm/io.h>
34 #include <asm/sysinfo.h>
35 #include <asm/compat.h>
36 #include <asm/diag.h>
37 #include <asm/cio.h>
38 #include <asm/ccwdev.h>
39 #include <asm/cpcmd.h>
40 
41 #include "qeth_core.h"
42 
43 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
44 	/* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
45 	/*                   N  P  A    M  L  V                      H  */
46 	[QETH_DBF_SETUP] = {"qeth_setup",
47 				8, 1,   8, 5, &debug_hex_ascii_view, NULL},
48 	[QETH_DBF_MSG]	 = {"qeth_msg", 8, 1, 11 * sizeof(long), 3,
49 			    &debug_sprintf_view, NULL},
50 	[QETH_DBF_CTRL]  = {"qeth_control",
51 		8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
52 };
53 EXPORT_SYMBOL_GPL(qeth_dbf);
54 
55 struct qeth_card_list_struct qeth_core_card_list;
56 EXPORT_SYMBOL_GPL(qeth_core_card_list);
57 struct kmem_cache *qeth_core_header_cache;
58 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
59 static struct kmem_cache *qeth_qdio_outbuf_cache;
60 
61 static struct device *qeth_core_root_dev;
62 static struct lock_class_key qdio_out_skb_queue_key;
63 static struct mutex qeth_mod_mutex;
64 
65 static void qeth_send_control_data_cb(struct qeth_channel *,
66 			struct qeth_cmd_buffer *);
67 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *);
68 static void qeth_setup_ccw(struct qeth_channel *, unsigned char *, __u32);
69 static void qeth_free_buffer_pool(struct qeth_card *);
70 static int qeth_qdio_establish(struct qeth_card *);
71 static void qeth_free_qdio_buffers(struct qeth_card *);
72 static void qeth_notify_skbs(struct qeth_qdio_out_q *queue,
73 		struct qeth_qdio_out_buffer *buf,
74 		enum iucv_tx_notify notification);
75 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf);
76 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
77 		struct qeth_qdio_out_buffer *buf,
78 		enum qeth_qdio_buffer_states newbufstate);
79 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *, int);
80 
81 struct workqueue_struct *qeth_wq;
82 EXPORT_SYMBOL_GPL(qeth_wq);
83 
84 int qeth_card_hw_is_reachable(struct qeth_card *card)
85 {
86 	return (card->state == CARD_STATE_SOFTSETUP) ||
87 		(card->state == CARD_STATE_UP);
88 }
89 EXPORT_SYMBOL_GPL(qeth_card_hw_is_reachable);
90 
91 static void qeth_close_dev_handler(struct work_struct *work)
92 {
93 	struct qeth_card *card;
94 
95 	card = container_of(work, struct qeth_card, close_dev_work);
96 	QETH_CARD_TEXT(card, 2, "cldevhdl");
97 	rtnl_lock();
98 	dev_close(card->dev);
99 	rtnl_unlock();
100 	ccwgroup_set_offline(card->gdev);
101 }
102 
103 void qeth_close_dev(struct qeth_card *card)
104 {
105 	QETH_CARD_TEXT(card, 2, "cldevsubm");
106 	queue_work(qeth_wq, &card->close_dev_work);
107 }
108 EXPORT_SYMBOL_GPL(qeth_close_dev);
109 
110 static const char *qeth_get_cardname(struct qeth_card *card)
111 {
112 	if (card->info.guestlan) {
113 		switch (card->info.type) {
114 		case QETH_CARD_TYPE_OSD:
115 			return " Virtual NIC QDIO";
116 		case QETH_CARD_TYPE_IQD:
117 			return " Virtual NIC Hiper";
118 		case QETH_CARD_TYPE_OSM:
119 			return " Virtual NIC QDIO - OSM";
120 		case QETH_CARD_TYPE_OSX:
121 			return " Virtual NIC QDIO - OSX";
122 		default:
123 			return " unknown";
124 		}
125 	} else {
126 		switch (card->info.type) {
127 		case QETH_CARD_TYPE_OSD:
128 			return " OSD Express";
129 		case QETH_CARD_TYPE_IQD:
130 			return " HiperSockets";
131 		case QETH_CARD_TYPE_OSN:
132 			return " OSN QDIO";
133 		case QETH_CARD_TYPE_OSM:
134 			return " OSM QDIO";
135 		case QETH_CARD_TYPE_OSX:
136 			return " OSX QDIO";
137 		default:
138 			return " unknown";
139 		}
140 	}
141 	return " n/a";
142 }
143 
144 /* max length to be returned: 14 */
145 const char *qeth_get_cardname_short(struct qeth_card *card)
146 {
147 	if (card->info.guestlan) {
148 		switch (card->info.type) {
149 		case QETH_CARD_TYPE_OSD:
150 			return "Virt.NIC QDIO";
151 		case QETH_CARD_TYPE_IQD:
152 			return "Virt.NIC Hiper";
153 		case QETH_CARD_TYPE_OSM:
154 			return "Virt.NIC OSM";
155 		case QETH_CARD_TYPE_OSX:
156 			return "Virt.NIC OSX";
157 		default:
158 			return "unknown";
159 		}
160 	} else {
161 		switch (card->info.type) {
162 		case QETH_CARD_TYPE_OSD:
163 			switch (card->info.link_type) {
164 			case QETH_LINK_TYPE_FAST_ETH:
165 				return "OSD_100";
166 			case QETH_LINK_TYPE_HSTR:
167 				return "HSTR";
168 			case QETH_LINK_TYPE_GBIT_ETH:
169 				return "OSD_1000";
170 			case QETH_LINK_TYPE_10GBIT_ETH:
171 				return "OSD_10GIG";
172 			case QETH_LINK_TYPE_LANE_ETH100:
173 				return "OSD_FE_LANE";
174 			case QETH_LINK_TYPE_LANE_TR:
175 				return "OSD_TR_LANE";
176 			case QETH_LINK_TYPE_LANE_ETH1000:
177 				return "OSD_GbE_LANE";
178 			case QETH_LINK_TYPE_LANE:
179 				return "OSD_ATM_LANE";
180 			default:
181 				return "OSD_Express";
182 			}
183 		case QETH_CARD_TYPE_IQD:
184 			return "HiperSockets";
185 		case QETH_CARD_TYPE_OSN:
186 			return "OSN";
187 		case QETH_CARD_TYPE_OSM:
188 			return "OSM_1000";
189 		case QETH_CARD_TYPE_OSX:
190 			return "OSX_10GIG";
191 		default:
192 			return "unknown";
193 		}
194 	}
195 	return "n/a";
196 }
197 
198 void qeth_set_recovery_task(struct qeth_card *card)
199 {
200 	card->recovery_task = current;
201 }
202 EXPORT_SYMBOL_GPL(qeth_set_recovery_task);
203 
204 void qeth_clear_recovery_task(struct qeth_card *card)
205 {
206 	card->recovery_task = NULL;
207 }
208 EXPORT_SYMBOL_GPL(qeth_clear_recovery_task);
209 
210 static bool qeth_is_recovery_task(const struct qeth_card *card)
211 {
212 	return card->recovery_task == current;
213 }
214 
215 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
216 			 int clear_start_mask)
217 {
218 	unsigned long flags;
219 
220 	spin_lock_irqsave(&card->thread_mask_lock, flags);
221 	card->thread_allowed_mask = threads;
222 	if (clear_start_mask)
223 		card->thread_start_mask &= threads;
224 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
225 	wake_up(&card->wait_q);
226 }
227 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
228 
229 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
230 {
231 	unsigned long flags;
232 	int rc = 0;
233 
234 	spin_lock_irqsave(&card->thread_mask_lock, flags);
235 	rc = (card->thread_running_mask & threads);
236 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
237 	return rc;
238 }
239 EXPORT_SYMBOL_GPL(qeth_threads_running);
240 
241 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
242 {
243 	if (qeth_is_recovery_task(card))
244 		return 0;
245 	return wait_event_interruptible(card->wait_q,
246 			qeth_threads_running(card, threads) == 0);
247 }
248 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
249 
250 void qeth_clear_working_pool_list(struct qeth_card *card)
251 {
252 	struct qeth_buffer_pool_entry *pool_entry, *tmp;
253 
254 	QETH_CARD_TEXT(card, 5, "clwrklst");
255 	list_for_each_entry_safe(pool_entry, tmp,
256 			    &card->qdio.in_buf_pool.entry_list, list){
257 			list_del(&pool_entry->list);
258 	}
259 }
260 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
261 
262 static int qeth_alloc_buffer_pool(struct qeth_card *card)
263 {
264 	struct qeth_buffer_pool_entry *pool_entry;
265 	void *ptr;
266 	int i, j;
267 
268 	QETH_CARD_TEXT(card, 5, "alocpool");
269 	for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
270 		pool_entry = kzalloc(sizeof(*pool_entry), GFP_KERNEL);
271 		if (!pool_entry) {
272 			qeth_free_buffer_pool(card);
273 			return -ENOMEM;
274 		}
275 		for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
276 			ptr = (void *) __get_free_page(GFP_KERNEL);
277 			if (!ptr) {
278 				while (j > 0)
279 					free_page((unsigned long)
280 						  pool_entry->elements[--j]);
281 				kfree(pool_entry);
282 				qeth_free_buffer_pool(card);
283 				return -ENOMEM;
284 			}
285 			pool_entry->elements[j] = ptr;
286 		}
287 		list_add(&pool_entry->init_list,
288 			 &card->qdio.init_pool.entry_list);
289 	}
290 	return 0;
291 }
292 
293 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
294 {
295 	QETH_CARD_TEXT(card, 2, "realcbp");
296 
297 	if ((card->state != CARD_STATE_DOWN) &&
298 	    (card->state != CARD_STATE_RECOVER))
299 		return -EPERM;
300 
301 	/* TODO: steel/add buffers from/to a running card's buffer pool (?) */
302 	qeth_clear_working_pool_list(card);
303 	qeth_free_buffer_pool(card);
304 	card->qdio.in_buf_pool.buf_count = bufcnt;
305 	card->qdio.init_pool.buf_count = bufcnt;
306 	return qeth_alloc_buffer_pool(card);
307 }
308 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
309 
310 static void qeth_free_qdio_queue(struct qeth_qdio_q *q)
311 {
312 	if (!q)
313 		return;
314 
315 	qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
316 	kfree(q);
317 }
318 
319 static struct qeth_qdio_q *qeth_alloc_qdio_queue(void)
320 {
321 	struct qeth_qdio_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
322 	int i;
323 
324 	if (!q)
325 		return NULL;
326 
327 	if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
328 		kfree(q);
329 		return NULL;
330 	}
331 
332 	for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
333 		q->bufs[i].buffer = q->qdio_bufs[i];
334 
335 	QETH_DBF_HEX(SETUP, 2, &q, sizeof(void *));
336 	return q;
337 }
338 
339 static int qeth_cq_init(struct qeth_card *card)
340 {
341 	int rc;
342 
343 	if (card->options.cq == QETH_CQ_ENABLED) {
344 		QETH_DBF_TEXT(SETUP, 2, "cqinit");
345 		qdio_reset_buffers(card->qdio.c_q->qdio_bufs,
346 				   QDIO_MAX_BUFFERS_PER_Q);
347 		card->qdio.c_q->next_buf_to_init = 127;
348 		rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
349 			     card->qdio.no_in_queues - 1, 0,
350 			     127);
351 		if (rc) {
352 			QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
353 			goto out;
354 		}
355 	}
356 	rc = 0;
357 out:
358 	return rc;
359 }
360 
361 static int qeth_alloc_cq(struct qeth_card *card)
362 {
363 	int rc;
364 
365 	if (card->options.cq == QETH_CQ_ENABLED) {
366 		int i;
367 		struct qdio_outbuf_state *outbuf_states;
368 
369 		QETH_DBF_TEXT(SETUP, 2, "cqon");
370 		card->qdio.c_q = qeth_alloc_qdio_queue();
371 		if (!card->qdio.c_q) {
372 			rc = -1;
373 			goto kmsg_out;
374 		}
375 		card->qdio.no_in_queues = 2;
376 		card->qdio.out_bufstates =
377 			kzalloc(card->qdio.no_out_queues *
378 				QDIO_MAX_BUFFERS_PER_Q *
379 				sizeof(struct qdio_outbuf_state), GFP_KERNEL);
380 		outbuf_states = card->qdio.out_bufstates;
381 		if (outbuf_states == NULL) {
382 			rc = -1;
383 			goto free_cq_out;
384 		}
385 		for (i = 0; i < card->qdio.no_out_queues; ++i) {
386 			card->qdio.out_qs[i]->bufstates = outbuf_states;
387 			outbuf_states += QDIO_MAX_BUFFERS_PER_Q;
388 		}
389 	} else {
390 		QETH_DBF_TEXT(SETUP, 2, "nocq");
391 		card->qdio.c_q = NULL;
392 		card->qdio.no_in_queues = 1;
393 	}
394 	QETH_DBF_TEXT_(SETUP, 2, "iqc%d", card->qdio.no_in_queues);
395 	rc = 0;
396 out:
397 	return rc;
398 free_cq_out:
399 	qeth_free_qdio_queue(card->qdio.c_q);
400 	card->qdio.c_q = NULL;
401 kmsg_out:
402 	dev_err(&card->gdev->dev, "Failed to create completion queue\n");
403 	goto out;
404 }
405 
406 static void qeth_free_cq(struct qeth_card *card)
407 {
408 	if (card->qdio.c_q) {
409 		--card->qdio.no_in_queues;
410 		qeth_free_qdio_queue(card->qdio.c_q);
411 		card->qdio.c_q = NULL;
412 	}
413 	kfree(card->qdio.out_bufstates);
414 	card->qdio.out_bufstates = NULL;
415 }
416 
417 static enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
418 							int delayed)
419 {
420 	enum iucv_tx_notify n;
421 
422 	switch (sbalf15) {
423 	case 0:
424 		n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
425 		break;
426 	case 4:
427 	case 16:
428 	case 17:
429 	case 18:
430 		n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
431 			TX_NOTIFY_UNREACHABLE;
432 		break;
433 	default:
434 		n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
435 			TX_NOTIFY_GENERALERROR;
436 		break;
437 	}
438 
439 	return n;
440 }
441 
442 static void qeth_cleanup_handled_pending(struct qeth_qdio_out_q *q, int bidx,
443 					 int forced_cleanup)
444 {
445 	if (q->card->options.cq != QETH_CQ_ENABLED)
446 		return;
447 
448 	if (q->bufs[bidx]->next_pending != NULL) {
449 		struct qeth_qdio_out_buffer *head = q->bufs[bidx];
450 		struct qeth_qdio_out_buffer *c = q->bufs[bidx]->next_pending;
451 
452 		while (c) {
453 			if (forced_cleanup ||
454 			    atomic_read(&c->state) ==
455 			      QETH_QDIO_BUF_HANDLED_DELAYED) {
456 				struct qeth_qdio_out_buffer *f = c;
457 				QETH_CARD_TEXT(f->q->card, 5, "fp");
458 				QETH_CARD_TEXT_(f->q->card, 5, "%lx", (long) f);
459 				/* release here to avoid interleaving between
460 				   outbound tasklet and inbound tasklet
461 				   regarding notifications and lifecycle */
462 				qeth_release_skbs(c);
463 
464 				c = f->next_pending;
465 				WARN_ON_ONCE(head->next_pending != f);
466 				head->next_pending = c;
467 				kmem_cache_free(qeth_qdio_outbuf_cache, f);
468 			} else {
469 				head = c;
470 				c = c->next_pending;
471 			}
472 
473 		}
474 	}
475 	if (forced_cleanup && (atomic_read(&(q->bufs[bidx]->state)) ==
476 					QETH_QDIO_BUF_HANDLED_DELAYED)) {
477 		/* for recovery situations */
478 		q->bufs[bidx]->aob = q->bufstates[bidx].aob;
479 		qeth_init_qdio_out_buf(q, bidx);
480 		QETH_CARD_TEXT(q->card, 2, "clprecov");
481 	}
482 }
483 
484 
485 static void qeth_qdio_handle_aob(struct qeth_card *card,
486 				 unsigned long phys_aob_addr)
487 {
488 	struct qaob *aob;
489 	struct qeth_qdio_out_buffer *buffer;
490 	enum iucv_tx_notify notification;
491 
492 	aob = (struct qaob *) phys_to_virt(phys_aob_addr);
493 	QETH_CARD_TEXT(card, 5, "haob");
494 	QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
495 	buffer = (struct qeth_qdio_out_buffer *) aob->user1;
496 	QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
497 
498 	if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
499 			   QETH_QDIO_BUF_IN_CQ) == QETH_QDIO_BUF_PRIMED) {
500 		notification = TX_NOTIFY_OK;
501 	} else {
502 		WARN_ON_ONCE(atomic_read(&buffer->state) !=
503 							QETH_QDIO_BUF_PENDING);
504 		atomic_set(&buffer->state, QETH_QDIO_BUF_IN_CQ);
505 		notification = TX_NOTIFY_DELAYED_OK;
506 	}
507 
508 	if (aob->aorc != 0)  {
509 		QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
510 		notification = qeth_compute_cq_notification(aob->aorc, 1);
511 	}
512 	qeth_notify_skbs(buffer->q, buffer, notification);
513 
514 	buffer->aob = NULL;
515 	qeth_clear_output_buffer(buffer->q, buffer,
516 				 QETH_QDIO_BUF_HANDLED_DELAYED);
517 
518 	/* from here on: do not touch buffer anymore */
519 	qdio_release_aob(aob);
520 }
521 
522 static inline int qeth_is_cq(struct qeth_card *card, unsigned int queue)
523 {
524 	return card->options.cq == QETH_CQ_ENABLED &&
525 	    card->qdio.c_q != NULL &&
526 	    queue != 0 &&
527 	    queue == card->qdio.no_in_queues - 1;
528 }
529 
530 
531 static int qeth_issue_next_read(struct qeth_card *card)
532 {
533 	int rc;
534 	struct qeth_cmd_buffer *iob;
535 
536 	QETH_CARD_TEXT(card, 5, "issnxrd");
537 	if (card->read.state != CH_STATE_UP)
538 		return -EIO;
539 	iob = qeth_get_buffer(&card->read);
540 	if (!iob) {
541 		dev_warn(&card->gdev->dev, "The qeth device driver "
542 			"failed to recover an error on the device\n");
543 		QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
544 			"available\n", dev_name(&card->gdev->dev));
545 		return -ENOMEM;
546 	}
547 	qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
548 	QETH_CARD_TEXT(card, 6, "noirqpnd");
549 	rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
550 			      (addr_t) iob, 0, 0);
551 	if (rc) {
552 		QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
553 			"rc=%i\n", dev_name(&card->gdev->dev), rc);
554 		atomic_set(&card->read.irq_pending, 0);
555 		card->read_or_write_problem = 1;
556 		qeth_schedule_recovery(card);
557 		wake_up(&card->wait_q);
558 	}
559 	return rc;
560 }
561 
562 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
563 {
564 	struct qeth_reply *reply;
565 
566 	reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
567 	if (reply) {
568 		refcount_set(&reply->refcnt, 1);
569 		atomic_set(&reply->received, 0);
570 		reply->card = card;
571 	}
572 	return reply;
573 }
574 
575 static void qeth_get_reply(struct qeth_reply *reply)
576 {
577 	refcount_inc(&reply->refcnt);
578 }
579 
580 static void qeth_put_reply(struct qeth_reply *reply)
581 {
582 	if (refcount_dec_and_test(&reply->refcnt))
583 		kfree(reply);
584 }
585 
586 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
587 		struct qeth_card *card)
588 {
589 	char *ipa_name;
590 	int com = cmd->hdr.command;
591 	ipa_name = qeth_get_ipa_cmd_name(com);
592 	if (rc)
593 		QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s/%s returned "
594 				"x%X \"%s\"\n",
595 				ipa_name, com, dev_name(&card->gdev->dev),
596 				QETH_CARD_IFNAME(card), rc,
597 				qeth_get_ipa_msg(rc));
598 	else
599 		QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s/%s succeeded\n",
600 				ipa_name, com, dev_name(&card->gdev->dev),
601 				QETH_CARD_IFNAME(card));
602 }
603 
604 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
605 		struct qeth_cmd_buffer *iob)
606 {
607 	struct qeth_ipa_cmd *cmd = NULL;
608 
609 	QETH_CARD_TEXT(card, 5, "chkipad");
610 	if (IS_IPA(iob->data)) {
611 		cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
612 		if (IS_IPA_REPLY(cmd)) {
613 			if (cmd->hdr.command != IPA_CMD_SETCCID &&
614 			    cmd->hdr.command != IPA_CMD_DELCCID &&
615 			    cmd->hdr.command != IPA_CMD_MODCCID &&
616 			    cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
617 				qeth_issue_ipa_msg(cmd,
618 						cmd->hdr.return_code, card);
619 			return cmd;
620 		} else {
621 			switch (cmd->hdr.command) {
622 			case IPA_CMD_STOPLAN:
623 				if (cmd->hdr.return_code ==
624 						IPA_RC_VEPA_TO_VEB_TRANSITION) {
625 					dev_err(&card->gdev->dev,
626 					   "Interface %s is down because the "
627 					   "adjacent port is no longer in "
628 					   "reflective relay mode\n",
629 					   QETH_CARD_IFNAME(card));
630 					qeth_close_dev(card);
631 				} else {
632 					dev_warn(&card->gdev->dev,
633 					   "The link for interface %s on CHPID"
634 					   " 0x%X failed\n",
635 					   QETH_CARD_IFNAME(card),
636 					   card->info.chpid);
637 					qeth_issue_ipa_msg(cmd,
638 						cmd->hdr.return_code, card);
639 				}
640 				card->lan_online = 0;
641 				if (card->dev && netif_carrier_ok(card->dev))
642 					netif_carrier_off(card->dev);
643 				return NULL;
644 			case IPA_CMD_STARTLAN:
645 				dev_info(&card->gdev->dev,
646 					   "The link for %s on CHPID 0x%X has"
647 					   " been restored\n",
648 					   QETH_CARD_IFNAME(card),
649 					   card->info.chpid);
650 				netif_carrier_on(card->dev);
651 				card->lan_online = 1;
652 				if (card->info.hwtrap)
653 					card->info.hwtrap = 2;
654 				qeth_schedule_recovery(card);
655 				return NULL;
656 			case IPA_CMD_SETBRIDGEPORT_IQD:
657 			case IPA_CMD_SETBRIDGEPORT_OSA:
658 			case IPA_CMD_ADDRESS_CHANGE_NOTIF:
659 				if (card->discipline->control_event_handler
660 								(card, cmd))
661 					return cmd;
662 				else
663 					return NULL;
664 			case IPA_CMD_MODCCID:
665 				return cmd;
666 			case IPA_CMD_REGISTER_LOCAL_ADDR:
667 				QETH_CARD_TEXT(card, 3, "irla");
668 				break;
669 			case IPA_CMD_UNREGISTER_LOCAL_ADDR:
670 				QETH_CARD_TEXT(card, 3, "urla");
671 				break;
672 			default:
673 				QETH_DBF_MESSAGE(2, "Received data is IPA "
674 					   "but not a reply!\n");
675 				break;
676 			}
677 		}
678 	}
679 	return cmd;
680 }
681 
682 void qeth_clear_ipacmd_list(struct qeth_card *card)
683 {
684 	struct qeth_reply *reply, *r;
685 	unsigned long flags;
686 
687 	QETH_CARD_TEXT(card, 4, "clipalst");
688 
689 	spin_lock_irqsave(&card->lock, flags);
690 	list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
691 		qeth_get_reply(reply);
692 		reply->rc = -EIO;
693 		atomic_inc(&reply->received);
694 		list_del_init(&reply->list);
695 		wake_up(&reply->wait_q);
696 		qeth_put_reply(reply);
697 	}
698 	spin_unlock_irqrestore(&card->lock, flags);
699 	atomic_set(&card->write.irq_pending, 0);
700 }
701 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
702 
703 static int qeth_check_idx_response(struct qeth_card *card,
704 	unsigned char *buffer)
705 {
706 	if (!buffer)
707 		return 0;
708 
709 	QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
710 	if ((buffer[2] & 0xc0) == 0xc0) {
711 		QETH_DBF_MESSAGE(2, "received an IDX TERMINATE "
712 			   "with cause code 0x%02x%s\n",
713 			   buffer[4],
714 			   ((buffer[4] == 0x22) ?
715 			    " -- try another portname" : ""));
716 		QETH_CARD_TEXT(card, 2, "ckidxres");
717 		QETH_CARD_TEXT(card, 2, " idxterm");
718 		QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
719 		if (buffer[4] == 0xf6) {
720 			dev_err(&card->gdev->dev,
721 			"The qeth device is not configured "
722 			"for the OSI layer required by z/VM\n");
723 			return -EPERM;
724 		}
725 		return -EIO;
726 	}
727 	return 0;
728 }
729 
730 static struct qeth_card *CARD_FROM_CDEV(struct ccw_device *cdev)
731 {
732 	struct qeth_card *card = dev_get_drvdata(&((struct ccwgroup_device *)
733 		dev_get_drvdata(&cdev->dev))->dev);
734 	return card;
735 }
736 
737 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
738 		__u32 len)
739 {
740 	struct qeth_card *card;
741 
742 	card = CARD_FROM_CDEV(channel->ccwdev);
743 	QETH_CARD_TEXT(card, 4, "setupccw");
744 	if (channel == &card->read)
745 		memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
746 	else
747 		memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
748 	channel->ccw.count = len;
749 	channel->ccw.cda = (__u32) __pa(iob);
750 }
751 
752 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
753 {
754 	__u8 index;
755 
756 	QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "getbuff");
757 	index = channel->io_buf_no;
758 	do {
759 		if (channel->iob[index].state == BUF_STATE_FREE) {
760 			channel->iob[index].state = BUF_STATE_LOCKED;
761 			channel->io_buf_no = (channel->io_buf_no + 1) %
762 				QETH_CMD_BUFFER_NO;
763 			memset(channel->iob[index].data, 0, QETH_BUFSIZE);
764 			return channel->iob + index;
765 		}
766 		index = (index + 1) % QETH_CMD_BUFFER_NO;
767 	} while (index != channel->io_buf_no);
768 
769 	return NULL;
770 }
771 
772 void qeth_release_buffer(struct qeth_channel *channel,
773 		struct qeth_cmd_buffer *iob)
774 {
775 	unsigned long flags;
776 
777 	QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "relbuff");
778 	spin_lock_irqsave(&channel->iob_lock, flags);
779 	memset(iob->data, 0, QETH_BUFSIZE);
780 	iob->state = BUF_STATE_FREE;
781 	iob->callback = qeth_send_control_data_cb;
782 	iob->rc = 0;
783 	spin_unlock_irqrestore(&channel->iob_lock, flags);
784 	wake_up(&channel->wait_q);
785 }
786 EXPORT_SYMBOL_GPL(qeth_release_buffer);
787 
788 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
789 {
790 	struct qeth_cmd_buffer *buffer = NULL;
791 	unsigned long flags;
792 
793 	spin_lock_irqsave(&channel->iob_lock, flags);
794 	buffer = __qeth_get_buffer(channel);
795 	spin_unlock_irqrestore(&channel->iob_lock, flags);
796 	return buffer;
797 }
798 
799 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
800 {
801 	struct qeth_cmd_buffer *buffer;
802 	wait_event(channel->wait_q,
803 		   ((buffer = qeth_get_buffer(channel)) != NULL));
804 	return buffer;
805 }
806 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
807 
808 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
809 {
810 	int cnt;
811 
812 	for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
813 		qeth_release_buffer(channel, &channel->iob[cnt]);
814 	channel->buf_no = 0;
815 	channel->io_buf_no = 0;
816 }
817 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
818 
819 static void qeth_send_control_data_cb(struct qeth_channel *channel,
820 		  struct qeth_cmd_buffer *iob)
821 {
822 	struct qeth_card *card;
823 	struct qeth_reply *reply, *r;
824 	struct qeth_ipa_cmd *cmd;
825 	unsigned long flags;
826 	int keep_reply;
827 	int rc = 0;
828 
829 	card = CARD_FROM_CDEV(channel->ccwdev);
830 	QETH_CARD_TEXT(card, 4, "sndctlcb");
831 	rc = qeth_check_idx_response(card, iob->data);
832 	switch (rc) {
833 	case 0:
834 		break;
835 	case -EIO:
836 		qeth_clear_ipacmd_list(card);
837 		qeth_schedule_recovery(card);
838 		/* fall through */
839 	default:
840 		goto out;
841 	}
842 
843 	cmd = qeth_check_ipa_data(card, iob);
844 	if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
845 		goto out;
846 	/*in case of OSN : check if cmd is set */
847 	if (card->info.type == QETH_CARD_TYPE_OSN &&
848 	    cmd &&
849 	    cmd->hdr.command != IPA_CMD_STARTLAN &&
850 	    card->osn_info.assist_cb != NULL) {
851 		card->osn_info.assist_cb(card->dev, cmd);
852 		goto out;
853 	}
854 
855 	spin_lock_irqsave(&card->lock, flags);
856 	list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
857 		if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
858 		    ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
859 			qeth_get_reply(reply);
860 			list_del_init(&reply->list);
861 			spin_unlock_irqrestore(&card->lock, flags);
862 			keep_reply = 0;
863 			if (reply->callback != NULL) {
864 				if (cmd) {
865 					reply->offset = (__u16)((char *)cmd -
866 							(char *)iob->data);
867 					keep_reply = reply->callback(card,
868 							reply,
869 							(unsigned long)cmd);
870 				} else
871 					keep_reply = reply->callback(card,
872 							reply,
873 							(unsigned long)iob);
874 			}
875 			if (cmd)
876 				reply->rc = (u16) cmd->hdr.return_code;
877 			else if (iob->rc)
878 				reply->rc = iob->rc;
879 			if (keep_reply) {
880 				spin_lock_irqsave(&card->lock, flags);
881 				list_add_tail(&reply->list,
882 					      &card->cmd_waiter_list);
883 				spin_unlock_irqrestore(&card->lock, flags);
884 			} else {
885 				atomic_inc(&reply->received);
886 				wake_up(&reply->wait_q);
887 			}
888 			qeth_put_reply(reply);
889 			goto out;
890 		}
891 	}
892 	spin_unlock_irqrestore(&card->lock, flags);
893 out:
894 	memcpy(&card->seqno.pdu_hdr_ack,
895 		QETH_PDU_HEADER_SEQ_NO(iob->data),
896 		QETH_SEQ_NO_LENGTH);
897 	qeth_release_buffer(channel, iob);
898 }
899 
900 static int qeth_setup_channel(struct qeth_channel *channel)
901 {
902 	int cnt;
903 
904 	QETH_DBF_TEXT(SETUP, 2, "setupch");
905 	for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
906 		channel->iob[cnt].data =
907 			kzalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
908 		if (channel->iob[cnt].data == NULL)
909 			break;
910 		channel->iob[cnt].state = BUF_STATE_FREE;
911 		channel->iob[cnt].channel = channel;
912 		channel->iob[cnt].callback = qeth_send_control_data_cb;
913 		channel->iob[cnt].rc = 0;
914 	}
915 	if (cnt < QETH_CMD_BUFFER_NO) {
916 		while (cnt-- > 0)
917 			kfree(channel->iob[cnt].data);
918 		return -ENOMEM;
919 	}
920 	channel->buf_no = 0;
921 	channel->io_buf_no = 0;
922 	atomic_set(&channel->irq_pending, 0);
923 	spin_lock_init(&channel->iob_lock);
924 
925 	init_waitqueue_head(&channel->wait_q);
926 	return 0;
927 }
928 
929 static int qeth_set_thread_start_bit(struct qeth_card *card,
930 		unsigned long thread)
931 {
932 	unsigned long flags;
933 
934 	spin_lock_irqsave(&card->thread_mask_lock, flags);
935 	if (!(card->thread_allowed_mask & thread) ||
936 	      (card->thread_start_mask & thread)) {
937 		spin_unlock_irqrestore(&card->thread_mask_lock, flags);
938 		return -EPERM;
939 	}
940 	card->thread_start_mask |= thread;
941 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
942 	return 0;
943 }
944 
945 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
946 {
947 	unsigned long flags;
948 
949 	spin_lock_irqsave(&card->thread_mask_lock, flags);
950 	card->thread_start_mask &= ~thread;
951 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
952 	wake_up(&card->wait_q);
953 }
954 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
955 
956 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
957 {
958 	unsigned long flags;
959 
960 	spin_lock_irqsave(&card->thread_mask_lock, flags);
961 	card->thread_running_mask &= ~thread;
962 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
963 	wake_up(&card->wait_q);
964 }
965 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
966 
967 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
968 {
969 	unsigned long flags;
970 	int rc = 0;
971 
972 	spin_lock_irqsave(&card->thread_mask_lock, flags);
973 	if (card->thread_start_mask & thread) {
974 		if ((card->thread_allowed_mask & thread) &&
975 		    !(card->thread_running_mask & thread)) {
976 			rc = 1;
977 			card->thread_start_mask &= ~thread;
978 			card->thread_running_mask |= thread;
979 		} else
980 			rc = -EPERM;
981 	}
982 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
983 	return rc;
984 }
985 
986 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
987 {
988 	int rc = 0;
989 
990 	wait_event(card->wait_q,
991 		   (rc = __qeth_do_run_thread(card, thread)) >= 0);
992 	return rc;
993 }
994 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
995 
996 void qeth_schedule_recovery(struct qeth_card *card)
997 {
998 	QETH_CARD_TEXT(card, 2, "startrec");
999 	if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
1000 		schedule_work(&card->kernel_thread_starter);
1001 }
1002 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
1003 
1004 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
1005 {
1006 	int dstat, cstat;
1007 	char *sense;
1008 	struct qeth_card *card;
1009 
1010 	sense = (char *) irb->ecw;
1011 	cstat = irb->scsw.cmd.cstat;
1012 	dstat = irb->scsw.cmd.dstat;
1013 	card = CARD_FROM_CDEV(cdev);
1014 
1015 	if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1016 		     SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1017 		     SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
1018 		QETH_CARD_TEXT(card, 2, "CGENCHK");
1019 		dev_warn(&cdev->dev, "The qeth device driver "
1020 			"failed to recover an error on the device\n");
1021 		QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x\n",
1022 			dev_name(&cdev->dev), dstat, cstat);
1023 		print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
1024 				16, 1, irb, 64, 1);
1025 		return 1;
1026 	}
1027 
1028 	if (dstat & DEV_STAT_UNIT_CHECK) {
1029 		if (sense[SENSE_RESETTING_EVENT_BYTE] &
1030 		    SENSE_RESETTING_EVENT_FLAG) {
1031 			QETH_CARD_TEXT(card, 2, "REVIND");
1032 			return 1;
1033 		}
1034 		if (sense[SENSE_COMMAND_REJECT_BYTE] &
1035 		    SENSE_COMMAND_REJECT_FLAG) {
1036 			QETH_CARD_TEXT(card, 2, "CMDREJi");
1037 			return 1;
1038 		}
1039 		if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
1040 			QETH_CARD_TEXT(card, 2, "AFFE");
1041 			return 1;
1042 		}
1043 		if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
1044 			QETH_CARD_TEXT(card, 2, "ZEROSEN");
1045 			return 0;
1046 		}
1047 		QETH_CARD_TEXT(card, 2, "DGENCHK");
1048 			return 1;
1049 	}
1050 	return 0;
1051 }
1052 
1053 static long __qeth_check_irb_error(struct ccw_device *cdev,
1054 		unsigned long intparm, struct irb *irb)
1055 {
1056 	struct qeth_card *card;
1057 
1058 	card = CARD_FROM_CDEV(cdev);
1059 
1060 	if (!card || !IS_ERR(irb))
1061 		return 0;
1062 
1063 	switch (PTR_ERR(irb)) {
1064 	case -EIO:
1065 		QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
1066 			dev_name(&cdev->dev));
1067 		QETH_CARD_TEXT(card, 2, "ckirberr");
1068 		QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
1069 		break;
1070 	case -ETIMEDOUT:
1071 		dev_warn(&cdev->dev, "A hardware operation timed out"
1072 			" on the device\n");
1073 		QETH_CARD_TEXT(card, 2, "ckirberr");
1074 		QETH_CARD_TEXT_(card, 2, "  rc%d", -ETIMEDOUT);
1075 		if (intparm == QETH_RCD_PARM) {
1076 			if (card->data.ccwdev == cdev) {
1077 				card->data.state = CH_STATE_DOWN;
1078 				wake_up(&card->wait_q);
1079 			}
1080 		}
1081 		break;
1082 	default:
1083 		QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
1084 			dev_name(&cdev->dev), PTR_ERR(irb));
1085 		QETH_CARD_TEXT(card, 2, "ckirberr");
1086 		QETH_CARD_TEXT(card, 2, "  rc???");
1087 	}
1088 	return PTR_ERR(irb);
1089 }
1090 
1091 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
1092 		struct irb *irb)
1093 {
1094 	int rc;
1095 	int cstat, dstat;
1096 	struct qeth_cmd_buffer *buffer;
1097 	struct qeth_channel *channel;
1098 	struct qeth_card *card;
1099 	struct qeth_cmd_buffer *iob;
1100 	__u8 index;
1101 
1102 	if (__qeth_check_irb_error(cdev, intparm, irb))
1103 		return;
1104 	cstat = irb->scsw.cmd.cstat;
1105 	dstat = irb->scsw.cmd.dstat;
1106 
1107 	card = CARD_FROM_CDEV(cdev);
1108 	if (!card)
1109 		return;
1110 
1111 	QETH_CARD_TEXT(card, 5, "irq");
1112 
1113 	if (card->read.ccwdev == cdev) {
1114 		channel = &card->read;
1115 		QETH_CARD_TEXT(card, 5, "read");
1116 	} else if (card->write.ccwdev == cdev) {
1117 		channel = &card->write;
1118 		QETH_CARD_TEXT(card, 5, "write");
1119 	} else {
1120 		channel = &card->data;
1121 		QETH_CARD_TEXT(card, 5, "data");
1122 	}
1123 	atomic_set(&channel->irq_pending, 0);
1124 
1125 	if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
1126 		channel->state = CH_STATE_STOPPED;
1127 
1128 	if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
1129 		channel->state = CH_STATE_HALTED;
1130 
1131 	/*let's wake up immediately on data channel*/
1132 	if ((channel == &card->data) && (intparm != 0) &&
1133 	    (intparm != QETH_RCD_PARM))
1134 		goto out;
1135 
1136 	if (intparm == QETH_CLEAR_CHANNEL_PARM) {
1137 		QETH_CARD_TEXT(card, 6, "clrchpar");
1138 		/* we don't have to handle this further */
1139 		intparm = 0;
1140 	}
1141 	if (intparm == QETH_HALT_CHANNEL_PARM) {
1142 		QETH_CARD_TEXT(card, 6, "hltchpar");
1143 		/* we don't have to handle this further */
1144 		intparm = 0;
1145 	}
1146 	if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1147 	    (dstat & DEV_STAT_UNIT_CHECK) ||
1148 	    (cstat)) {
1149 		if (irb->esw.esw0.erw.cons) {
1150 			dev_warn(&channel->ccwdev->dev,
1151 				"The qeth device driver failed to recover "
1152 				"an error on the device\n");
1153 			QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
1154 				"0x%X dstat 0x%X\n",
1155 				dev_name(&channel->ccwdev->dev), cstat, dstat);
1156 			print_hex_dump(KERN_WARNING, "qeth: irb ",
1157 				DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1158 			print_hex_dump(KERN_WARNING, "qeth: sense data ",
1159 				DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1160 		}
1161 		if (intparm == QETH_RCD_PARM) {
1162 			channel->state = CH_STATE_DOWN;
1163 			goto out;
1164 		}
1165 		rc = qeth_get_problem(cdev, irb);
1166 		if (rc) {
1167 			qeth_clear_ipacmd_list(card);
1168 			qeth_schedule_recovery(card);
1169 			goto out;
1170 		}
1171 	}
1172 
1173 	if (intparm == QETH_RCD_PARM) {
1174 		channel->state = CH_STATE_RCD_DONE;
1175 		goto out;
1176 	}
1177 	if (intparm) {
1178 		buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
1179 		buffer->state = BUF_STATE_PROCESSED;
1180 	}
1181 	if (channel == &card->data)
1182 		return;
1183 	if (channel == &card->read &&
1184 	    channel->state == CH_STATE_UP)
1185 		qeth_issue_next_read(card);
1186 
1187 	iob = channel->iob;
1188 	index = channel->buf_no;
1189 	while (iob[index].state == BUF_STATE_PROCESSED) {
1190 		if (iob[index].callback != NULL)
1191 			iob[index].callback(channel, iob + index);
1192 
1193 		index = (index + 1) % QETH_CMD_BUFFER_NO;
1194 	}
1195 	channel->buf_no = index;
1196 out:
1197 	wake_up(&card->wait_q);
1198 	return;
1199 }
1200 
1201 static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1202 		struct qeth_qdio_out_buffer *buf,
1203 		enum iucv_tx_notify notification)
1204 {
1205 	struct sk_buff *skb;
1206 
1207 	if (skb_queue_empty(&buf->skb_list))
1208 		goto out;
1209 	skb = skb_peek(&buf->skb_list);
1210 	while (skb) {
1211 		QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1212 		QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1213 		if (be16_to_cpu(skb->protocol) == ETH_P_AF_IUCV) {
1214 			if (skb->sk) {
1215 				struct iucv_sock *iucv = iucv_sk(skb->sk);
1216 				iucv->sk_txnotify(skb, notification);
1217 			}
1218 		}
1219 		if (skb_queue_is_last(&buf->skb_list, skb))
1220 			skb = NULL;
1221 		else
1222 			skb = skb_queue_next(&buf->skb_list, skb);
1223 	}
1224 out:
1225 	return;
1226 }
1227 
1228 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf)
1229 {
1230 	struct sk_buff *skb;
1231 	struct iucv_sock *iucv;
1232 	int notify_general_error = 0;
1233 
1234 	if (atomic_read(&buf->state) == QETH_QDIO_BUF_PENDING)
1235 		notify_general_error = 1;
1236 
1237 	/* release may never happen from within CQ tasklet scope */
1238 	WARN_ON_ONCE(atomic_read(&buf->state) == QETH_QDIO_BUF_IN_CQ);
1239 
1240 	skb = skb_dequeue(&buf->skb_list);
1241 	while (skb) {
1242 		QETH_CARD_TEXT(buf->q->card, 5, "skbr");
1243 		QETH_CARD_TEXT_(buf->q->card, 5, "%lx", (long) skb);
1244 		if (notify_general_error &&
1245 		    be16_to_cpu(skb->protocol) == ETH_P_AF_IUCV) {
1246 			if (skb->sk) {
1247 				iucv = iucv_sk(skb->sk);
1248 				iucv->sk_txnotify(skb, TX_NOTIFY_GENERALERROR);
1249 			}
1250 		}
1251 		refcount_dec(&skb->users);
1252 		dev_kfree_skb_any(skb);
1253 		skb = skb_dequeue(&buf->skb_list);
1254 	}
1255 }
1256 
1257 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1258 		struct qeth_qdio_out_buffer *buf,
1259 		enum qeth_qdio_buffer_states newbufstate)
1260 {
1261 	int i;
1262 
1263 	/* is PCI flag set on buffer? */
1264 	if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1265 		atomic_dec(&queue->set_pci_flags_count);
1266 
1267 	if (newbufstate == QETH_QDIO_BUF_EMPTY) {
1268 		qeth_release_skbs(buf);
1269 	}
1270 	for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
1271 		if (buf->buffer->element[i].addr && buf->is_header[i])
1272 			kmem_cache_free(qeth_core_header_cache,
1273 				buf->buffer->element[i].addr);
1274 		buf->is_header[i] = 0;
1275 		buf->buffer->element[i].length = 0;
1276 		buf->buffer->element[i].addr = NULL;
1277 		buf->buffer->element[i].eflags = 0;
1278 		buf->buffer->element[i].sflags = 0;
1279 	}
1280 	buf->buffer->element[15].eflags = 0;
1281 	buf->buffer->element[15].sflags = 0;
1282 	buf->next_element_to_fill = 0;
1283 	atomic_set(&buf->state, newbufstate);
1284 }
1285 
1286 static void qeth_clear_outq_buffers(struct qeth_qdio_out_q *q, int free)
1287 {
1288 	int j;
1289 
1290 	for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1291 		if (!q->bufs[j])
1292 			continue;
1293 		qeth_cleanup_handled_pending(q, j, 1);
1294 		qeth_clear_output_buffer(q, q->bufs[j], QETH_QDIO_BUF_EMPTY);
1295 		if (free) {
1296 			kmem_cache_free(qeth_qdio_outbuf_cache, q->bufs[j]);
1297 			q->bufs[j] = NULL;
1298 		}
1299 	}
1300 }
1301 
1302 void qeth_clear_qdio_buffers(struct qeth_card *card)
1303 {
1304 	int i;
1305 
1306 	QETH_CARD_TEXT(card, 2, "clearqdbf");
1307 	/* clear outbound buffers to free skbs */
1308 	for (i = 0; i < card->qdio.no_out_queues; ++i) {
1309 		if (card->qdio.out_qs[i]) {
1310 			qeth_clear_outq_buffers(card->qdio.out_qs[i], 0);
1311 		}
1312 	}
1313 }
1314 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
1315 
1316 static void qeth_free_buffer_pool(struct qeth_card *card)
1317 {
1318 	struct qeth_buffer_pool_entry *pool_entry, *tmp;
1319 	int i = 0;
1320 	list_for_each_entry_safe(pool_entry, tmp,
1321 				 &card->qdio.init_pool.entry_list, init_list){
1322 		for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
1323 			free_page((unsigned long)pool_entry->elements[i]);
1324 		list_del(&pool_entry->init_list);
1325 		kfree(pool_entry);
1326 	}
1327 }
1328 
1329 static void qeth_clean_channel(struct qeth_channel *channel)
1330 {
1331 	int cnt;
1332 
1333 	QETH_DBF_TEXT(SETUP, 2, "freech");
1334 	for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1335 		kfree(channel->iob[cnt].data);
1336 }
1337 
1338 static void qeth_set_single_write_queues(struct qeth_card *card)
1339 {
1340 	if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1341 	    (card->qdio.no_out_queues == 4))
1342 		qeth_free_qdio_buffers(card);
1343 
1344 	card->qdio.no_out_queues = 1;
1345 	if (card->qdio.default_out_queue != 0)
1346 		dev_info(&card->gdev->dev, "Priority Queueing not supported\n");
1347 
1348 	card->qdio.default_out_queue = 0;
1349 }
1350 
1351 static void qeth_set_multiple_write_queues(struct qeth_card *card)
1352 {
1353 	if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1354 	    (card->qdio.no_out_queues == 1)) {
1355 		qeth_free_qdio_buffers(card);
1356 		card->qdio.default_out_queue = 2;
1357 	}
1358 	card->qdio.no_out_queues = 4;
1359 }
1360 
1361 static void qeth_update_from_chp_desc(struct qeth_card *card)
1362 {
1363 	struct ccw_device *ccwdev;
1364 	struct channel_path_desc *chp_dsc;
1365 
1366 	QETH_DBF_TEXT(SETUP, 2, "chp_desc");
1367 
1368 	ccwdev = card->data.ccwdev;
1369 	chp_dsc = ccw_device_get_chp_desc(ccwdev, 0);
1370 	if (!chp_dsc)
1371 		goto out;
1372 
1373 	card->info.func_level = 0x4100 + chp_dsc->desc;
1374 	if (card->info.type == QETH_CARD_TYPE_IQD)
1375 		goto out;
1376 
1377 	/* CHPP field bit 6 == 1 -> single queue */
1378 	if ((chp_dsc->chpp & 0x02) == 0x02)
1379 		qeth_set_single_write_queues(card);
1380 	else
1381 		qeth_set_multiple_write_queues(card);
1382 out:
1383 	kfree(chp_dsc);
1384 	QETH_DBF_TEXT_(SETUP, 2, "nr:%x", card->qdio.no_out_queues);
1385 	QETH_DBF_TEXT_(SETUP, 2, "lvl:%02x", card->info.func_level);
1386 }
1387 
1388 static void qeth_init_qdio_info(struct qeth_card *card)
1389 {
1390 	QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1391 	atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1392 	/* inbound */
1393 	card->qdio.no_in_queues = 1;
1394 	card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1395 	if (card->info.type == QETH_CARD_TYPE_IQD)
1396 		card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1397 	else
1398 		card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1399 	card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1400 	INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1401 	INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1402 }
1403 
1404 static void qeth_set_intial_options(struct qeth_card *card)
1405 {
1406 	card->options.route4.type = NO_ROUTER;
1407 	card->options.route6.type = NO_ROUTER;
1408 	card->options.fake_broadcast = 0;
1409 	card->options.performance_stats = 0;
1410 	card->options.rx_sg_cb = QETH_RX_SG_CB;
1411 	card->options.isolation = ISOLATION_MODE_NONE;
1412 	card->options.cq = QETH_CQ_DISABLED;
1413 }
1414 
1415 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1416 {
1417 	unsigned long flags;
1418 	int rc = 0;
1419 
1420 	spin_lock_irqsave(&card->thread_mask_lock, flags);
1421 	QETH_CARD_TEXT_(card, 4, "  %02x%02x%02x",
1422 			(u8) card->thread_start_mask,
1423 			(u8) card->thread_allowed_mask,
1424 			(u8) card->thread_running_mask);
1425 	rc = (card->thread_start_mask & thread);
1426 	spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1427 	return rc;
1428 }
1429 
1430 static void qeth_start_kernel_thread(struct work_struct *work)
1431 {
1432 	struct task_struct *ts;
1433 	struct qeth_card *card = container_of(work, struct qeth_card,
1434 					kernel_thread_starter);
1435 	QETH_CARD_TEXT(card , 2, "strthrd");
1436 
1437 	if (card->read.state != CH_STATE_UP &&
1438 	    card->write.state != CH_STATE_UP)
1439 		return;
1440 	if (qeth_do_start_thread(card, QETH_RECOVER_THREAD)) {
1441 		ts = kthread_run(card->discipline->recover, (void *)card,
1442 				"qeth_recover");
1443 		if (IS_ERR(ts)) {
1444 			qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1445 			qeth_clear_thread_running_bit(card,
1446 				QETH_RECOVER_THREAD);
1447 		}
1448 	}
1449 }
1450 
1451 static void qeth_buffer_reclaim_work(struct work_struct *);
1452 static int qeth_setup_card(struct qeth_card *card)
1453 {
1454 
1455 	QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1456 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1457 
1458 	card->read.state  = CH_STATE_DOWN;
1459 	card->write.state = CH_STATE_DOWN;
1460 	card->data.state  = CH_STATE_DOWN;
1461 	card->state = CARD_STATE_DOWN;
1462 	card->lan_online = 0;
1463 	card->read_or_write_problem = 0;
1464 	card->dev = NULL;
1465 	spin_lock_init(&card->vlanlock);
1466 	spin_lock_init(&card->mclock);
1467 	spin_lock_init(&card->lock);
1468 	spin_lock_init(&card->ip_lock);
1469 	spin_lock_init(&card->thread_mask_lock);
1470 	mutex_init(&card->conf_mutex);
1471 	mutex_init(&card->discipline_mutex);
1472 	card->thread_start_mask = 0;
1473 	card->thread_allowed_mask = 0;
1474 	card->thread_running_mask = 0;
1475 	INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1476 	INIT_LIST_HEAD(&card->cmd_waiter_list);
1477 	init_waitqueue_head(&card->wait_q);
1478 	/* initial options */
1479 	qeth_set_intial_options(card);
1480 	/* IP address takeover */
1481 	INIT_LIST_HEAD(&card->ipato.entries);
1482 	card->ipato.enabled = false;
1483 	card->ipato.invert4 = false;
1484 	card->ipato.invert6 = false;
1485 	/* init QDIO stuff */
1486 	qeth_init_qdio_info(card);
1487 	INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1488 	INIT_WORK(&card->close_dev_work, qeth_close_dev_handler);
1489 	return 0;
1490 }
1491 
1492 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1493 {
1494 	struct qeth_card *card = container_of(slr, struct qeth_card,
1495 					qeth_service_level);
1496 	if (card->info.mcl_level[0])
1497 		seq_printf(m, "qeth: %s firmware level %s\n",
1498 			CARD_BUS_ID(card), card->info.mcl_level);
1499 }
1500 
1501 static struct qeth_card *qeth_alloc_card(void)
1502 {
1503 	struct qeth_card *card;
1504 
1505 	QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1506 	card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1507 	if (!card)
1508 		goto out;
1509 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1510 	if (qeth_setup_channel(&card->read))
1511 		goto out_ip;
1512 	if (qeth_setup_channel(&card->write))
1513 		goto out_channel;
1514 	card->options.layer2 = -1;
1515 	card->qeth_service_level.seq_print = qeth_core_sl_print;
1516 	register_service_level(&card->qeth_service_level);
1517 	return card;
1518 
1519 out_channel:
1520 	qeth_clean_channel(&card->read);
1521 out_ip:
1522 	kfree(card);
1523 out:
1524 	return NULL;
1525 }
1526 
1527 static void qeth_determine_card_type(struct qeth_card *card)
1528 {
1529 	QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1530 
1531 	card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1532 	card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1533 	card->info.type = CARD_RDEV(card)->id.driver_info;
1534 	card->qdio.no_out_queues = QETH_MAX_QUEUES;
1535 	if (card->info.type == QETH_CARD_TYPE_IQD)
1536 		card->info.is_multicast_different = 0x0103;
1537 	qeth_update_from_chp_desc(card);
1538 }
1539 
1540 static int qeth_clear_channel(struct qeth_channel *channel)
1541 {
1542 	unsigned long flags;
1543 	struct qeth_card *card;
1544 	int rc;
1545 
1546 	card = CARD_FROM_CDEV(channel->ccwdev);
1547 	QETH_CARD_TEXT(card, 3, "clearch");
1548 	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1549 	rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
1550 	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1551 
1552 	if (rc)
1553 		return rc;
1554 	rc = wait_event_interruptible_timeout(card->wait_q,
1555 			channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1556 	if (rc == -ERESTARTSYS)
1557 		return rc;
1558 	if (channel->state != CH_STATE_STOPPED)
1559 		return -ETIME;
1560 	channel->state = CH_STATE_DOWN;
1561 	return 0;
1562 }
1563 
1564 static int qeth_halt_channel(struct qeth_channel *channel)
1565 {
1566 	unsigned long flags;
1567 	struct qeth_card *card;
1568 	int rc;
1569 
1570 	card = CARD_FROM_CDEV(channel->ccwdev);
1571 	QETH_CARD_TEXT(card, 3, "haltch");
1572 	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1573 	rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
1574 	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1575 
1576 	if (rc)
1577 		return rc;
1578 	rc = wait_event_interruptible_timeout(card->wait_q,
1579 			channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1580 	if (rc == -ERESTARTSYS)
1581 		return rc;
1582 	if (channel->state != CH_STATE_HALTED)
1583 		return -ETIME;
1584 	return 0;
1585 }
1586 
1587 static int qeth_halt_channels(struct qeth_card *card)
1588 {
1589 	int rc1 = 0, rc2 = 0, rc3 = 0;
1590 
1591 	QETH_CARD_TEXT(card, 3, "haltchs");
1592 	rc1 = qeth_halt_channel(&card->read);
1593 	rc2 = qeth_halt_channel(&card->write);
1594 	rc3 = qeth_halt_channel(&card->data);
1595 	if (rc1)
1596 		return rc1;
1597 	if (rc2)
1598 		return rc2;
1599 	return rc3;
1600 }
1601 
1602 static int qeth_clear_channels(struct qeth_card *card)
1603 {
1604 	int rc1 = 0, rc2 = 0, rc3 = 0;
1605 
1606 	QETH_CARD_TEXT(card, 3, "clearchs");
1607 	rc1 = qeth_clear_channel(&card->read);
1608 	rc2 = qeth_clear_channel(&card->write);
1609 	rc3 = qeth_clear_channel(&card->data);
1610 	if (rc1)
1611 		return rc1;
1612 	if (rc2)
1613 		return rc2;
1614 	return rc3;
1615 }
1616 
1617 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1618 {
1619 	int rc = 0;
1620 
1621 	QETH_CARD_TEXT(card, 3, "clhacrd");
1622 
1623 	if (halt)
1624 		rc = qeth_halt_channels(card);
1625 	if (rc)
1626 		return rc;
1627 	return qeth_clear_channels(card);
1628 }
1629 
1630 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1631 {
1632 	int rc = 0;
1633 
1634 	QETH_CARD_TEXT(card, 3, "qdioclr");
1635 	switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1636 		QETH_QDIO_CLEANING)) {
1637 	case QETH_QDIO_ESTABLISHED:
1638 		if (card->info.type == QETH_CARD_TYPE_IQD)
1639 			rc = qdio_shutdown(CARD_DDEV(card),
1640 				QDIO_FLAG_CLEANUP_USING_HALT);
1641 		else
1642 			rc = qdio_shutdown(CARD_DDEV(card),
1643 				QDIO_FLAG_CLEANUP_USING_CLEAR);
1644 		if (rc)
1645 			QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1646 		atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1647 		break;
1648 	case QETH_QDIO_CLEANING:
1649 		return rc;
1650 	default:
1651 		break;
1652 	}
1653 	rc = qeth_clear_halt_card(card, use_halt);
1654 	if (rc)
1655 		QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1656 	card->state = CARD_STATE_DOWN;
1657 	return rc;
1658 }
1659 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1660 
1661 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1662 			       int *length)
1663 {
1664 	struct ciw *ciw;
1665 	char *rcd_buf;
1666 	int ret;
1667 	struct qeth_channel *channel = &card->data;
1668 	unsigned long flags;
1669 
1670 	/*
1671 	 * scan for RCD command in extended SenseID data
1672 	 */
1673 	ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1674 	if (!ciw || ciw->cmd == 0)
1675 		return -EOPNOTSUPP;
1676 	rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1677 	if (!rcd_buf)
1678 		return -ENOMEM;
1679 
1680 	channel->ccw.cmd_code = ciw->cmd;
1681 	channel->ccw.cda = (__u32) __pa(rcd_buf);
1682 	channel->ccw.count = ciw->count;
1683 	channel->ccw.flags = CCW_FLAG_SLI;
1684 	channel->state = CH_STATE_RCD;
1685 	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1686 	ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1687 				       QETH_RCD_PARM, LPM_ANYPATH, 0,
1688 				       QETH_RCD_TIMEOUT);
1689 	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1690 	if (!ret)
1691 		wait_event(card->wait_q,
1692 			   (channel->state == CH_STATE_RCD_DONE ||
1693 			    channel->state == CH_STATE_DOWN));
1694 	if (channel->state == CH_STATE_DOWN)
1695 		ret = -EIO;
1696 	else
1697 		channel->state = CH_STATE_DOWN;
1698 	if (ret) {
1699 		kfree(rcd_buf);
1700 		*buffer = NULL;
1701 		*length = 0;
1702 	} else {
1703 		*length = ciw->count;
1704 		*buffer = rcd_buf;
1705 	}
1706 	return ret;
1707 }
1708 
1709 static void qeth_configure_unitaddr(struct qeth_card *card, char *prcd)
1710 {
1711 	QETH_DBF_TEXT(SETUP, 2, "cfgunit");
1712 	card->info.chpid = prcd[30];
1713 	card->info.unit_addr2 = prcd[31];
1714 	card->info.cula = prcd[63];
1715 	card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1716 			       (prcd[0x11] == _ascebc['M']));
1717 }
1718 
1719 static enum qeth_discipline_id qeth_vm_detect_layer(struct qeth_card *card)
1720 {
1721 	enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1722 	struct diag26c_vnic_resp *response = NULL;
1723 	struct diag26c_vnic_req *request = NULL;
1724 	struct ccw_dev_id id;
1725 	char userid[80];
1726 	int rc = 0;
1727 
1728 	QETH_DBF_TEXT(SETUP, 2, "vmlayer");
1729 
1730 	cpcmd("QUERY USERID", userid, sizeof(userid), &rc);
1731 	if (rc)
1732 		goto out;
1733 
1734 	request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
1735 	response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
1736 	if (!request || !response) {
1737 		rc = -ENOMEM;
1738 		goto out;
1739 	}
1740 
1741 	ccw_device_get_id(CARD_RDEV(card), &id);
1742 	request->resp_buf_len = sizeof(*response);
1743 	request->resp_version = DIAG26C_VERSION6_VM65918;
1744 	request->req_format = DIAG26C_VNIC_INFO;
1745 	ASCEBC(userid, 8);
1746 	memcpy(&request->sys_name, userid, 8);
1747 	request->devno = id.devno;
1748 
1749 	QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1750 	rc = diag26c(request, response, DIAG26C_PORT_VNIC);
1751 	QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1752 	if (rc)
1753 		goto out;
1754 	QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
1755 
1756 	if (request->resp_buf_len < sizeof(*response) ||
1757 	    response->version != request->resp_version) {
1758 		rc = -EIO;
1759 		goto out;
1760 	}
1761 
1762 	if (response->protocol == VNIC_INFO_PROT_L2)
1763 		disc = QETH_DISCIPLINE_LAYER2;
1764 	else if (response->protocol == VNIC_INFO_PROT_L3)
1765 		disc = QETH_DISCIPLINE_LAYER3;
1766 
1767 out:
1768 	kfree(response);
1769 	kfree(request);
1770 	if (rc)
1771 		QETH_DBF_TEXT_(SETUP, 2, "err%x", rc);
1772 	return disc;
1773 }
1774 
1775 /* Determine whether the device requires a specific layer discipline */
1776 static enum qeth_discipline_id qeth_enforce_discipline(struct qeth_card *card)
1777 {
1778 	enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1779 
1780 	if (card->info.type == QETH_CARD_TYPE_OSM ||
1781 	    card->info.type == QETH_CARD_TYPE_OSN)
1782 		disc = QETH_DISCIPLINE_LAYER2;
1783 	else if (card->info.guestlan)
1784 		disc = (card->info.type == QETH_CARD_TYPE_IQD) ?
1785 				QETH_DISCIPLINE_LAYER3 :
1786 				qeth_vm_detect_layer(card);
1787 
1788 	switch (disc) {
1789 	case QETH_DISCIPLINE_LAYER2:
1790 		QETH_DBF_TEXT(SETUP, 3, "force l2");
1791 		break;
1792 	case QETH_DISCIPLINE_LAYER3:
1793 		QETH_DBF_TEXT(SETUP, 3, "force l3");
1794 		break;
1795 	default:
1796 		QETH_DBF_TEXT(SETUP, 3, "force no");
1797 	}
1798 
1799 	return disc;
1800 }
1801 
1802 static void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1803 {
1804 	QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1805 
1806 	if (prcd[74] == 0xF0 && prcd[75] == 0xF0 &&
1807 	    prcd[76] >= 0xF1 && prcd[76] <= 0xF4) {
1808 		card->info.blkt.time_total = 0;
1809 		card->info.blkt.inter_packet = 0;
1810 		card->info.blkt.inter_packet_jumbo = 0;
1811 	} else {
1812 		card->info.blkt.time_total = 250;
1813 		card->info.blkt.inter_packet = 5;
1814 		card->info.blkt.inter_packet_jumbo = 15;
1815 	}
1816 }
1817 
1818 static void qeth_init_tokens(struct qeth_card *card)
1819 {
1820 	card->token.issuer_rm_w = 0x00010103UL;
1821 	card->token.cm_filter_w = 0x00010108UL;
1822 	card->token.cm_connection_w = 0x0001010aUL;
1823 	card->token.ulp_filter_w = 0x0001010bUL;
1824 	card->token.ulp_connection_w = 0x0001010dUL;
1825 }
1826 
1827 static void qeth_init_func_level(struct qeth_card *card)
1828 {
1829 	switch (card->info.type) {
1830 	case QETH_CARD_TYPE_IQD:
1831 		card->info.func_level =	QETH_IDX_FUNC_LEVEL_IQD;
1832 		break;
1833 	case QETH_CARD_TYPE_OSD:
1834 	case QETH_CARD_TYPE_OSN:
1835 		card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1836 		break;
1837 	default:
1838 		break;
1839 	}
1840 }
1841 
1842 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1843 		void (*idx_reply_cb)(struct qeth_channel *,
1844 			struct qeth_cmd_buffer *))
1845 {
1846 	struct qeth_cmd_buffer *iob;
1847 	unsigned long flags;
1848 	int rc;
1849 	struct qeth_card *card;
1850 
1851 	QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1852 	card = CARD_FROM_CDEV(channel->ccwdev);
1853 	iob = qeth_get_buffer(channel);
1854 	if (!iob)
1855 		return -ENOMEM;
1856 	iob->callback = idx_reply_cb;
1857 	memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1858 	channel->ccw.count = QETH_BUFSIZE;
1859 	channel->ccw.cda = (__u32) __pa(iob->data);
1860 
1861 	wait_event(card->wait_q,
1862 		   atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1863 	QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1864 	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1865 	rc = ccw_device_start(channel->ccwdev,
1866 			      &channel->ccw, (addr_t) iob, 0, 0);
1867 	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1868 
1869 	if (rc) {
1870 		QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1871 		QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1872 		atomic_set(&channel->irq_pending, 0);
1873 		wake_up(&card->wait_q);
1874 		return rc;
1875 	}
1876 	rc = wait_event_interruptible_timeout(card->wait_q,
1877 			 channel->state == CH_STATE_UP, QETH_TIMEOUT);
1878 	if (rc == -ERESTARTSYS)
1879 		return rc;
1880 	if (channel->state != CH_STATE_UP) {
1881 		rc = -ETIME;
1882 		QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1883 		qeth_clear_cmd_buffers(channel);
1884 	} else
1885 		rc = 0;
1886 	return rc;
1887 }
1888 
1889 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1890 		void (*idx_reply_cb)(struct qeth_channel *,
1891 			struct qeth_cmd_buffer *))
1892 {
1893 	struct qeth_card *card;
1894 	struct qeth_cmd_buffer *iob;
1895 	unsigned long flags;
1896 	__u16 temp;
1897 	__u8 tmp;
1898 	int rc;
1899 	struct ccw_dev_id temp_devid;
1900 
1901 	card = CARD_FROM_CDEV(channel->ccwdev);
1902 
1903 	QETH_DBF_TEXT(SETUP, 2, "idxactch");
1904 
1905 	iob = qeth_get_buffer(channel);
1906 	if (!iob)
1907 		return -ENOMEM;
1908 	iob->callback = idx_reply_cb;
1909 	memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1910 	channel->ccw.count = IDX_ACTIVATE_SIZE;
1911 	channel->ccw.cda = (__u32) __pa(iob->data);
1912 	if (channel == &card->write) {
1913 		memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1914 		memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1915 		       &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1916 		card->seqno.trans_hdr++;
1917 	} else {
1918 		memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1919 		memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1920 		       &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1921 	}
1922 	tmp = ((__u8)card->info.portno) | 0x80;
1923 	memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1924 	memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1925 	       &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1926 	memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1927 	       &card->info.func_level, sizeof(__u16));
1928 	ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1929 	memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1930 	temp = (card->info.cula << 8) + card->info.unit_addr2;
1931 	memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1932 
1933 	wait_event(card->wait_q,
1934 		   atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1935 	QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1936 	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1937 	rc = ccw_device_start(channel->ccwdev,
1938 			      &channel->ccw, (addr_t) iob, 0, 0);
1939 	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1940 
1941 	if (rc) {
1942 		QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1943 			rc);
1944 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1945 		atomic_set(&channel->irq_pending, 0);
1946 		wake_up(&card->wait_q);
1947 		return rc;
1948 	}
1949 	rc = wait_event_interruptible_timeout(card->wait_q,
1950 			channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1951 	if (rc == -ERESTARTSYS)
1952 		return rc;
1953 	if (channel->state != CH_STATE_ACTIVATING) {
1954 		dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1955 			" failed to recover an error on the device\n");
1956 		QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1957 			dev_name(&channel->ccwdev->dev));
1958 		QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1959 		qeth_clear_cmd_buffers(channel);
1960 		return -ETIME;
1961 	}
1962 	return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1963 }
1964 
1965 static int qeth_peer_func_level(int level)
1966 {
1967 	if ((level & 0xff) == 8)
1968 		return (level & 0xff) + 0x400;
1969 	if (((level >> 8) & 3) == 1)
1970 		return (level & 0xff) + 0x200;
1971 	return level;
1972 }
1973 
1974 static void qeth_idx_write_cb(struct qeth_channel *channel,
1975 		struct qeth_cmd_buffer *iob)
1976 {
1977 	struct qeth_card *card;
1978 	__u16 temp;
1979 
1980 	QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1981 
1982 	if (channel->state == CH_STATE_DOWN) {
1983 		channel->state = CH_STATE_ACTIVATING;
1984 		goto out;
1985 	}
1986 	card = CARD_FROM_CDEV(channel->ccwdev);
1987 
1988 	if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1989 		if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == QETH_IDX_ACT_ERR_EXCL)
1990 			dev_err(&card->write.ccwdev->dev,
1991 				"The adapter is used exclusively by another "
1992 				"host\n");
1993 		else
1994 			QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
1995 				" negative reply\n",
1996 				dev_name(&card->write.ccwdev->dev));
1997 		goto out;
1998 	}
1999 	memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2000 	if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
2001 		QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
2002 			"function level mismatch (sent: 0x%x, received: "
2003 			"0x%x)\n", dev_name(&card->write.ccwdev->dev),
2004 			card->info.func_level, temp);
2005 		goto out;
2006 	}
2007 	channel->state = CH_STATE_UP;
2008 out:
2009 	qeth_release_buffer(channel, iob);
2010 }
2011 
2012 static void qeth_idx_read_cb(struct qeth_channel *channel,
2013 		struct qeth_cmd_buffer *iob)
2014 {
2015 	struct qeth_card *card;
2016 	__u16 temp;
2017 
2018 	QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
2019 	if (channel->state == CH_STATE_DOWN) {
2020 		channel->state = CH_STATE_ACTIVATING;
2021 		goto out;
2022 	}
2023 
2024 	card = CARD_FROM_CDEV(channel->ccwdev);
2025 	if (qeth_check_idx_response(card, iob->data))
2026 			goto out;
2027 
2028 	if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
2029 		switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
2030 		case QETH_IDX_ACT_ERR_EXCL:
2031 			dev_err(&card->write.ccwdev->dev,
2032 				"The adapter is used exclusively by another "
2033 				"host\n");
2034 			break;
2035 		case QETH_IDX_ACT_ERR_AUTH:
2036 		case QETH_IDX_ACT_ERR_AUTH_USER:
2037 			dev_err(&card->read.ccwdev->dev,
2038 				"Setting the device online failed because of "
2039 				"insufficient authorization\n");
2040 			break;
2041 		default:
2042 			QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
2043 				" negative reply\n",
2044 				dev_name(&card->read.ccwdev->dev));
2045 		}
2046 		QETH_CARD_TEXT_(card, 2, "idxread%c",
2047 			QETH_IDX_ACT_CAUSE_CODE(iob->data));
2048 		goto out;
2049 	}
2050 
2051 	memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2052 	if (temp != qeth_peer_func_level(card->info.func_level)) {
2053 		QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
2054 			"level mismatch (sent: 0x%x, received: 0x%x)\n",
2055 			dev_name(&card->read.ccwdev->dev),
2056 			card->info.func_level, temp);
2057 		goto out;
2058 	}
2059 	memcpy(&card->token.issuer_rm_r,
2060 	       QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
2061 	       QETH_MPC_TOKEN_LENGTH);
2062 	memcpy(&card->info.mcl_level[0],
2063 	       QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
2064 	channel->state = CH_STATE_UP;
2065 out:
2066 	qeth_release_buffer(channel, iob);
2067 }
2068 
2069 void qeth_prepare_control_data(struct qeth_card *card, int len,
2070 		struct qeth_cmd_buffer *iob)
2071 {
2072 	qeth_setup_ccw(&card->write, iob->data, len);
2073 	iob->callback = qeth_release_buffer;
2074 
2075 	memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
2076 	       &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
2077 	card->seqno.trans_hdr++;
2078 	memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
2079 	       &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
2080 	card->seqno.pdu_hdr++;
2081 	memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
2082 	       &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
2083 	QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
2084 }
2085 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
2086 
2087 /**
2088  * qeth_send_control_data() -	send control command to the card
2089  * @card:			qeth_card structure pointer
2090  * @len:			size of the command buffer
2091  * @iob:			qeth_cmd_buffer pointer
2092  * @reply_cb:			callback function pointer
2093  * @cb_card:			pointer to the qeth_card structure
2094  * @cb_reply:			pointer to the qeth_reply structure
2095  * @cb_cmd:			pointer to the original iob for non-IPA
2096  *				commands, or to the qeth_ipa_cmd structure
2097  *				for the IPA commands.
2098  * @reply_param:		private pointer passed to the callback
2099  *
2100  * Returns the value of the `return_code' field of the response
2101  * block returned from the hardware, or other error indication.
2102  * Value of zero indicates successful execution of the command.
2103  *
2104  * Callback function gets called one or more times, with cb_cmd
2105  * pointing to the response returned by the hardware. Callback
2106  * function must return non-zero if more reply blocks are expected,
2107  * and zero if the last or only reply block is received. Callback
2108  * function can get the value of the reply_param pointer from the
2109  * field 'param' of the structure qeth_reply.
2110  */
2111 
2112 int qeth_send_control_data(struct qeth_card *card, int len,
2113 		struct qeth_cmd_buffer *iob,
2114 		int (*reply_cb)(struct qeth_card *cb_card,
2115 				struct qeth_reply *cb_reply,
2116 				unsigned long cb_cmd),
2117 		void *reply_param)
2118 {
2119 	int rc;
2120 	unsigned long flags;
2121 	struct qeth_reply *reply = NULL;
2122 	unsigned long timeout, event_timeout;
2123 	struct qeth_ipa_cmd *cmd = NULL;
2124 
2125 	QETH_CARD_TEXT(card, 2, "sendctl");
2126 
2127 	if (card->read_or_write_problem) {
2128 		qeth_release_buffer(iob->channel, iob);
2129 		return -EIO;
2130 	}
2131 	reply = qeth_alloc_reply(card);
2132 	if (!reply) {
2133 		return -ENOMEM;
2134 	}
2135 	reply->callback = reply_cb;
2136 	reply->param = reply_param;
2137 	if (card->state == CARD_STATE_DOWN)
2138 		reply->seqno = QETH_IDX_COMMAND_SEQNO;
2139 	else
2140 		reply->seqno = card->seqno.ipa++;
2141 	init_waitqueue_head(&reply->wait_q);
2142 	spin_lock_irqsave(&card->lock, flags);
2143 	list_add_tail(&reply->list, &card->cmd_waiter_list);
2144 	spin_unlock_irqrestore(&card->lock, flags);
2145 
2146 	while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
2147 	qeth_prepare_control_data(card, len, iob);
2148 
2149 	if (IS_IPA(iob->data)) {
2150 		cmd = __ipa_cmd(iob);
2151 		event_timeout = QETH_IPA_TIMEOUT;
2152 	} else {
2153 		event_timeout = QETH_TIMEOUT;
2154 	}
2155 
2156 	timeout = jiffies + event_timeout;
2157 
2158 	QETH_CARD_TEXT(card, 6, "noirqpnd");
2159 	spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
2160 	rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
2161 			      (addr_t) iob, 0, 0);
2162 	spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
2163 	if (rc) {
2164 		QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
2165 			"ccw_device_start rc = %i\n",
2166 			dev_name(&card->write.ccwdev->dev), rc);
2167 		QETH_CARD_TEXT_(card, 2, " err%d", rc);
2168 		spin_lock_irqsave(&card->lock, flags);
2169 		list_del_init(&reply->list);
2170 		qeth_put_reply(reply);
2171 		spin_unlock_irqrestore(&card->lock, flags);
2172 		qeth_release_buffer(iob->channel, iob);
2173 		atomic_set(&card->write.irq_pending, 0);
2174 		wake_up(&card->wait_q);
2175 		return rc;
2176 	}
2177 
2178 	/* we have only one long running ipassist, since we can ensure
2179 	   process context of this command we can sleep */
2180 	if (cmd && cmd->hdr.command == IPA_CMD_SETIP &&
2181 	    cmd->hdr.prot_version == QETH_PROT_IPV4) {
2182 		if (!wait_event_timeout(reply->wait_q,
2183 		    atomic_read(&reply->received), event_timeout))
2184 			goto time_err;
2185 	} else {
2186 		while (!atomic_read(&reply->received)) {
2187 			if (time_after(jiffies, timeout))
2188 				goto time_err;
2189 			cpu_relax();
2190 		}
2191 	}
2192 
2193 	if (reply->rc == -EIO)
2194 		goto error;
2195 	rc = reply->rc;
2196 	qeth_put_reply(reply);
2197 	return rc;
2198 
2199 time_err:
2200 	reply->rc = -ETIME;
2201 	spin_lock_irqsave(&reply->card->lock, flags);
2202 	list_del_init(&reply->list);
2203 	spin_unlock_irqrestore(&reply->card->lock, flags);
2204 	atomic_inc(&reply->received);
2205 error:
2206 	atomic_set(&card->write.irq_pending, 0);
2207 	qeth_release_buffer(iob->channel, iob);
2208 	card->write.buf_no = (card->write.buf_no + 1) % QETH_CMD_BUFFER_NO;
2209 	rc = reply->rc;
2210 	qeth_put_reply(reply);
2211 	return rc;
2212 }
2213 EXPORT_SYMBOL_GPL(qeth_send_control_data);
2214 
2215 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2216 		unsigned long data)
2217 {
2218 	struct qeth_cmd_buffer *iob;
2219 
2220 	QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
2221 
2222 	iob = (struct qeth_cmd_buffer *) data;
2223 	memcpy(&card->token.cm_filter_r,
2224 	       QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2225 	       QETH_MPC_TOKEN_LENGTH);
2226 	QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2227 	return 0;
2228 }
2229 
2230 static int qeth_cm_enable(struct qeth_card *card)
2231 {
2232 	int rc;
2233 	struct qeth_cmd_buffer *iob;
2234 
2235 	QETH_DBF_TEXT(SETUP, 2, "cmenable");
2236 
2237 	iob = qeth_wait_for_buffer(&card->write);
2238 	memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2239 	memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2240 	       &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2241 	memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2242 	       &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2243 
2244 	rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2245 				    qeth_cm_enable_cb, NULL);
2246 	return rc;
2247 }
2248 
2249 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2250 		unsigned long data)
2251 {
2252 
2253 	struct qeth_cmd_buffer *iob;
2254 
2255 	QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
2256 
2257 	iob = (struct qeth_cmd_buffer *) data;
2258 	memcpy(&card->token.cm_connection_r,
2259 	       QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2260 	       QETH_MPC_TOKEN_LENGTH);
2261 	QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2262 	return 0;
2263 }
2264 
2265 static int qeth_cm_setup(struct qeth_card *card)
2266 {
2267 	int rc;
2268 	struct qeth_cmd_buffer *iob;
2269 
2270 	QETH_DBF_TEXT(SETUP, 2, "cmsetup");
2271 
2272 	iob = qeth_wait_for_buffer(&card->write);
2273 	memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2274 	memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2275 	       &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2276 	memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2277 	       &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2278 	memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2279 	       &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2280 	rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2281 				    qeth_cm_setup_cb, NULL);
2282 	return rc;
2283 
2284 }
2285 
2286 static int qeth_get_initial_mtu_for_card(struct qeth_card *card)
2287 {
2288 	switch (card->info.type) {
2289 	case QETH_CARD_TYPE_IQD:
2290 		return card->info.max_mtu;
2291 	case QETH_CARD_TYPE_OSD:
2292 	case QETH_CARD_TYPE_OSX:
2293 		if (!card->options.layer2)
2294 			return ETH_DATA_LEN - 8; /* L3: allow for LLC + SNAP */
2295 		/* fall through */
2296 	default:
2297 		return ETH_DATA_LEN;
2298 	}
2299 }
2300 
2301 static int qeth_get_mtu_outof_framesize(int framesize)
2302 {
2303 	switch (framesize) {
2304 	case 0x4000:
2305 		return 8192;
2306 	case 0x6000:
2307 		return 16384;
2308 	case 0xa000:
2309 		return 32768;
2310 	case 0xffff:
2311 		return 57344;
2312 	default:
2313 		return 0;
2314 	}
2315 }
2316 
2317 static int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
2318 {
2319 	switch (card->info.type) {
2320 	case QETH_CARD_TYPE_OSD:
2321 	case QETH_CARD_TYPE_OSM:
2322 	case QETH_CARD_TYPE_OSX:
2323 	case QETH_CARD_TYPE_IQD:
2324 		return ((mtu >= 576) &&
2325 			(mtu <= card->info.max_mtu));
2326 	case QETH_CARD_TYPE_OSN:
2327 	default:
2328 		return 1;
2329 	}
2330 }
2331 
2332 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2333 		unsigned long data)
2334 {
2335 
2336 	__u16 mtu, framesize;
2337 	__u16 len;
2338 	__u8 link_type;
2339 	struct qeth_cmd_buffer *iob;
2340 
2341 	QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
2342 
2343 	iob = (struct qeth_cmd_buffer *) data;
2344 	memcpy(&card->token.ulp_filter_r,
2345 	       QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2346 	       QETH_MPC_TOKEN_LENGTH);
2347 	if (card->info.type == QETH_CARD_TYPE_IQD) {
2348 		memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2349 		mtu = qeth_get_mtu_outof_framesize(framesize);
2350 		if (!mtu) {
2351 			iob->rc = -EINVAL;
2352 			QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2353 			return 0;
2354 		}
2355 		if (card->info.initial_mtu && (card->info.initial_mtu != mtu)) {
2356 			/* frame size has changed */
2357 			if (card->dev &&
2358 			    ((card->dev->mtu == card->info.initial_mtu) ||
2359 			     (card->dev->mtu > mtu)))
2360 				card->dev->mtu = mtu;
2361 			qeth_free_qdio_buffers(card);
2362 		}
2363 		card->info.initial_mtu = mtu;
2364 		card->info.max_mtu = mtu;
2365 		card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2366 	} else {
2367 		card->info.max_mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(
2368 			iob->data);
2369 		card->info.initial_mtu = min(card->info.max_mtu,
2370 					qeth_get_initial_mtu_for_card(card));
2371 		card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2372 	}
2373 
2374 	memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2375 	if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2376 		memcpy(&link_type,
2377 		       QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2378 		card->info.link_type = link_type;
2379 	} else
2380 		card->info.link_type = 0;
2381 	QETH_DBF_TEXT_(SETUP, 2, "link%d", card->info.link_type);
2382 	QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2383 	return 0;
2384 }
2385 
2386 static int qeth_ulp_enable(struct qeth_card *card)
2387 {
2388 	int rc;
2389 	char prot_type;
2390 	struct qeth_cmd_buffer *iob;
2391 
2392 	/*FIXME: trace view callbacks*/
2393 	QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
2394 
2395 	iob = qeth_wait_for_buffer(&card->write);
2396 	memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2397 
2398 	*(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2399 		(__u8) card->info.portno;
2400 	if (card->options.layer2)
2401 		if (card->info.type == QETH_CARD_TYPE_OSN)
2402 			prot_type = QETH_PROT_OSN2;
2403 		else
2404 			prot_type = QETH_PROT_LAYER2;
2405 	else
2406 		prot_type = QETH_PROT_TCPIP;
2407 
2408 	memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2409 	memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2410 	       &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2411 	memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2412 	       &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2413 	rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2414 				    qeth_ulp_enable_cb, NULL);
2415 	return rc;
2416 
2417 }
2418 
2419 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2420 		unsigned long data)
2421 {
2422 	struct qeth_cmd_buffer *iob;
2423 
2424 	QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
2425 
2426 	iob = (struct qeth_cmd_buffer *) data;
2427 	memcpy(&card->token.ulp_connection_r,
2428 	       QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2429 	       QETH_MPC_TOKEN_LENGTH);
2430 	if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2431 		     3)) {
2432 		QETH_DBF_TEXT(SETUP, 2, "olmlimit");
2433 		dev_err(&card->gdev->dev, "A connection could not be "
2434 			"established because of an OLM limit\n");
2435 		iob->rc = -EMLINK;
2436 	}
2437 	QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2438 	return 0;
2439 }
2440 
2441 static int qeth_ulp_setup(struct qeth_card *card)
2442 {
2443 	int rc;
2444 	__u16 temp;
2445 	struct qeth_cmd_buffer *iob;
2446 	struct ccw_dev_id dev_id;
2447 
2448 	QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
2449 
2450 	iob = qeth_wait_for_buffer(&card->write);
2451 	memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2452 
2453 	memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2454 	       &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2455 	memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2456 	       &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2457 	memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2458 	       &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2459 
2460 	ccw_device_get_id(CARD_DDEV(card), &dev_id);
2461 	memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2462 	temp = (card->info.cula << 8) + card->info.unit_addr2;
2463 	memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2464 	rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2465 				    qeth_ulp_setup_cb, NULL);
2466 	return rc;
2467 }
2468 
2469 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *q, int bidx)
2470 {
2471 	int rc;
2472 	struct qeth_qdio_out_buffer *newbuf;
2473 
2474 	rc = 0;
2475 	newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, GFP_ATOMIC);
2476 	if (!newbuf) {
2477 		rc = -ENOMEM;
2478 		goto out;
2479 	}
2480 	newbuf->buffer = q->qdio_bufs[bidx];
2481 	skb_queue_head_init(&newbuf->skb_list);
2482 	lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2483 	newbuf->q = q;
2484 	newbuf->aob = NULL;
2485 	newbuf->next_pending = q->bufs[bidx];
2486 	atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2487 	q->bufs[bidx] = newbuf;
2488 	if (q->bufstates) {
2489 		q->bufstates[bidx].user = newbuf;
2490 		QETH_CARD_TEXT_(q->card, 2, "nbs%d", bidx);
2491 		QETH_CARD_TEXT_(q->card, 2, "%lx", (long) newbuf);
2492 		QETH_CARD_TEXT_(q->card, 2, "%lx",
2493 				(long) newbuf->next_pending);
2494 	}
2495 out:
2496 	return rc;
2497 }
2498 
2499 static void qeth_free_qdio_out_buf(struct qeth_qdio_out_q *q)
2500 {
2501 	if (!q)
2502 		return;
2503 
2504 	qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2505 	kfree(q);
2506 }
2507 
2508 static struct qeth_qdio_out_q *qeth_alloc_qdio_out_buf(void)
2509 {
2510 	struct qeth_qdio_out_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
2511 
2512 	if (!q)
2513 		return NULL;
2514 
2515 	if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
2516 		kfree(q);
2517 		return NULL;
2518 	}
2519 	return q;
2520 }
2521 
2522 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2523 {
2524 	int i, j;
2525 
2526 	QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2527 
2528 	if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2529 		QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2530 		return 0;
2531 
2532 	QETH_DBF_TEXT(SETUP, 2, "inq");
2533 	card->qdio.in_q = qeth_alloc_qdio_queue();
2534 	if (!card->qdio.in_q)
2535 		goto out_nomem;
2536 
2537 	/* inbound buffer pool */
2538 	if (qeth_alloc_buffer_pool(card))
2539 		goto out_freeinq;
2540 
2541 	/* outbound */
2542 	card->qdio.out_qs =
2543 		kzalloc(card->qdio.no_out_queues *
2544 			sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
2545 	if (!card->qdio.out_qs)
2546 		goto out_freepool;
2547 	for (i = 0; i < card->qdio.no_out_queues; ++i) {
2548 		card->qdio.out_qs[i] = qeth_alloc_qdio_out_buf();
2549 		if (!card->qdio.out_qs[i])
2550 			goto out_freeoutq;
2551 		QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2552 		QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2553 		card->qdio.out_qs[i]->queue_no = i;
2554 		/* give outbound qeth_qdio_buffers their qdio_buffers */
2555 		for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2556 			WARN_ON(card->qdio.out_qs[i]->bufs[j] != NULL);
2557 			if (qeth_init_qdio_out_buf(card->qdio.out_qs[i], j))
2558 				goto out_freeoutqbufs;
2559 		}
2560 	}
2561 
2562 	/* completion */
2563 	if (qeth_alloc_cq(card))
2564 		goto out_freeoutq;
2565 
2566 	return 0;
2567 
2568 out_freeoutqbufs:
2569 	while (j > 0) {
2570 		--j;
2571 		kmem_cache_free(qeth_qdio_outbuf_cache,
2572 				card->qdio.out_qs[i]->bufs[j]);
2573 		card->qdio.out_qs[i]->bufs[j] = NULL;
2574 	}
2575 out_freeoutq:
2576 	while (i > 0) {
2577 		qeth_free_qdio_out_buf(card->qdio.out_qs[--i]);
2578 		qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2579 	}
2580 	kfree(card->qdio.out_qs);
2581 	card->qdio.out_qs = NULL;
2582 out_freepool:
2583 	qeth_free_buffer_pool(card);
2584 out_freeinq:
2585 	qeth_free_qdio_queue(card->qdio.in_q);
2586 	card->qdio.in_q = NULL;
2587 out_nomem:
2588 	atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2589 	return -ENOMEM;
2590 }
2591 
2592 static void qeth_free_qdio_buffers(struct qeth_card *card)
2593 {
2594 	int i, j;
2595 
2596 	if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
2597 		QETH_QDIO_UNINITIALIZED)
2598 		return;
2599 
2600 	qeth_free_cq(card);
2601 	cancel_delayed_work_sync(&card->buffer_reclaim_work);
2602 	for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2603 		if (card->qdio.in_q->bufs[j].rx_skb)
2604 			dev_kfree_skb_any(card->qdio.in_q->bufs[j].rx_skb);
2605 	}
2606 	qeth_free_qdio_queue(card->qdio.in_q);
2607 	card->qdio.in_q = NULL;
2608 	/* inbound buffer pool */
2609 	qeth_free_buffer_pool(card);
2610 	/* free outbound qdio_qs */
2611 	if (card->qdio.out_qs) {
2612 		for (i = 0; i < card->qdio.no_out_queues; ++i) {
2613 			qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2614 			qeth_free_qdio_out_buf(card->qdio.out_qs[i]);
2615 		}
2616 		kfree(card->qdio.out_qs);
2617 		card->qdio.out_qs = NULL;
2618 	}
2619 }
2620 
2621 static void qeth_create_qib_param_field(struct qeth_card *card,
2622 		char *param_field)
2623 {
2624 
2625 	param_field[0] = _ascebc['P'];
2626 	param_field[1] = _ascebc['C'];
2627 	param_field[2] = _ascebc['I'];
2628 	param_field[3] = _ascebc['T'];
2629 	*((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2630 	*((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2631 	*((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2632 }
2633 
2634 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2635 		char *param_field)
2636 {
2637 	param_field[16] = _ascebc['B'];
2638 	param_field[17] = _ascebc['L'];
2639 	param_field[18] = _ascebc['K'];
2640 	param_field[19] = _ascebc['T'];
2641 	*((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2642 	*((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2643 	*((unsigned int *) (&param_field[28])) =
2644 		card->info.blkt.inter_packet_jumbo;
2645 }
2646 
2647 static int qeth_qdio_activate(struct qeth_card *card)
2648 {
2649 	QETH_DBF_TEXT(SETUP, 3, "qdioact");
2650 	return qdio_activate(CARD_DDEV(card));
2651 }
2652 
2653 static int qeth_dm_act(struct qeth_card *card)
2654 {
2655 	int rc;
2656 	struct qeth_cmd_buffer *iob;
2657 
2658 	QETH_DBF_TEXT(SETUP, 2, "dmact");
2659 
2660 	iob = qeth_wait_for_buffer(&card->write);
2661 	memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2662 
2663 	memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2664 	       &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2665 	memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2666 	       &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2667 	rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2668 	return rc;
2669 }
2670 
2671 static int qeth_mpc_initialize(struct qeth_card *card)
2672 {
2673 	int rc;
2674 
2675 	QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2676 
2677 	rc = qeth_issue_next_read(card);
2678 	if (rc) {
2679 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2680 		return rc;
2681 	}
2682 	rc = qeth_cm_enable(card);
2683 	if (rc) {
2684 		QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2685 		goto out_qdio;
2686 	}
2687 	rc = qeth_cm_setup(card);
2688 	if (rc) {
2689 		QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2690 		goto out_qdio;
2691 	}
2692 	rc = qeth_ulp_enable(card);
2693 	if (rc) {
2694 		QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2695 		goto out_qdio;
2696 	}
2697 	rc = qeth_ulp_setup(card);
2698 	if (rc) {
2699 		QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2700 		goto out_qdio;
2701 	}
2702 	rc = qeth_alloc_qdio_buffers(card);
2703 	if (rc) {
2704 		QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2705 		goto out_qdio;
2706 	}
2707 	rc = qeth_qdio_establish(card);
2708 	if (rc) {
2709 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2710 		qeth_free_qdio_buffers(card);
2711 		goto out_qdio;
2712 	}
2713 	rc = qeth_qdio_activate(card);
2714 	if (rc) {
2715 		QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2716 		goto out_qdio;
2717 	}
2718 	rc = qeth_dm_act(card);
2719 	if (rc) {
2720 		QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2721 		goto out_qdio;
2722 	}
2723 
2724 	return 0;
2725 out_qdio:
2726 	qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2727 	qdio_free(CARD_DDEV(card));
2728 	return rc;
2729 }
2730 
2731 void qeth_print_status_message(struct qeth_card *card)
2732 {
2733 	switch (card->info.type) {
2734 	case QETH_CARD_TYPE_OSD:
2735 	case QETH_CARD_TYPE_OSM:
2736 	case QETH_CARD_TYPE_OSX:
2737 		/* VM will use a non-zero first character
2738 		 * to indicate a HiperSockets like reporting
2739 		 * of the level OSA sets the first character to zero
2740 		 * */
2741 		if (!card->info.mcl_level[0]) {
2742 			sprintf(card->info.mcl_level, "%02x%02x",
2743 				card->info.mcl_level[2],
2744 				card->info.mcl_level[3]);
2745 			break;
2746 		}
2747 		/* fallthrough */
2748 	case QETH_CARD_TYPE_IQD:
2749 		if ((card->info.guestlan) ||
2750 		    (card->info.mcl_level[0] & 0x80)) {
2751 			card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2752 				card->info.mcl_level[0]];
2753 			card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2754 				card->info.mcl_level[1]];
2755 			card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2756 				card->info.mcl_level[2]];
2757 			card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2758 				card->info.mcl_level[3]];
2759 			card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2760 		}
2761 		break;
2762 	default:
2763 		memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2764 	}
2765 	dev_info(&card->gdev->dev,
2766 		 "Device is a%s card%s%s%s\nwith link type %s.\n",
2767 		 qeth_get_cardname(card),
2768 		 (card->info.mcl_level[0]) ? " (level: " : "",
2769 		 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2770 		 (card->info.mcl_level[0]) ? ")" : "",
2771 		 qeth_get_cardname_short(card));
2772 }
2773 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2774 
2775 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2776 {
2777 	struct qeth_buffer_pool_entry *entry;
2778 
2779 	QETH_CARD_TEXT(card, 5, "inwrklst");
2780 
2781 	list_for_each_entry(entry,
2782 			    &card->qdio.init_pool.entry_list, init_list) {
2783 		qeth_put_buffer_pool_entry(card, entry);
2784 	}
2785 }
2786 
2787 static struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2788 					struct qeth_card *card)
2789 {
2790 	struct list_head *plh;
2791 	struct qeth_buffer_pool_entry *entry;
2792 	int i, free;
2793 	struct page *page;
2794 
2795 	if (list_empty(&card->qdio.in_buf_pool.entry_list))
2796 		return NULL;
2797 
2798 	list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2799 		entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2800 		free = 1;
2801 		for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2802 			if (page_count(virt_to_page(entry->elements[i])) > 1) {
2803 				free = 0;
2804 				break;
2805 			}
2806 		}
2807 		if (free) {
2808 			list_del_init(&entry->list);
2809 			return entry;
2810 		}
2811 	}
2812 
2813 	/* no free buffer in pool so take first one and swap pages */
2814 	entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2815 			struct qeth_buffer_pool_entry, list);
2816 	for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2817 		if (page_count(virt_to_page(entry->elements[i])) > 1) {
2818 			page = alloc_page(GFP_ATOMIC);
2819 			if (!page) {
2820 				return NULL;
2821 			} else {
2822 				free_page((unsigned long)entry->elements[i]);
2823 				entry->elements[i] = page_address(page);
2824 				if (card->options.performance_stats)
2825 					card->perf_stats.sg_alloc_page_rx++;
2826 			}
2827 		}
2828 	}
2829 	list_del_init(&entry->list);
2830 	return entry;
2831 }
2832 
2833 static int qeth_init_input_buffer(struct qeth_card *card,
2834 		struct qeth_qdio_buffer *buf)
2835 {
2836 	struct qeth_buffer_pool_entry *pool_entry;
2837 	int i;
2838 
2839 	if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2840 		buf->rx_skb = dev_alloc_skb(QETH_RX_PULL_LEN + ETH_HLEN);
2841 		if (!buf->rx_skb)
2842 			return 1;
2843 	}
2844 
2845 	pool_entry = qeth_find_free_buffer_pool_entry(card);
2846 	if (!pool_entry)
2847 		return 1;
2848 
2849 	/*
2850 	 * since the buffer is accessed only from the input_tasklet
2851 	 * there shouldn't be a need to synchronize; also, since we use
2852 	 * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2853 	 * buffers
2854 	 */
2855 
2856 	buf->pool_entry = pool_entry;
2857 	for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2858 		buf->buffer->element[i].length = PAGE_SIZE;
2859 		buf->buffer->element[i].addr =  pool_entry->elements[i];
2860 		if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2861 			buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2862 		else
2863 			buf->buffer->element[i].eflags = 0;
2864 		buf->buffer->element[i].sflags = 0;
2865 	}
2866 	return 0;
2867 }
2868 
2869 int qeth_init_qdio_queues(struct qeth_card *card)
2870 {
2871 	int i, j;
2872 	int rc;
2873 
2874 	QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2875 
2876 	/* inbound queue */
2877 	qdio_reset_buffers(card->qdio.in_q->qdio_bufs,
2878 			   QDIO_MAX_BUFFERS_PER_Q);
2879 	qeth_initialize_working_pool_list(card);
2880 	/*give only as many buffers to hardware as we have buffer pool entries*/
2881 	for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2882 		qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2883 	card->qdio.in_q->next_buf_to_init =
2884 		card->qdio.in_buf_pool.buf_count - 1;
2885 	rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2886 		     card->qdio.in_buf_pool.buf_count - 1);
2887 	if (rc) {
2888 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2889 		return rc;
2890 	}
2891 
2892 	/* completion */
2893 	rc = qeth_cq_init(card);
2894 	if (rc) {
2895 		return rc;
2896 	}
2897 
2898 	/* outbound queue */
2899 	for (i = 0; i < card->qdio.no_out_queues; ++i) {
2900 		qdio_reset_buffers(card->qdio.out_qs[i]->qdio_bufs,
2901 				   QDIO_MAX_BUFFERS_PER_Q);
2902 		for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2903 			qeth_clear_output_buffer(card->qdio.out_qs[i],
2904 					card->qdio.out_qs[i]->bufs[j],
2905 					QETH_QDIO_BUF_EMPTY);
2906 		}
2907 		card->qdio.out_qs[i]->card = card;
2908 		card->qdio.out_qs[i]->next_buf_to_fill = 0;
2909 		card->qdio.out_qs[i]->do_pack = 0;
2910 		atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2911 		atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2912 		atomic_set(&card->qdio.out_qs[i]->state,
2913 			   QETH_OUT_Q_UNLOCKED);
2914 	}
2915 	return 0;
2916 }
2917 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2918 
2919 static __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2920 {
2921 	switch (link_type) {
2922 	case QETH_LINK_TYPE_HSTR:
2923 		return 2;
2924 	default:
2925 		return 1;
2926 	}
2927 }
2928 
2929 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2930 		struct qeth_ipa_cmd *cmd, __u8 command,
2931 		enum qeth_prot_versions prot)
2932 {
2933 	memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2934 	cmd->hdr.command = command;
2935 	cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2936 	cmd->hdr.seqno = card->seqno.ipa;
2937 	cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2938 	cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2939 	if (card->options.layer2)
2940 		cmd->hdr.prim_version_no = 2;
2941 	else
2942 		cmd->hdr.prim_version_no = 1;
2943 	cmd->hdr.param_count = 1;
2944 	cmd->hdr.prot_version = prot;
2945 	cmd->hdr.ipa_supported = 0;
2946 	cmd->hdr.ipa_enabled = 0;
2947 }
2948 
2949 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2950 		enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2951 {
2952 	struct qeth_cmd_buffer *iob;
2953 	struct qeth_ipa_cmd *cmd;
2954 
2955 	iob = qeth_get_buffer(&card->write);
2956 	if (iob) {
2957 		cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2958 		qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
2959 	} else {
2960 		dev_warn(&card->gdev->dev,
2961 			 "The qeth driver ran out of channel command buffers\n");
2962 		QETH_DBF_MESSAGE(1, "%s The qeth driver ran out of channel command buffers",
2963 				 dev_name(&card->gdev->dev));
2964 	}
2965 
2966 	return iob;
2967 }
2968 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2969 
2970 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2971 		char prot_type)
2972 {
2973 	memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2974 	memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2975 	memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2976 	       &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2977 }
2978 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2979 
2980 /**
2981  * qeth_send_ipa_cmd() - send an IPA command
2982  *
2983  * See qeth_send_control_data() for explanation of the arguments.
2984  */
2985 
2986 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2987 		int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2988 			unsigned long),
2989 		void *reply_param)
2990 {
2991 	int rc;
2992 	char prot_type;
2993 
2994 	QETH_CARD_TEXT(card, 4, "sendipa");
2995 
2996 	if (card->options.layer2)
2997 		if (card->info.type == QETH_CARD_TYPE_OSN)
2998 			prot_type = QETH_PROT_OSN2;
2999 		else
3000 			prot_type = QETH_PROT_LAYER2;
3001 	else
3002 		prot_type = QETH_PROT_TCPIP;
3003 	qeth_prepare_ipa_cmd(card, iob, prot_type);
3004 	rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
3005 						iob, reply_cb, reply_param);
3006 	if (rc == -ETIME) {
3007 		qeth_clear_ipacmd_list(card);
3008 		qeth_schedule_recovery(card);
3009 	}
3010 	return rc;
3011 }
3012 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
3013 
3014 static int qeth_send_startlan(struct qeth_card *card)
3015 {
3016 	int rc;
3017 	struct qeth_cmd_buffer *iob;
3018 
3019 	QETH_DBF_TEXT(SETUP, 2, "strtlan");
3020 
3021 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_STARTLAN, 0);
3022 	if (!iob)
3023 		return -ENOMEM;
3024 	rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
3025 	return rc;
3026 }
3027 
3028 static int qeth_default_setadapterparms_cb(struct qeth_card *card,
3029 		struct qeth_reply *reply, unsigned long data)
3030 {
3031 	struct qeth_ipa_cmd *cmd;
3032 
3033 	QETH_CARD_TEXT(card, 4, "defadpcb");
3034 
3035 	cmd = (struct qeth_ipa_cmd *) data;
3036 	if (cmd->hdr.return_code == 0)
3037 		cmd->hdr.return_code =
3038 			cmd->data.setadapterparms.hdr.return_code;
3039 	return 0;
3040 }
3041 
3042 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
3043 		struct qeth_reply *reply, unsigned long data)
3044 {
3045 	struct qeth_ipa_cmd *cmd;
3046 
3047 	QETH_CARD_TEXT(card, 3, "quyadpcb");
3048 
3049 	cmd = (struct qeth_ipa_cmd *) data;
3050 	if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f) {
3051 		card->info.link_type =
3052 		      cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
3053 		QETH_DBF_TEXT_(SETUP, 2, "lnk %d", card->info.link_type);
3054 	}
3055 	card->options.adp.supported_funcs =
3056 		cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
3057 	return qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
3058 }
3059 
3060 static struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
3061 		__u32 command, __u32 cmdlen)
3062 {
3063 	struct qeth_cmd_buffer *iob;
3064 	struct qeth_ipa_cmd *cmd;
3065 
3066 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETADAPTERPARMS,
3067 				     QETH_PROT_IPV4);
3068 	if (iob) {
3069 		cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3070 		cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
3071 		cmd->data.setadapterparms.hdr.command_code = command;
3072 		cmd->data.setadapterparms.hdr.used_total = 1;
3073 		cmd->data.setadapterparms.hdr.seq_no = 1;
3074 	}
3075 
3076 	return iob;
3077 }
3078 
3079 int qeth_query_setadapterparms(struct qeth_card *card)
3080 {
3081 	int rc;
3082 	struct qeth_cmd_buffer *iob;
3083 
3084 	QETH_CARD_TEXT(card, 3, "queryadp");
3085 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
3086 				   sizeof(struct qeth_ipacmd_setadpparms));
3087 	if (!iob)
3088 		return -ENOMEM;
3089 	rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
3090 	return rc;
3091 }
3092 EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
3093 
3094 static int qeth_query_ipassists_cb(struct qeth_card *card,
3095 		struct qeth_reply *reply, unsigned long data)
3096 {
3097 	struct qeth_ipa_cmd *cmd;
3098 
3099 	QETH_DBF_TEXT(SETUP, 2, "qipasscb");
3100 
3101 	cmd = (struct qeth_ipa_cmd *) data;
3102 
3103 	switch (cmd->hdr.return_code) {
3104 	case IPA_RC_NOTSUPP:
3105 	case IPA_RC_L2_UNSUPPORTED_CMD:
3106 		QETH_DBF_TEXT(SETUP, 2, "ipaunsup");
3107 		card->options.ipa4.supported_funcs |= IPA_SETADAPTERPARMS;
3108 		card->options.ipa6.supported_funcs |= IPA_SETADAPTERPARMS;
3109 		return -0;
3110 	default:
3111 		if (cmd->hdr.return_code) {
3112 			QETH_DBF_MESSAGE(1, "%s IPA_CMD_QIPASSIST: Unhandled "
3113 						"rc=%d\n",
3114 						dev_name(&card->gdev->dev),
3115 						cmd->hdr.return_code);
3116 			return 0;
3117 		}
3118 	}
3119 
3120 	if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
3121 		card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
3122 		card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
3123 	} else if (cmd->hdr.prot_version == QETH_PROT_IPV6) {
3124 		card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
3125 		card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
3126 	} else
3127 		QETH_DBF_MESSAGE(1, "%s IPA_CMD_QIPASSIST: Flawed LIC detected"
3128 					"\n", dev_name(&card->gdev->dev));
3129 	return 0;
3130 }
3131 
3132 int qeth_query_ipassists(struct qeth_card *card, enum qeth_prot_versions prot)
3133 {
3134 	int rc;
3135 	struct qeth_cmd_buffer *iob;
3136 
3137 	QETH_DBF_TEXT_(SETUP, 2, "qipassi%i", prot);
3138 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_QIPASSIST, prot);
3139 	if (!iob)
3140 		return -ENOMEM;
3141 	rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
3142 	return rc;
3143 }
3144 EXPORT_SYMBOL_GPL(qeth_query_ipassists);
3145 
3146 static int qeth_query_switch_attributes_cb(struct qeth_card *card,
3147 				struct qeth_reply *reply, unsigned long data)
3148 {
3149 	struct qeth_ipa_cmd *cmd;
3150 	struct qeth_switch_info *sw_info;
3151 	struct qeth_query_switch_attributes *attrs;
3152 
3153 	QETH_CARD_TEXT(card, 2, "qswiatcb");
3154 	cmd = (struct qeth_ipa_cmd *) data;
3155 	sw_info = (struct qeth_switch_info *)reply->param;
3156 	if (cmd->data.setadapterparms.hdr.return_code == 0) {
3157 		attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
3158 		sw_info->capabilities = attrs->capabilities;
3159 		sw_info->settings = attrs->settings;
3160 		QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
3161 							sw_info->settings);
3162 	}
3163 	qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3164 
3165 	return 0;
3166 }
3167 
3168 int qeth_query_switch_attributes(struct qeth_card *card,
3169 				 struct qeth_switch_info *sw_info)
3170 {
3171 	struct qeth_cmd_buffer *iob;
3172 
3173 	QETH_CARD_TEXT(card, 2, "qswiattr");
3174 	if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
3175 		return -EOPNOTSUPP;
3176 	if (!netif_carrier_ok(card->dev))
3177 		return -ENOMEDIUM;
3178 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES,
3179 				sizeof(struct qeth_ipacmd_setadpparms_hdr));
3180 	if (!iob)
3181 		return -ENOMEM;
3182 	return qeth_send_ipa_cmd(card, iob,
3183 				qeth_query_switch_attributes_cb, sw_info);
3184 }
3185 EXPORT_SYMBOL_GPL(qeth_query_switch_attributes);
3186 
3187 static int qeth_query_setdiagass_cb(struct qeth_card *card,
3188 		struct qeth_reply *reply, unsigned long data)
3189 {
3190 	struct qeth_ipa_cmd *cmd;
3191 	__u16 rc;
3192 
3193 	cmd = (struct qeth_ipa_cmd *)data;
3194 	rc = cmd->hdr.return_code;
3195 	if (rc)
3196 		QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3197 	else
3198 		card->info.diagass_support = cmd->data.diagass.ext;
3199 	return 0;
3200 }
3201 
3202 static int qeth_query_setdiagass(struct qeth_card *card)
3203 {
3204 	struct qeth_cmd_buffer *iob;
3205 	struct qeth_ipa_cmd    *cmd;
3206 
3207 	QETH_DBF_TEXT(SETUP, 2, "qdiagass");
3208 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3209 	if (!iob)
3210 		return -ENOMEM;
3211 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3212 	cmd->data.diagass.subcmd_len = 16;
3213 	cmd->data.diagass.subcmd = QETH_DIAGS_CMD_QUERY;
3214 	return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3215 }
3216 
3217 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3218 {
3219 	unsigned long info = get_zeroed_page(GFP_KERNEL);
3220 	struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3221 	struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3222 	struct ccw_dev_id ccwid;
3223 	int level;
3224 
3225 	tid->chpid = card->info.chpid;
3226 	ccw_device_get_id(CARD_RDEV(card), &ccwid);
3227 	tid->ssid = ccwid.ssid;
3228 	tid->devno = ccwid.devno;
3229 	if (!info)
3230 		return;
3231 	level = stsi(NULL, 0, 0, 0);
3232 	if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3233 		tid->lparnr = info222->lpar_number;
3234 	if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3235 		EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3236 		memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3237 	}
3238 	free_page(info);
3239 	return;
3240 }
3241 
3242 static int qeth_hw_trap_cb(struct qeth_card *card,
3243 		struct qeth_reply *reply, unsigned long data)
3244 {
3245 	struct qeth_ipa_cmd *cmd;
3246 	__u16 rc;
3247 
3248 	cmd = (struct qeth_ipa_cmd *)data;
3249 	rc = cmd->hdr.return_code;
3250 	if (rc)
3251 		QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3252 	return 0;
3253 }
3254 
3255 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3256 {
3257 	struct qeth_cmd_buffer *iob;
3258 	struct qeth_ipa_cmd *cmd;
3259 
3260 	QETH_DBF_TEXT(SETUP, 2, "diagtrap");
3261 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3262 	if (!iob)
3263 		return -ENOMEM;
3264 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3265 	cmd->data.diagass.subcmd_len = 80;
3266 	cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRAP;
3267 	cmd->data.diagass.type = 1;
3268 	cmd->data.diagass.action = action;
3269 	switch (action) {
3270 	case QETH_DIAGS_TRAP_ARM:
3271 		cmd->data.diagass.options = 0x0003;
3272 		cmd->data.diagass.ext = 0x00010000 +
3273 			sizeof(struct qeth_trap_id);
3274 		qeth_get_trap_id(card,
3275 			(struct qeth_trap_id *)cmd->data.diagass.cdata);
3276 		break;
3277 	case QETH_DIAGS_TRAP_DISARM:
3278 		cmd->data.diagass.options = 0x0001;
3279 		break;
3280 	case QETH_DIAGS_TRAP_CAPTURE:
3281 		break;
3282 	}
3283 	return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3284 }
3285 EXPORT_SYMBOL_GPL(qeth_hw_trap);
3286 
3287 static int qeth_check_qdio_errors(struct qeth_card *card,
3288 				  struct qdio_buffer *buf,
3289 				  unsigned int qdio_error,
3290 				  const char *dbftext)
3291 {
3292 	if (qdio_error) {
3293 		QETH_CARD_TEXT(card, 2, dbftext);
3294 		QETH_CARD_TEXT_(card, 2, " F15=%02X",
3295 			       buf->element[15].sflags);
3296 		QETH_CARD_TEXT_(card, 2, " F14=%02X",
3297 			       buf->element[14].sflags);
3298 		QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3299 		if ((buf->element[15].sflags) == 0x12) {
3300 			card->stats.rx_dropped++;
3301 			return 0;
3302 		} else
3303 			return 1;
3304 	}
3305 	return 0;
3306 }
3307 
3308 static void qeth_queue_input_buffer(struct qeth_card *card, int index)
3309 {
3310 	struct qeth_qdio_q *queue = card->qdio.in_q;
3311 	struct list_head *lh;
3312 	int count;
3313 	int i;
3314 	int rc;
3315 	int newcount = 0;
3316 
3317 	count = (index < queue->next_buf_to_init)?
3318 		card->qdio.in_buf_pool.buf_count -
3319 		(queue->next_buf_to_init - index) :
3320 		card->qdio.in_buf_pool.buf_count -
3321 		(queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
3322 	/* only requeue at a certain threshold to avoid SIGAs */
3323 	if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3324 		for (i = queue->next_buf_to_init;
3325 		     i < queue->next_buf_to_init + count; ++i) {
3326 			if (qeth_init_input_buffer(card,
3327 				&queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
3328 				break;
3329 			} else {
3330 				newcount++;
3331 			}
3332 		}
3333 
3334 		if (newcount < count) {
3335 			/* we are in memory shortage so we switch back to
3336 			   traditional skb allocation and drop packages */
3337 			atomic_set(&card->force_alloc_skb, 3);
3338 			count = newcount;
3339 		} else {
3340 			atomic_add_unless(&card->force_alloc_skb, -1, 0);
3341 		}
3342 
3343 		if (!count) {
3344 			i = 0;
3345 			list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3346 				i++;
3347 			if (i == card->qdio.in_buf_pool.buf_count) {
3348 				QETH_CARD_TEXT(card, 2, "qsarbw");
3349 				card->reclaim_index = index;
3350 				schedule_delayed_work(
3351 					&card->buffer_reclaim_work,
3352 					QETH_RECLAIM_WORK_TIME);
3353 			}
3354 			return;
3355 		}
3356 
3357 		/*
3358 		 * according to old code it should be avoided to requeue all
3359 		 * 128 buffers in order to benefit from PCI avoidance.
3360 		 * this function keeps at least one buffer (the buffer at
3361 		 * 'index') un-requeued -> this buffer is the first buffer that
3362 		 * will be requeued the next time
3363 		 */
3364 		if (card->options.performance_stats) {
3365 			card->perf_stats.inbound_do_qdio_cnt++;
3366 			card->perf_stats.inbound_do_qdio_start_time =
3367 				qeth_get_micros();
3368 		}
3369 		rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
3370 			     queue->next_buf_to_init, count);
3371 		if (card->options.performance_stats)
3372 			card->perf_stats.inbound_do_qdio_time +=
3373 				qeth_get_micros() -
3374 				card->perf_stats.inbound_do_qdio_start_time;
3375 		if (rc) {
3376 			QETH_CARD_TEXT(card, 2, "qinberr");
3377 		}
3378 		queue->next_buf_to_init = (queue->next_buf_to_init + count) %
3379 					  QDIO_MAX_BUFFERS_PER_Q;
3380 	}
3381 }
3382 
3383 static void qeth_buffer_reclaim_work(struct work_struct *work)
3384 {
3385 	struct qeth_card *card = container_of(work, struct qeth_card,
3386 		buffer_reclaim_work.work);
3387 
3388 	QETH_CARD_TEXT_(card, 2, "brw:%x", card->reclaim_index);
3389 	qeth_queue_input_buffer(card, card->reclaim_index);
3390 }
3391 
3392 static void qeth_handle_send_error(struct qeth_card *card,
3393 		struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3394 {
3395 	int sbalf15 = buffer->buffer->element[15].sflags;
3396 
3397 	QETH_CARD_TEXT(card, 6, "hdsnderr");
3398 	if (card->info.type == QETH_CARD_TYPE_IQD) {
3399 		if (sbalf15 == 0) {
3400 			qdio_err = 0;
3401 		} else {
3402 			qdio_err = 1;
3403 		}
3404 	}
3405 	qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3406 
3407 	if (!qdio_err)
3408 		return;
3409 
3410 	if ((sbalf15 >= 15) && (sbalf15 <= 31))
3411 		return;
3412 
3413 	QETH_CARD_TEXT(card, 1, "lnkfail");
3414 	QETH_CARD_TEXT_(card, 1, "%04x %02x",
3415 		       (u16)qdio_err, (u8)sbalf15);
3416 }
3417 
3418 /**
3419  * qeth_prep_flush_pack_buffer - Prepares flushing of a packing buffer.
3420  * @queue: queue to check for packing buffer
3421  *
3422  * Returns number of buffers that were prepared for flush.
3423  */
3424 static int qeth_prep_flush_pack_buffer(struct qeth_qdio_out_q *queue)
3425 {
3426 	struct qeth_qdio_out_buffer *buffer;
3427 
3428 	buffer = queue->bufs[queue->next_buf_to_fill];
3429 	if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3430 	    (buffer->next_element_to_fill > 0)) {
3431 		/* it's a packing buffer */
3432 		atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3433 		queue->next_buf_to_fill =
3434 			(queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
3435 		return 1;
3436 	}
3437 	return 0;
3438 }
3439 
3440 /*
3441  * Switched to packing state if the number of used buffers on a queue
3442  * reaches a certain limit.
3443  */
3444 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3445 {
3446 	if (!queue->do_pack) {
3447 		if (atomic_read(&queue->used_buffers)
3448 		    >= QETH_HIGH_WATERMARK_PACK){
3449 			/* switch non-PACKING -> PACKING */
3450 			QETH_CARD_TEXT(queue->card, 6, "np->pack");
3451 			if (queue->card->options.performance_stats)
3452 				queue->card->perf_stats.sc_dp_p++;
3453 			queue->do_pack = 1;
3454 		}
3455 	}
3456 }
3457 
3458 /*
3459  * Switches from packing to non-packing mode. If there is a packing
3460  * buffer on the queue this buffer will be prepared to be flushed.
3461  * In that case 1 is returned to inform the caller. If no buffer
3462  * has to be flushed, zero is returned.
3463  */
3464 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3465 {
3466 	if (queue->do_pack) {
3467 		if (atomic_read(&queue->used_buffers)
3468 		    <= QETH_LOW_WATERMARK_PACK) {
3469 			/* switch PACKING -> non-PACKING */
3470 			QETH_CARD_TEXT(queue->card, 6, "pack->np");
3471 			if (queue->card->options.performance_stats)
3472 				queue->card->perf_stats.sc_p_dp++;
3473 			queue->do_pack = 0;
3474 			return qeth_prep_flush_pack_buffer(queue);
3475 		}
3476 	}
3477 	return 0;
3478 }
3479 
3480 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3481 			       int count)
3482 {
3483 	struct qeth_qdio_out_buffer *buf;
3484 	int rc;
3485 	int i;
3486 	unsigned int qdio_flags;
3487 
3488 	for (i = index; i < index + count; ++i) {
3489 		int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3490 		buf = queue->bufs[bidx];
3491 		buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3492 				SBAL_EFLAGS_LAST_ENTRY;
3493 
3494 		if (queue->bufstates)
3495 			queue->bufstates[bidx].user = buf;
3496 
3497 		if (queue->card->info.type == QETH_CARD_TYPE_IQD)
3498 			continue;
3499 
3500 		if (!queue->do_pack) {
3501 			if ((atomic_read(&queue->used_buffers) >=
3502 				(QETH_HIGH_WATERMARK_PACK -
3503 				 QETH_WATERMARK_PACK_FUZZ)) &&
3504 			    !atomic_read(&queue->set_pci_flags_count)) {
3505 				/* it's likely that we'll go to packing
3506 				 * mode soon */
3507 				atomic_inc(&queue->set_pci_flags_count);
3508 				buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3509 			}
3510 		} else {
3511 			if (!atomic_read(&queue->set_pci_flags_count)) {
3512 				/*
3513 				 * there's no outstanding PCI any more, so we
3514 				 * have to request a PCI to be sure the the PCI
3515 				 * will wake at some time in the future then we
3516 				 * can flush packed buffers that might still be
3517 				 * hanging around, which can happen if no
3518 				 * further send was requested by the stack
3519 				 */
3520 				atomic_inc(&queue->set_pci_flags_count);
3521 				buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3522 			}
3523 		}
3524 	}
3525 
3526 	netif_trans_update(queue->card->dev);
3527 	if (queue->card->options.performance_stats) {
3528 		queue->card->perf_stats.outbound_do_qdio_cnt++;
3529 		queue->card->perf_stats.outbound_do_qdio_start_time =
3530 			qeth_get_micros();
3531 	}
3532 	qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
3533 	if (atomic_read(&queue->set_pci_flags_count))
3534 		qdio_flags |= QDIO_FLAG_PCI_OUT;
3535 	rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
3536 		     queue->queue_no, index, count);
3537 	if (queue->card->options.performance_stats)
3538 		queue->card->perf_stats.outbound_do_qdio_time +=
3539 			qeth_get_micros() -
3540 			queue->card->perf_stats.outbound_do_qdio_start_time;
3541 	atomic_add(count, &queue->used_buffers);
3542 	if (rc) {
3543 		queue->card->stats.tx_errors += count;
3544 		/* ignore temporary SIGA errors without busy condition */
3545 		if (rc == -ENOBUFS)
3546 			return;
3547 		QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3548 		QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3549 		QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3550 		QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3551 		QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3552 
3553 		/* this must not happen under normal circumstances. if it
3554 		 * happens something is really wrong -> recover */
3555 		qeth_schedule_recovery(queue->card);
3556 		return;
3557 	}
3558 	if (queue->card->options.performance_stats)
3559 		queue->card->perf_stats.bufs_sent += count;
3560 }
3561 
3562 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3563 {
3564 	int index;
3565 	int flush_cnt = 0;
3566 	int q_was_packing = 0;
3567 
3568 	/*
3569 	 * check if weed have to switch to non-packing mode or if
3570 	 * we have to get a pci flag out on the queue
3571 	 */
3572 	if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3573 	    !atomic_read(&queue->set_pci_flags_count)) {
3574 		if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
3575 				QETH_OUT_Q_UNLOCKED) {
3576 			/*
3577 			 * If we get in here, there was no action in
3578 			 * do_send_packet. So, we check if there is a
3579 			 * packing buffer to be flushed here.
3580 			 */
3581 			netif_stop_queue(queue->card->dev);
3582 			index = queue->next_buf_to_fill;
3583 			q_was_packing = queue->do_pack;
3584 			/* queue->do_pack may change */
3585 			barrier();
3586 			flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3587 			if (!flush_cnt &&
3588 			    !atomic_read(&queue->set_pci_flags_count))
3589 				flush_cnt += qeth_prep_flush_pack_buffer(queue);
3590 			if (queue->card->options.performance_stats &&
3591 			    q_was_packing)
3592 				queue->card->perf_stats.bufs_sent_pack +=
3593 					flush_cnt;
3594 			if (flush_cnt)
3595 				qeth_flush_buffers(queue, index, flush_cnt);
3596 			atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3597 		}
3598 	}
3599 }
3600 
3601 void qeth_qdio_start_poll(struct ccw_device *ccwdev, int queue,
3602 		unsigned long card_ptr)
3603 {
3604 	struct qeth_card *card = (struct qeth_card *)card_ptr;
3605 
3606 	if (card->dev && (card->dev->flags & IFF_UP))
3607 		napi_schedule(&card->napi);
3608 }
3609 EXPORT_SYMBOL_GPL(qeth_qdio_start_poll);
3610 
3611 int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3612 {
3613 	int rc;
3614 
3615 	if (card->options.cq ==  QETH_CQ_NOTAVAILABLE) {
3616 		rc = -1;
3617 		goto out;
3618 	} else {
3619 		if (card->options.cq == cq) {
3620 			rc = 0;
3621 			goto out;
3622 		}
3623 
3624 		if (card->state != CARD_STATE_DOWN &&
3625 		    card->state != CARD_STATE_RECOVER) {
3626 			rc = -1;
3627 			goto out;
3628 		}
3629 
3630 		qeth_free_qdio_buffers(card);
3631 		card->options.cq = cq;
3632 		rc = 0;
3633 	}
3634 out:
3635 	return rc;
3636 
3637 }
3638 EXPORT_SYMBOL_GPL(qeth_configure_cq);
3639 
3640 
3641 static void qeth_qdio_cq_handler(struct qeth_card *card,
3642 		unsigned int qdio_err,
3643 		unsigned int queue, int first_element, int count) {
3644 	struct qeth_qdio_q *cq = card->qdio.c_q;
3645 	int i;
3646 	int rc;
3647 
3648 	if (!qeth_is_cq(card, queue))
3649 		goto out;
3650 
3651 	QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3652 	QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3653 	QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3654 
3655 	if (qdio_err) {
3656 		netif_stop_queue(card->dev);
3657 		qeth_schedule_recovery(card);
3658 		goto out;
3659 	}
3660 
3661 	if (card->options.performance_stats) {
3662 		card->perf_stats.cq_cnt++;
3663 		card->perf_stats.cq_start_time = qeth_get_micros();
3664 	}
3665 
3666 	for (i = first_element; i < first_element + count; ++i) {
3667 		int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3668 		struct qdio_buffer *buffer = cq->qdio_bufs[bidx];
3669 		int e;
3670 
3671 		e = 0;
3672 		while ((e < QDIO_MAX_ELEMENTS_PER_BUFFER) &&
3673 		       buffer->element[e].addr) {
3674 			unsigned long phys_aob_addr;
3675 
3676 			phys_aob_addr = (unsigned long) buffer->element[e].addr;
3677 			qeth_qdio_handle_aob(card, phys_aob_addr);
3678 			buffer->element[e].addr = NULL;
3679 			buffer->element[e].eflags = 0;
3680 			buffer->element[e].sflags = 0;
3681 			buffer->element[e].length = 0;
3682 
3683 			++e;
3684 		}
3685 
3686 		buffer->element[15].eflags = 0;
3687 		buffer->element[15].sflags = 0;
3688 	}
3689 	rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, queue,
3690 		    card->qdio.c_q->next_buf_to_init,
3691 		    count);
3692 	if (rc) {
3693 		dev_warn(&card->gdev->dev,
3694 			"QDIO reported an error, rc=%i\n", rc);
3695 		QETH_CARD_TEXT(card, 2, "qcqherr");
3696 	}
3697 	card->qdio.c_q->next_buf_to_init = (card->qdio.c_q->next_buf_to_init
3698 				   + count) % QDIO_MAX_BUFFERS_PER_Q;
3699 
3700 	netif_wake_queue(card->dev);
3701 
3702 	if (card->options.performance_stats) {
3703 		int delta_t = qeth_get_micros();
3704 		delta_t -= card->perf_stats.cq_start_time;
3705 		card->perf_stats.cq_time += delta_t;
3706 	}
3707 out:
3708 	return;
3709 }
3710 
3711 void qeth_qdio_input_handler(struct ccw_device *ccwdev, unsigned int qdio_err,
3712 		unsigned int queue, int first_elem, int count,
3713 		unsigned long card_ptr)
3714 {
3715 	struct qeth_card *card = (struct qeth_card *)card_ptr;
3716 
3717 	QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3718 	QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3719 
3720 	if (qeth_is_cq(card, queue))
3721 		qeth_qdio_cq_handler(card, qdio_err, queue, first_elem, count);
3722 	else if (qdio_err)
3723 		qeth_schedule_recovery(card);
3724 
3725 
3726 }
3727 EXPORT_SYMBOL_GPL(qeth_qdio_input_handler);
3728 
3729 void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3730 		unsigned int qdio_error, int __queue, int first_element,
3731 		int count, unsigned long card_ptr)
3732 {
3733 	struct qeth_card *card        = (struct qeth_card *) card_ptr;
3734 	struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3735 	struct qeth_qdio_out_buffer *buffer;
3736 	int i;
3737 
3738 	QETH_CARD_TEXT(card, 6, "qdouhdl");
3739 	if (qdio_error & QDIO_ERROR_FATAL) {
3740 		QETH_CARD_TEXT(card, 2, "achkcond");
3741 		netif_stop_queue(card->dev);
3742 		qeth_schedule_recovery(card);
3743 		return;
3744 	}
3745 	if (card->options.performance_stats) {
3746 		card->perf_stats.outbound_handler_cnt++;
3747 		card->perf_stats.outbound_handler_start_time =
3748 			qeth_get_micros();
3749 	}
3750 	for (i = first_element; i < (first_element + count); ++i) {
3751 		int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3752 		buffer = queue->bufs[bidx];
3753 		qeth_handle_send_error(card, buffer, qdio_error);
3754 
3755 		if (queue->bufstates &&
3756 		    (queue->bufstates[bidx].flags &
3757 		     QDIO_OUTBUF_STATE_FLAG_PENDING) != 0) {
3758 			WARN_ON_ONCE(card->options.cq != QETH_CQ_ENABLED);
3759 
3760 			if (atomic_cmpxchg(&buffer->state,
3761 					   QETH_QDIO_BUF_PRIMED,
3762 					   QETH_QDIO_BUF_PENDING) ==
3763 				QETH_QDIO_BUF_PRIMED) {
3764 				qeth_notify_skbs(queue, buffer,
3765 						 TX_NOTIFY_PENDING);
3766 			}
3767 			buffer->aob = queue->bufstates[bidx].aob;
3768 			QETH_CARD_TEXT_(queue->card, 5, "pel%d", bidx);
3769 			QETH_CARD_TEXT(queue->card, 5, "aob");
3770 			QETH_CARD_TEXT_(queue->card, 5, "%lx",
3771 					virt_to_phys(buffer->aob));
3772 			if (qeth_init_qdio_out_buf(queue, bidx)) {
3773 				QETH_CARD_TEXT(card, 2, "outofbuf");
3774 				qeth_schedule_recovery(card);
3775 			}
3776 		} else {
3777 			if (card->options.cq == QETH_CQ_ENABLED) {
3778 				enum iucv_tx_notify n;
3779 
3780 				n = qeth_compute_cq_notification(
3781 					buffer->buffer->element[15].sflags, 0);
3782 				qeth_notify_skbs(queue, buffer, n);
3783 			}
3784 
3785 			qeth_clear_output_buffer(queue, buffer,
3786 						QETH_QDIO_BUF_EMPTY);
3787 		}
3788 		qeth_cleanup_handled_pending(queue, bidx, 0);
3789 	}
3790 	atomic_sub(count, &queue->used_buffers);
3791 	/* check if we need to do something on this outbound queue */
3792 	if (card->info.type != QETH_CARD_TYPE_IQD)
3793 		qeth_check_outbound_queue(queue);
3794 
3795 	netif_wake_queue(queue->card->dev);
3796 	if (card->options.performance_stats)
3797 		card->perf_stats.outbound_handler_time += qeth_get_micros() -
3798 			card->perf_stats.outbound_handler_start_time;
3799 }
3800 EXPORT_SYMBOL_GPL(qeth_qdio_output_handler);
3801 
3802 /* We cannot use outbound queue 3 for unicast packets on HiperSockets */
3803 static inline int qeth_cut_iqd_prio(struct qeth_card *card, int queue_num)
3804 {
3805 	if ((card->info.type == QETH_CARD_TYPE_IQD) && (queue_num == 3))
3806 		return 2;
3807 	return queue_num;
3808 }
3809 
3810 /**
3811  * Note: Function assumes that we have 4 outbound queues.
3812  */
3813 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb,
3814 			int ipv, int cast_type)
3815 {
3816 	__be16 *tci;
3817 	u8 tos;
3818 
3819 	if (cast_type && card->info.is_multicast_different)
3820 		return card->info.is_multicast_different &
3821 			(card->qdio.no_out_queues - 1);
3822 
3823 	switch (card->qdio.do_prio_queueing) {
3824 	case QETH_PRIO_Q_ING_TOS:
3825 	case QETH_PRIO_Q_ING_PREC:
3826 		switch (ipv) {
3827 		case 4:
3828 			tos = ipv4_get_dsfield(ip_hdr(skb));
3829 			break;
3830 		case 6:
3831 			tos = ipv6_get_dsfield(ipv6_hdr(skb));
3832 			break;
3833 		default:
3834 			return card->qdio.default_out_queue;
3835 		}
3836 		if (card->qdio.do_prio_queueing == QETH_PRIO_Q_ING_PREC)
3837 			return qeth_cut_iqd_prio(card, ~tos >> 6 & 3);
3838 		if (tos & IPTOS_MINCOST)
3839 			return qeth_cut_iqd_prio(card, 3);
3840 		if (tos & IPTOS_RELIABILITY)
3841 			return 2;
3842 		if (tos & IPTOS_THROUGHPUT)
3843 			return 1;
3844 		if (tos & IPTOS_LOWDELAY)
3845 			return 0;
3846 		break;
3847 	case QETH_PRIO_Q_ING_SKB:
3848 		if (skb->priority > 5)
3849 			return 0;
3850 		return qeth_cut_iqd_prio(card, ~skb->priority >> 1 & 3);
3851 	case QETH_PRIO_Q_ING_VLAN:
3852 		tci = &((struct ethhdr *)skb->data)->h_proto;
3853 		if (be16_to_cpu(*tci) == ETH_P_8021Q)
3854 			return qeth_cut_iqd_prio(card,
3855 			~be16_to_cpu(*(tci + 1)) >> (VLAN_PRIO_SHIFT + 1) & 3);
3856 		break;
3857 	default:
3858 		break;
3859 	}
3860 	return card->qdio.default_out_queue;
3861 }
3862 EXPORT_SYMBOL_GPL(qeth_get_priority_queue);
3863 
3864 /**
3865  * qeth_get_elements_for_frags() -	find number of SBALEs for skb frags.
3866  * @skb:				SKB address
3867  *
3868  * Returns the number of pages, and thus QDIO buffer elements, needed to cover
3869  * fragmented part of the SKB. Returns zero for linear SKB.
3870  */
3871 int qeth_get_elements_for_frags(struct sk_buff *skb)
3872 {
3873 	int cnt, elements = 0;
3874 
3875 	for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3876 		struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[cnt];
3877 
3878 		elements += qeth_get_elements_for_range(
3879 			(addr_t)skb_frag_address(frag),
3880 			(addr_t)skb_frag_address(frag) + skb_frag_size(frag));
3881 	}
3882 	return elements;
3883 }
3884 EXPORT_SYMBOL_GPL(qeth_get_elements_for_frags);
3885 
3886 /**
3887  * qeth_get_elements_no() -	find number of SBALEs for skb data, inc. frags.
3888  * @card:			qeth card structure, to check max. elems.
3889  * @skb:			SKB address
3890  * @extra_elems:		extra elems needed, to check against max.
3891  * @data_offset:		range starts at skb->data + data_offset
3892  *
3893  * Returns the number of pages, and thus QDIO buffer elements, needed to cover
3894  * skb data, including linear part and fragments. Checks if the result plus
3895  * extra_elems fits under the limit for the card. Returns 0 if it does not.
3896  * Note: extra_elems is not included in the returned result.
3897  */
3898 int qeth_get_elements_no(struct qeth_card *card,
3899 		     struct sk_buff *skb, int extra_elems, int data_offset)
3900 {
3901 	int elements = qeth_get_elements_for_range(
3902 				(addr_t)skb->data + data_offset,
3903 				(addr_t)skb->data + skb_headlen(skb)) +
3904 			qeth_get_elements_for_frags(skb);
3905 
3906 	if ((elements + extra_elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
3907 		QETH_DBF_MESSAGE(2, "Invalid size of IP packet "
3908 			"(Number=%d / Length=%d). Discarded.\n",
3909 			elements + extra_elems, skb->len);
3910 		return 0;
3911 	}
3912 	return elements;
3913 }
3914 EXPORT_SYMBOL_GPL(qeth_get_elements_no);
3915 
3916 int qeth_hdr_chk_and_bounce(struct sk_buff *skb, struct qeth_hdr **hdr, int len)
3917 {
3918 	int hroom, inpage, rest;
3919 
3920 	if (((unsigned long)skb->data & PAGE_MASK) !=
3921 	    (((unsigned long)skb->data + len - 1) & PAGE_MASK)) {
3922 		hroom = skb_headroom(skb);
3923 		inpage = PAGE_SIZE - ((unsigned long) skb->data % PAGE_SIZE);
3924 		rest = len - inpage;
3925 		if (rest > hroom)
3926 			return 1;
3927 		memmove(skb->data - rest, skb->data, skb_headlen(skb));
3928 		skb->data -= rest;
3929 		skb->tail -= rest;
3930 		*hdr = (struct qeth_hdr *)skb->data;
3931 		QETH_DBF_MESSAGE(2, "skb bounce len: %d rest: %d\n", len, rest);
3932 	}
3933 	return 0;
3934 }
3935 EXPORT_SYMBOL_GPL(qeth_hdr_chk_and_bounce);
3936 
3937 /**
3938  * qeth_push_hdr() - push a qeth_hdr onto an skb.
3939  * @skb: skb that the qeth_hdr should be pushed onto.
3940  * @hdr: double pointer to a qeth_hdr. When returning with >= 0,
3941  *	 it contains a valid pointer to a qeth_hdr.
3942  * @len: length of the hdr that needs to be pushed on.
3943  *
3944  * Returns the pushed length. If the header can't be pushed on
3945  * (eg. because it would cross a page boundary), it is allocated from
3946  * the cache instead and 0 is returned.
3947  * Error to create the hdr is indicated by returning with < 0.
3948  */
3949 int qeth_push_hdr(struct sk_buff *skb, struct qeth_hdr **hdr, unsigned int len)
3950 {
3951 	if (skb_headroom(skb) >= len &&
3952 	    qeth_get_elements_for_range((addr_t)skb->data - len,
3953 					(addr_t)skb->data) == 1) {
3954 		*hdr = skb_push(skb, len);
3955 		return len;
3956 	}
3957 	/* fall back */
3958 	*hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
3959 	if (!*hdr)
3960 		return -ENOMEM;
3961 	return 0;
3962 }
3963 EXPORT_SYMBOL_GPL(qeth_push_hdr);
3964 
3965 static void __qeth_fill_buffer(struct sk_buff *skb,
3966 			       struct qeth_qdio_out_buffer *buf,
3967 			       bool is_first_elem, unsigned int offset)
3968 {
3969 	struct qdio_buffer *buffer = buf->buffer;
3970 	int element = buf->next_element_to_fill;
3971 	int length = skb_headlen(skb) - offset;
3972 	char *data = skb->data + offset;
3973 	int length_here, cnt;
3974 
3975 	/* map linear part into buffer element(s) */
3976 	while (length > 0) {
3977 		/* length_here is the remaining amount of data in this page */
3978 		length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
3979 		if (length < length_here)
3980 			length_here = length;
3981 
3982 		buffer->element[element].addr = data;
3983 		buffer->element[element].length = length_here;
3984 		length -= length_here;
3985 		if (is_first_elem) {
3986 			is_first_elem = false;
3987 			if (length || skb_is_nonlinear(skb))
3988 				/* skb needs additional elements */
3989 				buffer->element[element].eflags =
3990 					SBAL_EFLAGS_FIRST_FRAG;
3991 			else
3992 				buffer->element[element].eflags = 0;
3993 		} else {
3994 			buffer->element[element].eflags =
3995 				SBAL_EFLAGS_MIDDLE_FRAG;
3996 		}
3997 		data += length_here;
3998 		element++;
3999 	}
4000 
4001 	/* map page frags into buffer element(s) */
4002 	for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
4003 		skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
4004 
4005 		data = skb_frag_address(frag);
4006 		length = skb_frag_size(frag);
4007 		while (length > 0) {
4008 			length_here = PAGE_SIZE -
4009 				((unsigned long) data % PAGE_SIZE);
4010 			if (length < length_here)
4011 				length_here = length;
4012 
4013 			buffer->element[element].addr = data;
4014 			buffer->element[element].length = length_here;
4015 			buffer->element[element].eflags =
4016 				SBAL_EFLAGS_MIDDLE_FRAG;
4017 			length -= length_here;
4018 			data += length_here;
4019 			element++;
4020 		}
4021 	}
4022 
4023 	if (buffer->element[element - 1].eflags)
4024 		buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
4025 	buf->next_element_to_fill = element;
4026 }
4027 
4028 /**
4029  * qeth_fill_buffer() - map skb into an output buffer
4030  * @queue:	QDIO queue to submit the buffer on
4031  * @buf:	buffer to transport the skb
4032  * @skb:	skb to map into the buffer
4033  * @hdr:	qeth_hdr for this skb. Either at skb->data, or allocated
4034  *		from qeth_core_header_cache.
4035  * @offset:	when mapping the skb, start at skb->data + offset
4036  * @hd_len:	if > 0, build a dedicated header element of this size
4037  */
4038 static int qeth_fill_buffer(struct qeth_qdio_out_q *queue,
4039 			    struct qeth_qdio_out_buffer *buf,
4040 			    struct sk_buff *skb, struct qeth_hdr *hdr,
4041 			    unsigned int offset, unsigned int hd_len)
4042 {
4043 	struct qdio_buffer *buffer = buf->buffer;
4044 	bool is_first_elem = true;
4045 	int flush_cnt = 0;
4046 
4047 	refcount_inc(&skb->users);
4048 	skb_queue_tail(&buf->skb_list, skb);
4049 
4050 	/* build dedicated header element */
4051 	if (hd_len) {
4052 		int element = buf->next_element_to_fill;
4053 		is_first_elem = false;
4054 
4055 		buffer->element[element].addr = hdr;
4056 		buffer->element[element].length = hd_len;
4057 		buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
4058 		/* remember to free cache-allocated qeth_hdr: */
4059 		buf->is_header[element] = ((void *)hdr != skb->data);
4060 		buf->next_element_to_fill++;
4061 	}
4062 
4063 	__qeth_fill_buffer(skb, buf, is_first_elem, offset);
4064 
4065 	if (!queue->do_pack) {
4066 		QETH_CARD_TEXT(queue->card, 6, "fillbfnp");
4067 		/* set state to PRIMED -> will be flushed */
4068 		atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4069 		flush_cnt = 1;
4070 	} else {
4071 		QETH_CARD_TEXT(queue->card, 6, "fillbfpa");
4072 		if (queue->card->options.performance_stats)
4073 			queue->card->perf_stats.skbs_sent_pack++;
4074 		if (buf->next_element_to_fill >=
4075 				QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
4076 			/*
4077 			 * packed buffer if full -> set state PRIMED
4078 			 * -> will be flushed
4079 			 */
4080 			atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4081 			flush_cnt = 1;
4082 		}
4083 	}
4084 	return flush_cnt;
4085 }
4086 
4087 int qeth_do_send_packet_fast(struct qeth_qdio_out_q *queue, struct sk_buff *skb,
4088 			     struct qeth_hdr *hdr, unsigned int offset,
4089 			     unsigned int hd_len)
4090 {
4091 	int index = queue->next_buf_to_fill;
4092 	struct qeth_qdio_out_buffer *buffer = queue->bufs[index];
4093 
4094 	/*
4095 	 * check if buffer is empty to make sure that we do not 'overtake'
4096 	 * ourselves and try to fill a buffer that is already primed
4097 	 */
4098 	if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4099 		return -EBUSY;
4100 	queue->next_buf_to_fill = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
4101 	qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4102 	qeth_flush_buffers(queue, index, 1);
4103 	return 0;
4104 }
4105 EXPORT_SYMBOL_GPL(qeth_do_send_packet_fast);
4106 
4107 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4108 			struct sk_buff *skb, struct qeth_hdr *hdr,
4109 			unsigned int offset, unsigned int hd_len,
4110 			int elements_needed)
4111 {
4112 	struct qeth_qdio_out_buffer *buffer;
4113 	int start_index;
4114 	int flush_count = 0;
4115 	int do_pack = 0;
4116 	int tmp;
4117 	int rc = 0;
4118 
4119 	/* spin until we get the queue ... */
4120 	while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
4121 			      QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
4122 	start_index = queue->next_buf_to_fill;
4123 	buffer = queue->bufs[queue->next_buf_to_fill];
4124 	/*
4125 	 * check if buffer is empty to make sure that we do not 'overtake'
4126 	 * ourselves and try to fill a buffer that is already primed
4127 	 */
4128 	if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
4129 		atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4130 		return -EBUSY;
4131 	}
4132 	/* check if we need to switch packing state of this queue */
4133 	qeth_switch_to_packing_if_needed(queue);
4134 	if (queue->do_pack) {
4135 		do_pack = 1;
4136 		/* does packet fit in current buffer? */
4137 		if ((QETH_MAX_BUFFER_ELEMENTS(card) -
4138 		    buffer->next_element_to_fill) < elements_needed) {
4139 			/* ... no -> set state PRIMED */
4140 			atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4141 			flush_count++;
4142 			queue->next_buf_to_fill =
4143 				(queue->next_buf_to_fill + 1) %
4144 				QDIO_MAX_BUFFERS_PER_Q;
4145 			buffer = queue->bufs[queue->next_buf_to_fill];
4146 			/* we did a step forward, so check buffer state
4147 			 * again */
4148 			if (atomic_read(&buffer->state) !=
4149 			    QETH_QDIO_BUF_EMPTY) {
4150 				qeth_flush_buffers(queue, start_index,
4151 							   flush_count);
4152 				atomic_set(&queue->state,
4153 						QETH_OUT_Q_UNLOCKED);
4154 				rc = -EBUSY;
4155 				goto out;
4156 			}
4157 		}
4158 	}
4159 	tmp = qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4160 	queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
4161 				  QDIO_MAX_BUFFERS_PER_Q;
4162 	flush_count += tmp;
4163 	if (flush_count)
4164 		qeth_flush_buffers(queue, start_index, flush_count);
4165 	else if (!atomic_read(&queue->set_pci_flags_count))
4166 		atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
4167 	/*
4168 	 * queue->state will go from LOCKED -> UNLOCKED or from
4169 	 * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
4170 	 * (switch packing state or flush buffer to get another pci flag out).
4171 	 * In that case we will enter this loop
4172 	 */
4173 	while (atomic_dec_return(&queue->state)) {
4174 		start_index = queue->next_buf_to_fill;
4175 		/* check if we can go back to non-packing state */
4176 		tmp = qeth_switch_to_nonpacking_if_needed(queue);
4177 		/*
4178 		 * check if we need to flush a packing buffer to get a pci
4179 		 * flag out on the queue
4180 		 */
4181 		if (!tmp && !atomic_read(&queue->set_pci_flags_count))
4182 			tmp = qeth_prep_flush_pack_buffer(queue);
4183 		if (tmp) {
4184 			qeth_flush_buffers(queue, start_index, tmp);
4185 			flush_count += tmp;
4186 		}
4187 	}
4188 out:
4189 	/* at this point the queue is UNLOCKED again */
4190 	if (queue->card->options.performance_stats && do_pack)
4191 		queue->card->perf_stats.bufs_sent_pack += flush_count;
4192 
4193 	return rc;
4194 }
4195 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
4196 
4197 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
4198 		struct qeth_reply *reply, unsigned long data)
4199 {
4200 	struct qeth_ipa_cmd *cmd;
4201 	struct qeth_ipacmd_setadpparms *setparms;
4202 
4203 	QETH_CARD_TEXT(card, 4, "prmadpcb");
4204 
4205 	cmd = (struct qeth_ipa_cmd *) data;
4206 	setparms = &(cmd->data.setadapterparms);
4207 
4208 	qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
4209 	if (cmd->hdr.return_code) {
4210 		QETH_CARD_TEXT_(card, 4, "prmrc%x", cmd->hdr.return_code);
4211 		setparms->data.mode = SET_PROMISC_MODE_OFF;
4212 	}
4213 	card->info.promisc_mode = setparms->data.mode;
4214 	return 0;
4215 }
4216 
4217 void qeth_setadp_promisc_mode(struct qeth_card *card)
4218 {
4219 	enum qeth_ipa_promisc_modes mode;
4220 	struct net_device *dev = card->dev;
4221 	struct qeth_cmd_buffer *iob;
4222 	struct qeth_ipa_cmd *cmd;
4223 
4224 	QETH_CARD_TEXT(card, 4, "setprom");
4225 
4226 	if (((dev->flags & IFF_PROMISC) &&
4227 	     (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
4228 	    (!(dev->flags & IFF_PROMISC) &&
4229 	     (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
4230 		return;
4231 	mode = SET_PROMISC_MODE_OFF;
4232 	if (dev->flags & IFF_PROMISC)
4233 		mode = SET_PROMISC_MODE_ON;
4234 	QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
4235 
4236 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
4237 			sizeof(struct qeth_ipacmd_setadpparms_hdr) + 8);
4238 	if (!iob)
4239 		return;
4240 	cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
4241 	cmd->data.setadapterparms.data.mode = mode;
4242 	qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
4243 }
4244 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
4245 
4246 int qeth_change_mtu(struct net_device *dev, int new_mtu)
4247 {
4248 	struct qeth_card *card;
4249 	char dbf_text[15];
4250 
4251 	card = dev->ml_priv;
4252 
4253 	QETH_CARD_TEXT(card, 4, "chgmtu");
4254 	sprintf(dbf_text, "%8x", new_mtu);
4255 	QETH_CARD_TEXT(card, 4, dbf_text);
4256 
4257 	if (!qeth_mtu_is_valid(card, new_mtu))
4258 		return -EINVAL;
4259 	dev->mtu = new_mtu;
4260 	return 0;
4261 }
4262 EXPORT_SYMBOL_GPL(qeth_change_mtu);
4263 
4264 struct net_device_stats *qeth_get_stats(struct net_device *dev)
4265 {
4266 	struct qeth_card *card;
4267 
4268 	card = dev->ml_priv;
4269 
4270 	QETH_CARD_TEXT(card, 5, "getstat");
4271 
4272 	return &card->stats;
4273 }
4274 EXPORT_SYMBOL_GPL(qeth_get_stats);
4275 
4276 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
4277 		struct qeth_reply *reply, unsigned long data)
4278 {
4279 	struct qeth_ipa_cmd *cmd;
4280 
4281 	QETH_CARD_TEXT(card, 4, "chgmaccb");
4282 
4283 	cmd = (struct qeth_ipa_cmd *) data;
4284 	if (!card->options.layer2 ||
4285 	    !(card->info.mac_bits & QETH_LAYER2_MAC_READ)) {
4286 		ether_addr_copy(card->dev->dev_addr,
4287 				cmd->data.setadapterparms.data.change_addr.addr);
4288 		card->info.mac_bits |= QETH_LAYER2_MAC_READ;
4289 	}
4290 	qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4291 	return 0;
4292 }
4293 
4294 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
4295 {
4296 	int rc;
4297 	struct qeth_cmd_buffer *iob;
4298 	struct qeth_ipa_cmd *cmd;
4299 
4300 	QETH_CARD_TEXT(card, 4, "chgmac");
4301 
4302 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
4303 				   sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4304 				   sizeof(struct qeth_change_addr));
4305 	if (!iob)
4306 		return -ENOMEM;
4307 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4308 	cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
4309 	cmd->data.setadapterparms.data.change_addr.addr_size = ETH_ALEN;
4310 	ether_addr_copy(cmd->data.setadapterparms.data.change_addr.addr,
4311 			card->dev->dev_addr);
4312 	rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
4313 			       NULL);
4314 	return rc;
4315 }
4316 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
4317 
4318 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
4319 		struct qeth_reply *reply, unsigned long data)
4320 {
4321 	struct qeth_ipa_cmd *cmd;
4322 	struct qeth_set_access_ctrl *access_ctrl_req;
4323 	int fallback = *(int *)reply->param;
4324 
4325 	QETH_CARD_TEXT(card, 4, "setaccb");
4326 
4327 	cmd = (struct qeth_ipa_cmd *) data;
4328 	access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4329 	QETH_DBF_TEXT_(SETUP, 2, "setaccb");
4330 	QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4331 	QETH_DBF_TEXT_(SETUP, 2, "rc=%d",
4332 		cmd->data.setadapterparms.hdr.return_code);
4333 	if (cmd->data.setadapterparms.hdr.return_code !=
4334 						SET_ACCESS_CTRL_RC_SUCCESS)
4335 		QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%s,%d)==%d\n",
4336 				card->gdev->dev.kobj.name,
4337 				access_ctrl_req->subcmd_code,
4338 				cmd->data.setadapterparms.hdr.return_code);
4339 	switch (cmd->data.setadapterparms.hdr.return_code) {
4340 	case SET_ACCESS_CTRL_RC_SUCCESS:
4341 		if (card->options.isolation == ISOLATION_MODE_NONE) {
4342 			dev_info(&card->gdev->dev,
4343 			    "QDIO data connection isolation is deactivated\n");
4344 		} else {
4345 			dev_info(&card->gdev->dev,
4346 			    "QDIO data connection isolation is activated\n");
4347 		}
4348 		break;
4349 	case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
4350 		QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already "
4351 				"deactivated\n", dev_name(&card->gdev->dev));
4352 		if (fallback)
4353 			card->options.isolation = card->options.prev_isolation;
4354 		break;
4355 	case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
4356 		QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already"
4357 				" activated\n", dev_name(&card->gdev->dev));
4358 		if (fallback)
4359 			card->options.isolation = card->options.prev_isolation;
4360 		break;
4361 	case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
4362 		dev_err(&card->gdev->dev, "Adapter does not "
4363 			"support QDIO data connection isolation\n");
4364 		break;
4365 	case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
4366 		dev_err(&card->gdev->dev,
4367 			"Adapter is dedicated. "
4368 			"QDIO data connection isolation not supported\n");
4369 		if (fallback)
4370 			card->options.isolation = card->options.prev_isolation;
4371 		break;
4372 	case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
4373 		dev_err(&card->gdev->dev,
4374 			"TSO does not permit QDIO data connection isolation\n");
4375 		if (fallback)
4376 			card->options.isolation = card->options.prev_isolation;
4377 		break;
4378 	case SET_ACCESS_CTRL_RC_REFLREL_UNSUPPORTED:
4379 		dev_err(&card->gdev->dev, "The adjacent switch port does not "
4380 			"support reflective relay mode\n");
4381 		if (fallback)
4382 			card->options.isolation = card->options.prev_isolation;
4383 		break;
4384 	case SET_ACCESS_CTRL_RC_REFLREL_FAILED:
4385 		dev_err(&card->gdev->dev, "The reflective relay mode cannot be "
4386 					"enabled at the adjacent switch port");
4387 		if (fallback)
4388 			card->options.isolation = card->options.prev_isolation;
4389 		break;
4390 	case SET_ACCESS_CTRL_RC_REFLREL_DEACT_FAILED:
4391 		dev_warn(&card->gdev->dev, "Turning off reflective relay mode "
4392 					"at the adjacent switch failed\n");
4393 		break;
4394 	default:
4395 		/* this should never happen */
4396 		if (fallback)
4397 			card->options.isolation = card->options.prev_isolation;
4398 		break;
4399 	}
4400 	qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4401 	return 0;
4402 }
4403 
4404 static int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
4405 		enum qeth_ipa_isolation_modes isolation, int fallback)
4406 {
4407 	int rc;
4408 	struct qeth_cmd_buffer *iob;
4409 	struct qeth_ipa_cmd *cmd;
4410 	struct qeth_set_access_ctrl *access_ctrl_req;
4411 
4412 	QETH_CARD_TEXT(card, 4, "setacctl");
4413 
4414 	QETH_DBF_TEXT_(SETUP, 2, "setacctl");
4415 	QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4416 
4417 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
4418 				   sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4419 				   sizeof(struct qeth_set_access_ctrl));
4420 	if (!iob)
4421 		return -ENOMEM;
4422 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4423 	access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4424 	access_ctrl_req->subcmd_code = isolation;
4425 
4426 	rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
4427 			       &fallback);
4428 	QETH_DBF_TEXT_(SETUP, 2, "rc=%d", rc);
4429 	return rc;
4430 }
4431 
4432 int qeth_set_access_ctrl_online(struct qeth_card *card, int fallback)
4433 {
4434 	int rc = 0;
4435 
4436 	QETH_CARD_TEXT(card, 4, "setactlo");
4437 
4438 	if ((card->info.type == QETH_CARD_TYPE_OSD ||
4439 	     card->info.type == QETH_CARD_TYPE_OSX) &&
4440 	     qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
4441 		rc = qeth_setadpparms_set_access_ctrl(card,
4442 			card->options.isolation, fallback);
4443 		if (rc) {
4444 			QETH_DBF_MESSAGE(3,
4445 				"IPA(SET_ACCESS_CTRL,%s,%d) sent failed\n",
4446 				card->gdev->dev.kobj.name,
4447 				rc);
4448 			rc = -EOPNOTSUPP;
4449 		}
4450 	} else if (card->options.isolation != ISOLATION_MODE_NONE) {
4451 		card->options.isolation = ISOLATION_MODE_NONE;
4452 
4453 		dev_err(&card->gdev->dev, "Adapter does not "
4454 			"support QDIO data connection isolation\n");
4455 		rc = -EOPNOTSUPP;
4456 	}
4457 	return rc;
4458 }
4459 EXPORT_SYMBOL_GPL(qeth_set_access_ctrl_online);
4460 
4461 void qeth_tx_timeout(struct net_device *dev)
4462 {
4463 	struct qeth_card *card;
4464 
4465 	card = dev->ml_priv;
4466 	QETH_CARD_TEXT(card, 4, "txtimeo");
4467 	card->stats.tx_errors++;
4468 	qeth_schedule_recovery(card);
4469 }
4470 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
4471 
4472 static int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4473 {
4474 	struct qeth_card *card = dev->ml_priv;
4475 	int rc = 0;
4476 
4477 	switch (regnum) {
4478 	case MII_BMCR: /* Basic mode control register */
4479 		rc = BMCR_FULLDPLX;
4480 		if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
4481 		    (card->info.link_type != QETH_LINK_TYPE_OSN) &&
4482 		    (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
4483 			rc |= BMCR_SPEED100;
4484 		break;
4485 	case MII_BMSR: /* Basic mode status register */
4486 		rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4487 		     BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4488 		     BMSR_100BASE4;
4489 		break;
4490 	case MII_PHYSID1: /* PHYS ID 1 */
4491 		rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4492 		     dev->dev_addr[2];
4493 		rc = (rc >> 5) & 0xFFFF;
4494 		break;
4495 	case MII_PHYSID2: /* PHYS ID 2 */
4496 		rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4497 		break;
4498 	case MII_ADVERTISE: /* Advertisement control reg */
4499 		rc = ADVERTISE_ALL;
4500 		break;
4501 	case MII_LPA: /* Link partner ability reg */
4502 		rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4503 		     LPA_100BASE4 | LPA_LPACK;
4504 		break;
4505 	case MII_EXPANSION: /* Expansion register */
4506 		break;
4507 	case MII_DCOUNTER: /* disconnect counter */
4508 		break;
4509 	case MII_FCSCOUNTER: /* false carrier counter */
4510 		break;
4511 	case MII_NWAYTEST: /* N-way auto-neg test register */
4512 		break;
4513 	case MII_RERRCOUNTER: /* rx error counter */
4514 		rc = card->stats.rx_errors;
4515 		break;
4516 	case MII_SREVISION: /* silicon revision */
4517 		break;
4518 	case MII_RESV1: /* reserved 1 */
4519 		break;
4520 	case MII_LBRERROR: /* loopback, rx, bypass error */
4521 		break;
4522 	case MII_PHYADDR: /* physical address */
4523 		break;
4524 	case MII_RESV2: /* reserved 2 */
4525 		break;
4526 	case MII_TPISTATUS: /* TPI status for 10mbps */
4527 		break;
4528 	case MII_NCONFIG: /* network interface config */
4529 		break;
4530 	default:
4531 		break;
4532 	}
4533 	return rc;
4534 }
4535 
4536 static int qeth_send_ipa_snmp_cmd(struct qeth_card *card,
4537 		struct qeth_cmd_buffer *iob, int len,
4538 		int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
4539 			unsigned long),
4540 		void *reply_param)
4541 {
4542 	u16 s1, s2;
4543 
4544 	QETH_CARD_TEXT(card, 4, "sendsnmp");
4545 
4546 	memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4547 	memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4548 	       &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4549 	/* adjust PDU length fields in IPA_PDU_HEADER */
4550 	s1 = (u32) IPA_PDU_HEADER_SIZE + len;
4551 	s2 = (u32) len;
4552 	memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
4553 	memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
4554 	memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
4555 	memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
4556 	return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4557 				      reply_cb, reply_param);
4558 }
4559 
4560 static int qeth_snmp_command_cb(struct qeth_card *card,
4561 		struct qeth_reply *reply, unsigned long sdata)
4562 {
4563 	struct qeth_ipa_cmd *cmd;
4564 	struct qeth_arp_query_info *qinfo;
4565 	struct qeth_snmp_cmd *snmp;
4566 	unsigned char *data;
4567 	__u16 data_len;
4568 
4569 	QETH_CARD_TEXT(card, 3, "snpcmdcb");
4570 
4571 	cmd = (struct qeth_ipa_cmd *) sdata;
4572 	data = (unsigned char *)((char *)cmd - reply->offset);
4573 	qinfo = (struct qeth_arp_query_info *) reply->param;
4574 	snmp = &cmd->data.setadapterparms.data.snmp;
4575 
4576 	if (cmd->hdr.return_code) {
4577 		QETH_CARD_TEXT_(card, 4, "scer1%x", cmd->hdr.return_code);
4578 		return 0;
4579 	}
4580 	if (cmd->data.setadapterparms.hdr.return_code) {
4581 		cmd->hdr.return_code =
4582 			cmd->data.setadapterparms.hdr.return_code;
4583 		QETH_CARD_TEXT_(card, 4, "scer2%x", cmd->hdr.return_code);
4584 		return 0;
4585 	}
4586 	data_len = *((__u16 *)QETH_IPA_PDU_LEN_PDU1(data));
4587 	if (cmd->data.setadapterparms.hdr.seq_no == 1)
4588 		data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
4589 	else
4590 		data_len -= (__u16)((char *)&snmp->request - (char *)cmd);
4591 
4592 	/* check if there is enough room in userspace */
4593 	if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4594 		QETH_CARD_TEXT_(card, 4, "scer3%i", -ENOMEM);
4595 		cmd->hdr.return_code = IPA_RC_ENOMEM;
4596 		return 0;
4597 	}
4598 	QETH_CARD_TEXT_(card, 4, "snore%i",
4599 		       cmd->data.setadapterparms.hdr.used_total);
4600 	QETH_CARD_TEXT_(card, 4, "sseqn%i",
4601 		cmd->data.setadapterparms.hdr.seq_no);
4602 	/*copy entries to user buffer*/
4603 	if (cmd->data.setadapterparms.hdr.seq_no == 1) {
4604 		memcpy(qinfo->udata + qinfo->udata_offset,
4605 		       (char *)snmp,
4606 		       data_len + offsetof(struct qeth_snmp_cmd, data));
4607 		qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
4608 	} else {
4609 		memcpy(qinfo->udata + qinfo->udata_offset,
4610 		       (char *)&snmp->request, data_len);
4611 	}
4612 	qinfo->udata_offset += data_len;
4613 	/* check if all replies received ... */
4614 		QETH_CARD_TEXT_(card, 4, "srtot%i",
4615 			       cmd->data.setadapterparms.hdr.used_total);
4616 		QETH_CARD_TEXT_(card, 4, "srseq%i",
4617 			       cmd->data.setadapterparms.hdr.seq_no);
4618 	if (cmd->data.setadapterparms.hdr.seq_no <
4619 	    cmd->data.setadapterparms.hdr.used_total)
4620 		return 1;
4621 	return 0;
4622 }
4623 
4624 static int qeth_snmp_command(struct qeth_card *card, char __user *udata)
4625 {
4626 	struct qeth_cmd_buffer *iob;
4627 	struct qeth_ipa_cmd *cmd;
4628 	struct qeth_snmp_ureq *ureq;
4629 	unsigned int req_len;
4630 	struct qeth_arp_query_info qinfo = {0, };
4631 	int rc = 0;
4632 
4633 	QETH_CARD_TEXT(card, 3, "snmpcmd");
4634 
4635 	if (card->info.guestlan)
4636 		return -EOPNOTSUPP;
4637 
4638 	if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
4639 	    (!card->options.layer2)) {
4640 		return -EOPNOTSUPP;
4641 	}
4642 	/* skip 4 bytes (data_len struct member) to get req_len */
4643 	if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
4644 		return -EFAULT;
4645 	if (req_len > (QETH_BUFSIZE - IPA_PDU_HEADER_SIZE -
4646 		       sizeof(struct qeth_ipacmd_hdr) -
4647 		       sizeof(struct qeth_ipacmd_setadpparms_hdr)))
4648 		return -EINVAL;
4649 	ureq = memdup_user(udata, req_len + sizeof(struct qeth_snmp_ureq_hdr));
4650 	if (IS_ERR(ureq)) {
4651 		QETH_CARD_TEXT(card, 2, "snmpnome");
4652 		return PTR_ERR(ureq);
4653 	}
4654 	qinfo.udata_len = ureq->hdr.data_len;
4655 	qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
4656 	if (!qinfo.udata) {
4657 		kfree(ureq);
4658 		return -ENOMEM;
4659 	}
4660 	qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4661 
4662 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
4663 				   QETH_SNMP_SETADP_CMDLENGTH + req_len);
4664 	if (!iob) {
4665 		rc = -ENOMEM;
4666 		goto out;
4667 	}
4668 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4669 	memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
4670 	rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
4671 				    qeth_snmp_command_cb, (void *)&qinfo);
4672 	if (rc)
4673 		QETH_DBF_MESSAGE(2, "SNMP command failed on %s: (0x%x)\n",
4674 			   QETH_CARD_IFNAME(card), rc);
4675 	else {
4676 		if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
4677 			rc = -EFAULT;
4678 	}
4679 out:
4680 	kfree(ureq);
4681 	kfree(qinfo.udata);
4682 	return rc;
4683 }
4684 
4685 static int qeth_setadpparms_query_oat_cb(struct qeth_card *card,
4686 		struct qeth_reply *reply, unsigned long data)
4687 {
4688 	struct qeth_ipa_cmd *cmd;
4689 	struct qeth_qoat_priv *priv;
4690 	char *resdata;
4691 	int resdatalen;
4692 
4693 	QETH_CARD_TEXT(card, 3, "qoatcb");
4694 
4695 	cmd = (struct qeth_ipa_cmd *)data;
4696 	priv = (struct qeth_qoat_priv *)reply->param;
4697 	resdatalen = cmd->data.setadapterparms.hdr.cmdlength;
4698 	resdata = (char *)data + 28;
4699 
4700 	if (resdatalen > (priv->buffer_len - priv->response_len)) {
4701 		cmd->hdr.return_code = IPA_RC_FFFF;
4702 		return 0;
4703 	}
4704 
4705 	memcpy((priv->buffer + priv->response_len), resdata,
4706 		resdatalen);
4707 	priv->response_len += resdatalen;
4708 
4709 	if (cmd->data.setadapterparms.hdr.seq_no <
4710 	    cmd->data.setadapterparms.hdr.used_total)
4711 		return 1;
4712 	return 0;
4713 }
4714 
4715 static int qeth_query_oat_command(struct qeth_card *card, char __user *udata)
4716 {
4717 	int rc = 0;
4718 	struct qeth_cmd_buffer *iob;
4719 	struct qeth_ipa_cmd *cmd;
4720 	struct qeth_query_oat *oat_req;
4721 	struct qeth_query_oat_data oat_data;
4722 	struct qeth_qoat_priv priv;
4723 	void __user *tmp;
4724 
4725 	QETH_CARD_TEXT(card, 3, "qoatcmd");
4726 
4727 	if (!qeth_adp_supported(card, IPA_SETADP_QUERY_OAT)) {
4728 		rc = -EOPNOTSUPP;
4729 		goto out;
4730 	}
4731 
4732 	if (copy_from_user(&oat_data, udata,
4733 	    sizeof(struct qeth_query_oat_data))) {
4734 			rc = -EFAULT;
4735 			goto out;
4736 	}
4737 
4738 	priv.buffer_len = oat_data.buffer_len;
4739 	priv.response_len = 0;
4740 	priv.buffer =  kzalloc(oat_data.buffer_len, GFP_KERNEL);
4741 	if (!priv.buffer) {
4742 		rc = -ENOMEM;
4743 		goto out;
4744 	}
4745 
4746 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4747 				   sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4748 				   sizeof(struct qeth_query_oat));
4749 	if (!iob) {
4750 		rc = -ENOMEM;
4751 		goto out_free;
4752 	}
4753 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4754 	oat_req = &cmd->data.setadapterparms.data.query_oat;
4755 	oat_req->subcmd_code = oat_data.command;
4756 
4757 	rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_query_oat_cb,
4758 			       &priv);
4759 	if (!rc) {
4760 		if (is_compat_task())
4761 			tmp = compat_ptr(oat_data.ptr);
4762 		else
4763 			tmp = (void __user *)(unsigned long)oat_data.ptr;
4764 
4765 		if (copy_to_user(tmp, priv.buffer,
4766 		    priv.response_len)) {
4767 			rc = -EFAULT;
4768 			goto out_free;
4769 		}
4770 
4771 		oat_data.response_len = priv.response_len;
4772 
4773 		if (copy_to_user(udata, &oat_data,
4774 		    sizeof(struct qeth_query_oat_data)))
4775 			rc = -EFAULT;
4776 	} else
4777 		if (rc == IPA_RC_FFFF)
4778 			rc = -EFAULT;
4779 
4780 out_free:
4781 	kfree(priv.buffer);
4782 out:
4783 	return rc;
4784 }
4785 
4786 static int qeth_query_card_info_cb(struct qeth_card *card,
4787 				   struct qeth_reply *reply, unsigned long data)
4788 {
4789 	struct qeth_ipa_cmd *cmd;
4790 	struct qeth_query_card_info *card_info;
4791 	struct carrier_info *carrier_info;
4792 
4793 	QETH_CARD_TEXT(card, 2, "qcrdincb");
4794 	carrier_info = (struct carrier_info *)reply->param;
4795 	cmd = (struct qeth_ipa_cmd *)data;
4796 	card_info = &cmd->data.setadapterparms.data.card_info;
4797 	if (cmd->data.setadapterparms.hdr.return_code == 0) {
4798 		carrier_info->card_type = card_info->card_type;
4799 		carrier_info->port_mode = card_info->port_mode;
4800 		carrier_info->port_speed = card_info->port_speed;
4801 	}
4802 
4803 	qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4804 	return 0;
4805 }
4806 
4807 static int qeth_query_card_info(struct qeth_card *card,
4808 				struct carrier_info *carrier_info)
4809 {
4810 	struct qeth_cmd_buffer *iob;
4811 
4812 	QETH_CARD_TEXT(card, 2, "qcrdinfo");
4813 	if (!qeth_adp_supported(card, IPA_SETADP_QUERY_CARD_INFO))
4814 		return -EOPNOTSUPP;
4815 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_CARD_INFO,
4816 		sizeof(struct qeth_ipacmd_setadpparms_hdr));
4817 	if (!iob)
4818 		return -ENOMEM;
4819 	return qeth_send_ipa_cmd(card, iob, qeth_query_card_info_cb,
4820 					(void *)carrier_info);
4821 }
4822 
4823 /**
4824  * qeth_vm_request_mac() - Request a hypervisor-managed MAC address
4825  * @card: pointer to a qeth_card
4826  *
4827  * Returns
4828  *	0, if a MAC address has been set for the card's netdevice
4829  *	a return code, for various error conditions
4830  */
4831 int qeth_vm_request_mac(struct qeth_card *card)
4832 {
4833 	struct diag26c_mac_resp *response;
4834 	struct diag26c_mac_req *request;
4835 	struct ccw_dev_id id;
4836 	int rc;
4837 
4838 	QETH_DBF_TEXT(SETUP, 2, "vmreqmac");
4839 
4840 	if (!card->dev)
4841 		return -ENODEV;
4842 
4843 	request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
4844 	response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
4845 	if (!request || !response) {
4846 		rc = -ENOMEM;
4847 		goto out;
4848 	}
4849 
4850 	ccw_device_get_id(CARD_DDEV(card), &id);
4851 	request->resp_buf_len = sizeof(*response);
4852 	request->resp_version = DIAG26C_VERSION2;
4853 	request->op_code = DIAG26C_GET_MAC;
4854 	request->devno = id.devno;
4855 
4856 	QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4857 	rc = diag26c(request, response, DIAG26C_MAC_SERVICES);
4858 	QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4859 	if (rc)
4860 		goto out;
4861 	QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
4862 
4863 	if (request->resp_buf_len < sizeof(*response) ||
4864 	    response->version != request->resp_version) {
4865 		rc = -EIO;
4866 		QETH_DBF_TEXT(SETUP, 2, "badresp");
4867 		QETH_DBF_HEX(SETUP, 2, &request->resp_buf_len,
4868 			     sizeof(request->resp_buf_len));
4869 	} else if (!is_valid_ether_addr(response->mac)) {
4870 		rc = -EINVAL;
4871 		QETH_DBF_TEXT(SETUP, 2, "badmac");
4872 		QETH_DBF_HEX(SETUP, 2, response->mac, ETH_ALEN);
4873 	} else {
4874 		ether_addr_copy(card->dev->dev_addr, response->mac);
4875 	}
4876 
4877 out:
4878 	kfree(response);
4879 	kfree(request);
4880 	return rc;
4881 }
4882 EXPORT_SYMBOL_GPL(qeth_vm_request_mac);
4883 
4884 static int qeth_get_qdio_q_format(struct qeth_card *card)
4885 {
4886 	if (card->info.type == QETH_CARD_TYPE_IQD)
4887 		return QDIO_IQDIO_QFMT;
4888 	else
4889 		return QDIO_QETH_QFMT;
4890 }
4891 
4892 static void qeth_determine_capabilities(struct qeth_card *card)
4893 {
4894 	int rc;
4895 	int length;
4896 	char *prcd;
4897 	struct ccw_device *ddev;
4898 	int ddev_offline = 0;
4899 
4900 	QETH_DBF_TEXT(SETUP, 2, "detcapab");
4901 	ddev = CARD_DDEV(card);
4902 	if (!ddev->online) {
4903 		ddev_offline = 1;
4904 		rc = ccw_device_set_online(ddev);
4905 		if (rc) {
4906 			QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4907 			goto out;
4908 		}
4909 	}
4910 
4911 	rc = qeth_read_conf_data(card, (void **) &prcd, &length);
4912 	if (rc) {
4913 		QETH_DBF_MESSAGE(2, "%s qeth_read_conf_data returned %i\n",
4914 			dev_name(&card->gdev->dev), rc);
4915 		QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
4916 		goto out_offline;
4917 	}
4918 	qeth_configure_unitaddr(card, prcd);
4919 	if (ddev_offline)
4920 		qeth_configure_blkt_default(card, prcd);
4921 	kfree(prcd);
4922 
4923 	rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
4924 	if (rc)
4925 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
4926 
4927 	QETH_DBF_TEXT_(SETUP, 2, "qfmt%d", card->ssqd.qfmt);
4928 	QETH_DBF_TEXT_(SETUP, 2, "ac1:%02x", card->ssqd.qdioac1);
4929 	QETH_DBF_TEXT_(SETUP, 2, "ac2:%04x", card->ssqd.qdioac2);
4930 	QETH_DBF_TEXT_(SETUP, 2, "ac3:%04x", card->ssqd.qdioac3);
4931 	QETH_DBF_TEXT_(SETUP, 2, "icnt%d", card->ssqd.icnt);
4932 	if (!((card->ssqd.qfmt != QDIO_IQDIO_QFMT) ||
4933 	    ((card->ssqd.qdioac1 & CHSC_AC1_INITIATE_INPUTQ) == 0) ||
4934 	    ((card->ssqd.qdioac3 & CHSC_AC3_FORMAT2_CQ_AVAILABLE) == 0))) {
4935 		dev_info(&card->gdev->dev,
4936 			"Completion Queueing supported\n");
4937 	} else {
4938 		card->options.cq = QETH_CQ_NOTAVAILABLE;
4939 	}
4940 
4941 
4942 out_offline:
4943 	if (ddev_offline == 1)
4944 		ccw_device_set_offline(ddev);
4945 out:
4946 	return;
4947 }
4948 
4949 static void qeth_qdio_establish_cq(struct qeth_card *card,
4950 				   struct qdio_buffer **in_sbal_ptrs,
4951 				   void (**queue_start_poll)
4952 					(struct ccw_device *, int,
4953 					 unsigned long))
4954 {
4955 	int i;
4956 
4957 	if (card->options.cq == QETH_CQ_ENABLED) {
4958 		int offset = QDIO_MAX_BUFFERS_PER_Q *
4959 			     (card->qdio.no_in_queues - 1);
4960 		for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4961 			in_sbal_ptrs[offset + i] = (struct qdio_buffer *)
4962 				virt_to_phys(card->qdio.c_q->bufs[i].buffer);
4963 		}
4964 
4965 		queue_start_poll[card->qdio.no_in_queues - 1] = NULL;
4966 	}
4967 }
4968 
4969 static int qeth_qdio_establish(struct qeth_card *card)
4970 {
4971 	struct qdio_initialize init_data;
4972 	char *qib_param_field;
4973 	struct qdio_buffer **in_sbal_ptrs;
4974 	void (**queue_start_poll) (struct ccw_device *, int, unsigned long);
4975 	struct qdio_buffer **out_sbal_ptrs;
4976 	int i, j, k;
4977 	int rc = 0;
4978 
4979 	QETH_DBF_TEXT(SETUP, 2, "qdioest");
4980 
4981 	qib_param_field = kzalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
4982 			      GFP_KERNEL);
4983 	if (!qib_param_field) {
4984 		rc =  -ENOMEM;
4985 		goto out_free_nothing;
4986 	}
4987 
4988 	qeth_create_qib_param_field(card, qib_param_field);
4989 	qeth_create_qib_param_field_blkt(card, qib_param_field);
4990 
4991 	in_sbal_ptrs = kzalloc(card->qdio.no_in_queues *
4992 			       QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
4993 			       GFP_KERNEL);
4994 	if (!in_sbal_ptrs) {
4995 		rc = -ENOMEM;
4996 		goto out_free_qib_param;
4997 	}
4998 	for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4999 		in_sbal_ptrs[i] = (struct qdio_buffer *)
5000 			virt_to_phys(card->qdio.in_q->bufs[i].buffer);
5001 	}
5002 
5003 	queue_start_poll = kzalloc(sizeof(void *) * card->qdio.no_in_queues,
5004 				   GFP_KERNEL);
5005 	if (!queue_start_poll) {
5006 		rc = -ENOMEM;
5007 		goto out_free_in_sbals;
5008 	}
5009 	for (i = 0; i < card->qdio.no_in_queues; ++i)
5010 		queue_start_poll[i] = card->discipline->start_poll;
5011 
5012 	qeth_qdio_establish_cq(card, in_sbal_ptrs, queue_start_poll);
5013 
5014 	out_sbal_ptrs =
5015 		kzalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
5016 			sizeof(void *), GFP_KERNEL);
5017 	if (!out_sbal_ptrs) {
5018 		rc = -ENOMEM;
5019 		goto out_free_queue_start_poll;
5020 	}
5021 	for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
5022 		for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k) {
5023 			out_sbal_ptrs[k] = (struct qdio_buffer *)virt_to_phys(
5024 				card->qdio.out_qs[i]->bufs[j]->buffer);
5025 		}
5026 
5027 	memset(&init_data, 0, sizeof(struct qdio_initialize));
5028 	init_data.cdev                   = CARD_DDEV(card);
5029 	init_data.q_format               = qeth_get_qdio_q_format(card);
5030 	init_data.qib_param_field_format = 0;
5031 	init_data.qib_param_field        = qib_param_field;
5032 	init_data.no_input_qs            = card->qdio.no_in_queues;
5033 	init_data.no_output_qs           = card->qdio.no_out_queues;
5034 	init_data.input_handler 	 = card->discipline->input_handler;
5035 	init_data.output_handler	 = card->discipline->output_handler;
5036 	init_data.queue_start_poll_array = queue_start_poll;
5037 	init_data.int_parm               = (unsigned long) card;
5038 	init_data.input_sbal_addr_array  = (void **) in_sbal_ptrs;
5039 	init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
5040 	init_data.output_sbal_state_array = card->qdio.out_bufstates;
5041 	init_data.scan_threshold =
5042 		(card->info.type == QETH_CARD_TYPE_IQD) ? 1 : 32;
5043 
5044 	if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
5045 		QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
5046 		rc = qdio_allocate(&init_data);
5047 		if (rc) {
5048 			atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5049 			goto out;
5050 		}
5051 		rc = qdio_establish(&init_data);
5052 		if (rc) {
5053 			atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5054 			qdio_free(CARD_DDEV(card));
5055 		}
5056 	}
5057 
5058 	switch (card->options.cq) {
5059 	case QETH_CQ_ENABLED:
5060 		dev_info(&card->gdev->dev, "Completion Queue support enabled");
5061 		break;
5062 	case QETH_CQ_DISABLED:
5063 		dev_info(&card->gdev->dev, "Completion Queue support disabled");
5064 		break;
5065 	default:
5066 		break;
5067 	}
5068 out:
5069 	kfree(out_sbal_ptrs);
5070 out_free_queue_start_poll:
5071 	kfree(queue_start_poll);
5072 out_free_in_sbals:
5073 	kfree(in_sbal_ptrs);
5074 out_free_qib_param:
5075 	kfree(qib_param_field);
5076 out_free_nothing:
5077 	return rc;
5078 }
5079 
5080 static void qeth_core_free_card(struct qeth_card *card)
5081 {
5082 
5083 	QETH_DBF_TEXT(SETUP, 2, "freecrd");
5084 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
5085 	qeth_clean_channel(&card->read);
5086 	qeth_clean_channel(&card->write);
5087 	if (card->dev)
5088 		free_netdev(card->dev);
5089 	qeth_free_qdio_buffers(card);
5090 	unregister_service_level(&card->qeth_service_level);
5091 	kfree(card);
5092 }
5093 
5094 void qeth_trace_features(struct qeth_card *card)
5095 {
5096 	QETH_CARD_TEXT(card, 2, "features");
5097 	QETH_CARD_HEX(card, 2, &card->options.ipa4, sizeof(card->options.ipa4));
5098 	QETH_CARD_HEX(card, 2, &card->options.ipa6, sizeof(card->options.ipa6));
5099 	QETH_CARD_HEX(card, 2, &card->options.adp, sizeof(card->options.adp));
5100 	QETH_CARD_HEX(card, 2, &card->info.diagass_support,
5101 		      sizeof(card->info.diagass_support));
5102 }
5103 EXPORT_SYMBOL_GPL(qeth_trace_features);
5104 
5105 static struct ccw_device_id qeth_ids[] = {
5106 	{CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
5107 					.driver_info = QETH_CARD_TYPE_OSD},
5108 	{CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
5109 					.driver_info = QETH_CARD_TYPE_IQD},
5110 	{CCW_DEVICE_DEVTYPE(0x1731, 0x06, 0x1732, 0x06),
5111 					.driver_info = QETH_CARD_TYPE_OSN},
5112 	{CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
5113 					.driver_info = QETH_CARD_TYPE_OSM},
5114 	{CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
5115 					.driver_info = QETH_CARD_TYPE_OSX},
5116 	{},
5117 };
5118 MODULE_DEVICE_TABLE(ccw, qeth_ids);
5119 
5120 static struct ccw_driver qeth_ccw_driver = {
5121 	.driver = {
5122 		.owner = THIS_MODULE,
5123 		.name = "qeth",
5124 	},
5125 	.ids = qeth_ids,
5126 	.probe = ccwgroup_probe_ccwdev,
5127 	.remove = ccwgroup_remove_ccwdev,
5128 };
5129 
5130 int qeth_core_hardsetup_card(struct qeth_card *card)
5131 {
5132 	int retries = 3;
5133 	int rc;
5134 
5135 	QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
5136 	atomic_set(&card->force_alloc_skb, 0);
5137 	qeth_update_from_chp_desc(card);
5138 retry:
5139 	if (retries < 3)
5140 		QETH_DBF_MESSAGE(2, "%s Retrying to do IDX activates.\n",
5141 			dev_name(&card->gdev->dev));
5142 	rc = qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
5143 	ccw_device_set_offline(CARD_DDEV(card));
5144 	ccw_device_set_offline(CARD_WDEV(card));
5145 	ccw_device_set_offline(CARD_RDEV(card));
5146 	qdio_free(CARD_DDEV(card));
5147 	rc = ccw_device_set_online(CARD_RDEV(card));
5148 	if (rc)
5149 		goto retriable;
5150 	rc = ccw_device_set_online(CARD_WDEV(card));
5151 	if (rc)
5152 		goto retriable;
5153 	rc = ccw_device_set_online(CARD_DDEV(card));
5154 	if (rc)
5155 		goto retriable;
5156 retriable:
5157 	if (rc == -ERESTARTSYS) {
5158 		QETH_DBF_TEXT(SETUP, 2, "break1");
5159 		return rc;
5160 	} else if (rc) {
5161 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
5162 		if (--retries < 0)
5163 			goto out;
5164 		else
5165 			goto retry;
5166 	}
5167 	qeth_determine_capabilities(card);
5168 	qeth_init_tokens(card);
5169 	qeth_init_func_level(card);
5170 	rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
5171 	if (rc == -ERESTARTSYS) {
5172 		QETH_DBF_TEXT(SETUP, 2, "break2");
5173 		return rc;
5174 	} else if (rc) {
5175 		QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
5176 		if (--retries < 0)
5177 			goto out;
5178 		else
5179 			goto retry;
5180 	}
5181 	rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
5182 	if (rc == -ERESTARTSYS) {
5183 		QETH_DBF_TEXT(SETUP, 2, "break3");
5184 		return rc;
5185 	} else if (rc) {
5186 		QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
5187 		if (--retries < 0)
5188 			goto out;
5189 		else
5190 			goto retry;
5191 	}
5192 	card->read_or_write_problem = 0;
5193 	rc = qeth_mpc_initialize(card);
5194 	if (rc) {
5195 		QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
5196 		goto out;
5197 	}
5198 
5199 	rc = qeth_send_startlan(card);
5200 	if (rc) {
5201 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
5202 		if (rc == IPA_RC_LAN_OFFLINE) {
5203 			dev_warn(&card->gdev->dev,
5204 				"The LAN is offline\n");
5205 			card->lan_online = 0;
5206 		} else {
5207 			rc = -ENODEV;
5208 			goto out;
5209 		}
5210 	} else
5211 		card->lan_online = 1;
5212 
5213 	card->options.ipa4.supported_funcs = 0;
5214 	card->options.ipa6.supported_funcs = 0;
5215 	card->options.adp.supported_funcs = 0;
5216 	card->options.sbp.supported_funcs = 0;
5217 	card->info.diagass_support = 0;
5218 	rc = qeth_query_ipassists(card, QETH_PROT_IPV4);
5219 	if (rc == -ENOMEM)
5220 		goto out;
5221 	if (qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
5222 		rc = qeth_query_setadapterparms(card);
5223 		if (rc < 0) {
5224 			QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
5225 			goto out;
5226 		}
5227 	}
5228 	if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
5229 		rc = qeth_query_setdiagass(card);
5230 		if (rc < 0) {
5231 			QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
5232 			goto out;
5233 		}
5234 	}
5235 	return 0;
5236 out:
5237 	dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
5238 		"an error on the device\n");
5239 	QETH_DBF_MESSAGE(2, "%s Initialization in hardsetup failed! rc=%d\n",
5240 		dev_name(&card->gdev->dev), rc);
5241 	return rc;
5242 }
5243 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
5244 
5245 static void qeth_create_skb_frag(struct qdio_buffer_element *element,
5246 				 struct sk_buff *skb, int offset, int data_len)
5247 {
5248 	struct page *page = virt_to_page(element->addr);
5249 	unsigned int next_frag;
5250 
5251 	/* first fill the linear space */
5252 	if (!skb->len) {
5253 		unsigned int linear = min(data_len, skb_tailroom(skb));
5254 
5255 		skb_put_data(skb, element->addr + offset, linear);
5256 		data_len -= linear;
5257 		if (!data_len)
5258 			return;
5259 		offset += linear;
5260 		/* fall through to add page frag for remaining data */
5261 	}
5262 
5263 	next_frag = skb_shinfo(skb)->nr_frags;
5264 	get_page(page);
5265 	skb_add_rx_frag(skb, next_frag, page, offset, data_len, data_len);
5266 }
5267 
5268 static inline int qeth_is_last_sbale(struct qdio_buffer_element *sbale)
5269 {
5270 	return (sbale->eflags & SBAL_EFLAGS_LAST_ENTRY);
5271 }
5272 
5273 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
5274 		struct qeth_qdio_buffer *qethbuffer,
5275 		struct qdio_buffer_element **__element, int *__offset,
5276 		struct qeth_hdr **hdr)
5277 {
5278 	struct qdio_buffer_element *element = *__element;
5279 	struct qdio_buffer *buffer = qethbuffer->buffer;
5280 	int offset = *__offset;
5281 	struct sk_buff *skb;
5282 	int skb_len = 0;
5283 	void *data_ptr;
5284 	int data_len;
5285 	int headroom = 0;
5286 	int use_rx_sg = 0;
5287 
5288 	/* qeth_hdr must not cross element boundaries */
5289 	while (element->length < offset + sizeof(struct qeth_hdr)) {
5290 		if (qeth_is_last_sbale(element))
5291 			return NULL;
5292 		element++;
5293 		offset = 0;
5294 	}
5295 	*hdr = element->addr + offset;
5296 
5297 	offset += sizeof(struct qeth_hdr);
5298 	switch ((*hdr)->hdr.l2.id) {
5299 	case QETH_HEADER_TYPE_LAYER2:
5300 		skb_len = (*hdr)->hdr.l2.pkt_length;
5301 		break;
5302 	case QETH_HEADER_TYPE_LAYER3:
5303 		skb_len = (*hdr)->hdr.l3.length;
5304 		headroom = ETH_HLEN;
5305 		break;
5306 	case QETH_HEADER_TYPE_OSN:
5307 		skb_len = (*hdr)->hdr.osn.pdu_length;
5308 		headroom = sizeof(struct qeth_hdr);
5309 		break;
5310 	default:
5311 		break;
5312 	}
5313 
5314 	if (!skb_len)
5315 		return NULL;
5316 
5317 	if (((skb_len >= card->options.rx_sg_cb) &&
5318 	     (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
5319 	     (!atomic_read(&card->force_alloc_skb))) ||
5320 	    (card->options.cq == QETH_CQ_ENABLED))
5321 		use_rx_sg = 1;
5322 
5323 	if (use_rx_sg && qethbuffer->rx_skb) {
5324 		/* QETH_CQ_ENABLED only: */
5325 		skb = qethbuffer->rx_skb;
5326 		qethbuffer->rx_skb = NULL;
5327 	} else {
5328 		unsigned int linear = (use_rx_sg) ? QETH_RX_PULL_LEN : skb_len;
5329 
5330 		skb = dev_alloc_skb(linear + headroom);
5331 	}
5332 	if (!skb)
5333 		goto no_mem;
5334 	if (headroom)
5335 		skb_reserve(skb, headroom);
5336 
5337 	data_ptr = element->addr + offset;
5338 	while (skb_len) {
5339 		data_len = min(skb_len, (int)(element->length - offset));
5340 		if (data_len) {
5341 			if (use_rx_sg)
5342 				qeth_create_skb_frag(element, skb, offset,
5343 						     data_len);
5344 			else
5345 				skb_put_data(skb, data_ptr, data_len);
5346 		}
5347 		skb_len -= data_len;
5348 		if (skb_len) {
5349 			if (qeth_is_last_sbale(element)) {
5350 				QETH_CARD_TEXT(card, 4, "unexeob");
5351 				QETH_CARD_HEX(card, 2, buffer, sizeof(void *));
5352 				dev_kfree_skb_any(skb);
5353 				card->stats.rx_errors++;
5354 				return NULL;
5355 			}
5356 			element++;
5357 			offset = 0;
5358 			data_ptr = element->addr;
5359 		} else {
5360 			offset += data_len;
5361 		}
5362 	}
5363 	*__element = element;
5364 	*__offset = offset;
5365 	if (use_rx_sg && card->options.performance_stats) {
5366 		card->perf_stats.sg_skbs_rx++;
5367 		card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
5368 	}
5369 	return skb;
5370 no_mem:
5371 	if (net_ratelimit()) {
5372 		QETH_CARD_TEXT(card, 2, "noskbmem");
5373 	}
5374 	card->stats.rx_dropped++;
5375 	return NULL;
5376 }
5377 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
5378 
5379 int qeth_poll(struct napi_struct *napi, int budget)
5380 {
5381 	struct qeth_card *card = container_of(napi, struct qeth_card, napi);
5382 	int work_done = 0;
5383 	struct qeth_qdio_buffer *buffer;
5384 	int done;
5385 	int new_budget = budget;
5386 
5387 	if (card->options.performance_stats) {
5388 		card->perf_stats.inbound_cnt++;
5389 		card->perf_stats.inbound_start_time = qeth_get_micros();
5390 	}
5391 
5392 	while (1) {
5393 		if (!card->rx.b_count) {
5394 			card->rx.qdio_err = 0;
5395 			card->rx.b_count = qdio_get_next_buffers(
5396 				card->data.ccwdev, 0, &card->rx.b_index,
5397 				&card->rx.qdio_err);
5398 			if (card->rx.b_count <= 0) {
5399 				card->rx.b_count = 0;
5400 				break;
5401 			}
5402 			card->rx.b_element =
5403 				&card->qdio.in_q->bufs[card->rx.b_index]
5404 				.buffer->element[0];
5405 			card->rx.e_offset = 0;
5406 		}
5407 
5408 		while (card->rx.b_count) {
5409 			buffer = &card->qdio.in_q->bufs[card->rx.b_index];
5410 			if (!(card->rx.qdio_err &&
5411 			    qeth_check_qdio_errors(card, buffer->buffer,
5412 			    card->rx.qdio_err, "qinerr")))
5413 				work_done +=
5414 					card->discipline->process_rx_buffer(
5415 						card, new_budget, &done);
5416 			else
5417 				done = 1;
5418 
5419 			if (done) {
5420 				if (card->options.performance_stats)
5421 					card->perf_stats.bufs_rec++;
5422 				qeth_put_buffer_pool_entry(card,
5423 					buffer->pool_entry);
5424 				qeth_queue_input_buffer(card, card->rx.b_index);
5425 				card->rx.b_count--;
5426 				if (card->rx.b_count) {
5427 					card->rx.b_index =
5428 						(card->rx.b_index + 1) %
5429 						QDIO_MAX_BUFFERS_PER_Q;
5430 					card->rx.b_element =
5431 						&card->qdio.in_q
5432 						->bufs[card->rx.b_index]
5433 						.buffer->element[0];
5434 					card->rx.e_offset = 0;
5435 				}
5436 			}
5437 
5438 			if (work_done >= budget)
5439 				goto out;
5440 			else
5441 				new_budget = budget - work_done;
5442 		}
5443 	}
5444 
5445 	napi_complete_done(napi, work_done);
5446 	if (qdio_start_irq(card->data.ccwdev, 0))
5447 		napi_schedule(&card->napi);
5448 out:
5449 	if (card->options.performance_stats)
5450 		card->perf_stats.inbound_time += qeth_get_micros() -
5451 			card->perf_stats.inbound_start_time;
5452 	return work_done;
5453 }
5454 EXPORT_SYMBOL_GPL(qeth_poll);
5455 
5456 static int qeth_setassparms_inspect_rc(struct qeth_ipa_cmd *cmd)
5457 {
5458 	if (!cmd->hdr.return_code)
5459 		cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5460 	return cmd->hdr.return_code;
5461 }
5462 
5463 int qeth_setassparms_cb(struct qeth_card *card,
5464 			struct qeth_reply *reply, unsigned long data)
5465 {
5466 	struct qeth_ipa_cmd *cmd;
5467 
5468 	QETH_CARD_TEXT(card, 4, "defadpcb");
5469 
5470 	cmd = (struct qeth_ipa_cmd *) data;
5471 	if (cmd->hdr.return_code == 0) {
5472 		cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5473 		if (cmd->hdr.prot_version == QETH_PROT_IPV4)
5474 			card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
5475 		if (cmd->hdr.prot_version == QETH_PROT_IPV6)
5476 			card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
5477 	}
5478 	return 0;
5479 }
5480 EXPORT_SYMBOL_GPL(qeth_setassparms_cb);
5481 
5482 struct qeth_cmd_buffer *qeth_get_setassparms_cmd(struct qeth_card *card,
5483 						 enum qeth_ipa_funcs ipa_func,
5484 						 __u16 cmd_code, __u16 len,
5485 						 enum qeth_prot_versions prot)
5486 {
5487 	struct qeth_cmd_buffer *iob;
5488 	struct qeth_ipa_cmd *cmd;
5489 
5490 	QETH_CARD_TEXT(card, 4, "getasscm");
5491 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETASSPARMS, prot);
5492 
5493 	if (iob) {
5494 		cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5495 		cmd->data.setassparms.hdr.assist_no = ipa_func;
5496 		cmd->data.setassparms.hdr.length = 8 + len;
5497 		cmd->data.setassparms.hdr.command_code = cmd_code;
5498 		cmd->data.setassparms.hdr.return_code = 0;
5499 		cmd->data.setassparms.hdr.seq_no = 0;
5500 	}
5501 
5502 	return iob;
5503 }
5504 EXPORT_SYMBOL_GPL(qeth_get_setassparms_cmd);
5505 
5506 int qeth_send_setassparms(struct qeth_card *card,
5507 			  struct qeth_cmd_buffer *iob, __u16 len, long data,
5508 			  int (*reply_cb)(struct qeth_card *,
5509 					  struct qeth_reply *, unsigned long),
5510 			  void *reply_param)
5511 {
5512 	int rc;
5513 	struct qeth_ipa_cmd *cmd;
5514 
5515 	QETH_CARD_TEXT(card, 4, "sendassp");
5516 
5517 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5518 	if (len <= sizeof(__u32))
5519 		cmd->data.setassparms.data.flags_32bit = (__u32) data;
5520 	else   /* (len > sizeof(__u32)) */
5521 		memcpy(&cmd->data.setassparms.data, (void *) data, len);
5522 
5523 	rc = qeth_send_ipa_cmd(card, iob, reply_cb, reply_param);
5524 	return rc;
5525 }
5526 EXPORT_SYMBOL_GPL(qeth_send_setassparms);
5527 
5528 int qeth_send_simple_setassparms(struct qeth_card *card,
5529 				 enum qeth_ipa_funcs ipa_func,
5530 				 __u16 cmd_code, long data)
5531 {
5532 	int rc;
5533 	int length = 0;
5534 	struct qeth_cmd_buffer *iob;
5535 
5536 	QETH_CARD_TEXT(card, 4, "simassp4");
5537 	if (data)
5538 		length = sizeof(__u32);
5539 	iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
5540 				       length, QETH_PROT_IPV4);
5541 	if (!iob)
5542 		return -ENOMEM;
5543 	rc = qeth_send_setassparms(card, iob, length, data,
5544 				   qeth_setassparms_cb, NULL);
5545 	return rc;
5546 }
5547 EXPORT_SYMBOL_GPL(qeth_send_simple_setassparms);
5548 
5549 static void qeth_unregister_dbf_views(void)
5550 {
5551 	int x;
5552 	for (x = 0; x < QETH_DBF_INFOS; x++) {
5553 		debug_unregister(qeth_dbf[x].id);
5554 		qeth_dbf[x].id = NULL;
5555 	}
5556 }
5557 
5558 void qeth_dbf_longtext(debug_info_t *id, int level, char *fmt, ...)
5559 {
5560 	char dbf_txt_buf[32];
5561 	va_list args;
5562 
5563 	if (!debug_level_enabled(id, level))
5564 		return;
5565 	va_start(args, fmt);
5566 	vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
5567 	va_end(args);
5568 	debug_text_event(id, level, dbf_txt_buf);
5569 }
5570 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
5571 
5572 static int qeth_register_dbf_views(void)
5573 {
5574 	int ret;
5575 	int x;
5576 
5577 	for (x = 0; x < QETH_DBF_INFOS; x++) {
5578 		/* register the areas */
5579 		qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
5580 						qeth_dbf[x].pages,
5581 						qeth_dbf[x].areas,
5582 						qeth_dbf[x].len);
5583 		if (qeth_dbf[x].id == NULL) {
5584 			qeth_unregister_dbf_views();
5585 			return -ENOMEM;
5586 		}
5587 
5588 		/* register a view */
5589 		ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
5590 		if (ret) {
5591 			qeth_unregister_dbf_views();
5592 			return ret;
5593 		}
5594 
5595 		/* set a passing level */
5596 		debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
5597 	}
5598 
5599 	return 0;
5600 }
5601 
5602 int qeth_core_load_discipline(struct qeth_card *card,
5603 		enum qeth_discipline_id discipline)
5604 {
5605 	int rc = 0;
5606 
5607 	mutex_lock(&qeth_mod_mutex);
5608 	switch (discipline) {
5609 	case QETH_DISCIPLINE_LAYER3:
5610 		card->discipline = try_then_request_module(
5611 			symbol_get(qeth_l3_discipline), "qeth_l3");
5612 		break;
5613 	case QETH_DISCIPLINE_LAYER2:
5614 		card->discipline = try_then_request_module(
5615 			symbol_get(qeth_l2_discipline), "qeth_l2");
5616 		break;
5617 	default:
5618 		break;
5619 	}
5620 
5621 	if (!card->discipline) {
5622 		dev_err(&card->gdev->dev, "There is no kernel module to "
5623 			"support discipline %d\n", discipline);
5624 		rc = -EINVAL;
5625 	}
5626 	mutex_unlock(&qeth_mod_mutex);
5627 	return rc;
5628 }
5629 
5630 void qeth_core_free_discipline(struct qeth_card *card)
5631 {
5632 	if (card->options.layer2)
5633 		symbol_put(qeth_l2_discipline);
5634 	else
5635 		symbol_put(qeth_l3_discipline);
5636 	card->discipline = NULL;
5637 }
5638 
5639 const struct device_type qeth_generic_devtype = {
5640 	.name = "qeth_generic",
5641 	.groups = qeth_generic_attr_groups,
5642 };
5643 EXPORT_SYMBOL_GPL(qeth_generic_devtype);
5644 
5645 static const struct device_type qeth_osn_devtype = {
5646 	.name = "qeth_osn",
5647 	.groups = qeth_osn_attr_groups,
5648 };
5649 
5650 #define DBF_NAME_LEN	20
5651 
5652 struct qeth_dbf_entry {
5653 	char dbf_name[DBF_NAME_LEN];
5654 	debug_info_t *dbf_info;
5655 	struct list_head dbf_list;
5656 };
5657 
5658 static LIST_HEAD(qeth_dbf_list);
5659 static DEFINE_MUTEX(qeth_dbf_list_mutex);
5660 
5661 static debug_info_t *qeth_get_dbf_entry(char *name)
5662 {
5663 	struct qeth_dbf_entry *entry;
5664 	debug_info_t *rc = NULL;
5665 
5666 	mutex_lock(&qeth_dbf_list_mutex);
5667 	list_for_each_entry(entry, &qeth_dbf_list, dbf_list) {
5668 		if (strcmp(entry->dbf_name, name) == 0) {
5669 			rc = entry->dbf_info;
5670 			break;
5671 		}
5672 	}
5673 	mutex_unlock(&qeth_dbf_list_mutex);
5674 	return rc;
5675 }
5676 
5677 static int qeth_add_dbf_entry(struct qeth_card *card, char *name)
5678 {
5679 	struct qeth_dbf_entry *new_entry;
5680 
5681 	card->debug = debug_register(name, 2, 1, 8);
5682 	if (!card->debug) {
5683 		QETH_DBF_TEXT_(SETUP, 2, "%s", "qcdbf");
5684 		goto err;
5685 	}
5686 	if (debug_register_view(card->debug, &debug_hex_ascii_view))
5687 		goto err_dbg;
5688 	new_entry = kzalloc(sizeof(struct qeth_dbf_entry), GFP_KERNEL);
5689 	if (!new_entry)
5690 		goto err_dbg;
5691 	strncpy(new_entry->dbf_name, name, DBF_NAME_LEN);
5692 	new_entry->dbf_info = card->debug;
5693 	mutex_lock(&qeth_dbf_list_mutex);
5694 	list_add(&new_entry->dbf_list, &qeth_dbf_list);
5695 	mutex_unlock(&qeth_dbf_list_mutex);
5696 
5697 	return 0;
5698 
5699 err_dbg:
5700 	debug_unregister(card->debug);
5701 err:
5702 	return -ENOMEM;
5703 }
5704 
5705 static void qeth_clear_dbf_list(void)
5706 {
5707 	struct qeth_dbf_entry *entry, *tmp;
5708 
5709 	mutex_lock(&qeth_dbf_list_mutex);
5710 	list_for_each_entry_safe(entry, tmp, &qeth_dbf_list, dbf_list) {
5711 		list_del(&entry->dbf_list);
5712 		debug_unregister(entry->dbf_info);
5713 		kfree(entry);
5714 	}
5715 	mutex_unlock(&qeth_dbf_list_mutex);
5716 }
5717 
5718 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
5719 {
5720 	struct qeth_card *card;
5721 	struct device *dev;
5722 	int rc;
5723 	enum qeth_discipline_id enforced_disc;
5724 	unsigned long flags;
5725 	char dbf_name[DBF_NAME_LEN];
5726 
5727 	QETH_DBF_TEXT(SETUP, 2, "probedev");
5728 
5729 	dev = &gdev->dev;
5730 	if (!get_device(dev))
5731 		return -ENODEV;
5732 
5733 	QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
5734 
5735 	card = qeth_alloc_card();
5736 	if (!card) {
5737 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
5738 		rc = -ENOMEM;
5739 		goto err_dev;
5740 	}
5741 
5742 	snprintf(dbf_name, sizeof(dbf_name), "qeth_card_%s",
5743 		dev_name(&gdev->dev));
5744 	card->debug = qeth_get_dbf_entry(dbf_name);
5745 	if (!card->debug) {
5746 		rc = qeth_add_dbf_entry(card, dbf_name);
5747 		if (rc)
5748 			goto err_card;
5749 	}
5750 
5751 	card->read.ccwdev  = gdev->cdev[0];
5752 	card->write.ccwdev = gdev->cdev[1];
5753 	card->data.ccwdev  = gdev->cdev[2];
5754 	dev_set_drvdata(&gdev->dev, card);
5755 	card->gdev = gdev;
5756 	gdev->cdev[0]->handler = qeth_irq;
5757 	gdev->cdev[1]->handler = qeth_irq;
5758 	gdev->cdev[2]->handler = qeth_irq;
5759 
5760 	qeth_determine_card_type(card);
5761 	rc = qeth_setup_card(card);
5762 	if (rc) {
5763 		QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
5764 		goto err_card;
5765 	}
5766 
5767 	qeth_determine_capabilities(card);
5768 	enforced_disc = qeth_enforce_discipline(card);
5769 	switch (enforced_disc) {
5770 	case QETH_DISCIPLINE_UNDETERMINED:
5771 		gdev->dev.type = &qeth_generic_devtype;
5772 		break;
5773 	default:
5774 		card->info.layer_enforced = true;
5775 		rc = qeth_core_load_discipline(card, enforced_disc);
5776 		if (rc)
5777 			goto err_card;
5778 
5779 		gdev->dev.type = (card->info.type != QETH_CARD_TYPE_OSN)
5780 					? card->discipline->devtype
5781 					: &qeth_osn_devtype;
5782 		rc = card->discipline->setup(card->gdev);
5783 		if (rc)
5784 			goto err_disc;
5785 		break;
5786 	}
5787 
5788 	write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5789 	list_add_tail(&card->list, &qeth_core_card_list.list);
5790 	write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5791 	return 0;
5792 
5793 err_disc:
5794 	qeth_core_free_discipline(card);
5795 err_card:
5796 	qeth_core_free_card(card);
5797 err_dev:
5798 	put_device(dev);
5799 	return rc;
5800 }
5801 
5802 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
5803 {
5804 	unsigned long flags;
5805 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5806 
5807 	QETH_DBF_TEXT(SETUP, 2, "removedv");
5808 
5809 	if (card->discipline) {
5810 		card->discipline->remove(gdev);
5811 		qeth_core_free_discipline(card);
5812 	}
5813 
5814 	write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5815 	list_del(&card->list);
5816 	write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5817 	qeth_core_free_card(card);
5818 	dev_set_drvdata(&gdev->dev, NULL);
5819 	put_device(&gdev->dev);
5820 	return;
5821 }
5822 
5823 static int qeth_core_set_online(struct ccwgroup_device *gdev)
5824 {
5825 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5826 	int rc = 0;
5827 	enum qeth_discipline_id def_discipline;
5828 
5829 	if (!card->discipline) {
5830 		if (card->info.type == QETH_CARD_TYPE_IQD)
5831 			def_discipline = QETH_DISCIPLINE_LAYER3;
5832 		else
5833 			def_discipline = QETH_DISCIPLINE_LAYER2;
5834 		rc = qeth_core_load_discipline(card, def_discipline);
5835 		if (rc)
5836 			goto err;
5837 		rc = card->discipline->setup(card->gdev);
5838 		if (rc) {
5839 			qeth_core_free_discipline(card);
5840 			goto err;
5841 		}
5842 	}
5843 	rc = card->discipline->set_online(gdev);
5844 err:
5845 	return rc;
5846 }
5847 
5848 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
5849 {
5850 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5851 	return card->discipline->set_offline(gdev);
5852 }
5853 
5854 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
5855 {
5856 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5857 	qeth_set_allowed_threads(card, 0, 1);
5858 	if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
5859 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
5860 	qeth_qdio_clear_card(card, 0);
5861 	qeth_clear_qdio_buffers(card);
5862 	qdio_free(CARD_DDEV(card));
5863 }
5864 
5865 static int qeth_core_freeze(struct ccwgroup_device *gdev)
5866 {
5867 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5868 	if (card->discipline && card->discipline->freeze)
5869 		return card->discipline->freeze(gdev);
5870 	return 0;
5871 }
5872 
5873 static int qeth_core_thaw(struct ccwgroup_device *gdev)
5874 {
5875 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5876 	if (card->discipline && card->discipline->thaw)
5877 		return card->discipline->thaw(gdev);
5878 	return 0;
5879 }
5880 
5881 static int qeth_core_restore(struct ccwgroup_device *gdev)
5882 {
5883 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5884 	if (card->discipline && card->discipline->restore)
5885 		return card->discipline->restore(gdev);
5886 	return 0;
5887 }
5888 
5889 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
5890 	.driver = {
5891 		.owner = THIS_MODULE,
5892 		.name = "qeth",
5893 	},
5894 	.ccw_driver = &qeth_ccw_driver,
5895 	.setup = qeth_core_probe_device,
5896 	.remove = qeth_core_remove_device,
5897 	.set_online = qeth_core_set_online,
5898 	.set_offline = qeth_core_set_offline,
5899 	.shutdown = qeth_core_shutdown,
5900 	.prepare = NULL,
5901 	.complete = NULL,
5902 	.freeze = qeth_core_freeze,
5903 	.thaw = qeth_core_thaw,
5904 	.restore = qeth_core_restore,
5905 };
5906 
5907 static ssize_t group_store(struct device_driver *ddrv, const char *buf,
5908 			   size_t count)
5909 {
5910 	int err;
5911 
5912 	err = ccwgroup_create_dev(qeth_core_root_dev,
5913 				  &qeth_core_ccwgroup_driver, 3, buf);
5914 
5915 	return err ? err : count;
5916 }
5917 static DRIVER_ATTR_WO(group);
5918 
5919 static struct attribute *qeth_drv_attrs[] = {
5920 	&driver_attr_group.attr,
5921 	NULL,
5922 };
5923 static struct attribute_group qeth_drv_attr_group = {
5924 	.attrs = qeth_drv_attrs,
5925 };
5926 static const struct attribute_group *qeth_drv_attr_groups[] = {
5927 	&qeth_drv_attr_group,
5928 	NULL,
5929 };
5930 
5931 int qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5932 {
5933 	struct qeth_card *card = dev->ml_priv;
5934 	struct mii_ioctl_data *mii_data;
5935 	int rc = 0;
5936 
5937 	if (!card)
5938 		return -ENODEV;
5939 
5940 	if (!qeth_card_hw_is_reachable(card))
5941 		return -ENODEV;
5942 
5943 	if (card->info.type == QETH_CARD_TYPE_OSN)
5944 		return -EPERM;
5945 
5946 	switch (cmd) {
5947 	case SIOC_QETH_ADP_SET_SNMP_CONTROL:
5948 		rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
5949 		break;
5950 	case SIOC_QETH_GET_CARD_TYPE:
5951 		if ((card->info.type == QETH_CARD_TYPE_OSD ||
5952 		     card->info.type == QETH_CARD_TYPE_OSM ||
5953 		     card->info.type == QETH_CARD_TYPE_OSX) &&
5954 		    !card->info.guestlan)
5955 			return 1;
5956 		else
5957 			return 0;
5958 	case SIOCGMIIPHY:
5959 		mii_data = if_mii(rq);
5960 		mii_data->phy_id = 0;
5961 		break;
5962 	case SIOCGMIIREG:
5963 		mii_data = if_mii(rq);
5964 		if (mii_data->phy_id != 0)
5965 			rc = -EINVAL;
5966 		else
5967 			mii_data->val_out = qeth_mdio_read(dev,
5968 				mii_data->phy_id, mii_data->reg_num);
5969 		break;
5970 	case SIOC_QETH_QUERY_OAT:
5971 		rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
5972 		break;
5973 	default:
5974 		if (card->discipline->do_ioctl)
5975 			rc = card->discipline->do_ioctl(dev, rq, cmd);
5976 		else
5977 			rc = -EOPNOTSUPP;
5978 	}
5979 	if (rc)
5980 		QETH_CARD_TEXT_(card, 2, "ioce%x", rc);
5981 	return rc;
5982 }
5983 EXPORT_SYMBOL_GPL(qeth_do_ioctl);
5984 
5985 static struct {
5986 	const char str[ETH_GSTRING_LEN];
5987 } qeth_ethtool_stats_keys[] = {
5988 /*  0 */{"rx skbs"},
5989 	{"rx buffers"},
5990 	{"tx skbs"},
5991 	{"tx buffers"},
5992 	{"tx skbs no packing"},
5993 	{"tx buffers no packing"},
5994 	{"tx skbs packing"},
5995 	{"tx buffers packing"},
5996 	{"tx sg skbs"},
5997 	{"tx sg frags"},
5998 /* 10 */{"rx sg skbs"},
5999 	{"rx sg frags"},
6000 	{"rx sg page allocs"},
6001 	{"tx large kbytes"},
6002 	{"tx large count"},
6003 	{"tx pk state ch n->p"},
6004 	{"tx pk state ch p->n"},
6005 	{"tx pk watermark low"},
6006 	{"tx pk watermark high"},
6007 	{"queue 0 buffer usage"},
6008 /* 20 */{"queue 1 buffer usage"},
6009 	{"queue 2 buffer usage"},
6010 	{"queue 3 buffer usage"},
6011 	{"rx poll time"},
6012 	{"rx poll count"},
6013 	{"rx do_QDIO time"},
6014 	{"rx do_QDIO count"},
6015 	{"tx handler time"},
6016 	{"tx handler count"},
6017 	{"tx time"},
6018 /* 30 */{"tx count"},
6019 	{"tx do_QDIO time"},
6020 	{"tx do_QDIO count"},
6021 	{"tx csum"},
6022 	{"tx lin"},
6023 	{"tx linfail"},
6024 	{"cq handler count"},
6025 	{"cq handler time"}
6026 };
6027 
6028 int qeth_core_get_sset_count(struct net_device *dev, int stringset)
6029 {
6030 	switch (stringset) {
6031 	case ETH_SS_STATS:
6032 		return (sizeof(qeth_ethtool_stats_keys) / ETH_GSTRING_LEN);
6033 	default:
6034 		return -EINVAL;
6035 	}
6036 }
6037 EXPORT_SYMBOL_GPL(qeth_core_get_sset_count);
6038 
6039 void qeth_core_get_ethtool_stats(struct net_device *dev,
6040 		struct ethtool_stats *stats, u64 *data)
6041 {
6042 	struct qeth_card *card = dev->ml_priv;
6043 	data[0] = card->stats.rx_packets -
6044 				card->perf_stats.initial_rx_packets;
6045 	data[1] = card->perf_stats.bufs_rec;
6046 	data[2] = card->stats.tx_packets -
6047 				card->perf_stats.initial_tx_packets;
6048 	data[3] = card->perf_stats.bufs_sent;
6049 	data[4] = card->stats.tx_packets - card->perf_stats.initial_tx_packets
6050 			- card->perf_stats.skbs_sent_pack;
6051 	data[5] = card->perf_stats.bufs_sent - card->perf_stats.bufs_sent_pack;
6052 	data[6] = card->perf_stats.skbs_sent_pack;
6053 	data[7] = card->perf_stats.bufs_sent_pack;
6054 	data[8] = card->perf_stats.sg_skbs_sent;
6055 	data[9] = card->perf_stats.sg_frags_sent;
6056 	data[10] = card->perf_stats.sg_skbs_rx;
6057 	data[11] = card->perf_stats.sg_frags_rx;
6058 	data[12] = card->perf_stats.sg_alloc_page_rx;
6059 	data[13] = (card->perf_stats.large_send_bytes >> 10);
6060 	data[14] = card->perf_stats.large_send_cnt;
6061 	data[15] = card->perf_stats.sc_dp_p;
6062 	data[16] = card->perf_stats.sc_p_dp;
6063 	data[17] = QETH_LOW_WATERMARK_PACK;
6064 	data[18] = QETH_HIGH_WATERMARK_PACK;
6065 	data[19] = atomic_read(&card->qdio.out_qs[0]->used_buffers);
6066 	data[20] = (card->qdio.no_out_queues > 1) ?
6067 			atomic_read(&card->qdio.out_qs[1]->used_buffers) : 0;
6068 	data[21] = (card->qdio.no_out_queues > 2) ?
6069 			atomic_read(&card->qdio.out_qs[2]->used_buffers) : 0;
6070 	data[22] = (card->qdio.no_out_queues > 3) ?
6071 			atomic_read(&card->qdio.out_qs[3]->used_buffers) : 0;
6072 	data[23] = card->perf_stats.inbound_time;
6073 	data[24] = card->perf_stats.inbound_cnt;
6074 	data[25] = card->perf_stats.inbound_do_qdio_time;
6075 	data[26] = card->perf_stats.inbound_do_qdio_cnt;
6076 	data[27] = card->perf_stats.outbound_handler_time;
6077 	data[28] = card->perf_stats.outbound_handler_cnt;
6078 	data[29] = card->perf_stats.outbound_time;
6079 	data[30] = card->perf_stats.outbound_cnt;
6080 	data[31] = card->perf_stats.outbound_do_qdio_time;
6081 	data[32] = card->perf_stats.outbound_do_qdio_cnt;
6082 	data[33] = card->perf_stats.tx_csum;
6083 	data[34] = card->perf_stats.tx_lin;
6084 	data[35] = card->perf_stats.tx_linfail;
6085 	data[36] = card->perf_stats.cq_cnt;
6086 	data[37] = card->perf_stats.cq_time;
6087 }
6088 EXPORT_SYMBOL_GPL(qeth_core_get_ethtool_stats);
6089 
6090 void qeth_core_get_strings(struct net_device *dev, u32 stringset, u8 *data)
6091 {
6092 	switch (stringset) {
6093 	case ETH_SS_STATS:
6094 		memcpy(data, &qeth_ethtool_stats_keys,
6095 			sizeof(qeth_ethtool_stats_keys));
6096 		break;
6097 	default:
6098 		WARN_ON(1);
6099 		break;
6100 	}
6101 }
6102 EXPORT_SYMBOL_GPL(qeth_core_get_strings);
6103 
6104 void qeth_core_get_drvinfo(struct net_device *dev,
6105 		struct ethtool_drvinfo *info)
6106 {
6107 	struct qeth_card *card = dev->ml_priv;
6108 
6109 	strlcpy(info->driver, card->options.layer2 ? "qeth_l2" : "qeth_l3",
6110 		sizeof(info->driver));
6111 	strlcpy(info->version, "1.0", sizeof(info->version));
6112 	strlcpy(info->fw_version, card->info.mcl_level,
6113 		sizeof(info->fw_version));
6114 	snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
6115 		 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
6116 }
6117 EXPORT_SYMBOL_GPL(qeth_core_get_drvinfo);
6118 
6119 /* Helper function to fill 'advertising' and 'supported' which are the same. */
6120 /* Autoneg and full-duplex are supported and advertised unconditionally.     */
6121 /* Always advertise and support all speeds up to specified, and only one     */
6122 /* specified port type.							     */
6123 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd,
6124 				int maxspeed, int porttype)
6125 {
6126 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
6127 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
6128 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
6129 
6130 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
6131 	ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
6132 
6133 	switch (porttype) {
6134 	case PORT_TP:
6135 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
6136 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
6137 		break;
6138 	case PORT_FIBRE:
6139 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
6140 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
6141 		break;
6142 	default:
6143 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
6144 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
6145 		WARN_ON_ONCE(1);
6146 	}
6147 
6148 	/* fallthrough from high to low, to select all legal speeds: */
6149 	switch (maxspeed) {
6150 	case SPEED_10000:
6151 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6152 						     10000baseT_Full);
6153 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6154 						     10000baseT_Full);
6155 	case SPEED_1000:
6156 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6157 						     1000baseT_Full);
6158 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6159 						     1000baseT_Full);
6160 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6161 						     1000baseT_Half);
6162 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6163 						     1000baseT_Half);
6164 	case SPEED_100:
6165 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6166 						     100baseT_Full);
6167 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6168 						     100baseT_Full);
6169 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6170 						     100baseT_Half);
6171 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6172 						     100baseT_Half);
6173 	case SPEED_10:
6174 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6175 						     10baseT_Full);
6176 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6177 						     10baseT_Full);
6178 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6179 						     10baseT_Half);
6180 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6181 						     10baseT_Half);
6182 		/* end fallthrough */
6183 		break;
6184 	default:
6185 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6186 						     10baseT_Full);
6187 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6188 						     10baseT_Full);
6189 		ethtool_link_ksettings_add_link_mode(cmd, supported,
6190 						     10baseT_Half);
6191 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
6192 						     10baseT_Half);
6193 		WARN_ON_ONCE(1);
6194 	}
6195 }
6196 
6197 int qeth_core_ethtool_get_link_ksettings(struct net_device *netdev,
6198 		struct ethtool_link_ksettings *cmd)
6199 {
6200 	struct qeth_card *card = netdev->ml_priv;
6201 	enum qeth_link_types link_type;
6202 	struct carrier_info carrier_info;
6203 	int rc;
6204 
6205 	if ((card->info.type == QETH_CARD_TYPE_IQD) || (card->info.guestlan))
6206 		link_type = QETH_LINK_TYPE_10GBIT_ETH;
6207 	else
6208 		link_type = card->info.link_type;
6209 
6210 	cmd->base.duplex = DUPLEX_FULL;
6211 	cmd->base.autoneg = AUTONEG_ENABLE;
6212 	cmd->base.phy_address = 0;
6213 	cmd->base.mdio_support = 0;
6214 	cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
6215 	cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
6216 
6217 	switch (link_type) {
6218 	case QETH_LINK_TYPE_FAST_ETH:
6219 	case QETH_LINK_TYPE_LANE_ETH100:
6220 		cmd->base.speed = SPEED_100;
6221 		cmd->base.port = PORT_TP;
6222 		break;
6223 	case QETH_LINK_TYPE_GBIT_ETH:
6224 	case QETH_LINK_TYPE_LANE_ETH1000:
6225 		cmd->base.speed = SPEED_1000;
6226 		cmd->base.port = PORT_FIBRE;
6227 		break;
6228 	case QETH_LINK_TYPE_10GBIT_ETH:
6229 		cmd->base.speed = SPEED_10000;
6230 		cmd->base.port = PORT_FIBRE;
6231 		break;
6232 	default:
6233 		cmd->base.speed = SPEED_10;
6234 		cmd->base.port = PORT_TP;
6235 	}
6236 	qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port);
6237 
6238 	/* Check if we can obtain more accurate information.	 */
6239 	/* If QUERY_CARD_INFO command is not supported or fails, */
6240 	/* just return the heuristics that was filled above.	 */
6241 	if (!qeth_card_hw_is_reachable(card))
6242 		return -ENODEV;
6243 	rc = qeth_query_card_info(card, &carrier_info);
6244 	if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */
6245 		return 0;
6246 	if (rc) /* report error from the hardware operation */
6247 		return rc;
6248 	/* on success, fill in the information got from the hardware */
6249 
6250 	netdev_dbg(netdev,
6251 	"card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n",
6252 			carrier_info.card_type,
6253 			carrier_info.port_mode,
6254 			carrier_info.port_speed);
6255 
6256 	/* Update attributes for which we've obtained more authoritative */
6257 	/* information, leave the rest the way they where filled above.  */
6258 	switch (carrier_info.card_type) {
6259 	case CARD_INFO_TYPE_1G_COPPER_A:
6260 	case CARD_INFO_TYPE_1G_COPPER_B:
6261 		cmd->base.port = PORT_TP;
6262 		qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
6263 		break;
6264 	case CARD_INFO_TYPE_1G_FIBRE_A:
6265 	case CARD_INFO_TYPE_1G_FIBRE_B:
6266 		cmd->base.port = PORT_FIBRE;
6267 		qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
6268 		break;
6269 	case CARD_INFO_TYPE_10G_FIBRE_A:
6270 	case CARD_INFO_TYPE_10G_FIBRE_B:
6271 		cmd->base.port = PORT_FIBRE;
6272 		qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port);
6273 		break;
6274 	}
6275 
6276 	switch (carrier_info.port_mode) {
6277 	case CARD_INFO_PORTM_FULLDUPLEX:
6278 		cmd->base.duplex = DUPLEX_FULL;
6279 		break;
6280 	case CARD_INFO_PORTM_HALFDUPLEX:
6281 		cmd->base.duplex = DUPLEX_HALF;
6282 		break;
6283 	}
6284 
6285 	switch (carrier_info.port_speed) {
6286 	case CARD_INFO_PORTS_10M:
6287 		cmd->base.speed = SPEED_10;
6288 		break;
6289 	case CARD_INFO_PORTS_100M:
6290 		cmd->base.speed = SPEED_100;
6291 		break;
6292 	case CARD_INFO_PORTS_1G:
6293 		cmd->base.speed = SPEED_1000;
6294 		break;
6295 	case CARD_INFO_PORTS_10G:
6296 		cmd->base.speed = SPEED_10000;
6297 		break;
6298 	}
6299 
6300 	return 0;
6301 }
6302 EXPORT_SYMBOL_GPL(qeth_core_ethtool_get_link_ksettings);
6303 
6304 /* Callback to handle checksum offload command reply from OSA card.
6305  * Verify that required features have been enabled on the card.
6306  * Return error in hdr->return_code as this value is checked by caller.
6307  *
6308  * Always returns zero to indicate no further messages from the OSA card.
6309  */
6310 static int qeth_ipa_checksum_run_cmd_cb(struct qeth_card *card,
6311 					struct qeth_reply *reply,
6312 					unsigned long data)
6313 {
6314 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
6315 	struct qeth_checksum_cmd *chksum_cb =
6316 				(struct qeth_checksum_cmd *)reply->param;
6317 
6318 	QETH_CARD_TEXT(card, 4, "chkdoccb");
6319 	if (qeth_setassparms_inspect_rc(cmd))
6320 		return 0;
6321 
6322 	memset(chksum_cb, 0, sizeof(*chksum_cb));
6323 	if (cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_START) {
6324 		chksum_cb->supported =
6325 				cmd->data.setassparms.data.chksum.supported;
6326 		QETH_CARD_TEXT_(card, 3, "strt:%x", chksum_cb->supported);
6327 	}
6328 	if (cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_ENABLE) {
6329 		chksum_cb->supported =
6330 				cmd->data.setassparms.data.chksum.supported;
6331 		chksum_cb->enabled =
6332 				cmd->data.setassparms.data.chksum.enabled;
6333 		QETH_CARD_TEXT_(card, 3, "supp:%x", chksum_cb->supported);
6334 		QETH_CARD_TEXT_(card, 3, "enab:%x", chksum_cb->enabled);
6335 	}
6336 	return 0;
6337 }
6338 
6339 /* Send command to OSA card and check results. */
6340 static int qeth_ipa_checksum_run_cmd(struct qeth_card *card,
6341 				     enum qeth_ipa_funcs ipa_func,
6342 				     __u16 cmd_code, long data,
6343 				     struct qeth_checksum_cmd *chksum_cb)
6344 {
6345 	struct qeth_cmd_buffer *iob;
6346 	int rc = -ENOMEM;
6347 
6348 	QETH_CARD_TEXT(card, 4, "chkdocmd");
6349 	iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
6350 				       sizeof(__u32), QETH_PROT_IPV4);
6351 	if (iob)
6352 		rc = qeth_send_setassparms(card, iob, sizeof(__u32), data,
6353 					   qeth_ipa_checksum_run_cmd_cb,
6354 					   chksum_cb);
6355 	return rc;
6356 }
6357 
6358 static int qeth_send_checksum_on(struct qeth_card *card, int cstype)
6359 {
6360 	const __u32 required_features = QETH_IPA_CHECKSUM_IP_HDR |
6361 					QETH_IPA_CHECKSUM_UDP |
6362 					QETH_IPA_CHECKSUM_TCP;
6363 	struct qeth_checksum_cmd chksum_cb;
6364 	int rc;
6365 
6366 	rc = qeth_ipa_checksum_run_cmd(card, cstype, IPA_CMD_ASS_START, 0,
6367 				       &chksum_cb);
6368 	if (!rc) {
6369 		if ((required_features & chksum_cb.supported) !=
6370 		    required_features)
6371 			rc = -EIO;
6372 		else if (!(QETH_IPA_CHECKSUM_LP2LP & chksum_cb.supported) &&
6373 			 cstype == IPA_INBOUND_CHECKSUM)
6374 			dev_warn(&card->gdev->dev,
6375 				 "Hardware checksumming is performed only if %s and its peer use different OSA Express 3 ports\n",
6376 				 QETH_CARD_IFNAME(card));
6377 	}
6378 	if (rc) {
6379 		qeth_send_simple_setassparms(card, cstype, IPA_CMD_ASS_STOP, 0);
6380 		dev_warn(&card->gdev->dev,
6381 			 "Starting HW checksumming for %s failed, using SW checksumming\n",
6382 			 QETH_CARD_IFNAME(card));
6383 		return rc;
6384 	}
6385 	rc = qeth_ipa_checksum_run_cmd(card, cstype, IPA_CMD_ASS_ENABLE,
6386 				       chksum_cb.supported, &chksum_cb);
6387 	if (!rc) {
6388 		if ((required_features & chksum_cb.enabled) !=
6389 		    required_features)
6390 			rc = -EIO;
6391 	}
6392 	if (rc) {
6393 		qeth_send_simple_setassparms(card, cstype, IPA_CMD_ASS_STOP, 0);
6394 		dev_warn(&card->gdev->dev,
6395 			 "Enabling HW checksumming for %s failed, using SW checksumming\n",
6396 			 QETH_CARD_IFNAME(card));
6397 		return rc;
6398 	}
6399 
6400 	dev_info(&card->gdev->dev, "HW Checksumming (%sbound) enabled\n",
6401 		 cstype == IPA_INBOUND_CHECKSUM ? "in" : "out");
6402 	return 0;
6403 }
6404 
6405 static int qeth_set_ipa_csum(struct qeth_card *card, int on, int cstype)
6406 {
6407 	int rc = (on) ? qeth_send_checksum_on(card, cstype)
6408 		      : qeth_send_simple_setassparms(card, cstype,
6409 						     IPA_CMD_ASS_STOP, 0);
6410 	return rc ? -EIO : 0;
6411 }
6412 
6413 static int qeth_set_ipa_tso(struct qeth_card *card, int on)
6414 {
6415 	int rc;
6416 
6417 	QETH_CARD_TEXT(card, 3, "sttso");
6418 
6419 	if (on) {
6420 		rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
6421 						  IPA_CMD_ASS_START, 0);
6422 		if (rc) {
6423 			dev_warn(&card->gdev->dev,
6424 				 "Starting outbound TCP segmentation offload for %s failed\n",
6425 				 QETH_CARD_IFNAME(card));
6426 			return -EIO;
6427 		}
6428 		dev_info(&card->gdev->dev, "Outbound TSO enabled\n");
6429 	} else {
6430 		rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
6431 						  IPA_CMD_ASS_STOP, 0);
6432 	}
6433 	return rc;
6434 }
6435 
6436 #define QETH_HW_FEATURES (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_TSO)
6437 
6438 /**
6439  * qeth_recover_features() - Restore device features after recovery
6440  * @dev:	the recovering net_device
6441  *
6442  * Caller must hold rtnl lock.
6443  */
6444 void qeth_recover_features(struct net_device *dev)
6445 {
6446 	netdev_features_t features = dev->features;
6447 	struct qeth_card *card = dev->ml_priv;
6448 
6449 	/* force-off any feature that needs an IPA sequence.
6450 	 * netdev_update_features() will restart them.
6451 	 */
6452 	dev->features &= ~QETH_HW_FEATURES;
6453 	netdev_update_features(dev);
6454 
6455 	if (features == dev->features)
6456 		return;
6457 	dev_warn(&card->gdev->dev,
6458 		 "Device recovery failed to restore all offload features\n");
6459 }
6460 EXPORT_SYMBOL_GPL(qeth_recover_features);
6461 
6462 int qeth_set_features(struct net_device *dev, netdev_features_t features)
6463 {
6464 	struct qeth_card *card = dev->ml_priv;
6465 	netdev_features_t changed = dev->features ^ features;
6466 	int rc = 0;
6467 
6468 	QETH_DBF_TEXT(SETUP, 2, "setfeat");
6469 	QETH_DBF_HEX(SETUP, 2, &features, sizeof(features));
6470 
6471 	if ((changed & NETIF_F_IP_CSUM)) {
6472 		rc = qeth_set_ipa_csum(card,
6473 				       features & NETIF_F_IP_CSUM ? 1 : 0,
6474 				       IPA_OUTBOUND_CHECKSUM);
6475 		if (rc)
6476 			changed ^= NETIF_F_IP_CSUM;
6477 	}
6478 	if ((changed & NETIF_F_RXCSUM)) {
6479 		rc = qeth_set_ipa_csum(card,
6480 					features & NETIF_F_RXCSUM ? 1 : 0,
6481 					IPA_INBOUND_CHECKSUM);
6482 		if (rc)
6483 			changed ^= NETIF_F_RXCSUM;
6484 	}
6485 	if ((changed & NETIF_F_TSO)) {
6486 		rc = qeth_set_ipa_tso(card, features & NETIF_F_TSO ? 1 : 0);
6487 		if (rc)
6488 			changed ^= NETIF_F_TSO;
6489 	}
6490 
6491 	/* everything changed successfully? */
6492 	if ((dev->features ^ features) == changed)
6493 		return 0;
6494 	/* something went wrong. save changed features and return error */
6495 	dev->features ^= changed;
6496 	return -EIO;
6497 }
6498 EXPORT_SYMBOL_GPL(qeth_set_features);
6499 
6500 netdev_features_t qeth_fix_features(struct net_device *dev,
6501 				    netdev_features_t features)
6502 {
6503 	struct qeth_card *card = dev->ml_priv;
6504 
6505 	QETH_DBF_TEXT(SETUP, 2, "fixfeat");
6506 	if (!qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM))
6507 		features &= ~NETIF_F_IP_CSUM;
6508 	if (!qeth_is_supported(card, IPA_INBOUND_CHECKSUM))
6509 		features &= ~NETIF_F_RXCSUM;
6510 	if (!qeth_is_supported(card, IPA_OUTBOUND_TSO))
6511 		features &= ~NETIF_F_TSO;
6512 	/* if the card isn't up, remove features that require hw changes */
6513 	if (card->state == CARD_STATE_DOWN ||
6514 	    card->state == CARD_STATE_RECOVER)
6515 		features &= ~QETH_HW_FEATURES;
6516 	QETH_DBF_HEX(SETUP, 2, &features, sizeof(features));
6517 	return features;
6518 }
6519 EXPORT_SYMBOL_GPL(qeth_fix_features);
6520 
6521 netdev_features_t qeth_features_check(struct sk_buff *skb,
6522 				      struct net_device *dev,
6523 				      netdev_features_t features)
6524 {
6525 	/* GSO segmentation builds skbs with
6526 	 *	a (small) linear part for the headers, and
6527 	 *	page frags for the data.
6528 	 * Compared to a linear skb, the header-only part consumes an
6529 	 * additional buffer element. This reduces buffer utilization, and
6530 	 * hurts throughput. So compress small segments into one element.
6531 	 */
6532 	if (netif_needs_gso(skb, features)) {
6533 		/* match skb_segment(): */
6534 		unsigned int doffset = skb->data - skb_mac_header(skb);
6535 		unsigned int hsize = skb_shinfo(skb)->gso_size;
6536 		unsigned int hroom = skb_headroom(skb);
6537 
6538 		/* linearize only if resulting skb allocations are order-0: */
6539 		if (SKB_DATA_ALIGN(hroom + doffset + hsize) <= SKB_MAX_HEAD(0))
6540 			features &= ~NETIF_F_SG;
6541 	}
6542 
6543 	return vlan_features_check(skb, features);
6544 }
6545 EXPORT_SYMBOL_GPL(qeth_features_check);
6546 
6547 static int __init qeth_core_init(void)
6548 {
6549 	int rc;
6550 
6551 	pr_info("loading core functions\n");
6552 	INIT_LIST_HEAD(&qeth_core_card_list.list);
6553 	INIT_LIST_HEAD(&qeth_dbf_list);
6554 	rwlock_init(&qeth_core_card_list.rwlock);
6555 	mutex_init(&qeth_mod_mutex);
6556 
6557 	qeth_wq = create_singlethread_workqueue("qeth_wq");
6558 
6559 	rc = qeth_register_dbf_views();
6560 	if (rc)
6561 		goto out_err;
6562 	qeth_core_root_dev = root_device_register("qeth");
6563 	rc = PTR_ERR_OR_ZERO(qeth_core_root_dev);
6564 	if (rc)
6565 		goto register_err;
6566 	qeth_core_header_cache = kmem_cache_create("qeth_hdr",
6567 			sizeof(struct qeth_hdr) + ETH_HLEN, 64, 0, NULL);
6568 	if (!qeth_core_header_cache) {
6569 		rc = -ENOMEM;
6570 		goto slab_err;
6571 	}
6572 	qeth_qdio_outbuf_cache = kmem_cache_create("qeth_buf",
6573 			sizeof(struct qeth_qdio_out_buffer), 0, 0, NULL);
6574 	if (!qeth_qdio_outbuf_cache) {
6575 		rc = -ENOMEM;
6576 		goto cqslab_err;
6577 	}
6578 	rc = ccw_driver_register(&qeth_ccw_driver);
6579 	if (rc)
6580 		goto ccw_err;
6581 	qeth_core_ccwgroup_driver.driver.groups = qeth_drv_attr_groups;
6582 	rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
6583 	if (rc)
6584 		goto ccwgroup_err;
6585 
6586 	return 0;
6587 
6588 ccwgroup_err:
6589 	ccw_driver_unregister(&qeth_ccw_driver);
6590 ccw_err:
6591 	kmem_cache_destroy(qeth_qdio_outbuf_cache);
6592 cqslab_err:
6593 	kmem_cache_destroy(qeth_core_header_cache);
6594 slab_err:
6595 	root_device_unregister(qeth_core_root_dev);
6596 register_err:
6597 	qeth_unregister_dbf_views();
6598 out_err:
6599 	pr_err("Initializing the qeth device driver failed\n");
6600 	return rc;
6601 }
6602 
6603 static void __exit qeth_core_exit(void)
6604 {
6605 	qeth_clear_dbf_list();
6606 	destroy_workqueue(qeth_wq);
6607 	ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
6608 	ccw_driver_unregister(&qeth_ccw_driver);
6609 	kmem_cache_destroy(qeth_qdio_outbuf_cache);
6610 	kmem_cache_destroy(qeth_core_header_cache);
6611 	root_device_unregister(qeth_core_root_dev);
6612 	qeth_unregister_dbf_views();
6613 	pr_info("core functions removed\n");
6614 }
6615 
6616 module_init(qeth_core_init);
6617 module_exit(qeth_core_exit);
6618 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
6619 MODULE_DESCRIPTION("qeth core functions");
6620 MODULE_LICENSE("GPL");
6621