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