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