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 			scnprintf(card->info.mcl_level,
2805 				  sizeof(card->info.mcl_level),
2806 				  "%02x%02x",
2807 				  card->info.mcl_level[2],
2808 				  card->info.mcl_level[3]);
2809 			break;
2810 		}
2811 		fallthrough;
2812 	case QETH_CARD_TYPE_IQD:
2813 		if (IS_VM_NIC(card) || (card->info.mcl_level[0] & 0x80)) {
2814 			card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2815 				card->info.mcl_level[0]];
2816 			card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2817 				card->info.mcl_level[1]];
2818 			card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2819 				card->info.mcl_level[2]];
2820 			card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2821 				card->info.mcl_level[3]];
2822 			card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2823 		}
2824 		break;
2825 	default:
2826 		memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2827 	}
2828 	dev_info(&card->gdev->dev,
2829 		 "Device is a%s card%s%s%s\nwith link type %s.\n",
2830 		 qeth_get_cardname(card),
2831 		 (card->info.mcl_level[0]) ? " (level: " : "",
2832 		 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2833 		 (card->info.mcl_level[0]) ? ")" : "",
2834 		 qeth_get_cardname_short(card));
2835 }
2836 
2837 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2838 {
2839 	struct qeth_buffer_pool_entry *entry;
2840 
2841 	QETH_CARD_TEXT(card, 5, "inwrklst");
2842 
2843 	list_for_each_entry(entry,
2844 			    &card->qdio.init_pool.entry_list, init_list) {
2845 		qeth_put_buffer_pool_entry(card, entry);
2846 	}
2847 }
2848 
2849 static struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2850 					struct qeth_card *card)
2851 {
2852 	struct qeth_buffer_pool_entry *entry;
2853 	int i, free;
2854 
2855 	if (list_empty(&card->qdio.in_buf_pool.entry_list))
2856 		return NULL;
2857 
2858 	list_for_each_entry(entry, &card->qdio.in_buf_pool.entry_list, list) {
2859 		free = 1;
2860 		for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2861 			if (page_count(entry->elements[i]) > 1) {
2862 				free = 0;
2863 				break;
2864 			}
2865 		}
2866 		if (free) {
2867 			list_del_init(&entry->list);
2868 			return entry;
2869 		}
2870 	}
2871 
2872 	/* no free buffer in pool so take first one and swap pages */
2873 	entry = list_first_entry(&card->qdio.in_buf_pool.entry_list,
2874 				 struct qeth_buffer_pool_entry, list);
2875 	for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2876 		if (page_count(entry->elements[i]) > 1) {
2877 			struct page *page = dev_alloc_page();
2878 
2879 			if (!page)
2880 				return NULL;
2881 
2882 			__free_page(entry->elements[i]);
2883 			entry->elements[i] = page;
2884 			QETH_CARD_STAT_INC(card, rx_sg_alloc_page);
2885 		}
2886 	}
2887 	list_del_init(&entry->list);
2888 	return entry;
2889 }
2890 
2891 static int qeth_init_input_buffer(struct qeth_card *card,
2892 		struct qeth_qdio_buffer *buf)
2893 {
2894 	struct qeth_buffer_pool_entry *pool_entry = buf->pool_entry;
2895 	int i;
2896 
2897 	if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2898 		buf->rx_skb = netdev_alloc_skb(card->dev,
2899 					       ETH_HLEN +
2900 					       sizeof(struct ipv6hdr));
2901 		if (!buf->rx_skb)
2902 			return -ENOMEM;
2903 	}
2904 
2905 	if (!pool_entry) {
2906 		pool_entry = qeth_find_free_buffer_pool_entry(card);
2907 		if (!pool_entry)
2908 			return -ENOBUFS;
2909 
2910 		buf->pool_entry = pool_entry;
2911 	}
2912 
2913 	/*
2914 	 * since the buffer is accessed only from the input_tasklet
2915 	 * there shouldn't be a need to synchronize; also, since we use
2916 	 * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2917 	 * buffers
2918 	 */
2919 	for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2920 		buf->buffer->element[i].length = PAGE_SIZE;
2921 		buf->buffer->element[i].addr =
2922 			page_to_phys(pool_entry->elements[i]);
2923 		if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2924 			buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2925 		else
2926 			buf->buffer->element[i].eflags = 0;
2927 		buf->buffer->element[i].sflags = 0;
2928 	}
2929 	return 0;
2930 }
2931 
2932 static unsigned int qeth_tx_select_bulk_max(struct qeth_card *card,
2933 					    struct qeth_qdio_out_q *queue)
2934 {
2935 	if (!IS_IQD(card) ||
2936 	    qeth_iqd_is_mcast_queue(card, queue) ||
2937 	    card->options.cq == QETH_CQ_ENABLED ||
2938 	    qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd))
2939 		return 1;
2940 
2941 	return card->ssqd.mmwc ? card->ssqd.mmwc : 1;
2942 }
2943 
2944 static int qeth_init_qdio_queues(struct qeth_card *card)
2945 {
2946 	unsigned int rx_bufs = card->qdio.in_buf_pool.buf_count;
2947 	unsigned int i;
2948 	int rc;
2949 
2950 	QETH_CARD_TEXT(card, 2, "initqdqs");
2951 
2952 	/* inbound queue */
2953 	qdio_reset_buffers(card->qdio.in_q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2954 	memset(&card->rx, 0, sizeof(struct qeth_rx));
2955 
2956 	qeth_initialize_working_pool_list(card);
2957 	/*give only as many buffers to hardware as we have buffer pool entries*/
2958 	for (i = 0; i < rx_bufs; i++) {
2959 		rc = qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2960 		if (rc)
2961 			return rc;
2962 	}
2963 
2964 	card->qdio.in_q->next_buf_to_init = QDIO_BUFNR(rx_bufs);
2965 	rc = qdio_add_bufs_to_input_queue(CARD_DDEV(card), 0, 0, rx_bufs);
2966 	if (rc) {
2967 		QETH_CARD_TEXT_(card, 2, "1err%d", rc);
2968 		return rc;
2969 	}
2970 
2971 	/* completion */
2972 	rc = qeth_cq_init(card);
2973 	if (rc) {
2974 		return rc;
2975 	}
2976 
2977 	/* outbound queue */
2978 	for (i = 0; i < card->qdio.no_out_queues; ++i) {
2979 		struct qeth_qdio_out_q *queue = card->qdio.out_qs[i];
2980 
2981 		qdio_reset_buffers(queue->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2982 		queue->max_elements = QETH_MAX_BUFFER_ELEMENTS(card);
2983 		queue->next_buf_to_fill = 0;
2984 		queue->do_pack = 0;
2985 		queue->prev_hdr = NULL;
2986 		queue->coalesced_frames = 0;
2987 		queue->bulk_start = 0;
2988 		queue->bulk_count = 0;
2989 		queue->bulk_max = qeth_tx_select_bulk_max(card, queue);
2990 		atomic_set(&queue->used_buffers, 0);
2991 		atomic_set(&queue->set_pci_flags_count, 0);
2992 		netdev_tx_reset_queue(netdev_get_tx_queue(card->dev, i));
2993 	}
2994 	return 0;
2995 }
2996 
2997 static void qeth_ipa_finalize_cmd(struct qeth_card *card,
2998 				  struct qeth_cmd_buffer *iob)
2999 {
3000 	qeth_mpc_finalize_cmd(card, iob);
3001 
3002 	/* override with IPA-specific values: */
3003 	__ipa_cmd(iob)->hdr.seqno = card->seqno.ipa++;
3004 }
3005 
3006 static void qeth_prepare_ipa_cmd(struct qeth_card *card,
3007 				 struct qeth_cmd_buffer *iob, u16 cmd_length)
3008 {
3009 	u8 prot_type = qeth_mpc_select_prot_type(card);
3010 	u16 total_length = iob->length;
3011 
3012 	qeth_setup_ccw(__ccw_from_cmd(iob), CCW_CMD_WRITE, 0, total_length,
3013 		       iob->data);
3014 	iob->finalize = qeth_ipa_finalize_cmd;
3015 
3016 	memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
3017 	memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &total_length, 2);
3018 	memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
3019 	memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &cmd_length, 2);
3020 	memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &cmd_length, 2);
3021 	memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
3022 	       &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
3023 	memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &cmd_length, 2);
3024 }
3025 
3026 static bool qeth_ipa_match_reply(struct qeth_cmd_buffer *iob,
3027 				 struct qeth_cmd_buffer *reply)
3028 {
3029 	struct qeth_ipa_cmd *ipa_reply = __ipa_reply(reply);
3030 
3031 	return ipa_reply && (__ipa_cmd(iob)->hdr.seqno == ipa_reply->hdr.seqno);
3032 }
3033 
3034 struct qeth_cmd_buffer *qeth_ipa_alloc_cmd(struct qeth_card *card,
3035 					   enum qeth_ipa_cmds cmd_code,
3036 					   enum qeth_prot_versions prot,
3037 					   unsigned int data_length)
3038 {
3039 	struct qeth_cmd_buffer *iob;
3040 	struct qeth_ipacmd_hdr *hdr;
3041 
3042 	data_length += offsetof(struct qeth_ipa_cmd, data);
3043 	iob = qeth_alloc_cmd(&card->write, IPA_PDU_HEADER_SIZE + data_length, 1,
3044 			     QETH_IPA_TIMEOUT);
3045 	if (!iob)
3046 		return NULL;
3047 
3048 	qeth_prepare_ipa_cmd(card, iob, data_length);
3049 	iob->match = qeth_ipa_match_reply;
3050 
3051 	hdr = &__ipa_cmd(iob)->hdr;
3052 	hdr->command = cmd_code;
3053 	hdr->initiator = IPA_CMD_INITIATOR_HOST;
3054 	/* hdr->seqno is set by qeth_send_control_data() */
3055 	hdr->adapter_type = QETH_LINK_TYPE_FAST_ETH;
3056 	hdr->rel_adapter_no = (u8) card->dev->dev_port;
3057 	hdr->prim_version_no = IS_LAYER2(card) ? 2 : 1;
3058 	hdr->param_count = 1;
3059 	hdr->prot_version = prot;
3060 	return iob;
3061 }
3062 EXPORT_SYMBOL_GPL(qeth_ipa_alloc_cmd);
3063 
3064 static int qeth_send_ipa_cmd_cb(struct qeth_card *card,
3065 				struct qeth_reply *reply, unsigned long data)
3066 {
3067 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3068 
3069 	return (cmd->hdr.return_code) ? -EIO : 0;
3070 }
3071 
3072 /*
3073  * qeth_send_ipa_cmd() - send an IPA command
3074  *
3075  * See qeth_send_control_data() for explanation of the arguments.
3076  */
3077 
3078 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
3079 		int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
3080 			unsigned long),
3081 		void *reply_param)
3082 {
3083 	int rc;
3084 
3085 	QETH_CARD_TEXT(card, 4, "sendipa");
3086 
3087 	if (card->read_or_write_problem) {
3088 		qeth_put_cmd(iob);
3089 		return -EIO;
3090 	}
3091 
3092 	if (reply_cb == NULL)
3093 		reply_cb = qeth_send_ipa_cmd_cb;
3094 	rc = qeth_send_control_data(card, iob, reply_cb, reply_param);
3095 	if (rc == -ETIME) {
3096 		qeth_clear_ipacmd_list(card);
3097 		qeth_schedule_recovery(card);
3098 	}
3099 	return rc;
3100 }
3101 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
3102 
3103 static int qeth_send_startlan_cb(struct qeth_card *card,
3104 				 struct qeth_reply *reply, unsigned long data)
3105 {
3106 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3107 
3108 	if (cmd->hdr.return_code == IPA_RC_LAN_OFFLINE)
3109 		return -ENETDOWN;
3110 
3111 	return (cmd->hdr.return_code) ? -EIO : 0;
3112 }
3113 
3114 static int qeth_send_startlan(struct qeth_card *card)
3115 {
3116 	struct qeth_cmd_buffer *iob;
3117 
3118 	QETH_CARD_TEXT(card, 2, "strtlan");
3119 
3120 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_STARTLAN, QETH_PROT_NONE, 0);
3121 	if (!iob)
3122 		return -ENOMEM;
3123 	return qeth_send_ipa_cmd(card, iob, qeth_send_startlan_cb, NULL);
3124 }
3125 
3126 static int qeth_setadpparms_inspect_rc(struct qeth_ipa_cmd *cmd)
3127 {
3128 	if (!cmd->hdr.return_code)
3129 		cmd->hdr.return_code =
3130 			cmd->data.setadapterparms.hdr.return_code;
3131 	return cmd->hdr.return_code;
3132 }
3133 
3134 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
3135 		struct qeth_reply *reply, unsigned long data)
3136 {
3137 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3138 	struct qeth_query_cmds_supp *query_cmd;
3139 
3140 	QETH_CARD_TEXT(card, 3, "quyadpcb");
3141 	if (qeth_setadpparms_inspect_rc(cmd))
3142 		return -EIO;
3143 
3144 	query_cmd = &cmd->data.setadapterparms.data.query_cmds_supp;
3145 	if (query_cmd->lan_type & 0x7f) {
3146 		if (!qeth_is_supported_link_type(card, query_cmd->lan_type))
3147 			return -EPROTONOSUPPORT;
3148 
3149 		card->info.link_type = query_cmd->lan_type;
3150 		QETH_CARD_TEXT_(card, 2, "lnk %d", card->info.link_type);
3151 	}
3152 
3153 	card->options.adp.supported = query_cmd->supported_cmds;
3154 	return 0;
3155 }
3156 
3157 static struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
3158 						    enum qeth_ipa_setadp_cmd adp_cmd,
3159 						    unsigned int data_length)
3160 {
3161 	struct qeth_ipacmd_setadpparms_hdr *hdr;
3162 	struct qeth_cmd_buffer *iob;
3163 
3164 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETADAPTERPARMS, QETH_PROT_IPV4,
3165 				 data_length +
3166 				 offsetof(struct qeth_ipacmd_setadpparms,
3167 					  data));
3168 	if (!iob)
3169 		return NULL;
3170 
3171 	hdr = &__ipa_cmd(iob)->data.setadapterparms.hdr;
3172 	hdr->cmdlength = sizeof(*hdr) + data_length;
3173 	hdr->command_code = adp_cmd;
3174 	hdr->used_total = 1;
3175 	hdr->seq_no = 1;
3176 	return iob;
3177 }
3178 
3179 static int qeth_query_setadapterparms(struct qeth_card *card)
3180 {
3181 	int rc;
3182 	struct qeth_cmd_buffer *iob;
3183 
3184 	QETH_CARD_TEXT(card, 3, "queryadp");
3185 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
3186 				   SETADP_DATA_SIZEOF(query_cmds_supp));
3187 	if (!iob)
3188 		return -ENOMEM;
3189 	rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
3190 	return rc;
3191 }
3192 
3193 static int qeth_query_ipassists_cb(struct qeth_card *card,
3194 		struct qeth_reply *reply, unsigned long data)
3195 {
3196 	struct qeth_ipa_cmd *cmd;
3197 
3198 	QETH_CARD_TEXT(card, 2, "qipasscb");
3199 
3200 	cmd = (struct qeth_ipa_cmd *) data;
3201 
3202 	switch (cmd->hdr.return_code) {
3203 	case IPA_RC_SUCCESS:
3204 		break;
3205 	case IPA_RC_NOTSUPP:
3206 	case IPA_RC_L2_UNSUPPORTED_CMD:
3207 		QETH_CARD_TEXT(card, 2, "ipaunsup");
3208 		card->options.ipa4.supported |= IPA_SETADAPTERPARMS;
3209 		card->options.ipa6.supported |= IPA_SETADAPTERPARMS;
3210 		return -EOPNOTSUPP;
3211 	default:
3212 		QETH_DBF_MESSAGE(1, "IPA_CMD_QIPASSIST on device %x: Unhandled rc=%#x\n",
3213 				 CARD_DEVID(card), cmd->hdr.return_code);
3214 		return -EIO;
3215 	}
3216 
3217 	if (cmd->hdr.prot_version == QETH_PROT_IPV4)
3218 		card->options.ipa4 = cmd->hdr.assists;
3219 	else if (cmd->hdr.prot_version == QETH_PROT_IPV6)
3220 		card->options.ipa6 = cmd->hdr.assists;
3221 	else
3222 		QETH_DBF_MESSAGE(1, "IPA_CMD_QIPASSIST on device %x: Flawed LIC detected\n",
3223 				 CARD_DEVID(card));
3224 	return 0;
3225 }
3226 
3227 static int qeth_query_ipassists(struct qeth_card *card,
3228 				enum qeth_prot_versions prot)
3229 {
3230 	int rc;
3231 	struct qeth_cmd_buffer *iob;
3232 
3233 	QETH_CARD_TEXT_(card, 2, "qipassi%i", prot);
3234 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_QIPASSIST, prot, 0);
3235 	if (!iob)
3236 		return -ENOMEM;
3237 	rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
3238 	return rc;
3239 }
3240 
3241 static int qeth_query_switch_attributes_cb(struct qeth_card *card,
3242 				struct qeth_reply *reply, unsigned long data)
3243 {
3244 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3245 	struct qeth_query_switch_attributes *attrs;
3246 	struct qeth_switch_info *sw_info;
3247 
3248 	QETH_CARD_TEXT(card, 2, "qswiatcb");
3249 	if (qeth_setadpparms_inspect_rc(cmd))
3250 		return -EIO;
3251 
3252 	sw_info = (struct qeth_switch_info *)reply->param;
3253 	attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
3254 	sw_info->capabilities = attrs->capabilities;
3255 	sw_info->settings = attrs->settings;
3256 	QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
3257 			sw_info->settings);
3258 	return 0;
3259 }
3260 
3261 int qeth_query_switch_attributes(struct qeth_card *card,
3262 				 struct qeth_switch_info *sw_info)
3263 {
3264 	struct qeth_cmd_buffer *iob;
3265 
3266 	QETH_CARD_TEXT(card, 2, "qswiattr");
3267 	if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
3268 		return -EOPNOTSUPP;
3269 	if (!netif_carrier_ok(card->dev))
3270 		return -ENOMEDIUM;
3271 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES, 0);
3272 	if (!iob)
3273 		return -ENOMEM;
3274 	return qeth_send_ipa_cmd(card, iob,
3275 				qeth_query_switch_attributes_cb, sw_info);
3276 }
3277 
3278 struct qeth_cmd_buffer *qeth_get_diag_cmd(struct qeth_card *card,
3279 					  enum qeth_diags_cmds sub_cmd,
3280 					  unsigned int data_length)
3281 {
3282 	struct qeth_ipacmd_diagass *cmd;
3283 	struct qeth_cmd_buffer *iob;
3284 
3285 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SET_DIAG_ASS, QETH_PROT_NONE,
3286 				 DIAG_HDR_LEN + data_length);
3287 	if (!iob)
3288 		return NULL;
3289 
3290 	cmd = &__ipa_cmd(iob)->data.diagass;
3291 	cmd->subcmd_len = DIAG_SUB_HDR_LEN + data_length;
3292 	cmd->subcmd = sub_cmd;
3293 	return iob;
3294 }
3295 EXPORT_SYMBOL_GPL(qeth_get_diag_cmd);
3296 
3297 static int qeth_query_setdiagass_cb(struct qeth_card *card,
3298 		struct qeth_reply *reply, unsigned long data)
3299 {
3300 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3301 	u16 rc = cmd->hdr.return_code;
3302 
3303 	if (rc) {
3304 		QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3305 		return -EIO;
3306 	}
3307 
3308 	card->info.diagass_support = cmd->data.diagass.ext;
3309 	return 0;
3310 }
3311 
3312 static int qeth_query_setdiagass(struct qeth_card *card)
3313 {
3314 	struct qeth_cmd_buffer *iob;
3315 
3316 	QETH_CARD_TEXT(card, 2, "qdiagass");
3317 	iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_QUERY, 0);
3318 	if (!iob)
3319 		return -ENOMEM;
3320 	return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3321 }
3322 
3323 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3324 {
3325 	unsigned long info = get_zeroed_page(GFP_KERNEL);
3326 	struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3327 	struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3328 	struct ccw_dev_id ccwid;
3329 	int level;
3330 
3331 	tid->chpid = card->info.chpid;
3332 	ccw_device_get_id(CARD_RDEV(card), &ccwid);
3333 	tid->ssid = ccwid.ssid;
3334 	tid->devno = ccwid.devno;
3335 	if (!info)
3336 		return;
3337 	level = stsi(NULL, 0, 0, 0);
3338 	if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3339 		tid->lparnr = info222->lpar_number;
3340 	if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3341 		EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3342 		memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3343 	}
3344 	free_page(info);
3345 }
3346 
3347 static int qeth_hw_trap_cb(struct qeth_card *card,
3348 		struct qeth_reply *reply, unsigned long data)
3349 {
3350 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3351 	u16 rc = cmd->hdr.return_code;
3352 
3353 	if (rc) {
3354 		QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3355 		return -EIO;
3356 	}
3357 	return 0;
3358 }
3359 
3360 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3361 {
3362 	struct qeth_cmd_buffer *iob;
3363 	struct qeth_ipa_cmd *cmd;
3364 
3365 	QETH_CARD_TEXT(card, 2, "diagtrap");
3366 	iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRAP, 64);
3367 	if (!iob)
3368 		return -ENOMEM;
3369 	cmd = __ipa_cmd(iob);
3370 	cmd->data.diagass.type = 1;
3371 	cmd->data.diagass.action = action;
3372 	switch (action) {
3373 	case QETH_DIAGS_TRAP_ARM:
3374 		cmd->data.diagass.options = 0x0003;
3375 		cmd->data.diagass.ext = 0x00010000 +
3376 			sizeof(struct qeth_trap_id);
3377 		qeth_get_trap_id(card,
3378 			(struct qeth_trap_id *)cmd->data.diagass.cdata);
3379 		break;
3380 	case QETH_DIAGS_TRAP_DISARM:
3381 		cmd->data.diagass.options = 0x0001;
3382 		break;
3383 	case QETH_DIAGS_TRAP_CAPTURE:
3384 		break;
3385 	}
3386 	return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3387 }
3388 
3389 static int qeth_check_qdio_errors(struct qeth_card *card,
3390 				  struct qdio_buffer *buf,
3391 				  unsigned int qdio_error,
3392 				  const char *dbftext)
3393 {
3394 	if (qdio_error) {
3395 		QETH_CARD_TEXT(card, 2, dbftext);
3396 		QETH_CARD_TEXT_(card, 2, " F15=%02X",
3397 			       buf->element[15].sflags);
3398 		QETH_CARD_TEXT_(card, 2, " F14=%02X",
3399 			       buf->element[14].sflags);
3400 		QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3401 		if ((buf->element[15].sflags) == 0x12) {
3402 			QETH_CARD_STAT_INC(card, rx_fifo_errors);
3403 			return 0;
3404 		} else
3405 			return 1;
3406 	}
3407 	return 0;
3408 }
3409 
3410 static unsigned int qeth_rx_refill_queue(struct qeth_card *card,
3411 					 unsigned int count)
3412 {
3413 	struct qeth_qdio_q *queue = card->qdio.in_q;
3414 	struct list_head *lh;
3415 	int i;
3416 	int rc;
3417 	int newcount = 0;
3418 
3419 	/* only requeue at a certain threshold to avoid SIGAs */
3420 	if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3421 		for (i = queue->next_buf_to_init;
3422 		     i < queue->next_buf_to_init + count; ++i) {
3423 			if (qeth_init_input_buffer(card,
3424 				&queue->bufs[QDIO_BUFNR(i)])) {
3425 				break;
3426 			} else {
3427 				newcount++;
3428 			}
3429 		}
3430 
3431 		if (newcount < count) {
3432 			/* we are in memory shortage so we switch back to
3433 			   traditional skb allocation and drop packages */
3434 			atomic_set(&card->force_alloc_skb, 3);
3435 			count = newcount;
3436 		} else {
3437 			atomic_add_unless(&card->force_alloc_skb, -1, 0);
3438 		}
3439 
3440 		if (!count) {
3441 			i = 0;
3442 			list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3443 				i++;
3444 			if (i == card->qdio.in_buf_pool.buf_count) {
3445 				QETH_CARD_TEXT(card, 2, "qsarbw");
3446 				schedule_delayed_work(
3447 					&card->buffer_reclaim_work,
3448 					QETH_RECLAIM_WORK_TIME);
3449 			}
3450 			return 0;
3451 		}
3452 
3453 		rc = qdio_add_bufs_to_input_queue(CARD_DDEV(card), 0,
3454 						  queue->next_buf_to_init,
3455 						  count);
3456 		if (rc) {
3457 			QETH_CARD_TEXT(card, 2, "qinberr");
3458 		}
3459 		queue->next_buf_to_init = QDIO_BUFNR(queue->next_buf_to_init +
3460 						     count);
3461 		return count;
3462 	}
3463 
3464 	return 0;
3465 }
3466 
3467 static void qeth_buffer_reclaim_work(struct work_struct *work)
3468 {
3469 	struct qeth_card *card = container_of(to_delayed_work(work),
3470 					      struct qeth_card,
3471 					      buffer_reclaim_work);
3472 
3473 	local_bh_disable();
3474 	napi_schedule(&card->napi);
3475 	/* kick-start the NAPI softirq: */
3476 	local_bh_enable();
3477 }
3478 
3479 static void qeth_handle_send_error(struct qeth_card *card,
3480 		struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3481 {
3482 	int sbalf15 = buffer->buffer->element[15].sflags;
3483 
3484 	QETH_CARD_TEXT(card, 6, "hdsnderr");
3485 	qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3486 
3487 	if (!qdio_err)
3488 		return;
3489 
3490 	if ((sbalf15 >= 15) && (sbalf15 <= 31))
3491 		return;
3492 
3493 	QETH_CARD_TEXT(card, 1, "lnkfail");
3494 	QETH_CARD_TEXT_(card, 1, "%04x %02x",
3495 		       (u16)qdio_err, (u8)sbalf15);
3496 }
3497 
3498 /**
3499  * qeth_prep_flush_pack_buffer - Prepares flushing of a packing buffer.
3500  * @queue: queue to check for packing buffer
3501  *
3502  * Returns number of buffers that were prepared for flush.
3503  */
3504 static int qeth_prep_flush_pack_buffer(struct qeth_qdio_out_q *queue)
3505 {
3506 	struct qeth_qdio_out_buffer *buffer;
3507 
3508 	buffer = queue->bufs[queue->next_buf_to_fill];
3509 	if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3510 	    (buffer->next_element_to_fill > 0)) {
3511 		/* it's a packing buffer */
3512 		atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3513 		queue->next_buf_to_fill =
3514 			QDIO_BUFNR(queue->next_buf_to_fill + 1);
3515 		return 1;
3516 	}
3517 	return 0;
3518 }
3519 
3520 /*
3521  * Switched to packing state if the number of used buffers on a queue
3522  * reaches a certain limit.
3523  */
3524 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3525 {
3526 	if (!queue->do_pack) {
3527 		if (atomic_read(&queue->used_buffers)
3528 		    >= QETH_HIGH_WATERMARK_PACK){
3529 			/* switch non-PACKING -> PACKING */
3530 			QETH_CARD_TEXT(queue->card, 6, "np->pack");
3531 			QETH_TXQ_STAT_INC(queue, packing_mode_switch);
3532 			queue->do_pack = 1;
3533 		}
3534 	}
3535 }
3536 
3537 /*
3538  * Switches from packing to non-packing mode. If there is a packing
3539  * buffer on the queue this buffer will be prepared to be flushed.
3540  * In that case 1 is returned to inform the caller. If no buffer
3541  * has to be flushed, zero is returned.
3542  */
3543 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3544 {
3545 	if (queue->do_pack) {
3546 		if (atomic_read(&queue->used_buffers)
3547 		    <= QETH_LOW_WATERMARK_PACK) {
3548 			/* switch PACKING -> non-PACKING */
3549 			QETH_CARD_TEXT(queue->card, 6, "pack->np");
3550 			QETH_TXQ_STAT_INC(queue, packing_mode_switch);
3551 			queue->do_pack = 0;
3552 			return qeth_prep_flush_pack_buffer(queue);
3553 		}
3554 	}
3555 	return 0;
3556 }
3557 
3558 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3559 			       int count)
3560 {
3561 	struct qeth_qdio_out_buffer *buf = queue->bufs[index];
3562 	struct qeth_card *card = queue->card;
3563 	unsigned int frames, usecs;
3564 	struct qaob *aob = NULL;
3565 	int rc;
3566 	int i;
3567 
3568 	for (i = index; i < index + count; ++i) {
3569 		unsigned int bidx = QDIO_BUFNR(i);
3570 		struct sk_buff *skb;
3571 
3572 		buf = queue->bufs[bidx];
3573 		buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3574 				SBAL_EFLAGS_LAST_ENTRY;
3575 		queue->coalesced_frames += buf->frames;
3576 
3577 		if (IS_IQD(card)) {
3578 			skb_queue_walk(&buf->skb_list, skb)
3579 				skb_tx_timestamp(skb);
3580 		}
3581 	}
3582 
3583 	if (IS_IQD(card)) {
3584 		if (card->options.cq == QETH_CQ_ENABLED &&
3585 		    !qeth_iqd_is_mcast_queue(card, queue) &&
3586 		    count == 1) {
3587 			if (!buf->aob)
3588 				buf->aob = kmem_cache_zalloc(qeth_qaob_cache,
3589 							     GFP_ATOMIC);
3590 			if (buf->aob) {
3591 				struct qeth_qaob_priv1 *priv;
3592 
3593 				aob = buf->aob;
3594 				priv = (struct qeth_qaob_priv1 *)&aob->user1;
3595 				priv->state = QETH_QAOB_ISSUED;
3596 				priv->queue_no = queue->queue_no;
3597 			}
3598 		}
3599 	} else {
3600 		if (!queue->do_pack) {
3601 			if ((atomic_read(&queue->used_buffers) >=
3602 				(QETH_HIGH_WATERMARK_PACK -
3603 				 QETH_WATERMARK_PACK_FUZZ)) &&
3604 			    !atomic_read(&queue->set_pci_flags_count)) {
3605 				/* it's likely that we'll go to packing
3606 				 * mode soon */
3607 				atomic_inc(&queue->set_pci_flags_count);
3608 				buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3609 			}
3610 		} else {
3611 			if (!atomic_read(&queue->set_pci_flags_count)) {
3612 				/*
3613 				 * there's no outstanding PCI any more, so we
3614 				 * have to request a PCI to be sure the PCI
3615 				 * will wake at some time in the future then we
3616 				 * can flush packed buffers that might still be
3617 				 * hanging around, which can happen if no
3618 				 * further send was requested by the stack
3619 				 */
3620 				atomic_inc(&queue->set_pci_flags_count);
3621 				buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3622 			}
3623 		}
3624 	}
3625 
3626 	QETH_TXQ_STAT_INC(queue, doorbell);
3627 	rc = qdio_add_bufs_to_output_queue(CARD_DDEV(card), queue->queue_no,
3628 					   index, count, aob);
3629 
3630 	switch (rc) {
3631 	case 0:
3632 	case -ENOBUFS:
3633 		/* ignore temporary SIGA errors without busy condition */
3634 
3635 		/* Fake the TX completion interrupt: */
3636 		frames = READ_ONCE(queue->max_coalesced_frames);
3637 		usecs = READ_ONCE(queue->coalesce_usecs);
3638 
3639 		if (frames && queue->coalesced_frames >= frames) {
3640 			napi_schedule(&queue->napi);
3641 			queue->coalesced_frames = 0;
3642 			QETH_TXQ_STAT_INC(queue, coal_frames);
3643 		} else if (qeth_use_tx_irqs(card) &&
3644 			   atomic_read(&queue->used_buffers) >= 32) {
3645 			/* Old behaviour carried over from the qdio layer: */
3646 			napi_schedule(&queue->napi);
3647 			QETH_TXQ_STAT_INC(queue, coal_frames);
3648 		} else if (usecs) {
3649 			qeth_tx_arm_timer(queue, usecs);
3650 		}
3651 
3652 		break;
3653 	default:
3654 		QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3655 		QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3656 		QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3657 		QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3658 		QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3659 
3660 		/* this must not happen under normal circumstances. if it
3661 		 * happens something is really wrong -> recover */
3662 		qeth_schedule_recovery(queue->card);
3663 	}
3664 }
3665 
3666 static void qeth_flush_queue(struct qeth_qdio_out_q *queue)
3667 {
3668 	qeth_flush_buffers(queue, queue->bulk_start, queue->bulk_count);
3669 
3670 	queue->bulk_start = QDIO_BUFNR(queue->bulk_start + queue->bulk_count);
3671 	queue->prev_hdr = NULL;
3672 	queue->bulk_count = 0;
3673 }
3674 
3675 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3676 {
3677 	/*
3678 	 * check if weed have to switch to non-packing mode or if
3679 	 * we have to get a pci flag out on the queue
3680 	 */
3681 	if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3682 	    !atomic_read(&queue->set_pci_flags_count)) {
3683 		unsigned int index, flush_cnt;
3684 
3685 		spin_lock(&queue->lock);
3686 
3687 		index = queue->next_buf_to_fill;
3688 
3689 		flush_cnt = qeth_switch_to_nonpacking_if_needed(queue);
3690 		if (!flush_cnt && !atomic_read(&queue->set_pci_flags_count))
3691 			flush_cnt = qeth_prep_flush_pack_buffer(queue);
3692 
3693 		if (flush_cnt) {
3694 			qeth_flush_buffers(queue, index, flush_cnt);
3695 			QETH_TXQ_STAT_ADD(queue, bufs_pack, flush_cnt);
3696 		}
3697 
3698 		spin_unlock(&queue->lock);
3699 	}
3700 }
3701 
3702 static void qeth_qdio_poll(struct ccw_device *cdev, unsigned long card_ptr)
3703 {
3704 	struct qeth_card *card = (struct qeth_card *)card_ptr;
3705 
3706 	napi_schedule_irqoff(&card->napi);
3707 }
3708 
3709 int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3710 {
3711 	int rc;
3712 
3713 	if (card->options.cq ==  QETH_CQ_NOTAVAILABLE) {
3714 		rc = -1;
3715 		goto out;
3716 	} else {
3717 		if (card->options.cq == cq) {
3718 			rc = 0;
3719 			goto out;
3720 		}
3721 
3722 		qeth_free_qdio_queues(card);
3723 		card->options.cq = cq;
3724 		rc = 0;
3725 	}
3726 out:
3727 	return rc;
3728 
3729 }
3730 EXPORT_SYMBOL_GPL(qeth_configure_cq);
3731 
3732 static void qeth_qdio_handle_aob(struct qeth_card *card, struct qaob *aob)
3733 {
3734 	struct qeth_qaob_priv1 *priv = (struct qeth_qaob_priv1 *)&aob->user1;
3735 	unsigned int queue_no = priv->queue_no;
3736 
3737 	BUILD_BUG_ON(sizeof(*priv) > ARRAY_SIZE(aob->user1));
3738 
3739 	if (xchg(&priv->state, QETH_QAOB_DONE) == QETH_QAOB_PENDING &&
3740 	    queue_no < card->qdio.no_out_queues)
3741 		napi_schedule(&card->qdio.out_qs[queue_no]->napi);
3742 }
3743 
3744 static void qeth_qdio_cq_handler(struct qeth_card *card, unsigned int qdio_err,
3745 				 unsigned int queue, int first_element,
3746 				 int count)
3747 {
3748 	struct qeth_qdio_q *cq = card->qdio.c_q;
3749 	int i;
3750 	int rc;
3751 
3752 	QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3753 	QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3754 	QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3755 
3756 	if (qdio_err) {
3757 		netif_tx_stop_all_queues(card->dev);
3758 		qeth_schedule_recovery(card);
3759 		return;
3760 	}
3761 
3762 	for (i = first_element; i < first_element + count; ++i) {
3763 		struct qdio_buffer *buffer = cq->qdio_bufs[QDIO_BUFNR(i)];
3764 		int e = 0;
3765 
3766 		while ((e < QDIO_MAX_ELEMENTS_PER_BUFFER) &&
3767 		       buffer->element[e].addr) {
3768 			unsigned long phys_aob_addr = buffer->element[e].addr;
3769 
3770 			qeth_qdio_handle_aob(card, phys_to_virt(phys_aob_addr));
3771 			++e;
3772 		}
3773 		qeth_scrub_qdio_buffer(buffer, QDIO_MAX_ELEMENTS_PER_BUFFER);
3774 	}
3775 	rc = qdio_add_bufs_to_input_queue(CARD_DDEV(card), queue,
3776 					  cq->next_buf_to_init, count);
3777 	if (rc) {
3778 		dev_warn(&card->gdev->dev,
3779 			"QDIO reported an error, rc=%i\n", rc);
3780 		QETH_CARD_TEXT(card, 2, "qcqherr");
3781 	}
3782 
3783 	cq->next_buf_to_init = QDIO_BUFNR(cq->next_buf_to_init + count);
3784 }
3785 
3786 static void qeth_qdio_input_handler(struct ccw_device *ccwdev,
3787 				    unsigned int qdio_err, int queue,
3788 				    int first_elem, int count,
3789 				    unsigned long card_ptr)
3790 {
3791 	struct qeth_card *card = (struct qeth_card *)card_ptr;
3792 
3793 	QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3794 	QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3795 
3796 	if (qdio_err)
3797 		qeth_schedule_recovery(card);
3798 }
3799 
3800 static void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3801 				     unsigned int qdio_error, int __queue,
3802 				     int first_element, int count,
3803 				     unsigned long card_ptr)
3804 {
3805 	struct qeth_card *card        = (struct qeth_card *) card_ptr;
3806 
3807 	QETH_CARD_TEXT(card, 2, "achkcond");
3808 	netif_tx_stop_all_queues(card->dev);
3809 	qeth_schedule_recovery(card);
3810 }
3811 
3812 /*
3813  * Note: Function assumes that we have 4 outbound queues.
3814  */
3815 static int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb)
3816 {
3817 	struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
3818 	u8 tos;
3819 
3820 	switch (card->qdio.do_prio_queueing) {
3821 	case QETH_PRIO_Q_ING_TOS:
3822 	case QETH_PRIO_Q_ING_PREC:
3823 		switch (vlan_get_protocol(skb)) {
3824 		case htons(ETH_P_IP):
3825 			tos = ipv4_get_dsfield(ip_hdr(skb));
3826 			break;
3827 		case htons(ETH_P_IPV6):
3828 			tos = ipv6_get_dsfield(ipv6_hdr(skb));
3829 			break;
3830 		default:
3831 			return card->qdio.default_out_queue;
3832 		}
3833 		if (card->qdio.do_prio_queueing == QETH_PRIO_Q_ING_PREC)
3834 			return ~tos >> 6 & 3;
3835 		if (tos & IPTOS_MINCOST)
3836 			return 3;
3837 		if (tos & IPTOS_RELIABILITY)
3838 			return 2;
3839 		if (tos & IPTOS_THROUGHPUT)
3840 			return 1;
3841 		if (tos & IPTOS_LOWDELAY)
3842 			return 0;
3843 		break;
3844 	case QETH_PRIO_Q_ING_SKB:
3845 		if (skb->priority > 5)
3846 			return 0;
3847 		return ~skb->priority >> 1 & 3;
3848 	case QETH_PRIO_Q_ING_VLAN:
3849 		if (veth->h_vlan_proto == htons(ETH_P_8021Q))
3850 			return ~ntohs(veth->h_vlan_TCI) >>
3851 			       (VLAN_PRIO_SHIFT + 1) & 3;
3852 		break;
3853 	case QETH_PRIO_Q_ING_FIXED:
3854 		return card->qdio.default_out_queue;
3855 	default:
3856 		break;
3857 	}
3858 	return card->qdio.default_out_queue;
3859 }
3860 
3861 /**
3862  * qeth_get_elements_for_frags() -	find number of SBALEs for skb frags.
3863  * @skb:				SKB address
3864  *
3865  * Returns the number of pages, and thus QDIO buffer elements, needed to cover
3866  * fragmented part of the SKB. Returns zero for linear SKB.
3867  */
3868 static int qeth_get_elements_for_frags(struct sk_buff *skb)
3869 {
3870 	int cnt, elements = 0;
3871 
3872 	for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3873 		skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
3874 
3875 		elements += qeth_get_elements_for_range(
3876 			(addr_t)skb_frag_address(frag),
3877 			(addr_t)skb_frag_address(frag) + skb_frag_size(frag));
3878 	}
3879 	return elements;
3880 }
3881 
3882 /**
3883  * qeth_count_elements() -	Counts the number of QDIO buffer elements needed
3884  *				to transmit an skb.
3885  * @skb:			the skb to operate on.
3886  * @data_offset:		skip this part of the skb's linear data
3887  *
3888  * Returns the number of pages, and thus QDIO buffer elements, needed to map the
3889  * skb's data (both its linear part and paged fragments).
3890  */
3891 static unsigned int qeth_count_elements(struct sk_buff *skb,
3892 					unsigned int data_offset)
3893 {
3894 	unsigned int elements = qeth_get_elements_for_frags(skb);
3895 	addr_t end = (addr_t)skb->data + skb_headlen(skb);
3896 	addr_t start = (addr_t)skb->data + data_offset;
3897 
3898 	if (start != end)
3899 		elements += qeth_get_elements_for_range(start, end);
3900 	return elements;
3901 }
3902 
3903 #define QETH_HDR_CACHE_OBJ_SIZE		(sizeof(struct qeth_hdr_tso) + \
3904 					 MAX_TCP_HEADER)
3905 
3906 /**
3907  * qeth_add_hw_header() - add a HW header to an skb.
3908  * @queue: TX queue that the skb will be placed on.
3909  * @skb: skb that the HW header should be added to.
3910  * @hdr: double pointer to a qeth_hdr. When returning with >= 0,
3911  *	 it contains a valid pointer to a qeth_hdr.
3912  * @hdr_len: length of the HW header.
3913  * @proto_len: length of protocol headers that need to be in same page as the
3914  *	       HW header.
3915  * @elements: returns the required number of buffer elements for this skb.
3916  *
3917  * Returns the pushed length. If the header can't be pushed on
3918  * (eg. because it would cross a page boundary), it is allocated from
3919  * the cache instead and 0 is returned.
3920  * The number of needed buffer elements is returned in @elements.
3921  * Error to create the hdr is indicated by returning with < 0.
3922  */
3923 static int qeth_add_hw_header(struct qeth_qdio_out_q *queue,
3924 			      struct sk_buff *skb, struct qeth_hdr **hdr,
3925 			      unsigned int hdr_len, unsigned int proto_len,
3926 			      unsigned int *elements)
3927 {
3928 	gfp_t gfp = GFP_ATOMIC | (skb_pfmemalloc(skb) ? __GFP_MEMALLOC : 0);
3929 	const unsigned int contiguous = proto_len ? proto_len : 1;
3930 	const unsigned int max_elements = queue->max_elements;
3931 	unsigned int __elements;
3932 	addr_t start, end;
3933 	bool push_ok;
3934 	int rc;
3935 
3936 check_layout:
3937 	start = (addr_t)skb->data - hdr_len;
3938 	end = (addr_t)skb->data;
3939 
3940 	if (qeth_get_elements_for_range(start, end + contiguous) == 1) {
3941 		/* Push HW header into same page as first protocol header. */
3942 		push_ok = true;
3943 		/* ... but TSO always needs a separate element for headers: */
3944 		if (skb_is_gso(skb))
3945 			__elements = 1 + qeth_count_elements(skb, proto_len);
3946 		else
3947 			__elements = qeth_count_elements(skb, 0);
3948 	} else if (!proto_len && PAGE_ALIGNED(skb->data)) {
3949 		/* Push HW header into preceding page, flush with skb->data. */
3950 		push_ok = true;
3951 		__elements = 1 + qeth_count_elements(skb, 0);
3952 	} else {
3953 		/* Use header cache, copy protocol headers up. */
3954 		push_ok = false;
3955 		__elements = 1 + qeth_count_elements(skb, proto_len);
3956 	}
3957 
3958 	/* Compress skb to fit into one IO buffer: */
3959 	if (__elements > max_elements) {
3960 		if (!skb_is_nonlinear(skb)) {
3961 			/* Drop it, no easy way of shrinking it further. */
3962 			QETH_DBF_MESSAGE(2, "Dropped an oversized skb (Max Elements=%u / Actual=%u / Length=%u).\n",
3963 					 max_elements, __elements, skb->len);
3964 			return -E2BIG;
3965 		}
3966 
3967 		rc = skb_linearize(skb);
3968 		if (rc) {
3969 			QETH_TXQ_STAT_INC(queue, skbs_linearized_fail);
3970 			return rc;
3971 		}
3972 
3973 		QETH_TXQ_STAT_INC(queue, skbs_linearized);
3974 		/* Linearization changed the layout, re-evaluate: */
3975 		goto check_layout;
3976 	}
3977 
3978 	*elements = __elements;
3979 	/* Add the header: */
3980 	if (push_ok) {
3981 		*hdr = skb_push(skb, hdr_len);
3982 		return hdr_len;
3983 	}
3984 
3985 	/* Fall back to cache element with known-good alignment: */
3986 	if (hdr_len + proto_len > QETH_HDR_CACHE_OBJ_SIZE)
3987 		return -E2BIG;
3988 	*hdr = kmem_cache_alloc(qeth_core_header_cache, gfp);
3989 	if (!*hdr)
3990 		return -ENOMEM;
3991 	/* Copy protocol headers behind HW header: */
3992 	skb_copy_from_linear_data(skb, ((char *)*hdr) + hdr_len, proto_len);
3993 	return 0;
3994 }
3995 
3996 static bool qeth_iqd_may_bulk(struct qeth_qdio_out_q *queue,
3997 			      struct sk_buff *curr_skb,
3998 			      struct qeth_hdr *curr_hdr)
3999 {
4000 	struct qeth_qdio_out_buffer *buffer = queue->bufs[queue->bulk_start];
4001 	struct qeth_hdr *prev_hdr = queue->prev_hdr;
4002 
4003 	if (!prev_hdr)
4004 		return true;
4005 
4006 	/* All packets must have the same target: */
4007 	if (curr_hdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
4008 		struct sk_buff *prev_skb = skb_peek(&buffer->skb_list);
4009 
4010 		return ether_addr_equal(eth_hdr(prev_skb)->h_dest,
4011 					eth_hdr(curr_skb)->h_dest) &&
4012 		       qeth_l2_same_vlan(&prev_hdr->hdr.l2, &curr_hdr->hdr.l2);
4013 	}
4014 
4015 	return qeth_l3_same_next_hop(&prev_hdr->hdr.l3, &curr_hdr->hdr.l3) &&
4016 	       qeth_l3_iqd_same_vlan(&prev_hdr->hdr.l3, &curr_hdr->hdr.l3);
4017 }
4018 
4019 /**
4020  * qeth_fill_buffer() - map skb into an output buffer
4021  * @buf:	buffer to transport the skb
4022  * @skb:	skb to map into the buffer
4023  * @hdr:	qeth_hdr for this skb. Either at skb->data, or allocated
4024  *		from qeth_core_header_cache.
4025  * @offset:	when mapping the skb, start at skb->data + offset
4026  * @hd_len:	if > 0, build a dedicated header element of this size
4027  */
4028 static unsigned int qeth_fill_buffer(struct qeth_qdio_out_buffer *buf,
4029 				     struct sk_buff *skb, struct qeth_hdr *hdr,
4030 				     unsigned int offset, unsigned int hd_len)
4031 {
4032 	struct qdio_buffer *buffer = buf->buffer;
4033 	int element = buf->next_element_to_fill;
4034 	int length = skb_headlen(skb) - offset;
4035 	char *data = skb->data + offset;
4036 	unsigned int elem_length, cnt;
4037 	bool is_first_elem = true;
4038 
4039 	__skb_queue_tail(&buf->skb_list, skb);
4040 
4041 	/* build dedicated element for HW Header */
4042 	if (hd_len) {
4043 		is_first_elem = false;
4044 
4045 		buffer->element[element].addr = virt_to_phys(hdr);
4046 		buffer->element[element].length = hd_len;
4047 		buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
4048 
4049 		/* HW header is allocated from cache: */
4050 		if ((void *)hdr != skb->data)
4051 			__set_bit(element, buf->from_kmem_cache);
4052 		/* HW header was pushed and is contiguous with linear part: */
4053 		else if (length > 0 && !PAGE_ALIGNED(data) &&
4054 			 (data == (char *)hdr + hd_len))
4055 			buffer->element[element].eflags |=
4056 				SBAL_EFLAGS_CONTIGUOUS;
4057 
4058 		element++;
4059 	}
4060 
4061 	/* map linear part into buffer element(s) */
4062 	while (length > 0) {
4063 		elem_length = min_t(unsigned int, length,
4064 				    PAGE_SIZE - offset_in_page(data));
4065 
4066 		buffer->element[element].addr = virt_to_phys(data);
4067 		buffer->element[element].length = elem_length;
4068 		length -= elem_length;
4069 		if (is_first_elem) {
4070 			is_first_elem = false;
4071 			if (length || skb_is_nonlinear(skb))
4072 				/* skb needs additional elements */
4073 				buffer->element[element].eflags =
4074 					SBAL_EFLAGS_FIRST_FRAG;
4075 			else
4076 				buffer->element[element].eflags = 0;
4077 		} else {
4078 			buffer->element[element].eflags =
4079 				SBAL_EFLAGS_MIDDLE_FRAG;
4080 		}
4081 
4082 		data += elem_length;
4083 		element++;
4084 	}
4085 
4086 	/* map page frags into buffer element(s) */
4087 	for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
4088 		skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
4089 
4090 		data = skb_frag_address(frag);
4091 		length = skb_frag_size(frag);
4092 		while (length > 0) {
4093 			elem_length = min_t(unsigned int, length,
4094 					    PAGE_SIZE - offset_in_page(data));
4095 
4096 			buffer->element[element].addr = virt_to_phys(data);
4097 			buffer->element[element].length = elem_length;
4098 			buffer->element[element].eflags =
4099 				SBAL_EFLAGS_MIDDLE_FRAG;
4100 
4101 			length -= elem_length;
4102 			data += elem_length;
4103 			element++;
4104 		}
4105 	}
4106 
4107 	if (buffer->element[element - 1].eflags)
4108 		buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
4109 	buf->next_element_to_fill = element;
4110 	return element;
4111 }
4112 
4113 static int __qeth_xmit(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4114 		       struct sk_buff *skb, unsigned int elements,
4115 		       struct qeth_hdr *hdr, unsigned int offset,
4116 		       unsigned int hd_len)
4117 {
4118 	unsigned int bytes = qdisc_pkt_len(skb);
4119 	struct qeth_qdio_out_buffer *buffer;
4120 	unsigned int next_element;
4121 	struct netdev_queue *txq;
4122 	bool stopped = false;
4123 	bool flush;
4124 
4125 	buffer = queue->bufs[QDIO_BUFNR(queue->bulk_start + queue->bulk_count)];
4126 	txq = netdev_get_tx_queue(card->dev, skb_get_queue_mapping(skb));
4127 
4128 	/* Just a sanity check, the wake/stop logic should ensure that we always
4129 	 * get a free buffer.
4130 	 */
4131 	if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4132 		return -EBUSY;
4133 
4134 	flush = !qeth_iqd_may_bulk(queue, skb, hdr);
4135 
4136 	if (flush ||
4137 	    (buffer->next_element_to_fill + elements > queue->max_elements)) {
4138 		if (buffer->next_element_to_fill > 0) {
4139 			atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4140 			queue->bulk_count++;
4141 		}
4142 
4143 		if (queue->bulk_count >= queue->bulk_max)
4144 			flush = true;
4145 
4146 		if (flush)
4147 			qeth_flush_queue(queue);
4148 
4149 		buffer = queue->bufs[QDIO_BUFNR(queue->bulk_start +
4150 						queue->bulk_count)];
4151 
4152 		/* Sanity-check again: */
4153 		if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4154 			return -EBUSY;
4155 	}
4156 
4157 	if (buffer->next_element_to_fill == 0 &&
4158 	    atomic_inc_return(&queue->used_buffers) >= QDIO_MAX_BUFFERS_PER_Q) {
4159 		/* If a TX completion happens right _here_ and misses to wake
4160 		 * the txq, then our re-check below will catch the race.
4161 		 */
4162 		QETH_TXQ_STAT_INC(queue, stopped);
4163 		netif_tx_stop_queue(txq);
4164 		stopped = true;
4165 	}
4166 
4167 	next_element = qeth_fill_buffer(buffer, skb, hdr, offset, hd_len);
4168 	buffer->bytes += bytes;
4169 	buffer->frames += skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1;
4170 	queue->prev_hdr = hdr;
4171 
4172 	flush = __netdev_tx_sent_queue(txq, bytes,
4173 				       !stopped && netdev_xmit_more());
4174 
4175 	if (flush || next_element >= queue->max_elements) {
4176 		atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4177 		queue->bulk_count++;
4178 
4179 		if (queue->bulk_count >= queue->bulk_max)
4180 			flush = true;
4181 
4182 		if (flush)
4183 			qeth_flush_queue(queue);
4184 	}
4185 
4186 	if (stopped && !qeth_out_queue_is_full(queue))
4187 		netif_tx_start_queue(txq);
4188 	return 0;
4189 }
4190 
4191 static int qeth_do_send_packet(struct qeth_card *card,
4192 			       struct qeth_qdio_out_q *queue,
4193 			       struct sk_buff *skb, struct qeth_hdr *hdr,
4194 			       unsigned int offset, unsigned int hd_len,
4195 			       unsigned int elements_needed)
4196 {
4197 	unsigned int start_index = queue->next_buf_to_fill;
4198 	struct qeth_qdio_out_buffer *buffer;
4199 	unsigned int next_element;
4200 	struct netdev_queue *txq;
4201 	bool stopped = false;
4202 	int flush_count = 0;
4203 	int do_pack = 0;
4204 	int rc = 0;
4205 
4206 	buffer = queue->bufs[queue->next_buf_to_fill];
4207 
4208 	/* Just a sanity check, the wake/stop logic should ensure that we always
4209 	 * get a free buffer.
4210 	 */
4211 	if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4212 		return -EBUSY;
4213 
4214 	txq = netdev_get_tx_queue(card->dev, skb_get_queue_mapping(skb));
4215 
4216 	/* check if we need to switch packing state of this queue */
4217 	qeth_switch_to_packing_if_needed(queue);
4218 	if (queue->do_pack) {
4219 		do_pack = 1;
4220 		/* does packet fit in current buffer? */
4221 		if (buffer->next_element_to_fill + elements_needed >
4222 		    queue->max_elements) {
4223 			/* ... no -> set state PRIMED */
4224 			atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4225 			flush_count++;
4226 			queue->next_buf_to_fill =
4227 				QDIO_BUFNR(queue->next_buf_to_fill + 1);
4228 			buffer = queue->bufs[queue->next_buf_to_fill];
4229 
4230 			/* We stepped forward, so sanity-check again: */
4231 			if (atomic_read(&buffer->state) !=
4232 			    QETH_QDIO_BUF_EMPTY) {
4233 				qeth_flush_buffers(queue, start_index,
4234 							   flush_count);
4235 				rc = -EBUSY;
4236 				goto out;
4237 			}
4238 		}
4239 	}
4240 
4241 	if (buffer->next_element_to_fill == 0 &&
4242 	    atomic_inc_return(&queue->used_buffers) >= QDIO_MAX_BUFFERS_PER_Q) {
4243 		/* If a TX completion happens right _here_ and misses to wake
4244 		 * the txq, then our re-check below will catch the race.
4245 		 */
4246 		QETH_TXQ_STAT_INC(queue, stopped);
4247 		netif_tx_stop_queue(txq);
4248 		stopped = true;
4249 	}
4250 
4251 	next_element = qeth_fill_buffer(buffer, skb, hdr, offset, hd_len);
4252 	buffer->bytes += qdisc_pkt_len(skb);
4253 	buffer->frames += skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1;
4254 
4255 	if (queue->do_pack)
4256 		QETH_TXQ_STAT_INC(queue, skbs_pack);
4257 	if (!queue->do_pack || stopped || next_element >= queue->max_elements) {
4258 		flush_count++;
4259 		atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4260 		queue->next_buf_to_fill =
4261 				QDIO_BUFNR(queue->next_buf_to_fill + 1);
4262 	}
4263 
4264 	if (flush_count)
4265 		qeth_flush_buffers(queue, start_index, flush_count);
4266 
4267 out:
4268 	if (do_pack)
4269 		QETH_TXQ_STAT_ADD(queue, bufs_pack, flush_count);
4270 
4271 	if (stopped && !qeth_out_queue_is_full(queue))
4272 		netif_tx_start_queue(txq);
4273 	return rc;
4274 }
4275 
4276 static void qeth_fill_tso_ext(struct qeth_hdr_tso *hdr,
4277 			      unsigned int payload_len, struct sk_buff *skb,
4278 			      unsigned int proto_len)
4279 {
4280 	struct qeth_hdr_ext_tso *ext = &hdr->ext;
4281 
4282 	ext->hdr_tot_len = sizeof(*ext);
4283 	ext->imb_hdr_no = 1;
4284 	ext->hdr_type = 1;
4285 	ext->hdr_version = 1;
4286 	ext->hdr_len = 28;
4287 	ext->payload_len = payload_len;
4288 	ext->mss = skb_shinfo(skb)->gso_size;
4289 	ext->dg_hdr_len = proto_len;
4290 }
4291 
4292 int qeth_xmit(struct qeth_card *card, struct sk_buff *skb,
4293 	      struct qeth_qdio_out_q *queue, __be16 proto,
4294 	      void (*fill_header)(struct qeth_qdio_out_q *queue,
4295 				  struct qeth_hdr *hdr, struct sk_buff *skb,
4296 				  __be16 proto, unsigned int data_len))
4297 {
4298 	unsigned int proto_len, hw_hdr_len;
4299 	unsigned int frame_len = skb->len;
4300 	bool is_tso = skb_is_gso(skb);
4301 	unsigned int data_offset = 0;
4302 	struct qeth_hdr *hdr = NULL;
4303 	unsigned int hd_len = 0;
4304 	unsigned int elements;
4305 	int push_len, rc;
4306 
4307 	if (is_tso) {
4308 		hw_hdr_len = sizeof(struct qeth_hdr_tso);
4309 		proto_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
4310 	} else {
4311 		hw_hdr_len = sizeof(struct qeth_hdr);
4312 		proto_len = (IS_IQD(card) && IS_LAYER2(card)) ? ETH_HLEN : 0;
4313 	}
4314 
4315 	rc = skb_cow_head(skb, hw_hdr_len);
4316 	if (rc)
4317 		return rc;
4318 
4319 	push_len = qeth_add_hw_header(queue, skb, &hdr, hw_hdr_len, proto_len,
4320 				      &elements);
4321 	if (push_len < 0)
4322 		return push_len;
4323 	if (is_tso || !push_len) {
4324 		/* HW header needs its own buffer element. */
4325 		hd_len = hw_hdr_len + proto_len;
4326 		data_offset = push_len + proto_len;
4327 	}
4328 	memset(hdr, 0, hw_hdr_len);
4329 	fill_header(queue, hdr, skb, proto, frame_len);
4330 	if (is_tso)
4331 		qeth_fill_tso_ext((struct qeth_hdr_tso *) hdr,
4332 				  frame_len - proto_len, skb, proto_len);
4333 
4334 	if (IS_IQD(card)) {
4335 		rc = __qeth_xmit(card, queue, skb, elements, hdr, data_offset,
4336 				 hd_len);
4337 	} else {
4338 		/* TODO: drop skb_orphan() once TX completion is fast enough */
4339 		skb_orphan(skb);
4340 		spin_lock(&queue->lock);
4341 		rc = qeth_do_send_packet(card, queue, skb, hdr, data_offset,
4342 					 hd_len, elements);
4343 		spin_unlock(&queue->lock);
4344 	}
4345 
4346 	if (rc && !push_len)
4347 		kmem_cache_free(qeth_core_header_cache, hdr);
4348 
4349 	return rc;
4350 }
4351 EXPORT_SYMBOL_GPL(qeth_xmit);
4352 
4353 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
4354 		struct qeth_reply *reply, unsigned long data)
4355 {
4356 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4357 	struct qeth_ipacmd_setadpparms *setparms;
4358 
4359 	QETH_CARD_TEXT(card, 4, "prmadpcb");
4360 
4361 	setparms = &(cmd->data.setadapterparms);
4362 	if (qeth_setadpparms_inspect_rc(cmd)) {
4363 		QETH_CARD_TEXT_(card, 4, "prmrc%x", cmd->hdr.return_code);
4364 		setparms->data.mode = SET_PROMISC_MODE_OFF;
4365 	}
4366 	card->info.promisc_mode = setparms->data.mode;
4367 	return (cmd->hdr.return_code) ? -EIO : 0;
4368 }
4369 
4370 void qeth_setadp_promisc_mode(struct qeth_card *card, bool enable)
4371 {
4372 	enum qeth_ipa_promisc_modes mode = enable ? SET_PROMISC_MODE_ON :
4373 						    SET_PROMISC_MODE_OFF;
4374 	struct qeth_cmd_buffer *iob;
4375 	struct qeth_ipa_cmd *cmd;
4376 
4377 	QETH_CARD_TEXT(card, 4, "setprom");
4378 	QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
4379 
4380 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
4381 				   SETADP_DATA_SIZEOF(mode));
4382 	if (!iob)
4383 		return;
4384 	cmd = __ipa_cmd(iob);
4385 	cmd->data.setadapterparms.data.mode = mode;
4386 	qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
4387 }
4388 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
4389 
4390 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
4391 		struct qeth_reply *reply, unsigned long data)
4392 {
4393 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4394 	struct qeth_ipacmd_setadpparms *adp_cmd;
4395 
4396 	QETH_CARD_TEXT(card, 4, "chgmaccb");
4397 	if (qeth_setadpparms_inspect_rc(cmd))
4398 		return -EIO;
4399 
4400 	adp_cmd = &cmd->data.setadapterparms;
4401 	if (!is_valid_ether_addr(adp_cmd->data.change_addr.addr))
4402 		return -EADDRNOTAVAIL;
4403 
4404 	if (IS_LAYER2(card) && IS_OSD(card) && !IS_VM_NIC(card) &&
4405 	    !(adp_cmd->hdr.flags & QETH_SETADP_FLAGS_VIRTUAL_MAC))
4406 		return -EADDRNOTAVAIL;
4407 
4408 	eth_hw_addr_set(card->dev, adp_cmd->data.change_addr.addr);
4409 	return 0;
4410 }
4411 
4412 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
4413 {
4414 	int rc;
4415 	struct qeth_cmd_buffer *iob;
4416 	struct qeth_ipa_cmd *cmd;
4417 
4418 	QETH_CARD_TEXT(card, 4, "chgmac");
4419 
4420 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
4421 				   SETADP_DATA_SIZEOF(change_addr));
4422 	if (!iob)
4423 		return -ENOMEM;
4424 	cmd = __ipa_cmd(iob);
4425 	cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
4426 	cmd->data.setadapterparms.data.change_addr.addr_size = ETH_ALEN;
4427 	ether_addr_copy(cmd->data.setadapterparms.data.change_addr.addr,
4428 			card->dev->dev_addr);
4429 	rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
4430 			       NULL);
4431 	return rc;
4432 }
4433 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
4434 
4435 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
4436 		struct qeth_reply *reply, unsigned long data)
4437 {
4438 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4439 	struct qeth_set_access_ctrl *access_ctrl_req;
4440 
4441 	QETH_CARD_TEXT(card, 4, "setaccb");
4442 
4443 	access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4444 	QETH_CARD_TEXT_(card, 2, "rc=%d",
4445 			cmd->data.setadapterparms.hdr.return_code);
4446 	if (cmd->data.setadapterparms.hdr.return_code !=
4447 						SET_ACCESS_CTRL_RC_SUCCESS)
4448 		QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%#x) on device %x: %#x\n",
4449 				 access_ctrl_req->subcmd_code, CARD_DEVID(card),
4450 				 cmd->data.setadapterparms.hdr.return_code);
4451 	switch (qeth_setadpparms_inspect_rc(cmd)) {
4452 	case SET_ACCESS_CTRL_RC_SUCCESS:
4453 		if (access_ctrl_req->subcmd_code == ISOLATION_MODE_NONE)
4454 			dev_info(&card->gdev->dev,
4455 			    "QDIO data connection isolation is deactivated\n");
4456 		else
4457 			dev_info(&card->gdev->dev,
4458 			    "QDIO data connection isolation is activated\n");
4459 		return 0;
4460 	case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
4461 		QETH_DBF_MESSAGE(2, "QDIO data connection isolation on device %x already deactivated\n",
4462 				 CARD_DEVID(card));
4463 		return 0;
4464 	case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
4465 		QETH_DBF_MESSAGE(2, "QDIO data connection isolation on device %x already activated\n",
4466 				 CARD_DEVID(card));
4467 		return 0;
4468 	case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
4469 		dev_err(&card->gdev->dev, "Adapter does not "
4470 			"support QDIO data connection isolation\n");
4471 		return -EOPNOTSUPP;
4472 	case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
4473 		dev_err(&card->gdev->dev,
4474 			"Adapter is dedicated. "
4475 			"QDIO data connection isolation not supported\n");
4476 		return -EOPNOTSUPP;
4477 	case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
4478 		dev_err(&card->gdev->dev,
4479 			"TSO does not permit QDIO data connection isolation\n");
4480 		return -EPERM;
4481 	case SET_ACCESS_CTRL_RC_REFLREL_UNSUPPORTED:
4482 		dev_err(&card->gdev->dev, "The adjacent switch port does not "
4483 			"support reflective relay mode\n");
4484 		return -EOPNOTSUPP;
4485 	case SET_ACCESS_CTRL_RC_REFLREL_FAILED:
4486 		dev_err(&card->gdev->dev, "The reflective relay mode cannot be "
4487 					"enabled at the adjacent switch port");
4488 		return -EREMOTEIO;
4489 	case SET_ACCESS_CTRL_RC_REFLREL_DEACT_FAILED:
4490 		dev_warn(&card->gdev->dev, "Turning off reflective relay mode "
4491 					"at the adjacent switch failed\n");
4492 		/* benign error while disabling ISOLATION_MODE_FWD */
4493 		return 0;
4494 	default:
4495 		return -EIO;
4496 	}
4497 }
4498 
4499 int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
4500 				     enum qeth_ipa_isolation_modes mode)
4501 {
4502 	int rc;
4503 	struct qeth_cmd_buffer *iob;
4504 	struct qeth_ipa_cmd *cmd;
4505 	struct qeth_set_access_ctrl *access_ctrl_req;
4506 
4507 	QETH_CARD_TEXT(card, 4, "setacctl");
4508 
4509 	if (!qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
4510 		dev_err(&card->gdev->dev,
4511 			"Adapter does not support QDIO data connection isolation\n");
4512 		return -EOPNOTSUPP;
4513 	}
4514 
4515 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
4516 				   SETADP_DATA_SIZEOF(set_access_ctrl));
4517 	if (!iob)
4518 		return -ENOMEM;
4519 	cmd = __ipa_cmd(iob);
4520 	access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4521 	access_ctrl_req->subcmd_code = mode;
4522 
4523 	rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
4524 			       NULL);
4525 	if (rc) {
4526 		QETH_CARD_TEXT_(card, 2, "rc=%d", rc);
4527 		QETH_DBF_MESSAGE(3, "IPA(SET_ACCESS_CTRL(%d) on device %x: sent failed\n",
4528 				 rc, CARD_DEVID(card));
4529 	}
4530 
4531 	return rc;
4532 }
4533 
4534 void qeth_tx_timeout(struct net_device *dev, unsigned int txqueue)
4535 {
4536 	struct qeth_card *card;
4537 
4538 	card = dev->ml_priv;
4539 	QETH_CARD_TEXT(card, 4, "txtimeo");
4540 	qeth_schedule_recovery(card);
4541 }
4542 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
4543 
4544 static int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4545 {
4546 	struct qeth_card *card = dev->ml_priv;
4547 	int rc = 0;
4548 
4549 	switch (regnum) {
4550 	case MII_BMCR: /* Basic mode control register */
4551 		rc = BMCR_FULLDPLX;
4552 		if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
4553 		    (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH) &&
4554 		    (card->info.link_type != QETH_LINK_TYPE_25GBIT_ETH))
4555 			rc |= BMCR_SPEED100;
4556 		break;
4557 	case MII_BMSR: /* Basic mode status register */
4558 		rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4559 		     BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4560 		     BMSR_100BASE4;
4561 		break;
4562 	case MII_PHYSID1: /* PHYS ID 1 */
4563 		rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4564 		     dev->dev_addr[2];
4565 		rc = (rc >> 5) & 0xFFFF;
4566 		break;
4567 	case MII_PHYSID2: /* PHYS ID 2 */
4568 		rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4569 		break;
4570 	case MII_ADVERTISE: /* Advertisement control reg */
4571 		rc = ADVERTISE_ALL;
4572 		break;
4573 	case MII_LPA: /* Link partner ability reg */
4574 		rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4575 		     LPA_100BASE4 | LPA_LPACK;
4576 		break;
4577 	case MII_EXPANSION: /* Expansion register */
4578 		break;
4579 	case MII_DCOUNTER: /* disconnect counter */
4580 		break;
4581 	case MII_FCSCOUNTER: /* false carrier counter */
4582 		break;
4583 	case MII_NWAYTEST: /* N-way auto-neg test register */
4584 		break;
4585 	case MII_RERRCOUNTER: /* rx error counter */
4586 		rc = card->stats.rx_length_errors +
4587 		     card->stats.rx_frame_errors +
4588 		     card->stats.rx_fifo_errors;
4589 		break;
4590 	case MII_SREVISION: /* silicon revision */
4591 		break;
4592 	case MII_RESV1: /* reserved 1 */
4593 		break;
4594 	case MII_LBRERROR: /* loopback, rx, bypass error */
4595 		break;
4596 	case MII_PHYADDR: /* physical address */
4597 		break;
4598 	case MII_RESV2: /* reserved 2 */
4599 		break;
4600 	case MII_TPISTATUS: /* TPI status for 10mbps */
4601 		break;
4602 	case MII_NCONFIG: /* network interface config */
4603 		break;
4604 	default:
4605 		break;
4606 	}
4607 	return rc;
4608 }
4609 
4610 static int qeth_snmp_command_cb(struct qeth_card *card,
4611 				struct qeth_reply *reply, unsigned long data)
4612 {
4613 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4614 	struct qeth_arp_query_info *qinfo = reply->param;
4615 	struct qeth_ipacmd_setadpparms *adp_cmd;
4616 	unsigned int data_len;
4617 	void *snmp_data;
4618 
4619 	QETH_CARD_TEXT(card, 3, "snpcmdcb");
4620 
4621 	if (cmd->hdr.return_code) {
4622 		QETH_CARD_TEXT_(card, 4, "scer1%x", cmd->hdr.return_code);
4623 		return -EIO;
4624 	}
4625 	if (cmd->data.setadapterparms.hdr.return_code) {
4626 		cmd->hdr.return_code =
4627 			cmd->data.setadapterparms.hdr.return_code;
4628 		QETH_CARD_TEXT_(card, 4, "scer2%x", cmd->hdr.return_code);
4629 		return -EIO;
4630 	}
4631 
4632 	adp_cmd = &cmd->data.setadapterparms;
4633 	data_len = adp_cmd->hdr.cmdlength - sizeof(adp_cmd->hdr);
4634 	if (adp_cmd->hdr.seq_no == 1) {
4635 		snmp_data = &adp_cmd->data.snmp;
4636 	} else {
4637 		snmp_data = &adp_cmd->data.snmp.request;
4638 		data_len -= offsetof(struct qeth_snmp_cmd, request);
4639 	}
4640 
4641 	/* check if there is enough room in userspace */
4642 	if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4643 		QETH_CARD_TEXT_(card, 4, "scer3%i", -ENOSPC);
4644 		return -ENOSPC;
4645 	}
4646 	QETH_CARD_TEXT_(card, 4, "snore%i",
4647 			cmd->data.setadapterparms.hdr.used_total);
4648 	QETH_CARD_TEXT_(card, 4, "sseqn%i",
4649 			cmd->data.setadapterparms.hdr.seq_no);
4650 	/*copy entries to user buffer*/
4651 	memcpy(qinfo->udata + qinfo->udata_offset, snmp_data, data_len);
4652 	qinfo->udata_offset += data_len;
4653 
4654 	if (cmd->data.setadapterparms.hdr.seq_no <
4655 	    cmd->data.setadapterparms.hdr.used_total)
4656 		return 1;
4657 	return 0;
4658 }
4659 
4660 static int qeth_snmp_command(struct qeth_card *card, char __user *udata)
4661 {
4662 	struct qeth_snmp_ureq __user *ureq;
4663 	struct qeth_cmd_buffer *iob;
4664 	unsigned int req_len;
4665 	struct qeth_arp_query_info qinfo = {0, };
4666 	int rc = 0;
4667 
4668 	QETH_CARD_TEXT(card, 3, "snmpcmd");
4669 
4670 	if (IS_VM_NIC(card))
4671 		return -EOPNOTSUPP;
4672 
4673 	if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
4674 	    IS_LAYER3(card))
4675 		return -EOPNOTSUPP;
4676 
4677 	ureq = (struct qeth_snmp_ureq __user *) udata;
4678 	if (get_user(qinfo.udata_len, &ureq->hdr.data_len) ||
4679 	    get_user(req_len, &ureq->hdr.req_len))
4680 		return -EFAULT;
4681 
4682 	/* Sanitize user input, to avoid overflows in iob size calculation: */
4683 	if (req_len > QETH_BUFSIZE)
4684 		return -EINVAL;
4685 
4686 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL, req_len);
4687 	if (!iob)
4688 		return -ENOMEM;
4689 
4690 	if (copy_from_user(&__ipa_cmd(iob)->data.setadapterparms.data.snmp,
4691 			   &ureq->cmd, req_len)) {
4692 		qeth_put_cmd(iob);
4693 		return -EFAULT;
4694 	}
4695 
4696 	qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
4697 	if (!qinfo.udata) {
4698 		qeth_put_cmd(iob);
4699 		return -ENOMEM;
4700 	}
4701 	qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4702 
4703 	rc = qeth_send_ipa_cmd(card, iob, qeth_snmp_command_cb, &qinfo);
4704 	if (rc)
4705 		QETH_DBF_MESSAGE(2, "SNMP command failed on device %x: (%#x)\n",
4706 				 CARD_DEVID(card), rc);
4707 	else {
4708 		if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
4709 			rc = -EFAULT;
4710 	}
4711 
4712 	kfree(qinfo.udata);
4713 	return rc;
4714 }
4715 
4716 static int qeth_setadpparms_query_oat_cb(struct qeth_card *card,
4717 					 struct qeth_reply *reply,
4718 					 unsigned long data)
4719 {
4720 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
4721 	struct qeth_qoat_priv *priv = reply->param;
4722 	int resdatalen;
4723 
4724 	QETH_CARD_TEXT(card, 3, "qoatcb");
4725 	if (qeth_setadpparms_inspect_rc(cmd))
4726 		return -EIO;
4727 
4728 	resdatalen = cmd->data.setadapterparms.hdr.cmdlength;
4729 
4730 	if (resdatalen > (priv->buffer_len - priv->response_len))
4731 		return -ENOSPC;
4732 
4733 	memcpy(priv->buffer + priv->response_len,
4734 	       &cmd->data.setadapterparms.hdr, resdatalen);
4735 	priv->response_len += resdatalen;
4736 
4737 	if (cmd->data.setadapterparms.hdr.seq_no <
4738 	    cmd->data.setadapterparms.hdr.used_total)
4739 		return 1;
4740 	return 0;
4741 }
4742 
4743 static int qeth_query_oat_command(struct qeth_card *card, char __user *udata)
4744 {
4745 	int rc = 0;
4746 	struct qeth_cmd_buffer *iob;
4747 	struct qeth_ipa_cmd *cmd;
4748 	struct qeth_query_oat *oat_req;
4749 	struct qeth_query_oat_data oat_data;
4750 	struct qeth_qoat_priv priv;
4751 	void __user *tmp;
4752 
4753 	QETH_CARD_TEXT(card, 3, "qoatcmd");
4754 
4755 	if (!qeth_adp_supported(card, IPA_SETADP_QUERY_OAT))
4756 		return -EOPNOTSUPP;
4757 
4758 	if (copy_from_user(&oat_data, udata, sizeof(oat_data)))
4759 		return -EFAULT;
4760 
4761 	priv.buffer_len = oat_data.buffer_len;
4762 	priv.response_len = 0;
4763 	priv.buffer = vzalloc(oat_data.buffer_len);
4764 	if (!priv.buffer)
4765 		return -ENOMEM;
4766 
4767 	iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4768 				   SETADP_DATA_SIZEOF(query_oat));
4769 	if (!iob) {
4770 		rc = -ENOMEM;
4771 		goto out_free;
4772 	}
4773 	cmd = __ipa_cmd(iob);
4774 	oat_req = &cmd->data.setadapterparms.data.query_oat;
4775 	oat_req->subcmd_code = oat_data.command;
4776 
4777 	rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_query_oat_cb, &priv);
4778 	if (!rc) {
4779 		tmp = is_compat_task() ? compat_ptr(oat_data.ptr) :
4780 					 u64_to_user_ptr(oat_data.ptr);
4781 		oat_data.response_len = priv.response_len;
4782 
4783 		if (copy_to_user(tmp, priv.buffer, priv.response_len) ||
4784 		    copy_to_user(udata, &oat_data, sizeof(oat_data)))
4785 			rc = -EFAULT;
4786 	}
4787 
4788 out_free:
4789 	vfree(priv.buffer);
4790 	return rc;
4791 }
4792 
4793 static int qeth_init_link_info_oat_cb(struct qeth_card *card,
4794 				      struct qeth_reply *reply_priv,
4795 				      unsigned long data)
4796 {
4797 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
4798 	struct qeth_link_info *link_info = reply_priv->param;
4799 	struct qeth_query_oat_physical_if *phys_if;
4800 	struct qeth_query_oat_reply *reply;
4801 
4802 	QETH_CARD_TEXT(card, 2, "qoatincb");
4803 	if (qeth_setadpparms_inspect_rc(cmd))
4804 		return -EIO;
4805 
4806 	/* Multi-part reply is unexpected, don't bother: */
4807 	if (cmd->data.setadapterparms.hdr.used_total > 1)
4808 		return -EINVAL;
4809 
4810 	/* Expect the reply to start with phys_if data: */
4811 	reply = &cmd->data.setadapterparms.data.query_oat.reply[0];
4812 	if (reply->type != QETH_QOAT_REPLY_TYPE_PHYS_IF ||
4813 	    reply->length < sizeof(*reply))
4814 		return -EINVAL;
4815 
4816 	phys_if = &reply->phys_if;
4817 
4818 	switch (phys_if->speed_duplex) {
4819 	case QETH_QOAT_PHYS_SPEED_10M_HALF:
4820 		link_info->speed = SPEED_10;
4821 		link_info->duplex = DUPLEX_HALF;
4822 		break;
4823 	case QETH_QOAT_PHYS_SPEED_10M_FULL:
4824 		link_info->speed = SPEED_10;
4825 		link_info->duplex = DUPLEX_FULL;
4826 		break;
4827 	case QETH_QOAT_PHYS_SPEED_100M_HALF:
4828 		link_info->speed = SPEED_100;
4829 		link_info->duplex = DUPLEX_HALF;
4830 		break;
4831 	case QETH_QOAT_PHYS_SPEED_100M_FULL:
4832 		link_info->speed = SPEED_100;
4833 		link_info->duplex = DUPLEX_FULL;
4834 		break;
4835 	case QETH_QOAT_PHYS_SPEED_1000M_HALF:
4836 		link_info->speed = SPEED_1000;
4837 		link_info->duplex = DUPLEX_HALF;
4838 		break;
4839 	case QETH_QOAT_PHYS_SPEED_1000M_FULL:
4840 		link_info->speed = SPEED_1000;
4841 		link_info->duplex = DUPLEX_FULL;
4842 		break;
4843 	case QETH_QOAT_PHYS_SPEED_10G_FULL:
4844 		link_info->speed = SPEED_10000;
4845 		link_info->duplex = DUPLEX_FULL;
4846 		break;
4847 	case QETH_QOAT_PHYS_SPEED_25G_FULL:
4848 		link_info->speed = SPEED_25000;
4849 		link_info->duplex = DUPLEX_FULL;
4850 		break;
4851 	case QETH_QOAT_PHYS_SPEED_UNKNOWN:
4852 	default:
4853 		link_info->speed = SPEED_UNKNOWN;
4854 		link_info->duplex = DUPLEX_UNKNOWN;
4855 		break;
4856 	}
4857 
4858 	switch (phys_if->media_type) {
4859 	case QETH_QOAT_PHYS_MEDIA_COPPER:
4860 		link_info->port = PORT_TP;
4861 		link_info->link_mode = QETH_LINK_MODE_UNKNOWN;
4862 		break;
4863 	case QETH_QOAT_PHYS_MEDIA_FIBRE_SHORT:
4864 		link_info->port = PORT_FIBRE;
4865 		link_info->link_mode = QETH_LINK_MODE_FIBRE_SHORT;
4866 		break;
4867 	case QETH_QOAT_PHYS_MEDIA_FIBRE_LONG:
4868 		link_info->port = PORT_FIBRE;
4869 		link_info->link_mode = QETH_LINK_MODE_FIBRE_LONG;
4870 		break;
4871 	default:
4872 		link_info->port = PORT_OTHER;
4873 		link_info->link_mode = QETH_LINK_MODE_UNKNOWN;
4874 		break;
4875 	}
4876 
4877 	return 0;
4878 }
4879 
4880 static void qeth_init_link_info(struct qeth_card *card)
4881 {
4882 	qeth_default_link_info(card);
4883 
4884 	/* Get more accurate data via QUERY OAT: */
4885 	if (qeth_adp_supported(card, IPA_SETADP_QUERY_OAT)) {
4886 		struct qeth_link_info link_info;
4887 		struct qeth_cmd_buffer *iob;
4888 
4889 		iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4890 					   SETADP_DATA_SIZEOF(query_oat));
4891 		if (iob) {
4892 			struct qeth_ipa_cmd *cmd = __ipa_cmd(iob);
4893 			struct qeth_query_oat *oat_req;
4894 
4895 			oat_req = &cmd->data.setadapterparms.data.query_oat;
4896 			oat_req->subcmd_code = QETH_QOAT_SCOPE_INTERFACE;
4897 
4898 			if (!qeth_send_ipa_cmd(card, iob,
4899 					       qeth_init_link_info_oat_cb,
4900 					       &link_info)) {
4901 				if (link_info.speed != SPEED_UNKNOWN)
4902 					card->info.link_info.speed = link_info.speed;
4903 				if (link_info.duplex != DUPLEX_UNKNOWN)
4904 					card->info.link_info.duplex = link_info.duplex;
4905 				if (link_info.port != PORT_OTHER)
4906 					card->info.link_info.port = link_info.port;
4907 				if (link_info.link_mode != QETH_LINK_MODE_UNKNOWN)
4908 					card->info.link_info.link_mode = link_info.link_mode;
4909 			}
4910 		}
4911 	}
4912 }
4913 
4914 /**
4915  * qeth_vm_request_mac() - Request a hypervisor-managed MAC address
4916  * @card: pointer to a qeth_card
4917  *
4918  * Returns
4919  *	0, if a MAC address has been set for the card's netdevice
4920  *	a return code, for various error conditions
4921  */
4922 int qeth_vm_request_mac(struct qeth_card *card)
4923 {
4924 	struct diag26c_mac_resp *response;
4925 	struct diag26c_mac_req *request;
4926 	int rc;
4927 
4928 	QETH_CARD_TEXT(card, 2, "vmreqmac");
4929 
4930 	request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
4931 	response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
4932 	if (!request || !response) {
4933 		rc = -ENOMEM;
4934 		goto out;
4935 	}
4936 
4937 	request->resp_buf_len = sizeof(*response);
4938 	request->resp_version = DIAG26C_VERSION2;
4939 	request->op_code = DIAG26C_GET_MAC;
4940 	request->devno = card->info.ddev_devno;
4941 
4942 	QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4943 	rc = diag26c(request, response, DIAG26C_MAC_SERVICES);
4944 	QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4945 	if (rc)
4946 		goto out;
4947 	QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
4948 
4949 	if (request->resp_buf_len < sizeof(*response) ||
4950 	    response->version != request->resp_version) {
4951 		rc = -EIO;
4952 		QETH_CARD_TEXT(card, 2, "badresp");
4953 		QETH_CARD_HEX(card, 2, &request->resp_buf_len,
4954 			      sizeof(request->resp_buf_len));
4955 	} else if (!is_valid_ether_addr(response->mac)) {
4956 		rc = -EINVAL;
4957 		QETH_CARD_TEXT(card, 2, "badmac");
4958 		QETH_CARD_HEX(card, 2, response->mac, ETH_ALEN);
4959 	} else {
4960 		eth_hw_addr_set(card->dev, response->mac);
4961 	}
4962 
4963 out:
4964 	kfree(response);
4965 	kfree(request);
4966 	return rc;
4967 }
4968 EXPORT_SYMBOL_GPL(qeth_vm_request_mac);
4969 
4970 static void qeth_determine_capabilities(struct qeth_card *card)
4971 {
4972 	struct qeth_channel *channel = &card->data;
4973 	struct ccw_device *ddev = channel->ccwdev;
4974 	int rc;
4975 	int ddev_offline = 0;
4976 
4977 	QETH_CARD_TEXT(card, 2, "detcapab");
4978 	if (!ddev->online) {
4979 		ddev_offline = 1;
4980 		rc = qeth_start_channel(channel);
4981 		if (rc) {
4982 			QETH_CARD_TEXT_(card, 2, "3err%d", rc);
4983 			goto out;
4984 		}
4985 	}
4986 
4987 	rc = qeth_read_conf_data(card);
4988 	if (rc) {
4989 		QETH_DBF_MESSAGE(2, "qeth_read_conf_data on device %x returned %i\n",
4990 				 CARD_DEVID(card), rc);
4991 		QETH_CARD_TEXT_(card, 2, "5err%d", rc);
4992 		goto out_offline;
4993 	}
4994 
4995 	rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
4996 	if (rc)
4997 		QETH_CARD_TEXT_(card, 2, "6err%d", rc);
4998 
4999 	QETH_CARD_TEXT_(card, 2, "qfmt%d", card->ssqd.qfmt);
5000 	QETH_CARD_TEXT_(card, 2, "ac1:%02x", card->ssqd.qdioac1);
5001 	QETH_CARD_TEXT_(card, 2, "ac2:%04x", card->ssqd.qdioac2);
5002 	QETH_CARD_TEXT_(card, 2, "ac3:%04x", card->ssqd.qdioac3);
5003 	QETH_CARD_TEXT_(card, 2, "icnt%d", card->ssqd.icnt);
5004 	if (!((card->ssqd.qfmt != QDIO_IQDIO_QFMT) ||
5005 	    ((card->ssqd.qdioac1 & CHSC_AC1_INITIATE_INPUTQ) == 0) ||
5006 	    ((card->ssqd.qdioac3 & CHSC_AC3_FORMAT2_CQ_AVAILABLE) == 0))) {
5007 		dev_info(&card->gdev->dev,
5008 			"Completion Queueing supported\n");
5009 	} else {
5010 		card->options.cq = QETH_CQ_NOTAVAILABLE;
5011 	}
5012 
5013 out_offline:
5014 	if (ddev_offline == 1)
5015 		qeth_stop_channel(channel);
5016 out:
5017 	return;
5018 }
5019 
5020 static void qeth_read_ccw_conf_data(struct qeth_card *card)
5021 {
5022 	struct qeth_card_info *info = &card->info;
5023 	struct ccw_device *cdev = CARD_DDEV(card);
5024 	struct ccw_dev_id dev_id;
5025 
5026 	QETH_CARD_TEXT(card, 2, "ccwconfd");
5027 	ccw_device_get_id(cdev, &dev_id);
5028 
5029 	info->ddev_devno = dev_id.devno;
5030 	info->ids_valid = !ccw_device_get_cssid(cdev, &info->cssid) &&
5031 			  !ccw_device_get_iid(cdev, &info->iid) &&
5032 			  !ccw_device_get_chid(cdev, 0, &info->chid);
5033 	info->ssid = dev_id.ssid;
5034 
5035 	dev_info(&card->gdev->dev, "CHID: %x CHPID: %x\n",
5036 		 info->chid, info->chpid);
5037 
5038 	QETH_CARD_TEXT_(card, 3, "devn%x", info->ddev_devno);
5039 	QETH_CARD_TEXT_(card, 3, "cssid:%x", info->cssid);
5040 	QETH_CARD_TEXT_(card, 3, "iid:%x", info->iid);
5041 	QETH_CARD_TEXT_(card, 3, "ssid:%x", info->ssid);
5042 	QETH_CARD_TEXT_(card, 3, "chpid:%x", info->chpid);
5043 	QETH_CARD_TEXT_(card, 3, "chid:%x", info->chid);
5044 	QETH_CARD_TEXT_(card, 3, "idval%x", info->ids_valid);
5045 }
5046 
5047 static int qeth_qdio_establish(struct qeth_card *card)
5048 {
5049 	struct qdio_buffer **out_sbal_ptrs[QETH_MAX_OUT_QUEUES];
5050 	struct qdio_buffer **in_sbal_ptrs[QETH_MAX_IN_QUEUES];
5051 	struct qeth_qib_parms *qib_parms = NULL;
5052 	struct qdio_initialize init_data;
5053 	unsigned int no_input_qs = 1;
5054 	unsigned int i;
5055 	int rc = 0;
5056 
5057 	QETH_CARD_TEXT(card, 2, "qdioest");
5058 
5059 	if (!IS_IQD(card) && !IS_VM_NIC(card)) {
5060 		qib_parms = kzalloc(sizeof_field(struct qib, parm), GFP_KERNEL);
5061 		if (!qib_parms)
5062 			return -ENOMEM;
5063 
5064 		qeth_fill_qib_parms(card, qib_parms);
5065 	}
5066 
5067 	in_sbal_ptrs[0] = card->qdio.in_q->qdio_bufs;
5068 	if (card->options.cq == QETH_CQ_ENABLED) {
5069 		in_sbal_ptrs[1] = card->qdio.c_q->qdio_bufs;
5070 		no_input_qs++;
5071 	}
5072 
5073 	for (i = 0; i < card->qdio.no_out_queues; i++)
5074 		out_sbal_ptrs[i] = card->qdio.out_qs[i]->qdio_bufs;
5075 
5076 	memset(&init_data, 0, sizeof(struct qdio_initialize));
5077 	init_data.q_format		 = IS_IQD(card) ? QDIO_IQDIO_QFMT :
5078 							  QDIO_QETH_QFMT;
5079 	init_data.qib_param_field_format = 0;
5080 	init_data.qib_param_field	 = (void *)qib_parms;
5081 	init_data.no_input_qs		 = no_input_qs;
5082 	init_data.no_output_qs           = card->qdio.no_out_queues;
5083 	init_data.input_handler		 = qeth_qdio_input_handler;
5084 	init_data.output_handler	 = qeth_qdio_output_handler;
5085 	init_data.irq_poll		 = qeth_qdio_poll;
5086 	init_data.int_parm               = (unsigned long) card;
5087 	init_data.input_sbal_addr_array  = in_sbal_ptrs;
5088 	init_data.output_sbal_addr_array = out_sbal_ptrs;
5089 
5090 	if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
5091 		QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
5092 		rc = qdio_allocate(CARD_DDEV(card), init_data.no_input_qs,
5093 				   init_data.no_output_qs);
5094 		if (rc) {
5095 			atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5096 			goto out;
5097 		}
5098 		rc = qdio_establish(CARD_DDEV(card), &init_data);
5099 		if (rc) {
5100 			atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5101 			qdio_free(CARD_DDEV(card));
5102 		}
5103 	}
5104 
5105 	switch (card->options.cq) {
5106 	case QETH_CQ_ENABLED:
5107 		dev_info(&card->gdev->dev, "Completion Queue support enabled");
5108 		break;
5109 	case QETH_CQ_DISABLED:
5110 		dev_info(&card->gdev->dev, "Completion Queue support disabled");
5111 		break;
5112 	default:
5113 		break;
5114 	}
5115 
5116 out:
5117 	kfree(qib_parms);
5118 	return rc;
5119 }
5120 
5121 static void qeth_core_free_card(struct qeth_card *card)
5122 {
5123 	QETH_CARD_TEXT(card, 2, "freecrd");
5124 
5125 	unregister_service_level(&card->qeth_service_level);
5126 	debugfs_remove_recursive(card->debugfs);
5127 	qeth_put_cmd(card->read_cmd);
5128 	destroy_workqueue(card->event_wq);
5129 	dev_set_drvdata(&card->gdev->dev, NULL);
5130 	kfree(card);
5131 }
5132 
5133 static void qeth_trace_features(struct qeth_card *card)
5134 {
5135 	QETH_CARD_TEXT(card, 2, "features");
5136 	QETH_CARD_HEX(card, 2, &card->options.ipa4, sizeof(card->options.ipa4));
5137 	QETH_CARD_HEX(card, 2, &card->options.ipa6, sizeof(card->options.ipa6));
5138 	QETH_CARD_HEX(card, 2, &card->options.adp, sizeof(card->options.adp));
5139 	QETH_CARD_HEX(card, 2, &card->info.diagass_support,
5140 		      sizeof(card->info.diagass_support));
5141 }
5142 
5143 static struct ccw_device_id qeth_ids[] = {
5144 	{CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
5145 					.driver_info = QETH_CARD_TYPE_OSD},
5146 	{CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
5147 					.driver_info = QETH_CARD_TYPE_IQD},
5148 	{CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
5149 					.driver_info = QETH_CARD_TYPE_OSM},
5150 #ifdef CONFIG_QETH_OSX
5151 	{CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
5152 					.driver_info = QETH_CARD_TYPE_OSX},
5153 #endif
5154 	{},
5155 };
5156 MODULE_DEVICE_TABLE(ccw, qeth_ids);
5157 
5158 static struct ccw_driver qeth_ccw_driver = {
5159 	.driver = {
5160 		.owner = THIS_MODULE,
5161 		.name = "qeth",
5162 	},
5163 	.ids = qeth_ids,
5164 	.probe = ccwgroup_probe_ccwdev,
5165 	.remove = ccwgroup_remove_ccwdev,
5166 };
5167 
5168 static int qeth_hardsetup_card(struct qeth_card *card, bool *carrier_ok)
5169 {
5170 	int retries = 3;
5171 	int rc;
5172 
5173 	QETH_CARD_TEXT(card, 2, "hrdsetup");
5174 	atomic_set(&card->force_alloc_skb, 0);
5175 	rc = qeth_update_from_chp_desc(card);
5176 	if (rc)
5177 		return rc;
5178 retry:
5179 	if (retries < 3)
5180 		QETH_DBF_MESSAGE(2, "Retrying to do IDX activates on device %x.\n",
5181 				 CARD_DEVID(card));
5182 	rc = qeth_qdio_clear_card(card, !IS_IQD(card));
5183 	qeth_stop_channel(&card->data);
5184 	qeth_stop_channel(&card->write);
5185 	qeth_stop_channel(&card->read);
5186 	qdio_free(CARD_DDEV(card));
5187 
5188 	rc = qeth_start_channel(&card->read);
5189 	if (rc)
5190 		goto retriable;
5191 	rc = qeth_start_channel(&card->write);
5192 	if (rc)
5193 		goto retriable;
5194 	rc = qeth_start_channel(&card->data);
5195 	if (rc)
5196 		goto retriable;
5197 retriable:
5198 	if (rc == -ERESTARTSYS) {
5199 		QETH_CARD_TEXT(card, 2, "break1");
5200 		return rc;
5201 	} else if (rc) {
5202 		QETH_CARD_TEXT_(card, 2, "1err%d", rc);
5203 		if (--retries < 0)
5204 			goto out;
5205 		else
5206 			goto retry;
5207 	}
5208 
5209 	qeth_determine_capabilities(card);
5210 	qeth_read_ccw_conf_data(card);
5211 	qeth_idx_init(card);
5212 
5213 	rc = qeth_idx_activate_read_channel(card);
5214 	if (rc == -EINTR) {
5215 		QETH_CARD_TEXT(card, 2, "break2");
5216 		return rc;
5217 	} else if (rc) {
5218 		QETH_CARD_TEXT_(card, 2, "3err%d", rc);
5219 		if (--retries < 0)
5220 			goto out;
5221 		else
5222 			goto retry;
5223 	}
5224 
5225 	rc = qeth_idx_activate_write_channel(card);
5226 	if (rc == -EINTR) {
5227 		QETH_CARD_TEXT(card, 2, "break3");
5228 		return rc;
5229 	} else if (rc) {
5230 		QETH_CARD_TEXT_(card, 2, "4err%d", rc);
5231 		if (--retries < 0)
5232 			goto out;
5233 		else
5234 			goto retry;
5235 	}
5236 	card->read_or_write_problem = 0;
5237 	rc = qeth_mpc_initialize(card);
5238 	if (rc) {
5239 		QETH_CARD_TEXT_(card, 2, "5err%d", rc);
5240 		goto out;
5241 	}
5242 
5243 	rc = qeth_send_startlan(card);
5244 	if (rc) {
5245 		QETH_CARD_TEXT_(card, 2, "6err%d", rc);
5246 		if (rc == -ENETDOWN) {
5247 			dev_warn(&card->gdev->dev, "The LAN is offline\n");
5248 			*carrier_ok = false;
5249 		} else {
5250 			goto out;
5251 		}
5252 	} else {
5253 		*carrier_ok = true;
5254 	}
5255 
5256 	card->options.ipa4.supported = 0;
5257 	card->options.ipa6.supported = 0;
5258 	card->options.adp.supported = 0;
5259 	card->options.sbp.supported_funcs = 0;
5260 	card->info.diagass_support = 0;
5261 	rc = qeth_query_ipassists(card, QETH_PROT_IPV4);
5262 	if (rc == -ENOMEM)
5263 		goto out;
5264 	if (qeth_is_supported(card, IPA_IPV6)) {
5265 		rc = qeth_query_ipassists(card, QETH_PROT_IPV6);
5266 		if (rc == -ENOMEM)
5267 			goto out;
5268 	}
5269 	if (qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
5270 		rc = qeth_query_setadapterparms(card);
5271 		if (rc < 0) {
5272 			QETH_CARD_TEXT_(card, 2, "7err%d", rc);
5273 			goto out;
5274 		}
5275 	}
5276 	if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
5277 		rc = qeth_query_setdiagass(card);
5278 		if (rc)
5279 			QETH_CARD_TEXT_(card, 2, "8err%d", rc);
5280 	}
5281 
5282 	qeth_trace_features(card);
5283 
5284 	if (!qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP) ||
5285 	    (card->info.hwtrap && qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM)))
5286 		card->info.hwtrap = 0;
5287 
5288 	if (card->options.isolation != ISOLATION_MODE_NONE) {
5289 		rc = qeth_setadpparms_set_access_ctrl(card,
5290 						      card->options.isolation);
5291 		if (rc)
5292 			goto out;
5293 	}
5294 
5295 	qeth_init_link_info(card);
5296 
5297 	rc = qeth_init_qdio_queues(card);
5298 	if (rc) {
5299 		QETH_CARD_TEXT_(card, 2, "9err%d", rc);
5300 		goto out;
5301 	}
5302 
5303 	return 0;
5304 out:
5305 	dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
5306 		"an error on the device\n");
5307 	QETH_DBF_MESSAGE(2, "Initialization for device %x failed in hardsetup! rc=%d\n",
5308 			 CARD_DEVID(card), rc);
5309 	return rc;
5310 }
5311 
5312 static int qeth_set_online(struct qeth_card *card,
5313 			   const struct qeth_discipline *disc)
5314 {
5315 	bool carrier_ok;
5316 	int rc;
5317 
5318 	mutex_lock(&card->conf_mutex);
5319 	QETH_CARD_TEXT(card, 2, "setonlin");
5320 
5321 	rc = qeth_hardsetup_card(card, &carrier_ok);
5322 	if (rc) {
5323 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
5324 		rc = -ENODEV;
5325 		goto err_hardsetup;
5326 	}
5327 
5328 	qeth_print_status_message(card);
5329 
5330 	if (card->dev->reg_state != NETREG_REGISTERED)
5331 		/* no need for locking / error handling at this early stage: */
5332 		qeth_set_real_num_tx_queues(card, qeth_tx_actual_queues(card));
5333 
5334 	rc = disc->set_online(card, carrier_ok);
5335 	if (rc)
5336 		goto err_online;
5337 
5338 	/* let user_space know that device is online */
5339 	kobject_uevent(&card->gdev->dev.kobj, KOBJ_CHANGE);
5340 
5341 	mutex_unlock(&card->conf_mutex);
5342 	return 0;
5343 
5344 err_online:
5345 err_hardsetup:
5346 	qeth_qdio_clear_card(card, 0);
5347 	qeth_clear_working_pool_list(card);
5348 	qeth_flush_local_addrs(card);
5349 
5350 	qeth_stop_channel(&card->data);
5351 	qeth_stop_channel(&card->write);
5352 	qeth_stop_channel(&card->read);
5353 	qdio_free(CARD_DDEV(card));
5354 
5355 	mutex_unlock(&card->conf_mutex);
5356 	return rc;
5357 }
5358 
5359 int qeth_set_offline(struct qeth_card *card, const struct qeth_discipline *disc,
5360 		     bool resetting)
5361 {
5362 	int rc, rc2, rc3;
5363 
5364 	mutex_lock(&card->conf_mutex);
5365 	QETH_CARD_TEXT(card, 3, "setoffl");
5366 
5367 	if ((!resetting && card->info.hwtrap) || card->info.hwtrap == 2) {
5368 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
5369 		card->info.hwtrap = 1;
5370 	}
5371 
5372 	/* cancel any stalled cmd that might block the rtnl: */
5373 	qeth_clear_ipacmd_list(card);
5374 
5375 	rtnl_lock();
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 	vscnprintf(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 	scnprintf(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