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