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