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