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