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