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