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