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