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