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