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