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