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